mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user