From 483af656e6ec51ff1263743eb4957d7d70e6c51f Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Tue, 19 Dec 2023 10:08:45 +0800 Subject: 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 * minor adjustments Signed-off-by: Dave Henderson --------- Signed-off-by: Eng Zer Jun Signed-off-by: Dave Henderson Co-authored-by: Dave Henderson --- context_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'context_test.go') 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) -- cgit v1.2.3