-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use
t.Setenv
to set env vars in tests (#1940)
* 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]> * minor adjustments Signed-off-by: Dave Henderson <[email protected]> --------- Signed-off-by: Eng Zer Jun <[email protected]> Signed-off-by: Dave Henderson <[email protected]> Co-authored-by: Dave Henderson <[email protected]>
- Loading branch information
1 parent
e6835bc
commit 483af65
Showing
11 changed files
with
264 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -484,15 +484,13 @@ func TestGitAuth(t *testing.T) { | |
assert.NilError(t, err) | ||
assert.DeepEqual(t, &http.BasicAuth{Username: "user", Password: "swordfish"}, a) | ||
|
||
os.Setenv("GIT_HTTP_PASSWORD", "swordfish") | ||
defer os.Unsetenv("GIT_HTTP_PASSWORD") | ||
t.Setenv("GIT_HTTP_PASSWORD", "swordfish") | ||
a, err = g.auth(mustParseURL("git+https://[email protected]/foo")) | ||
assert.NilError(t, err) | ||
assert.DeepEqual(t, &http.BasicAuth{Username: "user", Password: "swordfish"}, a) | ||
os.Unsetenv("GIT_HTTP_PASSWORD") | ||
|
||
os.Setenv("GIT_HTTP_TOKEN", "mytoken") | ||
defer os.Unsetenv("GIT_HTTP_TOKEN") | ||
t.Setenv("GIT_HTTP_TOKEN", "mytoken") | ||
a, err = g.auth(mustParseURL("git+https://[email protected]/foo")) | ||
assert.NilError(t, err) | ||
assert.DeepEqual(t, &http.TokenAuth{Token: "mytoken"}, a) | ||
|
@@ -508,25 +506,26 @@ func TestGitAuth(t *testing.T) { | |
assert.Equal(t, "git", sa.User) | ||
} | ||
|
||
key := string(testdata.PEMBytes["ed25519"]) | ||
os.Setenv("GIT_SSH_KEY", key) | ||
defer os.Unsetenv("GIT_SSH_KEY") | ||
a, err = g.auth(mustParseURL("git+ssh://[email protected]/foo")) | ||
assert.NilError(t, err) | ||
ka, ok := a.(*ssh.PublicKeys) | ||
assert.Equal(t, true, ok) | ||
assert.Equal(t, "git", ka.User) | ||
os.Unsetenv("GIT_SSH_KEY") | ||
|
||
key = base64.StdEncoding.EncodeToString(testdata.PEMBytes["ed25519"]) | ||
os.Setenv("GIT_SSH_KEY", key) | ||
defer os.Unsetenv("GIT_SSH_KEY") | ||
a, err = g.auth(mustParseURL("git+ssh://[email protected]/foo")) | ||
assert.NilError(t, err) | ||
ka, ok = a.(*ssh.PublicKeys) | ||
assert.Equal(t, true, ok) | ||
assert.Equal(t, "git", ka.User) | ||
os.Unsetenv("GIT_SSH_KEY") | ||
t.Run("plain string ed25519", func(t *testing.T) { | ||
key := string(testdata.PEMBytes["ed25519"]) | ||
t.Setenv("GIT_SSH_KEY", key) | ||
a, err = g.auth(mustParseURL("git+ssh://[email protected]/foo")) | ||
assert.NilError(t, err) | ||
ka, ok := a.(*ssh.PublicKeys) | ||
assert.Equal(t, true, ok) | ||
assert.Equal(t, "git", ka.User) | ||
}) | ||
|
||
t.Run("base64 ed25519", func(t *testing.T) { | ||
key := base64.StdEncoding.EncodeToString(testdata.PEMBytes["ed25519"]) | ||
t.Setenv("GIT_SSH_KEY", key) | ||
a, err = g.auth(mustParseURL("git+ssh://[email protected]/foo")) | ||
assert.NilError(t, err) | ||
ka, ok := a.(*ssh.PublicKeys) | ||
assert.Equal(t, true, ok) | ||
assert.Equal(t, "git", ka.User) | ||
os.Unsetenv("GIT_SSH_KEY") | ||
}) | ||
} | ||
|
||
func TestRefFromURL(t *testing.T) { | ||
|
Oops, something went wrong.