Skip to content

Commit 2d964d1

Browse files
committed
Do not log lines whose check.State is in --status
1 parent 82be7e0 commit 2d964d1

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

cmd/common.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,29 @@ func parseSkipIds(skipIds string) map[string]bool {
157157
return skipIdMap
158158
}
159159

160+
func parseStatus(statusList string) map[check.State]bool {
161+
var statusMap = make(map[check.State]bool, 0)
162+
if statusList != "" {
163+
for _, status := range strings.Split(statusList, ",") {
164+
statusMap[check.State(strings.ToUpper(strings.Trim(status, " ")))] = true
165+
}
166+
}
167+
return statusMap
168+
}
169+
170+
func printStatus(state check.State) bool {
171+
if statusList == "" {
172+
return true
173+
}
174+
statusMap := parseStatus(statusList)
175+
return statusMap[state]
176+
}
177+
160178
// colorPrint outputs the state in a specific colour, along with a message string
161179
func colorPrint(state check.State, s string) {
180+
if !printStatus(state) {
181+
return
182+
}
162183
colors[state].Printf("[%s] ", state)
163184
fmt.Printf("%s", s)
164185
}

cmd/common_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,55 @@ func TestWriteStdoutOutputTotal(t *testing.T) {
750750
assert.Contains(t, string(out), "49 checks PASS")
751751
}
752752

753+
func TestWriteStdoutOutputStatusList(t *testing.T) {
754+
type testCase struct {
755+
name string
756+
statusList string
757+
758+
notContains []string
759+
}
760+
testCases := []testCase{
761+
{
762+
name: "statusList PASS",
763+
statusList: "PASS",
764+
notContains: []string{"INFO", "WARN", "ERRO"},
765+
},
766+
{
767+
name: "statusList PASS,INFO",
768+
statusList: "PASS,INFO",
769+
notContains: []string{"WARN", "ERRO"},
770+
},
771+
{
772+
name: "statusList empty",
773+
statusList: "",
774+
notContains: nil,
775+
},
776+
}
777+
778+
controlsCollection, err := parseControlsJsonFile("./testdata/controlsCollection.json")
779+
if err != nil {
780+
t.Error(err)
781+
}
782+
783+
for _, tt := range testCases {
784+
rescueStdout := os.Stdout
785+
786+
r, w, _ := os.Pipe()
787+
788+
os.Stdout = w
789+
statusList = tt.statusList
790+
writeStdoutOutput(controlsCollection)
791+
w.Close()
792+
out, _ := ioutil.ReadAll(r)
793+
794+
os.Stdout = rescueStdout
795+
796+
for _, n := range tt.notContains {
797+
assert.NotContains(t, string(out), fmt.Sprintf("[%s]", n))
798+
}
799+
}
800+
}
801+
753802
func parseControlsJsonFile(filepath string) ([]*check.Controls, error) {
754803
var result []*check.Controls
755804

0 commit comments

Comments
 (0)