From 8df0e9062eb29376f610f26f19ec1ac7c7e8db3c Mon Sep 17 00:00:00 2001 From: Davide Petilli Date: Tue, 9 Aug 2022 15:09:27 +0200 Subject: [PATCH] fix: config loading order (#121) --- configuration/configuration.go | 7 +++++- configuration/configuration_test.go | 39 ++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/configuration/configuration.go b/configuration/configuration.go index d46ba118..6adca68d 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -110,11 +110,12 @@ func arePathsNotSet(cPaths []string) bool { func defaultConfigPaths() []string { result := make([]string, 0, 4) - result = append(result, ".") + // First global config if runtime.GOOS != windowsOs { result = append(result, "/etc/gremlins") } + // Then $XDG_CONFIG_HOME xchLocation, _ := homedir.Expand("~/.config") if x := os.Getenv(xdgConfigHomeKey); x != "" { xchLocation = x @@ -122,12 +123,16 @@ func defaultConfigPaths() []string { xchLocation = filepath.Join(xchLocation, "gremlins", "gremlins") result = append(result, xchLocation) + // Then $HOME homeLocation, err := homedir.Expand("~/.gremlins") if err != nil { return result } result = append(result, homeLocation) + // Finally the current directory + result = append(result, ".") + return result } diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index cad1f815..f3764171 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -17,6 +17,7 @@ package configuration import ( + "os" "path/filepath" "runtime" "testing" @@ -130,12 +131,27 @@ func TestConfigPaths(t *testing.T) { home, _ := homedir.Dir() t.Run("it lookups in default locations", func(t *testing.T) { + oldDir, _ := os.Getwd() + _ = os.Chdir("testdata/config1") + defer func(dir string) { + _ = os.Chdir(dir) + }(oldDir) + var want []string - want = append(want, ".") + + // First global if runtime.GOOS != "windows" { want = append(want, "/etc/gremlins") } - want = append(want, filepath.Join(home, ".config", "gremlins", "gremlins"), filepath.Join(home, ".gremlins")) + + // Then $XDG_CONFIG_HOME and $HOME + want = append(want, + filepath.Join(home, ".config", "gremlins", "gremlins"), + filepath.Join(home, ".gremlins"), + ) + + // Last current folder + want = append(want, ".") got := defaultConfigPaths() @@ -145,14 +161,29 @@ func TestConfigPaths(t *testing.T) { }) t.Run("when XDG_CONFIG_HOME is set, it lookups in that locations", func(t *testing.T) { + oldDir, _ := os.Getwd() + _ = os.Chdir("testdata/config1") + defer func(dir string) { + _ = os.Chdir(dir) + }(oldDir) + customPath := filepath.Join("my", "custom", "path") t.Setenv("XDG_CONFIG_HOME", customPath) + var want []string - want = append(want, ".") + + // First global if runtime.GOOS != "windows" { want = append(want, "/etc/gremlins") } - want = append(want, filepath.Join(customPath, "gremlins", "gremlins"), filepath.Join(home, ".gremlins")) + + // Then $XDG_CONFIG_HOME and $HOME + want = append(want, + filepath.Join(customPath, "gremlins", "gremlins"), + filepath.Join(home, ".gremlins")) + + // Last the current directory + want = append(want, ".") got := defaultConfigPaths()