From fe50154f17f08069c64fd5f902d726fa7a125578 Mon Sep 17 00:00:00 2001 From: Manuel Tiago Pereira Date: Sat, 23 Oct 2021 17:36:48 +0100 Subject: [PATCH] Do not log lines whose check.State is in --status --- cmd/common.go | 21 ++++++++++++++++++++ cmd/common_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/cmd/common.go b/cmd/common.go index 60f2913..e6fab38 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -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) } diff --git a/cmd/common_test.go b/cmd/common_test.go index e0f38d0..0887d22 100644 --- a/cmd/common_test.go +++ b/cmd/common_test.go @@ -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