Skip to content

Commit 7e86c53

Browse files
authored
Merge pull request #45 from hashicorp/bflad-testing-T-Setenv
fs: Replace os.Setenv() usage in testing with (testing.T).Setenv()
2 parents 6196e63 + f2a9ff8 commit 7e86c53

File tree

3 files changed

+4
-27
lines changed

3 files changed

+4
-27
lines changed

fs/fs_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package fs
22

33
import (
44
"context"
5-
"os"
65
"testing"
76

87
"github.com/hashicorp/go-version"
@@ -25,11 +24,7 @@ func TestExactVersion(t *testing.T) {
2524

2625
// TODO: mock out command execution?
2726

28-
originalPath := os.Getenv("PATH")
29-
os.Setenv("PATH", "")
30-
t.Cleanup(func() {
31-
os.Setenv("PATH", originalPath)
32-
})
27+
t.Setenv("PATH", "")
3328

3429
ev := &ExactVersion{
3530
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 := testutil.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 := testutil.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 := testutil.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)