Skip to content

Commit

Permalink
Merge pull request #45 from hashicorp/bflad-testing-T-Setenv
Browse files Browse the repository at this point in the history
fs: Replace os.Setenv() usage in testing with (testing.T).Setenv()
  • Loading branch information
bflad committed Nov 30, 2021
2 parents 6196e63 + f2a9ff8 commit 7e86c53
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 27 deletions.
7 changes: 1 addition & 6 deletions fs/fs_test.go
Expand Up @@ -2,7 +2,6 @@ package fs

import (
"context"
"os"
"testing"

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

// TODO: mock out command execution?

originalPath := os.Getenv("PATH")
os.Setenv("PATH", "")
t.Cleanup(func() {
os.Setenv("PATH", originalPath)
})
t.Setenv("PATH", "")

ev := &ExactVersion{
Product: product.Terraform,
Expand Down
16 changes: 2 additions & 14 deletions fs/fs_unix_test.go
Expand Up @@ -17,14 +17,8 @@ import (
func TestAnyVersion_notExecutable(t *testing.T) {
testutil.EndToEndTest(t)

originalPath := os.Getenv("PATH")
os.Setenv("PATH", "")
t.Cleanup(func() {
os.Setenv("PATH", originalPath)
})

dirPath, fileName := testutil.CreateTempFile(t, "")
os.Setenv("PATH", dirPath)
t.Setenv("PATH", dirPath)

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

originalPath := os.Getenv("PATH")
os.Setenv("PATH", "")
t.Cleanup(func() {
os.Setenv("PATH", originalPath)
})

dirPath, fileName := testutil.CreateTempFile(t, "")
os.Setenv("PATH", dirPath)
t.Setenv("PATH", dirPath)

fullPath := filepath.Join(dirPath, fileName)
err := os.Chmod(fullPath, 0700)
Expand Down
8 changes: 1 addition & 7 deletions fs/fs_windows_test.go
Expand Up @@ -14,14 +14,8 @@ import (
func TestAnyVersion_executable(t *testing.T) {
testutil.EndToEndTest(t)

originalPath := os.Getenv("path")
os.Setenv("path", "")
t.Cleanup(func() {
os.Setenv("path", originalPath)
})

dirPath, fileName := testutil.CreateTempFile(t, "")
os.Setenv("path", dirPath)
t.Setenv("path", dirPath)

av := &AnyVersion{
Product: &product.Product{
Expand Down

0 comments on commit 7e86c53

Please sign in to comment.