From 87acf77aefec799a88e0048ee842518342ab196b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 Oct 2024 00:48:16 +0200 Subject: [PATCH] cli/hints: add tests Signed-off-by: Sebastiaan van Stijn --- cli/hints/hints.go | 4 +++- cli/hints/hints_test.go | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 cli/hints/hints_test.go diff --git a/cli/hints/hints.go b/cli/hints/hints.go index f99df8fda0..aed3577893 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 0000000000..194bb78e51 --- /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) + }) + } +}