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)

View File

@@ -7,7 +7,6 @@
package filesys
import (
"io/ioutil"
"os"
"path"
"path/filepath"
@@ -19,10 +18,7 @@ import (
func makeTestDir(t *testing.T) (FileSystem, string) {
t.Helper()
fSys := MakeFsOnDisk()
td, err := ioutil.TempDir("", "kustomize_testing_dir")
if err != nil {
t.Fatalf("unexpected error %s", err)
}
td := t.TempDir()
testDir, err := filepath.EvalSymlinks(td)
if err != nil {
t.Fatalf("unexpected error %s", err)
@@ -37,8 +33,7 @@ func makeTestDir(t *testing.T) (FileSystem, string) {
}
func TestCleanedAbs_1(t *testing.T) {
fSys, testDir := makeTestDir(t)
defer os.RemoveAll(testDir)
fSys, _ := makeTestDir(t)
d, f, err := fSys.CleanedAbs("")
if err != nil {
@@ -57,8 +52,7 @@ func TestCleanedAbs_1(t *testing.T) {
}
func TestCleanedAbs_2(t *testing.T) {
fSys, testDir := makeTestDir(t)
defer os.RemoveAll(testDir)
fSys, _ := makeTestDir(t)
d, f, err := fSys.CleanedAbs("/")
if err != nil {
@@ -74,7 +68,6 @@ func TestCleanedAbs_2(t *testing.T) {
func TestCleanedAbs_3(t *testing.T) {
fSys, testDir := makeTestDir(t)
defer os.RemoveAll(testDir)
err := fSys.WriteFile(
filepath.Join(testDir, "foo"), []byte(`foo`))
@@ -96,7 +89,6 @@ func TestCleanedAbs_3(t *testing.T) {
func TestCleanedAbs_4(t *testing.T) {
fSys, testDir := makeTestDir(t)
defer os.RemoveAll(testDir)
err := fSys.MkdirAll(filepath.Join(testDir, "d1", "d2"))
if err != nil {
@@ -136,7 +128,6 @@ func TestCleanedAbs_4(t *testing.T) {
func TestReadFilesRealFS(t *testing.T) {
fSys, testDir := makeTestDir(t)
defer os.RemoveAll(testDir)
dir := path.Join(testDir, "dir")
nestedDir := path.Join(dir, "nestedDir")

View File

@@ -5,7 +5,6 @@ package fixsetters
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -407,14 +406,9 @@ spec:
t.Run(test.name, func(t *testing.T) {
openAPIFileName := "Krmfile"
dir, err := ioutil.TempDir("", "")
if !assert.NoError(t, err) {
t.FailNow()
}
dir := t.TempDir()
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "deploy.yaml"), []byte(test.input), 0600)
err := ioutil.WriteFile(filepath.Join(dir, "deploy.yaml"), []byte(test.input), 0600)
if !assert.NoError(t, err) {
t.FailNow()
}

View File

@@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -24,11 +23,7 @@ import (
)
func TestCommand_dockerfile(t *testing.T) {
d, err := ioutil.TempDir("", "kustomize")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(d)
d := t.TempDir()
// create a function
cmd := command.Build(&framework.SimpleProcessor{}, command.StandaloneEnabled, false)

View File

@@ -872,11 +872,9 @@ func TestFormatFileOrDirectory_skipJsonExtFile(t *testing.T) {
// TestFormatFileOrDirectory_directory verifies that yaml files will be formatted,
// and other files will be ignored
func TestFormatFileOrDirectory_directory(t *testing.T) {
d, err := ioutil.TempDir("", "yamlfmt")
assert.NoError(t, err)
defer os.RemoveAll(d)
d := t.TempDir()
err = os.Mkdir(filepath.Join(d, "config"), 0700)
err := os.Mkdir(filepath.Join(d, "config"), 0700)
assert.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(d, "c1.yaml"), testyaml.UnformattedYaml1, 0600)

View File

@@ -4,8 +4,6 @@
package filters_test
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
@@ -28,11 +26,7 @@ func TestMerge3_Merge(t *testing.T) {
datadir = filepath.Join(filepath.Dir(datadir), "testdata")
// setup the local directory
dir, err := ioutil.TempDir("", "kyaml-test")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(dir)
dir := t.TempDir()
if !assert.NoError(t, copyutil.CopyDir(
filepath.Join(datadir, "dataset1-localupdates"),
@@ -40,7 +34,7 @@ func TestMerge3_Merge(t *testing.T) {
t.FailNow()
}
err = filters.Merge3{
err := filters.Merge3{
OriginalPath: filepath.Join(datadir, "dataset1"),
UpdatedPath: filepath.Join(datadir, "dataset1-remoteupdates"),
DestPath: filepath.Join(dir, "dataset1"),
@@ -74,11 +68,7 @@ func TestMerge3_Merge_path(t *testing.T) {
datadir = filepath.Join(filepath.Dir(datadir), "testdata2")
// setup the local directory
dir, err := ioutil.TempDir("", "kyaml-test")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(dir)
dir := t.TempDir()
if !assert.NoError(t, copyutil.CopyDir(
filepath.Join(datadir, "dataset1-localupdates"),
@@ -86,7 +76,7 @@ func TestMerge3_Merge_path(t *testing.T) {
t.FailNow()
}
err = filters.Merge3{
err := filters.Merge3{
OriginalPath: filepath.Join(datadir, "dataset1"),
UpdatedPath: filepath.Join(datadir, "dataset1-remoteupdates"),
DestPath: filepath.Join(dir, "dataset1"),
@@ -119,11 +109,7 @@ func TestMerge3_Merge_fail(t *testing.T) {
datadir = filepath.Join(filepath.Dir(datadir), "testdata2")
// setup the local directory
dir, err := ioutil.TempDir("", "kyaml-test")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(dir)
dir := t.TempDir()
if !assert.NoError(t, copyutil.CopyDir(
filepath.Join(datadir, "dataset1-localupdates"),
@@ -131,7 +117,7 @@ func TestMerge3_Merge_fail(t *testing.T) {
t.FailNow()
}
err = filters.Merge3{
err := filters.Merge3{
OriginalPath: filepath.Join(datadir, "dataset1"),
UpdatedPath: filepath.Join(datadir, "dataset1-remoteupdates"),
DestPath: filepath.Join(dir, "dataset1"),

View File

@@ -42,8 +42,7 @@ func TestIgnoreFilesMatcher_readIgnoreFile(t *testing.T) {
// onDisk creates a temp directory and returns a nil FileSystem, testing
// the normal conditions under which ignoreFileMatcher is used.
"onDisk": func(writeIgnoreFile bool) (string, filesys.FileSystem) { //nolint:unparam
dir, err := ioutil.TempDir("", "kyaml-test")
require.NoError(t, err)
dir := t.TempDir()
if writeIgnoreFile {
ignoreFilePath := filepath.Join(dir, ignoreFileName)

View File

@@ -39,12 +39,8 @@ func TestSubDirsWithFile(t *testing.T) {
},
}
dir, err := ioutil.TempDir("", "")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(dir)
err = createTestDirStructure(dir)
dir := t.TempDir()
err := createTestDirStructure(dir)
if !assert.NoError(t, err) {
t.FailNow()
}

View File

@@ -648,7 +648,6 @@ metadata:
t.Run(tt.name, func(t *testing.T) {
// setup the test directory
d := setupTest(t)
defer os.RemoveAll(d)
// write the functions to files
var fnPaths []string
@@ -663,11 +662,7 @@ metadata:
// if out of package, write to a separate temp directory
if f.newFnPath || fnPath == "" {
// create a new fn directory
fnPath, err = ioutil.TempDir("", "kustomize-test")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(fnPath)
fnPath = t.TempDir()
fnPaths = append(fnPaths, fnPath)
}
dir = fnPath
@@ -916,7 +911,6 @@ metadata:
func TestCmd_Execute(t *testing.T) {
dir := setupTest(t)
defer os.RemoveAll(dir)
// write a test filter to the directory of configuration
if !assert.NoError(t, ioutil.WriteFile(
@@ -952,7 +946,6 @@ func (f *TestFilter) GetExit() error {
func TestCmd_Execute_deferFailure(t *testing.T) {
dir := setupTest(t)
defer os.RemoveAll(dir)
// write a test filter to the directory of configuration
if !assert.NoError(t, ioutil.WriteFile(
@@ -1026,7 +1019,6 @@ replace: StatefulSet
// TestCmd_Execute_setOutput tests the execution of a filter reading and writing to a dir
func TestCmd_Execute_setFunctionPaths(t *testing.T) {
dir := setupTest(t)
defer os.RemoveAll(dir)
// write a test filter to a separate directory
tmpF, err := ioutil.TempFile("", "filter*.yaml")
@@ -1062,7 +1054,6 @@ func TestCmd_Execute_setFunctionPaths(t *testing.T) {
// TestCmd_Execute_setOutput tests the execution of a filter using an io.Writer as output
func TestCmd_Execute_setOutput(t *testing.T) {
dir := setupTest(t)
defer os.RemoveAll(dir)
// write a test filter
if !assert.NoError(t, ioutil.WriteFile(
@@ -1094,7 +1085,6 @@ func TestCmd_Execute_setOutput(t *testing.T) {
// TestCmd_Execute_setInput tests the execution of a filter using an io.Reader as input
func TestCmd_Execute_setInput(t *testing.T) {
dir := setupTest(t)
defer os.RemoveAll(dir)
if !assert.NoError(t, ioutil.WriteFile(
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) {
return
@@ -1109,11 +1099,7 @@ func TestCmd_Execute_setInput(t *testing.T) {
t.FailNow()
}
outDir, err := ioutil.TempDir("", "kustomize-test")
defer os.RemoveAll(outDir)
if !assert.NoError(t, err) {
t.FailNow()
}
outDir := t.TempDir()
if !assert.NoError(t, ioutil.WriteFile(
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) {
@@ -1142,7 +1128,6 @@ func TestCmd_Execute_setInput(t *testing.T) {
// TestCmd_Execute_enableLogSteps tests the execution of a filter with LogSteps enabled.
func TestCmd_Execute_enableLogSteps(t *testing.T) {
dir := setupTest(t)
defer os.RemoveAll(dir)
// write a test filter to the directory of configuration
if !assert.NoError(t, ioutil.WriteFile(
@@ -1240,10 +1225,7 @@ metadata:
// setupTest initializes a temp test directory containing test data
func setupTest(t *testing.T) string {
t.Helper()
dir, err := ioutil.TempDir("", "kustomize-kyaml-test")
if !assert.NoError(t, err) {
t.FailNow()
}
dir := t.TempDir()
_, filename, _, ok := runtime.Caller(0)
if !assert.True(t, ok) {
@@ -1256,9 +1238,23 @@ func setupTest(t *testing.T) string {
if !assert.NoError(t, copyutil.CopyDir(ds, dir)) {
t.FailNow()
}
cwd, err := os.Getwd()
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.NoError(t, os.Chdir(filepath.Dir(dir))) {
t.FailNow()
}
// Change back the current working directory when the test finishes
t.Cleanup(func() {
if !assert.NoError(t, os.Chdir(cwd)) {
t.FailNow()
}
})
return dir
}

View File

@@ -5,7 +5,6 @@ package setters2
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -1003,10 +1002,8 @@ spec:
// initSchema initializes the openAPI with the definitions from s
func SettersSchema(t *testing.T, s string) *spec.Schema {
t.Helper()
dir, err := ioutil.TempDir("", "")
assert.NoError(t, err)
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(s), 0600)
dir := t.TempDir()
err := ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(s), 0600)
if !assert.NoError(t, err) {
t.FailNow()
}

View File

@@ -5,7 +5,6 @@ package settersutil
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -83,20 +82,10 @@ spec:
for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
srcDir, err := ioutil.TempDir("", "")
if !assert.NoError(t, err) {
t.FailNow()
}
srcDir := t.TempDir()
destDir := t.TempDir()
destDir, err := ioutil.TempDir("", "")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(srcDir)
defer os.RemoveAll(destDir)
err = ioutil.WriteFile(filepath.Join(srcDir, "Krmfile"), []byte(test.srcOpenAPIFile), 0600)
err := ioutil.WriteFile(filepath.Join(srcDir, "Krmfile"), []byte(test.srcOpenAPIFile), 0600)
if !assert.NoError(t, err) {
t.FailNow()
}

View File

@@ -6,7 +6,6 @@ package setters2
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -141,10 +140,8 @@ openAPI:
t.Run(test.name, func(t *testing.T) {
openapi.ResetOpenAPI()
defer openapi.ResetOpenAPI()
dir, err := ioutil.TempDir("", "")
assert.NoError(t, err)
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(test.inputOpenAPIfile), 0600)
dir := t.TempDir()
err := ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(test.inputOpenAPIfile), 0600)
if !assert.NoError(t, err) {
t.FailNow()
}