Skip to content

Commit b494b72

Browse files
committed
fs: Replace os.Setenv() usage in testing with (testing.T).Setenv()
Reference: https://pkg.go.dev/testing#T.Setenv Added in Go 1.17. It will automatically reset the environment variable after each test.
1 parent 4fbe413 commit b494b72

File tree

3 files changed

+4
-26
lines changed

3 files changed

+4
-26
lines changed

fs/fs_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ func TestExactVersion(t *testing.T) {
2727

2828
// TODO: mock out command execution?
2929

30-
originalPath := os.Getenv("PATH")
31-
os.Setenv("PATH", "")
32-
t.Cleanup(func() {
33-
os.Setenv("PATH", originalPath)
34-
})
30+
t.Setenv("PATH", "")
3531

3632
ev := &ExactVersion{
3733
Product: product.Terraform,

fs/fs_unix_test.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@ import (
1717
func TestAnyVersion_notExecutable(t *testing.T) {
1818
testutil.EndToEndTest(t)
1919

20-
originalPath := os.Getenv("PATH")
21-
os.Setenv("PATH", "")
22-
t.Cleanup(func() {
23-
os.Setenv("PATH", originalPath)
24-
})
25-
2620
dirPath, fileName := createTempFile(t, "")
27-
os.Setenv("PATH", dirPath)
21+
t.Setenv("PATH", dirPath)
2822

2923
av := &AnyVersion{
3024
Product: &product.Product{
@@ -41,14 +35,8 @@ func TestAnyVersion_notExecutable(t *testing.T) {
4135
func TestAnyVersion_executable(t *testing.T) {
4236
testutil.EndToEndTest(t)
4337

44-
originalPath := os.Getenv("PATH")
45-
os.Setenv("PATH", "")
46-
t.Cleanup(func() {
47-
os.Setenv("PATH", originalPath)
48-
})
49-
5038
dirPath, fileName := createTempFile(t, "")
51-
os.Setenv("PATH", dirPath)
39+
t.Setenv("PATH", dirPath)
5240

5341
fullPath := filepath.Join(dirPath, fileName)
5442
err := os.Chmod(fullPath, 0700)

fs/fs_windows_test.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ import (
1414
func TestAnyVersion_executable(t *testing.T) {
1515
testutil.EndToEndTest(t)
1616

17-
originalPath := os.Getenv("path")
18-
os.Setenv("path", "")
19-
t.Cleanup(func() {
20-
os.Setenv("path", originalPath)
21-
})
22-
2317
dirPath, fileName := createTempFile(t, "")
24-
os.Setenv("path", dirPath)
18+
t.Setenv("path", dirPath)
2519

2620
av := &AnyVersion{
2721
Product: &product.Product{

0 commit comments

Comments
 (0)