From 36d794febed917de18ecae93c5d82f0af1c410f7 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Thu, 31 Aug 2023 10:40:11 -0400 Subject: [PATCH] fix quiet flag (#2081) Signed-off-by: Alex Goodman --- cmd/syft/cli/cli.go | 2 +- test/cli/packages_cmd_test.go | 18 ++++++++++++++++++ test/cli/trait_assertions_test.go | 7 +++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cmd/syft/cli/cli.go b/cmd/syft/cli/cli.go index 187cf09c41c..b1347b274dd 100644 --- a/cmd/syft/cli/cli.go +++ b/cmd/syft/cli/cli.go @@ -29,7 +29,7 @@ func New(id clio.Identification) clio.Application { // select a UI based on the logging configuration and state of stdin (if stdin is a tty) func(cfg clio.Config) ([]clio.UI, error) { noUI := ui.None(cfg.Log.Quiet) - if !cfg.Log.AllowUI(os.Stdin) { + if !cfg.Log.AllowUI(os.Stdin) || cfg.Log.Quiet { return []clio.UI{noUI}, nil } diff --git a/test/cli/packages_cmd_test.go b/test/cli/packages_cmd_test.go index 52efac9202b..dc100ebebcd 100644 --- a/test/cli/packages_cmd_test.go +++ b/test/cli/packages_cmd_test.go @@ -36,6 +36,24 @@ func TestPackagesCmdFlags(t *testing.T) { assertSuccessfulReturnCode, }, }, + { + name: "quiet-flag-with-logger", + args: []string{"packages", "-qvv", "-o", "json", coverageImage}, + assertions: []traitAssertion{ + assertJsonReport, + assertNoStderr, + assertSuccessfulReturnCode, + }, + }, + { + name: "quiet-flag-with-tui", + args: []string{"packages", "-q", "-o", "json", coverageImage}, + assertions: []traitAssertion{ + assertJsonReport, + assertNoStderr, + assertSuccessfulReturnCode, + }, + }, { name: "multiple-output-flags", args: []string{"packages", "-o", "table", "-o", "json=" + tmp + ".tmp/multiple-output-flag-test.json", coverageImage}, diff --git a/test/cli/trait_assertions_test.go b/test/cli/trait_assertions_test.go index d441d021bf3..828b407bf69 100644 --- a/test/cli/trait_assertions_test.go +++ b/test/cli/trait_assertions_test.go @@ -83,6 +83,13 @@ func assertNotInOutput(data string) traitAssertion { } } +func assertNoStderr(tb testing.TB, _, stderr string, _ int) { + tb.Helper() + if len(stderr) > 0 { + tb.Errorf("expected stderr to be empty, but got %q", stderr) + } +} + func assertInOutput(data string) traitAssertion { return func(tb testing.TB, stdout, stderr string, _ int) { tb.Helper()