Skip to content

Commit

Permalink
Do not log lines whose check.State is in --status
Browse files Browse the repository at this point in the history
  • Loading branch information
mtpereira committed Oct 23, 2021
1 parent 82be7e0 commit 2d964d1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,29 @@ func parseSkipIds(skipIds string) map[string]bool {
return skipIdMap
}

func parseStatus(statusList string) map[check.State]bool {
var statusMap = make(map[check.State]bool, 0)
if statusList != "" {
for _, status := range strings.Split(statusList, ",") {
statusMap[check.State(strings.ToUpper(strings.Trim(status, " ")))] = true
}
}
return statusMap
}

func printStatus(state check.State) bool {
if statusList == "" {
return true
}
statusMap := parseStatus(statusList)
return statusMap[state]
}

// colorPrint outputs the state in a specific colour, along with a message string
func colorPrint(state check.State, s string) {
if !printStatus(state) {
return
}
colors[state].Printf("[%s] ", state)
fmt.Printf("%s", s)
}
Expand Down
49 changes: 49 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,55 @@ func TestWriteStdoutOutputTotal(t *testing.T) {
assert.Contains(t, string(out), "49 checks PASS")
}

func TestWriteStdoutOutputStatusList(t *testing.T) {
type testCase struct {
name string
statusList string

notContains []string
}
testCases := []testCase{
{
name: "statusList PASS",
statusList: "PASS",
notContains: []string{"INFO", "WARN", "ERRO"},
},
{
name: "statusList PASS,INFO",
statusList: "PASS,INFO",
notContains: []string{"WARN", "ERRO"},
},
{
name: "statusList empty",
statusList: "",
notContains: nil,
},
}

controlsCollection, err := parseControlsJsonFile("./testdata/controlsCollection.json")
if err != nil {
t.Error(err)
}

for _, tt := range testCases {
rescueStdout := os.Stdout

r, w, _ := os.Pipe()

os.Stdout = w
statusList = tt.statusList
writeStdoutOutput(controlsCollection)
w.Close()
out, _ := ioutil.ReadAll(r)

os.Stdout = rescueStdout

for _, n := range tt.notContains {
assert.NotContains(t, string(out), fmt.Sprintf("[%s]", n))
}
}
}

func parseControlsJsonFile(filepath string) ([]*check.Controls, error) {
var result []*check.Controls

Expand Down

0 comments on commit 2d964d1

Please sign in to comment.