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

* test: use `T.TempDir` to create temporary test directory

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>

* test: fix `TestInit_noargs` on Windows

--- FAIL: TestInit_noargs (0.01s)
    testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestInit_noargs3136084632\001: The process cannot access the file because it is being used by another process.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test: fix `TestTreeCommandDefaultCurDir_files` on Windows

--- FAIL: TestTreeCommandDefaultCurDir_files (0.01s)
    testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestTreeCommandDefaultCurDir_files716679291\001: The process cannot access the file because it is being used by another process.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test: fix `TestCreateSetterCommand` on Windows

--- FAIL: TestCreateSetterCommand (13.27s)
    --- FAIL: TestCreateSetterCommand/add_replicas (1.45s)
        testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestCreateSetterCommandadd_replicas176222064\001\k8s-cli-487197005.yaml: The process cannot access the file because it is being used by another process.
    ...
    and all subtests
    ...

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test: fix `TestCreateSubstitutionCommand` on Windows

--- FAIL: TestCreateSubstitutionCommand (4.16s)
    --- FAIL: TestCreateSubstitutionCommand/substitution_replicas (1.30s)
        testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestCreateSubstitutionCommandsubstitution_replicas1352417113\001\k8s-cli-3183612276.yaml: The process cannot access the file because it is being used by another process.
    ...
    and all subtests
    ...

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test: fix `TestDeleteSetterCommand` on Windows

--- FAIL: TestDeleteSetterCommand (4.36s)
    --- FAIL: TestDeleteSetterCommand/delete_replicas (1.31s)
        testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestDeleteSetterCommanddelete_replicas3949811929\001\k8s-cli-957077271.yaml: The process cannot access the file because it is being used by another process.
    ...
    and all subtests
    ...

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test: fix `TestDeleteSubstitutionCommand` on Windows

--- FAIL: TestDeleteSubstitutionCommand (2.35s)
    --- FAIL: TestDeleteSubstitutionCommand/delete_only_subst_if_setter_has_same_name_-_long_ref (1.15s)
        testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestDeleteSubstitutionCommanddelete_only_subst_if_setter_has_same_name_-_long_ref2039728641\001\k8s-cli-1602861478.yaml: The process cannot access the file because it is being used by another process.
    ...
    and all subtests
    ...

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test: fix `TestSetCommand` on Windows

--- FAIL: TestSetCommand (13.76s)
    --- FAIL: TestSetCommand/set_replicas (1.13s)
        testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestSetCommandset_replicas3781384539\001\k8s-cli-1030344459.yaml: The process cannot access the file because it is being used by another process.
    ...
    and all subtests
    ...

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-04-19 00:32:41 +08:00
committed by GitHub
parent a464ed0c59
commit 9452a031ba
32 changed files with 196 additions and 492 deletions

View File

@@ -16,12 +16,10 @@ import (
// TestDiff_identical verifies identical directories return an empty set
func TestDiff_identical(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildest")
assert.NoError(t, err)
s := t.TempDir()
d := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = ioutil.WriteFile(
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
@@ -41,12 +39,10 @@ func TestDiff_identical(t *testing.T) {
// TestDiff_additionalSourceFiles verifies if there are additional files
// in the source, the diff will contain them
func TestDiff_additionalSourceFiles(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildest")
assert.NoError(t, err)
s := t.TempDir()
d := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = os.Mkdir(filepath.Join(s, "a2"), 0700)
@@ -63,12 +59,10 @@ func TestDiff_additionalSourceFiles(t *testing.T) {
// TestDiff_additionalDestFiles verifies if there are additional files
// in the dest, the diff will contain them
func TestDiff_additionalDestFiles(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildest")
assert.NoError(t, err)
s := t.TempDir()
d := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = os.Mkdir(filepath.Join(d, "a1"), 0700)
@@ -86,12 +80,10 @@ func TestDiff_additionalDestFiles(t *testing.T) {
// differ between the source and destination, the diff
// contains the differing files
func TestDiff_srcDestContentsDiffer(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildest")
assert.NoError(t, err)
s := t.TempDir()
d := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = ioutil.WriteFile(
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
@@ -113,12 +105,10 @@ func TestDiff_srcDestContentsDiffer(t *testing.T) {
// TestDiff_srcDestContentsDifferInDirs verifies if identical files
// exist in different directories, they are included in the diff
func TestDiff_srcDestContentsDifferInDirs(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildest")
assert.NoError(t, err)
s := t.TempDir()
d := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = ioutil.WriteFile(
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
@@ -143,12 +133,10 @@ func TestDiff_srcDestContentsDifferInDirs(t *testing.T) {
// TestDiff_skipGitSrc verifies that .git directories in the source
// are not looked at
func TestDiff_skipGitSrc(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildest")
assert.NoError(t, err)
s := t.TempDir()
d := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = ioutil.WriteFile(
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
@@ -184,12 +172,10 @@ func TestDiff_skipGitSrc(t *testing.T) {
// TestDiff_skipGitDest verifies that .git directories in the destination
// are not looked at
func TestDiff_skipGitDest(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildest")
assert.NoError(t, err)
s := t.TempDir()
d := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = ioutil.WriteFile(
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
@@ -215,13 +201,11 @@ func TestDiff_skipGitDest(t *testing.T) {
// TestSyncFile tests if destination file is replaced by source file content
func TestSyncFile(t *testing.T) {
d1, err := ioutil.TempDir("", "")
assert.NoError(t, err)
d2, err := ioutil.TempDir("", "")
assert.NoError(t, err)
d1 := t.TempDir()
d2 := t.TempDir()
f1Name := d1 + "/temp.txt"
f2Name := d2 + "/temp.txt"
err = ioutil.WriteFile(f1Name, []byte("abc"), 0600)
err := ioutil.WriteFile(f1Name, []byte("abc"), 0600)
assert.NoError(t, err)
err = ioutil.WriteFile(f2Name, []byte("def"), 0644)
expectedFileInfo, _ := os.Stat(f2Name)
@@ -237,13 +221,11 @@ func TestSyncFile(t *testing.T) {
// TestSyncFileNoDestFile tests if new file is created at destination with source file content
func TestSyncFileNoDestFile(t *testing.T) {
d1, err := ioutil.TempDir("", "")
assert.NoError(t, err)
d2, err := ioutil.TempDir("", "")
assert.NoError(t, err)
d1 := t.TempDir()
d2 := t.TempDir()
f1Name := d1 + "/temp.txt"
f2Name := d2 + "/temp.txt"
err = ioutil.WriteFile(f1Name, []byte("abc"), 0644)
err := ioutil.WriteFile(f1Name, []byte("abc"), 0644)
assert.NoError(t, err)
err = SyncFile(f1Name, f2Name)
assert.NoError(t, err)
@@ -257,13 +239,11 @@ func TestSyncFileNoDestFile(t *testing.T) {
// TestSyncFileNoSrcFile tests if destination file is deleted if source file doesn't exist
func TestSyncFileNoSrcFile(t *testing.T) {
d1, err := ioutil.TempDir("", "")
assert.NoError(t, err)
d2, err := ioutil.TempDir("", "")
assert.NoError(t, err)
d1 := t.TempDir()
d2 := t.TempDir()
f1Name := d1 + "/temp.txt"
f2Name := d2 + "/temp.txt"
err = ioutil.WriteFile(f2Name, []byte("abc"), 0644)
err := ioutil.WriteFile(f2Name, []byte("abc"), 0644)
assert.NoError(t, err)
err = SyncFile(f1Name, f2Name)
assert.NoError(t, err)
@@ -294,12 +274,10 @@ metadata:
}
func TestCopyDir(t *testing.T) {
s, err := ioutil.TempDir("", "copyutilsrc")
assert.NoError(t, err)
v, err := ioutil.TempDir("", "copyutilvalidate")
assert.NoError(t, err)
s := t.TempDir()
v := t.TempDir()
err = os.Mkdir(filepath.Join(s, "a1"), 0700)
err := os.Mkdir(filepath.Join(s, "a1"), 0700)
assert.NoError(t, err)
err = ioutil.WriteFile(
filepath.Join(s, "a1", "f.yaml"), []byte(`a`), 0600)
@@ -327,8 +305,7 @@ func TestCopyDir(t *testing.T) {
filepath.Join(v, ".gitlab-ci.yml"), []byte(`a`), 0600)
assert.NoError(t, err)
d, err := ioutil.TempDir("", "copyutildestination")
assert.NoError(t, err)
d := t.TempDir()
err = CopyDir(s, d)
assert.NoError(t, err)