mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 13:02:14 +00:00
Cleanup environment in tests that call os.Unsetenv
testing.T.Cleanup ensures the environment is restored after a test and any of its parallel sub-tests. It's possible that some of these can be simplified further to T.Setenv(key, ""), but I did not investigate.
This commit is contained in:
parent
2181eea484
commit
e8d3a4a105
@ -65,71 +65,70 @@ func TestTranslationUsingEnvVar(t *testing.T) {
|
|||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
setenvFn func()
|
setenv map[string]string
|
||||||
expectedStr string
|
expectedStr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Only LC_ALL is set",
|
name: "Only LC_ALL is set",
|
||||||
setenvFn: func() { os.Setenv("LC_ALL", knownTestLocale) },
|
setenv: map[string]string{"LC_ALL": knownTestLocale},
|
||||||
expectedStr: expectedStrEnUSLocale,
|
expectedStr: expectedStrEnUSLocale,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Only LC_MESSAGES is set",
|
name: "Only LC_MESSAGES is set",
|
||||||
setenvFn: func() { os.Setenv("LC_MESSAGES", knownTestLocale) },
|
setenv: map[string]string{"LC_MESSAGES": knownTestLocale},
|
||||||
expectedStr: expectedStrEnUSLocale,
|
expectedStr: expectedStrEnUSLocale,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Only LANG",
|
name: "Only LANG",
|
||||||
setenvFn: func() { os.Setenv("LANG", knownTestLocale) },
|
setenv: map[string]string{"LANG": knownTestLocale},
|
||||||
expectedStr: expectedStrEnUSLocale,
|
expectedStr: expectedStrEnUSLocale,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "LC_MESSAGES overrides LANG",
|
name: "LC_MESSAGES overrides LANG",
|
||||||
setenvFn: func() {
|
setenv: map[string]string{
|
||||||
os.Setenv("LANG", "be_BY.UTF-8") // Unknown locale
|
"LANG": "be_BY.UTF-8", // Unknown locale
|
||||||
os.Setenv("LC_MESSAGES", knownTestLocale)
|
"LC_MESSAGES": knownTestLocale,
|
||||||
},
|
},
|
||||||
expectedStr: expectedStrEnUSLocale,
|
expectedStr: expectedStrEnUSLocale,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "LC_ALL overrides LANG",
|
name: "LC_ALL overrides LANG",
|
||||||
setenvFn: func() {
|
setenv: map[string]string{
|
||||||
os.Setenv("LANG", "be_BY.UTF-8") // Unknown locale
|
"LANG": "be_BY.UTF-8", // Unknown locale
|
||||||
os.Setenv("LC_ALL", knownTestLocale)
|
"LC_ALL": knownTestLocale,
|
||||||
},
|
},
|
||||||
expectedStr: expectedStrEnUSLocale,
|
expectedStr: expectedStrEnUSLocale,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "LC_ALL overrides LC_MESSAGES",
|
name: "LC_ALL overrides LC_MESSAGES",
|
||||||
setenvFn: func() {
|
setenv: map[string]string{
|
||||||
os.Setenv("LC_MESSAGES", "be_BY.UTF-8") // Unknown locale
|
"LC_MESSAGES": "be_BY.UTF-8", // Unknown locale
|
||||||
os.Setenv("LC_ALL", knownTestLocale)
|
"LC_ALL": knownTestLocale,
|
||||||
},
|
},
|
||||||
expectedStr: expectedStrEnUSLocale,
|
expectedStr: expectedStrEnUSLocale,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Unknown locale in LANG",
|
name: "Unknown locale in LANG",
|
||||||
setenvFn: func() { os.Setenv("LANG", "be_BY.UTF-8") },
|
setenv: map[string]string{"LANG": "be_BY.UTF-8"},
|
||||||
expectedStr: expectedStrFallback,
|
expectedStr: expectedStrFallback,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Unknown locale in LC_MESSAGES",
|
name: "Unknown locale in LC_MESSAGES",
|
||||||
setenvFn: func() { os.Setenv("LC_MESSAGES", "be_BY.UTF-8") },
|
setenv: map[string]string{"LC_MESSAGES": "be_BY.UTF-8"},
|
||||||
expectedStr: expectedStrFallback,
|
expectedStr: expectedStrFallback,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Unknown locale in LC_ALL",
|
name: "Unknown locale in LC_ALL",
|
||||||
setenvFn: func() { os.Setenv("LC_ALL", "be_BY.UTF-8") },
|
setenv: map[string]string{"LC_ALL": "be_BY.UTF-8"},
|
||||||
expectedStr: expectedStrFallback,
|
expectedStr: expectedStrFallback,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Invalid env var",
|
name: "Invalid env var",
|
||||||
setenvFn: func() { os.Setenv("LC_MESSAGES", "fake.locale.UTF-8") },
|
setenv: map[string]string{"LC_MESSAGES": "fake.locale.UTF-8"},
|
||||||
expectedStr: expectedStrFallback,
|
expectedStr: expectedStrFallback,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "No env vars",
|
name: "No env vars",
|
||||||
setenvFn: func() {},
|
|
||||||
expectedStr: expectedStrFallback,
|
expectedStr: expectedStrFallback,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -139,13 +138,14 @@ func TestTranslationUsingEnvVar(t *testing.T) {
|
|||||||
for _, envVar := range envVarsToBackup {
|
for _, envVar := range envVarsToBackup {
|
||||||
if envVarValue := os.Getenv(envVar); envVarValue != "" {
|
if envVarValue := os.Getenv(envVar); envVarValue != "" {
|
||||||
envVarValue, envVar := envVarValue, envVar
|
envVarValue, envVar := envVarValue, envVar
|
||||||
|
t.Cleanup(func() { os.Setenv(envVar, envVarValue) })
|
||||||
os.Unsetenv(envVar)
|
os.Unsetenv(envVar)
|
||||||
// Restore env var at the end
|
|
||||||
defer func() { os.Setenv(envVar, envVarValue) }()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test.setenvFn()
|
for envVar, envVarValue := range test.setenv {
|
||||||
|
t.Setenv(envVar, envVarValue)
|
||||||
|
}
|
||||||
|
|
||||||
err := LoadTranslations("test", nil)
|
err := LoadTranslations("test", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user