Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cli tests: use TestMain, t.Setenv, t.Cleanup #931

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,36 @@ import (
"github.com/spf13/afero"
)

func init() {
func TestMain(m *testing.M) {
os.Unsetenv("HOME")
os.Unsetenv("XDG_CONFIG_HOME")
AppFs = afero.NewMemMapFs()
os.Exit(m.Run())
}

func TestXDGConfigDirIsPrefferedFirst(t *testing.T) {
t.Cleanup(func() {
// reset fs after test
AppFs = afero.NewMemMapFs()
})

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)
t.Setenv("XDG_CONFIG_HOME", xdgDirPath)

afero.WriteFile(AppFs, homeDirPath+"/revive.toml", []byte("\n"), 0644)
os.Setenv("HOME", homeDirPath)
t.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()
}

func TestHomeConfigDir(t *testing.T) {
Expand All @@ -43,7 +45,7 @@ func TestHomeConfigDir(t *testing.T) {
AppFs.MkdirAll(homeDirPath, 0755)

afero.WriteFile(AppFs, homeDirPath+"/revive.toml", []byte("\n"), 0644)
os.Setenv("HOME", homeDirPath)
t.Setenv("HOME", homeDirPath)

got := buildDefaultConfigPath()
want := homeDirPath + "/revive.toml"
Expand All @@ -58,7 +60,7 @@ func TestXDGConfigDir(t *testing.T) {
AppFs.MkdirAll(xdgDirPath, 0755)

afero.WriteFile(AppFs, xdgDirPath+"/revive.toml", []byte("\n"), 0644)
os.Setenv("XDG_CONFIG_HOME", xdgDirPath)
t.Setenv("XDG_CONFIG_HOME", xdgDirPath)

got := buildDefaultConfigPath()
want := xdgDirPath + "/revive.toml"
Expand All @@ -70,7 +72,7 @@ func TestXDGConfigDir(t *testing.T) {

func TestXDGConfigDirNoFile(t *testing.T) {
xdgDirPath := "/tmp-iofs/xdg/config"
os.Setenv("XDG_CONFIG_HOME", xdgDirPath)
t.Setenv("XDG_CONFIG_HOME", xdgDirPath)

got := buildDefaultConfigPath()
want := xdgDirPath + "/revive.toml"
Expand Down
Loading