Skip to content

Commit 5185382

Browse files
Juneezeetekton-robot
authored andcommitted
test: use T.Setenv to set env vars in tests
This commit replaces `os.Setenv` with `t.Setenv` in tests. The environment variable is automatically restored to its original value when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.Setenv Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 0fe2931 commit 5185382

File tree

8 files changed

+20
-79
lines changed

8 files changed

+20
-79
lines changed

pkg/apis/config/artifact_bucket_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package config_test
1818

1919
import (
20-
"os"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -79,12 +78,8 @@ func TestGetArtifactBucketConfigName(t *testing.T) {
7978
expected: "config-artifact-bucket-test",
8079
}} {
8180
t.Run(tc.description, func(t *testing.T) {
82-
original := os.Getenv("CONFIG_ARTIFACT_BUCKET_NAME")
83-
defer t.Cleanup(func() {
84-
os.Setenv("CONFIG_ARTIFACT_BUCKET_NAME", original)
85-
})
8681
if tc.artifactsBucketEnvValue != "" {
87-
os.Setenv("CONFIG_ARTIFACT_BUCKET_NAME", tc.artifactsBucketEnvValue)
82+
t.Setenv("CONFIG_ARTIFACT_BUCKET_NAME", tc.artifactsBucketEnvValue)
8883
}
8984
got := config.GetArtifactBucketConfigName()
9085
want := tc.expected

pkg/apis/config/artifact_pvc_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package config_test
1818

1919
import (
20-
"os"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -77,12 +76,8 @@ func TestGetArtifactPVCConfigName(t *testing.T) {
7776
expected: "config-artifact-pvc-test",
7877
}} {
7978
t.Run(tc.description, func(t *testing.T) {
80-
original := os.Getenv("CONFIG_ARTIFACT_PVC_NAME")
81-
defer t.Cleanup(func() {
82-
os.Setenv("CONFIG_ARTIFACT_PVC_NAME", original)
83-
})
8479
if tc.artifactsPVCEnvValue != "" {
85-
os.Setenv("CONFIG_ARTIFACT_PVC_NAME", tc.artifactsPVCEnvValue)
80+
t.Setenv("CONFIG_ARTIFACT_PVC_NAME", tc.artifactsPVCEnvValue)
8681
}
8782
got := config.GetArtifactPVCConfigName()
8883
want := tc.expected

pkg/apis/config/feature_flags_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package config_test
1818

1919
import (
20-
"os"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -142,12 +141,8 @@ func TestGetFeatureFlagsConfigName(t *testing.T) {
142141
expected: "feature-flags-test",
143142
}} {
144143
t.Run(tc.description, func(t *testing.T) {
145-
original := os.Getenv("CONFIG_FEATURE_FLAGS_NAME")
146-
defer t.Cleanup(func() {
147-
os.Setenv("CONFIG_FEATURE_FLAGS_NAME", original)
148-
})
149144
if tc.featureFlagEnvValue != "" {
150-
os.Setenv("CONFIG_FEATURE_FLAGS_NAME", tc.featureFlagEnvValue)
145+
t.Setenv("CONFIG_FEATURE_FLAGS_NAME", tc.featureFlagEnvValue)
151146
}
152147
got := config.GetFeatureFlagsConfigName()
153148
want := tc.expected

pkg/credentials/dockercreds/creds_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestFlagHandling(t *testing.T) {
5353
t.Fatalf("flag.CommandLine.Parse() = %v", err)
5454
}
5555

56-
os.Setenv("HOME", credentials.VolumePath)
56+
t.Setenv("HOME", credentials.VolumePath)
5757
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
5858
t.Fatalf("Write() = %v", err)
5959
}
@@ -103,7 +103,7 @@ func TestFlagHandlingTwice(t *testing.T) {
103103
t.Fatalf("flag.CommandLine.Parse() = %v", err)
104104
}
105105

106-
os.Setenv("HOME", credentials.VolumePath)
106+
t.Setenv("HOME", credentials.VolumePath)
107107
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
108108
t.Fatalf("Write() = %v", err)
109109
}
@@ -281,7 +281,7 @@ func TestMultipleFlagHandling(t *testing.T) {
281281
t.Fatalf("flag.CommandLine.Parse() = %v", err)
282282
}
283283

284-
os.Setenv("HOME", credentials.VolumePath)
284+
t.Setenv("HOME", credentials.VolumePath)
285285
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
286286
t.Fatalf("Write() = %v", err)
287287
}
@@ -313,7 +313,7 @@ func TestNoAuthProvided(t *testing.T) {
313313
if err != nil {
314314
t.Fatalf("flag.CommandLine.Parse() = %v", err)
315315
}
316-
os.Setenv("HOME", credentials.VolumePath)
316+
t.Setenv("HOME", credentials.VolumePath)
317317
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
318318
t.Fatalf("Write() = %v", err)
319319
}

pkg/credentials/gitcreds/creds_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestBasicFlagHandling(t *testing.T) {
5353
t.Fatalf("flag.CommandLine.Parse() = %v", err)
5454
}
5555

56-
os.Setenv("HOME", credentials.VolumePath)
56+
t.Setenv("HOME", credentials.VolumePath)
5757
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
5858
t.Fatalf("Write() = %v", err)
5959
}
@@ -117,7 +117,7 @@ func TestBasicFlagHandlingTwice(t *testing.T) {
117117
t.Fatalf("flag.CommandLine.Parse() = %v", err)
118118
}
119119

120-
os.Setenv("HOME", credentials.VolumePath)
120+
t.Setenv("HOME", credentials.VolumePath)
121121
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
122122
t.Fatalf("Write() = %v", err)
123123
}
@@ -209,7 +209,7 @@ func TestSSHFlagHandling(t *testing.T) {
209209
t.Fatalf("flag.CommandLine.Parse() = %v", err)
210210
}
211211

212-
os.Setenv("HOME", credentials.VolumePath)
212+
t.Setenv("HOME", credentials.VolumePath)
213213
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
214214
t.Fatalf("Write() = %v", err)
215215
}
@@ -294,7 +294,7 @@ func TestSSHFlagHandlingThrice(t *testing.T) {
294294
t.Fatalf("flag.CommandLine.Parse() = %v", err)
295295
}
296296

297-
os.Setenv("HOME", credentials.VolumePath)
297+
t.Setenv("HOME", credentials.VolumePath)
298298
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
299299
t.Fatalf("Write() = %v", err)
300300
}
@@ -472,7 +472,7 @@ func TestBasicBackslashInUsername(t *testing.T) {
472472
t.Fatalf("flag.CommandLine.Parse() = %v", err)
473473
}
474474

475-
os.Setenv("HOME", credentials.VolumePath)
475+
t.Setenv("HOME", credentials.VolumePath)
476476
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
477477
t.Fatalf("Write() = %v", err)
478478
}

pkg/git/git_test.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,27 +29,10 @@ import (
2929

3030
const fileMode = 0755 // rwxr-xr-x
3131

32-
func withTemporaryGitConfig(t *testing.T) func() {
32+
func withTemporaryGitConfig(t *testing.T) {
3333
gitConfigDir := t.TempDir()
3434
key := "GIT_CONFIG_GLOBAL"
35-
t.Helper()
36-
oldValue, envVarExists := os.LookupEnv(key)
37-
if err := os.Setenv(key, filepath.Join(gitConfigDir, "config")); err != nil {
38-
t.Fatal(err)
39-
}
40-
clean := func() {
41-
t.Helper()
42-
if !envVarExists {
43-
if err := os.Unsetenv(key); err != nil {
44-
t.Fatal(err)
45-
}
46-
return
47-
}
48-
if err := os.Setenv(key, oldValue); err != nil {
49-
t.Fatal(err)
50-
}
51-
}
52-
return clean
35+
t.Setenv(key, filepath.Join(gitConfigDir, "config"))
5336
}
5437

5538
func TestValidateGitSSHURLFormat(t *testing.T) {
@@ -264,8 +247,7 @@ func TestEnsureHomeEnv(t *testing.T) {
264247
homeenv = t.TempDir()
265248
}
266249
if tt.homeenvSet {
267-
cleanup := setEnv("HOME", homeenv, t)
268-
defer cleanup()
250+
t.Setenv("HOME", homeenv)
269251
}
270252
// Create SSH creds directory in directory specified by HOME envvar
271253
if err := os.MkdirAll(filepath.Join(homeenv, ".ssh"), fileMode); err != nil {
@@ -432,14 +414,6 @@ func createTempGit(t *testing.T, logger *zap.SugaredLogger, gitDir string, submo
432414
}
433415
}
434416

435-
func setEnv(key, value string, t *testing.T) func() {
436-
previous := os.Getenv(key)
437-
if err := os.Setenv(key, value); err != nil {
438-
t.Errorf("Error setting env var %s to %s: %v", key, value, err)
439-
}
440-
return func() { os.Setenv(key, previous) }
441-
}
442-
443417
func checkLogMessage(logMessage string, log *observer.ObservedLogs, logLine int, t *testing.T) {
444418
if logMessage != "" {
445419
allLogLines := log.All()

pkg/resolution/resolver/git/resolver_test.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -627,27 +627,10 @@ func writeAndCommitToTestRepo(t *testing.T, worktree *git.Worktree, repoDir stri
627627
}
628628

629629
// withTemporaryGitConfig resets the .gitconfig for the duration of the test.
630-
func withTemporaryGitConfig(t *testing.T) func() {
630+
func withTemporaryGitConfig(t *testing.T) {
631631
gitConfigDir := t.TempDir()
632632
key := "GIT_CONFIG_GLOBAL"
633-
t.Helper()
634-
oldValue, envVarExists := os.LookupEnv(key)
635-
if err := os.Setenv(key, filepath.Join(gitConfigDir, "config")); err != nil {
636-
t.Fatal(err)
637-
}
638-
clean := func() {
639-
t.Helper()
640-
if !envVarExists {
641-
if err := os.Unsetenv(key); err != nil {
642-
t.Fatal(err)
643-
}
644-
return
645-
}
646-
if err := os.Setenv(key, oldValue); err != nil {
647-
t.Fatal(err)
648-
}
649-
}
650-
return clean
633+
t.Setenv(key, filepath.Join(gitConfigDir, "config"))
651634
}
652635

653636
func createRequest(repoURL, pathInRepo, revision, specificCommit string, useNthCommit int, commitsByBranch map[string][]string) *v1alpha1.ResolutionRequest {

test/pullrequest/e2e_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ func TestPullRequest(t *testing.T) {
8484
// If provided, set the AUTH_TOKEN environment variable that the
8585
// pullrequest binary expects.
8686
if tc.token != "" {
87-
os.Setenv("AUTH_TOKEN", tc.token)
88-
defer os.Unsetenv("AUTH_TOKEN")
87+
t.Setenv("AUTH_TOKEN", tc.token)
8988
}
9089

9190
if *proxy {
@@ -138,7 +137,7 @@ func startProxy(t *testing.T, dataPath string, header http.Header) {
138137
fmt.Println("http.Serve:", err)
139138
}
140139
}()
141-
os.Setenv("http_proxy", l.Addr().String())
140+
t.Setenv("http_proxy", l.Addr().String())
142141
}
143142

144143
// updateDeps makes sure command is installed locally to resolve any

0 commit comments

Comments
 (0)