diff --git a/cli/main_test.go b/cli/main_test.go index 26fb3ca74..58b521e19 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -8,19 +8,45 @@ import ( ) func init() { + os.Unsetenv("HOME") + os.Unsetenv("XDG_CONFIG_HOME") + AppFs = afero.NewMemMapFs() +} + +func TestXDGConfigDirIsPrefferedFirst(t *testing.T) { + + xdgDirPath := "/tmp-iofs/xdg/config" + homeDirPath := "/tmp-iofs/home/tester" + AppFs.MkdirAll(xdgDirPath, 0755) + AppFs.MkdirAll(homeDirPath, 0755) + + afero.WriteFile(AppFs, xdgDirPath+"/revive.toml", []byte("\n"), 0644) + os.Setenv("XDG_CONFIG_HOME", xdgDirPath) + + afero.WriteFile(AppFs, homeDirPath+"/revive.toml", []byte("\n"), 0644) + os.Setenv("HOME", homeDirPath) + + got := buildDefaultConfigPath() + want := xdgDirPath + "/revive.toml" + + if got != want { + t.Errorf("got %q, wanted %q", got, want) + } + + //reset fs after test + AppFs = afero.NewMemMapFs() - AppFs = afero.NewMemMapFs() } func TestHomeConfigDir(t *testing.T) { - - homeDirPath := "/tmp-iofs/home/tester" + + homeDirPath := "/tmp-iofs/home/tester" AppFs.MkdirAll(homeDirPath, 0755) afero.WriteFile(AppFs, homeDirPath+"/revive.toml", []byte("\n"), 0644) os.Setenv("HOME", homeDirPath) - - got := buildDefaultConfigPath() + + got := buildDefaultConfigPath() want := homeDirPath + "/revive.toml" if got != want { @@ -49,9 +75,9 @@ func TestXDGConfigDir(t *testing.T) { func TestXDGConfigDirNoFile(t *testing.T) { xdgDirPath := "/tmp-iofs/xdg/config" - os.Setenv("XDG_CONFIG_HOME", xdgDirPath) - - got := buildDefaultConfigPath() + os.Setenv("XDG_CONFIG_HOME", xdgDirPath) + + got := buildDefaultConfigPath() want := xdgDirPath + "/revive.toml" if got != want {