1
0
mirror of https://github.com/moby/moby.git synced 2025-12-04 19:23:06 +03:00

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>
This commit is contained in:
Eng Zer Jun
2022-04-23 17:01:58 +08:00
parent e78f6f9c68
commit 36049a04d2
10 changed files with 39 additions and 153 deletions

View File

@@ -243,8 +243,6 @@ func (s *DockerSuite) TestRunAttachDetachFromConfig(c *testing.T) {
keyA := []byte{97}
// Setup config
homeKey := homedir.Key()
homeVal := homedir.Get()
tmpDir, err := os.MkdirTemp("", "fake-home")
assert.NilError(c, err)
defer os.RemoveAll(tmpDir)
@@ -253,8 +251,7 @@ func (s *DockerSuite) TestRunAttachDetachFromConfig(c *testing.T) {
os.Mkdir(dotDocker, 0600)
tmpCfg := filepath.Join(dotDocker, "config.json")
defer func() { os.Setenv(homeKey, homeVal) }()
os.Setenv(homeKey, tmpDir)
c.Setenv(homedir.Key(), tmpDir)
data := `{
"detachKeys": "ctrl-a,a"
@@ -326,8 +323,6 @@ func (s *DockerSuite) TestRunAttachDetachKeysOverrideConfig(c *testing.T) {
keyA := []byte{97}
// Setup config
homeKey := homedir.Key()
homeVal := homedir.Get()
tmpDir, err := os.MkdirTemp("", "fake-home")
assert.NilError(c, err)
defer os.RemoveAll(tmpDir)
@@ -336,8 +331,7 @@ func (s *DockerSuite) TestRunAttachDetachKeysOverrideConfig(c *testing.T) {
os.Mkdir(dotDocker, 0600)
tmpCfg := filepath.Join(dotDocker, "config.json")
defer func() { os.Setenv(homeKey, homeVal) }()
os.Setenv(homeKey, tmpDir)
c.Setenv(homedir.Key(), tmpDir)
data := `{
"detachKeys": "ctrl-e,e"