diff options
| author | Eng Zer Jun <engzerjun@gmail.com> | 2023-12-19 10:08:45 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-19 02:08:45 +0000 |
| commit | 483af656e6ec51ff1263743eb4957d7d70e6c51f (patch) | |
| tree | b85def06f7fbb7e08720a021f0d2a97a7c065907 /context_test.go | |
| parent | e6835bcb487cf5795a53561c3a50bf2090847714 (diff) | |
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 <engzerjun@gmail.com>
* minor adjustments
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
---------
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Co-authored-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'context_test.go')
| -rw-r--r-- | context_test.go | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/context_test.go b/context_test.go index 8fd4da71..aa3a4468 100644 --- a/context_test.go +++ b/context_test.go @@ -21,7 +21,7 @@ func TestEnvMapifiesEnvironment(t *testing.T) { func TestEnvGetsUpdatedEnvironment(t *testing.T) { c := &tmplctx{} assert.Empty(t, c.Env()["FOO"]) - require.NoError(t, os.Setenv("FOO", "foo")) + t.Setenv("FOO", "foo") assert.Equal(t, c.Env()["FOO"], "foo") } @@ -42,8 +42,7 @@ func TestCreateContext(t *testing.T) { ".": {URL: ub}, }, } - os.Setenv("foo", "foo: bar") - defer os.Unsetenv("foo") + t.Setenv("foo", "foo: bar") c, err = createTmplContext(ctx, []string{"foo"}, d) require.NoError(t, err) assert.IsType(t, &tmplctx{}, c) @@ -51,8 +50,7 @@ func TestCreateContext(t *testing.T) { ds := ((*tctx)["foo"]).(map[string]interface{}) assert.Equal(t, "bar", ds["foo"]) - os.Setenv("bar", "bar: baz") - defer os.Unsetenv("bar") + t.Setenv("bar", "bar: baz") c, err = createTmplContext(ctx, []string{"."}, d) require.NoError(t, err) assert.IsType(t, map[string]interface{}{}, c) |
