Skip to content

Commit ba53524

Browse files
authored
fix: use temp directory in creds tests to prevent auth.json pollution (knative#3311)
* fix: use temp directory in creds tests to prevent auth.json pollution Fixes knative#3158 When HOME is empty during tests, testConfigPath now falls back to t.TempDir() instead of returning an empty string. This prevents auth.json from being created in the pkg/creds/ directory. * chore: trigger CI re-run * chore: remove auth.json from .gitignore Since the test now uses a temp directory, auth.json is no longer created in pkg/creds/, so we don't need to ignore it anymore.
1 parent cf46238 commit ba53524

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,3 @@ __pycache__
4545
# TODO: Update this test to be from a temp directory with a hard-coded impl:
4646
# https://github.com/knative/func/issues/3196
4747
/pkg/builders/testdata/go-fn-with-private-deps/.s2i
48-
49-
# TODO: update the test which creates this to run in a temp directory:
50-
# https://github.com/knative/func/issues/3158
51-
/pkg/creds/auth.json

pkg/creds/credentials_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,13 @@ func testHomeEnvName() string {
10241024
func testConfigPath(t *testing.T) string {
10251025
t.Helper()
10261026
home := os.Getenv(testHomeEnvName())
1027-
var configPath string
1028-
if home != "" { // if HOME is not set, don't create config dir
1029-
configPath = filepath.Join(home, ".config", "func")
1030-
if err := os.MkdirAll(configPath, os.ModePerm); err != nil {
1031-
t.Fatal(err)
1032-
}
1027+
// using a temp directory to avoid polluting the source tree with auth.json
1028+
if home == "" {
1029+
home = t.TempDir()
1030+
}
1031+
configPath := filepath.Join(home, ".config", "func")
1032+
if err := os.MkdirAll(configPath, os.ModePerm); err != nil {
1033+
t.Fatal(err)
10331034
}
10341035
return configPath
10351036
}
@@ -1039,6 +1040,9 @@ func testConfigPath(t *testing.T) string {
10391040
func testConfigPathError(t *testing.T) (string, error) {
10401041
t.Helper()
10411042
home := os.Getenv(testHomeEnvName())
1043+
if home == "" {
1044+
home = t.TempDir()
1045+
}
10421046
configPath := filepath.Join(home, ".config", "func")
10431047
return configPath, os.MkdirAll(configPath, os.ModePerm)
10441048
}

0 commit comments

Comments
 (0)