From f93b45ee815dca6ea34e0c10620dd18fed0f74c3 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Thu, 2 Nov 2023 12:24:29 +0200 Subject: [PATCH] cli: refactor tests: use TestMain, t.Setenv --- cli/main_test.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cli/main_test.go b/cli/main_test.go index 143361415..b2fabaf3d 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -7,13 +7,18 @@ 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" @@ -21,10 +26,10 @@ func TestXDGConfigDirIsPrefferedFirst(t *testing.T) { 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" @@ -32,9 +37,6 @@ func TestXDGConfigDirIsPrefferedFirst(t *testing.T) { if got != want { t.Errorf("got %q, wanted %q", got, want) } - - //reset fs after test - AppFs = afero.NewMemMapFs() } func TestHomeConfigDir(t *testing.T) { @@ -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" @@ -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" @@ -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"