From d9b257e145ce79a086ec82e756c17b8576e2c64d Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Mon, 2 Jan 2023 00:54:49 -0600 Subject: [PATCH] moar tests --- internal/registry/registry.go | 2 +- pkg/env/env.go | 9 ------ pkg/runtime/runtime.go | 13 ++++----- pkg/runtime/runtime_test.go | 52 +++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/internal/registry/registry.go b/internal/registry/registry.go index 65348f4..c3f2a2f 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -66,7 +66,7 @@ func Execute(version string) error { ccRoot.Annotations["version"] = version ccRoot.CompletionOptions.HiddenDefaultCmd = true ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet()) - ccRoot.AddCommand(commands.Help) + ccRoot.SetHelpCommand(commands.Help) ccRoot.AddCommand(commands.Version) ccRoot.AddCommand(commands.GenerateCompletions) diff --git a/pkg/env/env.go b/pkg/env/env.go index 5de2064..cd7c4ae 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -11,12 +11,3 @@ var NoColor = "NO_COLOR" var ForceColor = "COLOR" var ValidationDisabled = "SKIP_VALIDATION" var Debug = "DEBUG" - -// FlagNames are flags also available as environment variables. -var FlagNames = map[string]string{ - "no-color": NoColor, - "color": ForceColor, - "silent": Silent, - "verbose": Verbose, - "skip-validation": ValidationDisabled, -} diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index dd14be4..b69f0cd 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -65,18 +65,17 @@ func VerboseEnabled() bool { } func SilenceEnabled() bool { - if isTrueIsh(os.Getenv(env.Silent)) { - return true - } - if VerboseEnabled() { - return false - } for _, arg := range os.Args { if arg == "--silent" { return true } } - return false + + if VerboseEnabled() { + return false + } + + return isTrueIsh(os.Getenv(env.Silent)) } func ColorEnabled() bool { diff --git a/pkg/runtime/runtime_test.go b/pkg/runtime/runtime_test.go index 2a19268..7148714 100644 --- a/pkg/runtime/runtime_test.go +++ b/pkg/runtime/runtime_test.go @@ -83,6 +83,58 @@ func TestEnabled(t *testing.T) { } } +func TestSilent(t *testing.T) { + origArgs := os.Args + t.Cleanup(func() { + os.Args = origArgs + }) + t.Run("SILENT = silence", func(t *testing.T) { + t.Setenv(env.Silent, "1") + t.Setenv(env.Verbose, "") + os.Args = []string{} + if !SilenceEnabled() { + t.Fail() + } + }) + + t.Run("SILENT + VERBOSE = silence", func(t *testing.T) { + t.Setenv(env.Silent, "1") + t.Setenv(env.Verbose, "1") + os.Args = []string{} + if SilenceEnabled() { + t.Fail() + } + }) + + t.Run("VERBOSE + --silent = silent", func(t *testing.T) { + t.Setenv(env.Silent, "") + t.Setenv(env.Verbose, "1") + os.Args = []string{"some", "random", "--silent", "args"} + if !SilenceEnabled() { + t.Fail() + } + }) + + t.Run("--silent = silent", func(t *testing.T) { + t.Setenv(env.Silent, "") + t.Setenv(env.Verbose, "") + os.Args = []string{"some", "random", "--silent", "args"} + if !SilenceEnabled() { + t.Fail() + } + }) + + t.Run("nothing = nothing", func(t *testing.T) { + t.Setenv(env.Silent, "") + t.Setenv(env.Verbose, "") + os.Args = []string{"some", "random", "args"} + if SilenceEnabled() { + t.Fail() + } + }) + +} + func TestEnvironmentMapEnabled(t *testing.T) { trueString := strconv.FormatBool(true) os.Setenv(env.ForceColor, trueString)