tests: use T.Setenv instead of os.Setenv

This commit is contained in:
Oleksandr Redko 2023-02-18 12:53:16 +02:00
parent 44b1dbe0f6
commit 63c68c6c5b
4 changed files with 14 additions and 25 deletions

View File

@ -123,8 +123,7 @@ func testWithTempDir(t *testing.T, fn func(tempDir string)) {
confDir := filepath.Join(tempDir, "conf") confDir := filepath.Join(tempDir, "conf")
mustMkdir(t, confDir, 0700) mustMkdir(t, confDir, 0700)
defer os.Setenv("CAMLI_CONFIG_DIR", os.Getenv("CAMLI_CONFIG_DIR")) t.Setenv("CAMLI_CONFIG_DIR", confDir)
os.Setenv("CAMLI_CONFIG_DIR", confDir)
if err := os.WriteFile(filepath.Join(confDir, "client-config.json"), []byte("{}"), 0644); err != nil { if err := os.WriteFile(filepath.Join(confDir, "client-config.json"), []byte("{}"), 0644); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -114,9 +114,8 @@ func TestDecodeDownsample(t *testing.T) {
// a subprocess to simulate unavailability. // a subprocess to simulate unavailability.
func TestUnavailable(t *testing.T) { func TestUnavailable(t *testing.T) {
checkAvailability = sync.Once{} checkAvailability = sync.Once{}
defer os.Setenv("CAMLI_DISABLE_DJPEG", "0")
if ok, _ := strconv.ParseBool(os.Getenv("CAMLI_DISABLE_DJPEG")); !ok { if ok, _ := strconv.ParseBool(os.Getenv("CAMLI_DISABLE_DJPEG")); !ok {
os.Setenv("CAMLI_DISABLE_DJPEG", "1") t.Setenv("CAMLI_DISABLE_DJPEG", "1")
out, err := exec.Command(os.Args[0], "-test.v", out, err := exec.Command(os.Args[0], "-test.v",
"-test.run=TestUnavailable$").CombinedOutput() "-test.run=TestUnavailable$").CombinedOutput()
if err != nil { if err != nil {
@ -149,14 +148,12 @@ func TestFailed(t *testing.T) {
t.Skip("djpeg isn't available.") t.Skip("djpeg isn't available.")
} }
oldPath := os.Getenv("PATH")
defer os.Setenv("PATH", oldPath)
// Use djpeg that exits after calling false. // Use djpeg that exits after calling false.
newPath, err := filepath.Abs("testdata") newPath, err := filepath.Abs("testdata")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
os.Setenv("PATH", newPath) t.Setenv("PATH", newPath)
t.Log("PATH", os.Getenv("PATH")) t.Log("PATH", os.Getenv("PATH"))
t.Log(exec.LookPath("djpeg")) t.Log(exec.LookPath("djpeg"))

View File

@ -69,8 +69,7 @@ func TestOpenCamliIncludeNoFile(t *testing.T) {
// Test that error occurs if no such file // Test that error occurs if no such file
const notExist = "this_config_doesnt_exist.config" const notExist = "this_config_doesnt_exist.config"
defer os.Setenv("CAMLI_CONFIG_DIR", os.Getenv("CAMLI_CONFIG_DIR")) t.Setenv("CAMLI_CONFIG_DIR", filepath.Join(os.TempDir(), "/x/y/z/not-exist"))
os.Setenv("CAMLI_CONFIG_DIR", filepath.Join(os.TempDir(), "/x/y/z/not-exist"))
_, e := findCamliInclude(notExist) _, e := findCamliInclude(notExist)
if e == nil { if e == nil {
@ -88,8 +87,7 @@ func TestOpenCamliIncludeCWD(t *testing.T) {
defer os.Remove(path) defer os.Remove(path)
// Setting CAMLI_CONFIG_DIR just to avoid triggering failInTests in CamliConfigDir // Setting CAMLI_CONFIG_DIR just to avoid triggering failInTests in CamliConfigDir
defer os.Setenv("CAMLI_CONFIG_DIR", os.Getenv("CAMLI_CONFIG_DIR")) t.Setenv("CAMLI_CONFIG_DIR", "whatever") // Restore after test
os.Setenv("CAMLI_CONFIG_DIR", "whatever") // Restore after test
checkOpen(t, path) checkOpen(t, path)
} }
@ -102,8 +100,7 @@ func TestOpenCamliIncludeDir(t *testing.T) {
t.Errorf("Couldn't create test config file, aborting test: %v", e) t.Errorf("Couldn't create test config file, aborting test: %v", e)
return return
} }
os.Setenv("CAMLI_CONFIG_DIR", td) t.Setenv("CAMLI_CONFIG_DIR", td)
defer os.Setenv("CAMLI_CONFIG_DIR", "")
checkOpen(t, name) checkOpen(t, name)
} }
@ -117,19 +114,17 @@ func TestOpenCamliIncludePath(t *testing.T) {
t.Errorf("Couldn't create test config file, aborting test: %v", e) t.Errorf("Couldn't create test config file, aborting test: %v", e)
return return
} }
defer os.Setenv("CAMLI_INCLUDE_PATH", "")
defer os.Setenv("CAMLI_CONFIG_DIR", os.Getenv("CAMLI_CONFIG_DIR")) t.Setenv("CAMLI_CONFIG_DIR", filepath.Join(td, "/x/y/z/not-exist"))
os.Setenv("CAMLI_CONFIG_DIR", filepath.Join(td, "/x/y/z/not-exist"))
os.Setenv("CAMLI_INCLUDE_PATH", td) t.Setenv("CAMLI_INCLUDE_PATH", td)
checkOpen(t, name) checkOpen(t, name)
const sep = string(filepath.ListSeparator) const sep = string(filepath.ListSeparator)
os.Setenv("CAMLI_INCLUDE_PATH", "/not/a/camli/config/dir"+sep+td) t.Setenv("CAMLI_INCLUDE_PATH", "/not/a/camli/config/dir"+sep+td)
checkOpen(t, name) checkOpen(t, name)
os.Setenv("CAMLI_INCLUDE_PATH", "/not/a/camli/config/dir"+sep+td+sep+"/another/fake/camli/dir") t.Setenv("CAMLI_INCLUDE_PATH", "/not/a/camli/config/dir"+sep+td+sep+"/another/fake/camli/dir")
checkOpen(t, name) checkOpen(t, name)
} }

View File

@ -252,11 +252,10 @@ func TestExpansionsInHighlevelConfig(t *testing.T) {
t.Fatalf("failed to find perkeep.org GOPATH root: %v", err) t.Fatalf("failed to find perkeep.org GOPATH root: %v", err)
} }
const keyID = "26F5ABDA" const keyID = "26F5ABDA"
os.Setenv("TMP_EXPANSION_TEST", keyID) t.Setenv("TMP_EXPANSION_TEST", keyID)
os.Setenv("TMP_EXPANSION_SECRING", filepath.Join(camroot, filepath.FromSlash("pkg/jsonsign/testdata/test-secring.gpg"))) t.Setenv("TMP_EXPANSION_SECRING", filepath.Join(camroot, filepath.FromSlash("pkg/jsonsign/testdata/test-secring.gpg")))
// Setting CAMLI_CONFIG_DIR to avoid triggering failInTests in osutil.PerkeepConfigDir // Setting CAMLI_CONFIG_DIR to avoid triggering failInTests in osutil.PerkeepConfigDir
defer os.Setenv("CAMLI_CONFIG_DIR", os.Getenv("CAMLI_CONFIG_DIR")) // restore after test t.Setenv("CAMLI_CONFIG_DIR", "whatever")
os.Setenv("CAMLI_CONFIG_DIR", "whatever")
conf, err := serverinit.Load([]byte(` conf, err := serverinit.Load([]byte(`
{ {
"auth": "localhost", "auth": "localhost",
@ -294,8 +293,7 @@ func TestInstallHandlers(t *testing.T) {
} }
// Setting CAMLI_CONFIG_DIR to avoid triggering failInTests in osutil.PerkeepConfigDir // Setting CAMLI_CONFIG_DIR to avoid triggering failInTests in osutil.PerkeepConfigDir
defer os.Setenv("CAMLI_CONFIG_DIR", os.Getenv("CAMLI_CONFIG_DIR")) // restore after test t.Setenv("CAMLI_CONFIG_DIR", "whatever")
os.Setenv("CAMLI_CONFIG_DIR", "whatever")
lowConf, err := serverinit.Load(confData) lowConf, err := serverinit.Load(confData)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)