Skip to content

Commit 8b8dfbd

Browse files
acc: Unset all env vars in inprocess mode (#2822)
## Why We need to cleanup existing env vars in the parent CLI process in inprocess mode. Just setting new environment variables is not enough. This was noticed in #2720 and is necessarily for it to work since otherwise we end up with conflicting auth. ## Tests Manually, before local tests would fail when non PAT auth was configured in terminal, for example `selftest/server` fails with when OAuth is configured: ``` Error: inner token: Post "[UUID]/oauth2/token": Post "[UUID]/oauth2/token": unsupported protocol scheme "" ``` Now these local tests pass.
1 parent eb4bcf5 commit 8b8dfbd

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

acceptance/internal/cmd_server.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func StartCmdServer(t *testing.T) *testserver.Server {
2121
var env map[string]string
2222
require.NoError(t, json.Unmarshal([]byte(q.Get("env")), &env))
2323

24-
for key, val := range env {
25-
defer Setenv(t, key, val)()
26-
}
24+
// Change current process's environment to match the callsite.
25+
defer configureEnv(t, env)()
2726

28-
defer Chdir(t, q.Get("cwd"))()
27+
// Change current working directory to match the callsite.
28+
defer chdir(t, q.Get("cwd"))()
2929

3030
c := testcli.NewRunner(t, context.Background(), args...)
3131
c.Verbose = false
@@ -44,9 +44,9 @@ func StartCmdServer(t *testing.T) *testserver.Server {
4444
return server
4545
}
4646

47-
// Chdir variant that is intended to be used with defer so that it can switch back before function ends.
47+
// chdir variant that is intended to be used with defer so that it can switch back before function ends.
4848
// This is unlike testutil.Chdir which switches back only when tests end.
49-
func Chdir(t *testing.T, cwd string) func() {
49+
func chdir(t *testing.T, cwd string) func() {
5050
require.NotEmpty(t, cwd)
5151
prevDir, err := os.Getwd()
5252
require.NoError(t, err)
@@ -57,18 +57,21 @@ func Chdir(t *testing.T, cwd string) func() {
5757
}
5858
}
5959

60-
// Setenv variant that is intended to be used with defer so that it can switch back before function ends.
61-
// This is unlike t.Setenv which switches back only when tests end.
62-
func Setenv(t *testing.T, key, value string) func() {
63-
prevVal, exists := os.LookupEnv(key)
60+
func configureEnv(t *testing.T, env map[string]string) func() {
61+
oldEnv := os.Environ()
6462

65-
require.NoError(t, os.Setenv(key, value))
63+
// Set current process's environment to match the input.
64+
os.Clearenv()
65+
for key, val := range env {
66+
os.Setenv(key, val)
67+
}
6668

69+
// Function callback to use with defer to restore original environment.
6770
return func() {
68-
if exists {
69-
_ = os.Setenv(key, prevVal)
70-
} else {
71-
_ = os.Unsetenv(key)
71+
os.Clearenv()
72+
for _, kv := range oldEnv {
73+
kvs := strings.SplitN(kv, "=", 2)
74+
os.Setenv(kvs[0], kvs[1])
7275
}
7376
}
7477
}

0 commit comments

Comments
 (0)