Skip to content

Commit

Permalink
test if xdg config dir is preffered first
Browse files Browse the repository at this point in the history
  • Loading branch information
mfederowicz committed Oct 3, 2023
1 parent 6f25fe5 commit 70273b5
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 70273b5

Please sign in to comment.