Isolate user configuration discovery tests by platform
This commit is contained in:
@@ -326,21 +326,29 @@ func TestSearchSourceObservesFileChangesBetweenLoads(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSearchSourceObservesUserConfigEnvironmentChanges(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Setenv("APPDATA", filepath.Join(t.TempDir(), "first"))
|
||||
} else {
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "first"))
|
||||
setBase := func(directory string) string {
|
||||
t.Helper()
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
t.Setenv("AppData", directory)
|
||||
case "darwin", "ios":
|
||||
t.Setenv("HOME", directory)
|
||||
case "plan9":
|
||||
t.Setenv("home", directory)
|
||||
default:
|
||||
t.Setenv("XDG_CONFIG_HOME", directory)
|
||||
}
|
||||
base, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return base
|
||||
}
|
||||
type config struct {
|
||||
Value string `yaml:"value"`
|
||||
}
|
||||
firstBase := os.Getenv("XDG_CONFIG_HOME")
|
||||
if runtime.GOOS == "windows" {
|
||||
firstBase = os.Getenv("APPDATA")
|
||||
}
|
||||
firstBase := setBase(filepath.Join(t.TempDir(), "first"))
|
||||
first := writeImportFile(t, firstBase, "app/app.yml", "value: first\n")
|
||||
secondBase := filepath.Join(t.TempDir(), "second")
|
||||
second := writeImportFile(t, secondBase, "app/app.yml", "value: second\n")
|
||||
source := SearchConfig("app", UserConfigDirectory())
|
||||
load := func() string {
|
||||
loaded, err := testLoad[config](source, Options[config]{})
|
||||
@@ -352,28 +360,11 @@ func TestSearchSourceObservesUserConfigEnvironmentChanges(t *testing.T) {
|
||||
if value := load(); value != "first" {
|
||||
t.Fatalf("first user-config search value = %q (%s)", value, first)
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Setenv("APPDATA", secondBase)
|
||||
} else {
|
||||
t.Setenv("XDG_CONFIG_HOME", secondBase)
|
||||
}
|
||||
secondBase := setBase(filepath.Join(t.TempDir(), "second"))
|
||||
second := writeImportFile(t, secondBase, "app/app.yml", "value: second\n")
|
||||
if value := load(); value != "second" {
|
||||
t.Fatalf("updated user-config search value = %q (%s)", value, second)
|
||||
}
|
||||
|
||||
// A reached dynamic base failure is terminal, but an earlier match must
|
||||
// prevent the invalid environment value from being resolved at all.
|
||||
direct := writeImportFile(t, t.TempDir(), "direct.yml", "value: direct\n")
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Setenv("APPDATA", "relative")
|
||||
} else {
|
||||
t.Setenv("XDG_CONFIG_HOME", "relative")
|
||||
}
|
||||
early := SearchConfig("app", Searches(Paths(direct), UserConfigDirectory()))
|
||||
loaded, err := testLoad[config](early, Options[config]{})
|
||||
if err != nil || loaded.Config.Value != "direct" {
|
||||
t.Fatalf("early match with unavailable later base = %#v, %v", loaded, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchSourceObservesWorkingDirectoryChanges(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user