mirror of
https://github.com/spf13/cobra
synced 2025-04-27 17:17:20 +00:00
Distinguish env var getter test cases better
This commit is contained in:
parent
c96f1a622a
commit
9740ecead4
1 changed files with 9 additions and 3 deletions
|
@ -3521,6 +3521,7 @@ func TestGetFlagCompletion(t *testing.T) {
|
||||||
|
|
||||||
func TestGetEnvConfig(t *testing.T) {
|
func TestGetEnvConfig(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
use string
|
use string
|
||||||
suffix string
|
suffix string
|
||||||
cmdVar string
|
cmdVar string
|
||||||
|
@ -3530,6 +3531,7 @@ func TestGetEnvConfig(t *testing.T) {
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
desc: "Command envvar overrides global",
|
||||||
use: "root",
|
use: "root",
|
||||||
suffix: "test",
|
suffix: "test",
|
||||||
cmdVar: "ROOT_TEST",
|
cmdVar: "ROOT_TEST",
|
||||||
|
@ -3539,6 +3541,7 @@ func TestGetEnvConfig(t *testing.T) {
|
||||||
expected: "cmd",
|
expected: "cmd",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
desc: "Missing/empty command envvar falls back to global",
|
||||||
use: "root",
|
use: "root",
|
||||||
suffix: "test",
|
suffix: "test",
|
||||||
cmdVar: "ROOT_TEST",
|
cmdVar: "ROOT_TEST",
|
||||||
|
@ -3548,6 +3551,7 @@ func TestGetEnvConfig(t *testing.T) {
|
||||||
expected: "global",
|
expected: "global",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
desc: "Missing/empty command and global envvars fall back to empty",
|
||||||
use: "root",
|
use: "root",
|
||||||
suffix: "test",
|
suffix: "test",
|
||||||
cmdVar: "ROOT_TEST",
|
cmdVar: "ROOT_TEST",
|
||||||
|
@ -3557,6 +3561,7 @@ func TestGetEnvConfig(t *testing.T) {
|
||||||
expected: "",
|
expected: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
desc: "Periods in command use transform to underscores in env var name",
|
||||||
use: "foo.bar",
|
use: "foo.bar",
|
||||||
suffix: "test",
|
suffix: "test",
|
||||||
cmdVar: "FOO_BAR_TEST",
|
cmdVar: "FOO_BAR_TEST",
|
||||||
|
@ -3566,6 +3571,7 @@ func TestGetEnvConfig(t *testing.T) {
|
||||||
expected: "cmd",
|
expected: "cmd",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
desc: "Dashes in command use transform to underscores in env var name",
|
||||||
use: "quux-BAZ",
|
use: "quux-BAZ",
|
||||||
suffix: "test",
|
suffix: "test",
|
||||||
cmdVar: "QUUX_BAZ_TEST",
|
cmdVar: "QUUX_BAZ_TEST",
|
||||||
|
@ -3577,8 +3583,8 @@ func TestGetEnvConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
// Could make env handling cleaner with t.Setenv with Go >= 1.17
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
func() {
|
// Could make env handling cleaner with t.Setenv with Go >= 1.17
|
||||||
err := os.Setenv(tc.cmdVar, tc.cmdVal)
|
err := os.Setenv(tc.cmdVar, tc.cmdVal)
|
||||||
defer func() {
|
defer func() {
|
||||||
assertNoErr(t, os.Unsetenv(tc.cmdVar))
|
assertNoErr(t, os.Unsetenv(tc.cmdVar))
|
||||||
|
@ -3594,6 +3600,6 @@ func TestGetEnvConfig(t *testing.T) {
|
||||||
if got != tc.expected {
|
if got != tc.expected {
|
||||||
t.Errorf("expected: %q, got: %q", tc.expected, got)
|
t.Errorf("expected: %q, got: %q", tc.expected, got)
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue