test: use T.TempDir to create temporary test directory (#30803)

This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-04-09 00:34:16 +08:00
committed by GitHub
parent 5b615882e2
commit fedd315275
66 changed files with 373 additions and 852 deletions

View File

@@ -75,7 +75,7 @@ func TestUntaint_lockedState(t *testing.T) {
)
})
statePath := testStateFile(t, state)
unlock, err := testLockState(testDataDir, statePath)
unlock, err := testLockState(t, testDataDir, statePath)
if err != nil {
t.Fatal(err)
}
@@ -106,8 +106,7 @@ func TestUntaint_lockedState(t *testing.T) {
func TestUntaint_backup(t *testing.T) {
// Get a temp cwd
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
testCwd(t)
// Write the temp state
state := states.BuildState(func(s *states.SyncState) {
@@ -162,8 +161,7 @@ test_instance.foo:
func TestUntaint_backupDisable(t *testing.T) {
// Get a temp cwd
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
testCwd(t)
// Write the temp state
state := states.BuildState(func(s *states.SyncState) {
@@ -234,8 +232,7 @@ func TestUntaint_badState(t *testing.T) {
func TestUntaint_defaultState(t *testing.T) {
// Get a temp cwd
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
testCwd(t)
// Write the temp state
state := states.BuildState(func(s *states.SyncState) {
@@ -282,8 +279,7 @@ test_instance.foo:
func TestUntaint_defaultWorkspaceState(t *testing.T) {
// Get a temp cwd
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
testCwd(t)
// Write the temp state
state := states.BuildState(func(s *states.SyncState) {
@@ -420,8 +416,7 @@ because -allow-missing was set.
func TestUntaint_stateOut(t *testing.T) {
// Get a temp cwd
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
testCwd(t)
// Write the temp state
state := states.BuildState(func(s *states.SyncState) {