Skip to content

Commit

Permalink
Distinguish env var getter test cases better
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Dec 11, 2023
1 parent c96f1a6 commit 977823c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3521,6 +3521,7 @@ func TestGetFlagCompletion(t *testing.T) {

func TestGetEnvConfig(t *testing.T) {
testCases := []struct {
desc string
use string
suffix string
cmdVar string
Expand All @@ -3530,6 +3531,7 @@ func TestGetEnvConfig(t *testing.T) {
expected string
}{
{
desc: "Command envvar overrides global",
use: "root",
suffix: "test",
cmdVar: "ROOT_TEST",
Expand All @@ -3539,6 +3541,7 @@ func TestGetEnvConfig(t *testing.T) {
expected: "cmd",
},
{
desc: "Missing/empty command envvar falls back to global",
use: "root",
suffix: "test",
cmdVar: "ROOT_TEST",
Expand All @@ -3548,6 +3551,7 @@ func TestGetEnvConfig(t *testing.T) {
expected: "global",
},
{
desc: "Missing/empty command and global envvars fall back to empty",
use: "root",
suffix: "test",
cmdVar: "ROOT_TEST",
Expand All @@ -3557,6 +3561,7 @@ func TestGetEnvConfig(t *testing.T) {
expected: "",
},
{
desc: "Periods in command use transform to undescores in env var name",
use: "foo.bar",
suffix: "test",
cmdVar: "FOO_BAR_TEST",
Expand All @@ -3566,6 +3571,7 @@ func TestGetEnvConfig(t *testing.T) {
expected: "cmd",
},
{
desc: "Dashes in command use transform to undescores in env var name",
use: "quux-BAZ",
suffix: "test",
cmdVar: "QUUX_BAZ_TEST",
Expand All @@ -3577,8 +3583,8 @@ func TestGetEnvConfig(t *testing.T) {
}

for _, tc := range testCases {
// Could make env handling cleaner with t.Setenv with Go >= 1.17
func() {
t.Run(tc.desc, func(t *testing.T) {
// Could make env handling cleaner with t.Setenv with Go >= 1.17
err := os.Setenv(tc.cmdVar, tc.cmdVal)
defer func() {
assertNoErr(t, os.Unsetenv(tc.cmdVar))
Expand All @@ -3594,6 +3600,6 @@ func TestGetEnvConfig(t *testing.T) {
if got != tc.expected {
t.Errorf("expected: %q, got: %q", tc.expected, got)
}
}()
})
}
}

0 comments on commit 977823c

Please sign in to comment.