moar tests

This commit is contained in:
Roberto Hidalgo 2023-01-02 00:54:49 -06:00
parent 0d24a80075
commit d9b257e145
4 changed files with 59 additions and 17 deletions

View File

@ -66,7 +66,7 @@ func Execute(version string) error {
ccRoot.Annotations["version"] = version ccRoot.Annotations["version"] = version
ccRoot.CompletionOptions.HiddenDefaultCmd = true ccRoot.CompletionOptions.HiddenDefaultCmd = true
ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet()) ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet())
ccRoot.AddCommand(commands.Help) ccRoot.SetHelpCommand(commands.Help)
ccRoot.AddCommand(commands.Version) ccRoot.AddCommand(commands.Version)
ccRoot.AddCommand(commands.GenerateCompletions) ccRoot.AddCommand(commands.GenerateCompletions)

9
pkg/env/env.go vendored
View File

@ -11,12 +11,3 @@ var NoColor = "NO_COLOR"
var ForceColor = "COLOR" var ForceColor = "COLOR"
var ValidationDisabled = "SKIP_VALIDATION" var ValidationDisabled = "SKIP_VALIDATION"
var Debug = "DEBUG" 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,
}

View File

@ -65,18 +65,17 @@ func VerboseEnabled() bool {
} }
func SilenceEnabled() bool { func SilenceEnabled() bool {
if isTrueIsh(os.Getenv(env.Silent)) {
return true
}
if VerboseEnabled() {
return false
}
for _, arg := range os.Args { for _, arg := range os.Args {
if arg == "--silent" { if arg == "--silent" {
return true return true
} }
} }
return false
if VerboseEnabled() {
return false
}
return isTrueIsh(os.Getenv(env.Silent))
} }
func ColorEnabled() bool { func ColorEnabled() bool {

View File

@ -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) { func TestEnvironmentMapEnabled(t *testing.T) {
trueString := strconv.FormatBool(true) trueString := strconv.FormatBool(true)
os.Setenv(env.ForceColor, trueString) os.Setenv(env.ForceColor, trueString)