diff --git a/cli/hints/hints.go b/cli/hints/hints.go index f99df8fda02d..aed3577893f6 100644 --- a/cli/hints/hints.go +++ b/cli/hints/hints.go @@ -5,7 +5,9 @@ import ( "strconv" ) -// Enabled returns whether cli hints are enabled or not +// Enabled returns whether cli hints are enabled or not. Hints are enabled by +// default, but can be disabled through the "DOCKER_CLI_HINTS" environment +// variable. func Enabled() bool { if v := os.Getenv("DOCKER_CLI_HINTS"); v != "" { enabled, err := strconv.ParseBool(v) diff --git a/cli/hints/hints_test.go b/cli/hints/hints_test.go new file mode 100644 index 000000000000..194bb78e5129 --- /dev/null +++ b/cli/hints/hints_test.go @@ -0,0 +1,52 @@ +package hints + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestEnabled(t *testing.T) { + tests := []struct { + doc string + env string + expected bool + }{ + { + doc: "default", + expected: true, + }, + { + doc: "DOCKER_CLI_HINTS=1", + env: "1", + expected: true, + }, + { + doc: "DOCKER_CLI_HINTS=true", + env: "true", + expected: true, + }, + { + doc: "DOCKER_CLI_HINTS=0", + env: "0", + expected: false, + }, + { + doc: "DOCKER_CLI_HINTS=false", + env: "false", + expected: false, + }, + { + doc: "DOCKER_CLI_HINTS=not-a-bool", + env: "not-a-bool", + expected: true, + }, + } + + for _, tc := range tests { + t.Run(tc.doc, func(t *testing.T) { + t.Setenv("DOCKER_CLI_HINTS", tc.env) + assert.Equal(t, Enabled(), tc.expected) + }) + } +}