diff --git a/flag_test.go b/flag_test.go index 5587f0e206..26ada26fa5 100644 --- a/flag_test.go +++ b/flag_test.go @@ -507,10 +507,7 @@ func TestStringFlagWithEnvVarHelpOutput(t *testing.T) { fl := &StringFlag{Name: test.name, Aliases: test.aliases, Value: test.value, EnvVars: []string{"APP_FOO"}} output := fl.String() - expectedSuffix := " [$APP_FOO]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_FOO%]" - } + expectedSuffix := suffixForEnv("APP_FOO") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } @@ -599,10 +596,7 @@ func TestPathFlagWithEnvVarHelpOutput(t *testing.T) { fl := &PathFlag{Name: test.name, Aliases: test.aliases, Value: test.value, EnvVars: []string{"APP_PATH"}} output := fl.String() - expectedSuffix := " [$APP_PATH]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_PATH%]" - } + expectedSuffix := suffixForEnv("APP_PATH") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } @@ -696,10 +690,7 @@ func TestStringSliceFlagWithEnvVarHelpOutput(t *testing.T) { fl := &StringSliceFlag{Name: test.name, Aliases: test.aliases, Value: test.value, EnvVars: []string{"APP_QWWX"}} output := fl.String() - expectedSuffix := " [$APP_QWWX]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_QWWX%]" - } + expectedSuffix := suffixForEnv("APP_QWWX") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%q does not end with"+expectedSuffix, output) } @@ -797,10 +788,7 @@ func TestIntFlagWithEnvVarHelpOutput(t *testing.T) { fl := &IntFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := fl.String() - expectedSuffix := " [$APP_BAR]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_BAR%]" - } + expectedSuffix := suffixForEnv("APP_BAR") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } @@ -859,16 +847,21 @@ func TestInt64FlagWithEnvVarHelpOutput(t *testing.T) { fl := IntFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := fl.String() - expectedSuffix := " [$APP_BAR]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_BAR%]" - } + expectedSuffix := suffixForEnv("APP_BAR") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } } } +func suffixForEnv(s string) string { + expectedSuffix := fmt.Sprintf(" [$%s]", s) + if runtime.GOOS == "windows" { + expectedSuffix = fmt.Sprintf(" [%s%s%s]", "%", s, "%") + } + return expectedSuffix +} + func TestInt64FlagValueFromContext(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Int64("myflag", 42, "doc") @@ -910,10 +903,7 @@ func TestUintFlagWithEnvVarHelpOutput(t *testing.T) { fl := UintFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := fl.String() - expectedSuffix := " [$APP_BAR]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_BAR%]" - } + expectedSuffix := suffixForEnv("APP_BAR") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } @@ -961,10 +951,7 @@ func TestUint64FlagWithEnvVarHelpOutput(t *testing.T) { fl := UintFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := fl.String() - expectedSuffix := " [$APP_BAR]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_BAR%]" - } + expectedSuffix := suffixForEnv("APP_BAR") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } @@ -1012,10 +999,7 @@ func TestDurationFlagWithEnvVarHelpOutput(t *testing.T) { fl := &DurationFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := fl.String() - expectedSuffix := " [$APP_BAR]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_BAR%]" - } + expectedSuffix := suffixForEnv("APP_BAR") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } @@ -1072,10 +1056,7 @@ func TestIntSliceFlagWithEnvVarHelpOutput(t *testing.T) { fl := &IntSliceFlag{Name: test.name, Aliases: test.aliases, Value: test.value, EnvVars: []string{"APP_SMURF"}} output := fl.String() - expectedSuffix := " [$APP_SMURF]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_SMURF%]" - } + expectedSuffix := suffixForEnv("APP_SMURF") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%q does not end with"+expectedSuffix, output) } @@ -1211,10 +1192,7 @@ func TestInt64SliceFlagWithEnvVarHelpOutput(t *testing.T) { fl := Int64SliceFlag{Name: test.name, Value: test.value, EnvVars: []string{"APP_SMURF"}} output := fl.String() - expectedSuffix := " [$APP_SMURF]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_SMURF%]" - } + expectedSuffix := suffixForEnv("APP_SMURF") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%q does not end with"+expectedSuffix, output) } @@ -1365,10 +1343,7 @@ func TestUintSliceFlagWithEnvVarHelpOutput(t *testing.T) { fl := UintSliceFlag{Name: test.name, Value: test.value, EnvVars: []string{"APP_SMURF"}} output := fl.String() - expectedSuffix := " [$APP_SMURF]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_SMURF%]" - } + expectedSuffix := suffixForEnv("APP_SMURF") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%q does not end with"+expectedSuffix, output) } @@ -1511,10 +1486,7 @@ func TestUint64SliceFlagWithEnvVarHelpOutput(t *testing.T) { fl := Uint64SliceFlag{Name: test.name, Value: test.value, EnvVars: []string{"APP_SMURF"}} output := fl.String() - expectedSuffix := " [$APP_SMURF]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_SMURF%]" - } + expectedSuffix := suffixForEnv("APP_SMURF") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%q does not end with"+expectedSuffix, output) } @@ -1653,10 +1625,7 @@ func TestFloat64FlagWithEnvVarHelpOutput(t *testing.T) { fl := &Float64Flag{Name: test.name, EnvVars: []string{"APP_BAZ"}} output := fl.String() - expectedSuffix := " [$APP_BAZ]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_BAZ%]" - } + expectedSuffix := suffixForEnv("APP_BAZ") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) } @@ -1713,10 +1682,7 @@ func TestFloat64SliceFlagWithEnvVarHelpOutput(t *testing.T) { fl := Float64SliceFlag{Name: test.name, Value: test.value, EnvVars: []string{"APP_SMURF"}} output := fl.String() - expectedSuffix := " [$APP_SMURF]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_SMURF%]" - } + expectedSuffix := suffixForEnv("APP_SMURF") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%q does not end with"+expectedSuffix, output) } @@ -1836,10 +1802,7 @@ func TestGenericFlagWithEnvVarHelpOutput(t *testing.T) { fl := &GenericFlag{Name: test.name, EnvVars: []string{"APP_ZAP"}} output := fl.String() - expectedSuffix := " [$APP_ZAP]" - if runtime.GOOS == "windows" { - expectedSuffix = " [%APP_ZAP%]" - } + expectedSuffix := suffixForEnv("APP_ZAP") if !strings.HasSuffix(output, expectedSuffix) { t.Errorf("%s does not end with"+expectedSuffix, output) }