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
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>
268 lines
6.4 KiB
Go
268 lines
6.4 KiB
Go
// Copyright 2019 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package kio
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
|
)
|
|
|
|
func TestIgnoreFilesMatcher_readIgnoreFile(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
writeIgnoreFile bool
|
|
isMatch bool
|
|
}{
|
|
{
|
|
name: "has .krmignore file",
|
|
writeIgnoreFile: true,
|
|
isMatch: true,
|
|
},
|
|
{
|
|
name: "no .krmignore file",
|
|
writeIgnoreFile: false,
|
|
isMatch: false,
|
|
},
|
|
}
|
|
|
|
const (
|
|
ignoreFileName = ".krmignore"
|
|
testFileName = "testfile.yaml"
|
|
ignoreFileBody = "\n" + testFileName + "\n"
|
|
)
|
|
|
|
fsMakers := map[string]func(bool) (string, filesys.FileSystem){
|
|
// 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 := t.TempDir()
|
|
|
|
if writeIgnoreFile {
|
|
ignoreFilePath := filepath.Join(dir, ignoreFileName)
|
|
require.NoError(t, ioutil.WriteFile(ignoreFilePath, []byte(ignoreFileBody), 0600))
|
|
}
|
|
testFilePath := filepath.Join(dir, testFileName)
|
|
require.NoError(t, ioutil.WriteFile(testFilePath, []byte{}, 0600))
|
|
return dir, nil
|
|
},
|
|
|
|
// inMem creates an in-memory FileSystem and returns it.
|
|
"inMem": func(writeIgnoreFile bool) (string, filesys.FileSystem) {
|
|
fs := filesys.MakeEmptyDirInMemory()
|
|
if writeIgnoreFile {
|
|
require.NoError(t, fs.WriteFile(ignoreFileName, []byte(ignoreFileBody)))
|
|
}
|
|
require.NoError(t, fs.WriteFile(testFileName, nil))
|
|
return ".", fs
|
|
},
|
|
}
|
|
|
|
for name, fsMaker := range fsMakers {
|
|
t.Run(name, func(t *testing.T) {
|
|
fsMaker := fsMaker
|
|
for i := range testCases {
|
|
test := testCases[i]
|
|
dir, fs := fsMaker(test.writeIgnoreFile)
|
|
t.Run(test.name, func(t *testing.T) {
|
|
m := ignoreFilesMatcher{}
|
|
m.fs.Set(fs)
|
|
require.NoError(t, m.readIgnoreFile(dir))
|
|
require.Equal(t, test.isMatch, m.matchFile(filepath.Join(dir, testFileName)))
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
var (
|
|
readFileA = []byte(`
|
|
a: a
|
|
---
|
|
c: c
|
|
`)
|
|
readFileB = []byte(`
|
|
b: b
|
|
`)
|
|
)
|
|
|
|
func TestLocalPackageReader_Read_ignoreFile(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
directories []string
|
|
files map[string][]byte
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "ignore file",
|
|
directories: []string{
|
|
filepath.Join("a", "b"),
|
|
filepath.Join("a", "c"),
|
|
},
|
|
files: map[string][]byte{
|
|
"pkgFile": {},
|
|
filepath.Join("a", "b", "a_test.yaml"): readFileA,
|
|
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
|
".krmignore": []byte(`
|
|
a/c/c_test.yaml
|
|
`,
|
|
),
|
|
},
|
|
expected: []string{
|
|
`a: a`,
|
|
`c: c`,
|
|
},
|
|
},
|
|
{
|
|
name: "ignore folder",
|
|
directories: []string{
|
|
filepath.Join("a", "b"),
|
|
filepath.Join("a", "c"),
|
|
},
|
|
files: map[string][]byte{
|
|
"pkgFile": {},
|
|
filepath.Join("a", "b", "a_test.yaml"): readFileA,
|
|
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
|
".krmignore": []byte(`
|
|
a/c
|
|
`,
|
|
),
|
|
},
|
|
expected: []string{
|
|
`a: a`,
|
|
`c: c`,
|
|
},
|
|
},
|
|
{
|
|
name: "krmignore file in subpackage",
|
|
directories: []string{
|
|
filepath.Join("a", "c"),
|
|
},
|
|
files: map[string][]byte{
|
|
"pkgFile": {},
|
|
filepath.Join("a", "c", "a_test.yaml"): readFileA,
|
|
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
|
".krmignore": []byte(`
|
|
d/e/f.yaml
|
|
`,
|
|
),
|
|
filepath.Join("a", "c", "pkgFile"): {},
|
|
filepath.Join("a", "c", ".krmignore"): []byte(`
|
|
a_test.yaml
|
|
`),
|
|
},
|
|
expected: []string{
|
|
`b: b`,
|
|
},
|
|
},
|
|
{
|
|
name: "krmignore files does not affect subpackages",
|
|
directories: []string{
|
|
filepath.Join("a", "c"),
|
|
},
|
|
files: map[string][]byte{
|
|
"pkgFile": {},
|
|
filepath.Join("a", "c", "a_test.yaml"): readFileA,
|
|
filepath.Join("a", "c", "c_test.yaml"): readFileB,
|
|
".krmignore": []byte(`
|
|
a/c/c_test.yaml
|
|
`,
|
|
),
|
|
filepath.Join("a", "c", "pkgFile"): {},
|
|
filepath.Join("a", "c", ".krmignore"): []byte(`
|
|
a_test.yaml
|
|
`),
|
|
},
|
|
expected: []string{
|
|
`b: b`,
|
|
},
|
|
},
|
|
{
|
|
name: "handles a combination of packages and directories",
|
|
directories: []string{
|
|
"a",
|
|
filepath.Join("d", "e"),
|
|
"f",
|
|
},
|
|
files: map[string][]byte{
|
|
"pkgFile": {},
|
|
filepath.Join("d", "pkgFile"): {},
|
|
filepath.Join("d", "e", "pkgFile"): {},
|
|
filepath.Join("f", "pkgFile"): {},
|
|
"manifest.yaml": []byte(`root: root`),
|
|
filepath.Join("a", "manifest.yaml"): []byte(`a: a`),
|
|
filepath.Join("d", "manifest.yaml"): []byte(`d: d`),
|
|
filepath.Join("d", "e", "manifest.yaml"): []byte(`e: e`),
|
|
filepath.Join("f", "manifest.yaml"): []byte(`f: f`),
|
|
filepath.Join("d", ".krmignore"): []byte(`
|
|
manifest.yaml
|
|
`),
|
|
},
|
|
expected: []string{
|
|
`a: a`,
|
|
`e: e`,
|
|
`f: f`,
|
|
`root: root`,
|
|
},
|
|
},
|
|
{
|
|
name: "ignore file can exclude subpackages",
|
|
directories: []string{
|
|
"a",
|
|
},
|
|
files: map[string][]byte{
|
|
"pkgFile": {},
|
|
filepath.Join("a", "pkgFile"): {},
|
|
"manifest.yaml": []byte(`root: root`),
|
|
filepath.Join("a", "manifest.yaml"): []byte(`a: a`),
|
|
".krmignore": []byte(`
|
|
a
|
|
`),
|
|
},
|
|
expected: []string{
|
|
`root: root`,
|
|
},
|
|
},
|
|
}
|
|
|
|
for i := range testCases {
|
|
test := testCases[i]
|
|
t.Run(test.name, func(t *testing.T) {
|
|
s := SetupDirectories(t, test.directories...)
|
|
defer s.Clean()
|
|
for path, content := range test.files {
|
|
s.WriteFile(t, path, content)
|
|
}
|
|
|
|
// empty path
|
|
rfr := LocalPackageReader{
|
|
PackagePath: s.Root,
|
|
IncludeSubpackages: true,
|
|
PackageFileName: "pkgFile",
|
|
OmitReaderAnnotations: true,
|
|
}
|
|
nodes, err := rfr.Read()
|
|
if !assert.NoError(t, err) {
|
|
assert.FailNow(t, err.Error())
|
|
}
|
|
|
|
if !assert.Len(t, nodes, len(test.expected)) {
|
|
assert.FailNow(t, "wrong number items")
|
|
}
|
|
|
|
for i, node := range nodes {
|
|
val, err := node.String()
|
|
assert.NoError(t, err)
|
|
want := strings.ReplaceAll(test.expected[i], "${SEP}", string(filepath.Separator))
|
|
assert.Equal(t, strings.TrimSpace(want), strings.TrimSpace(val))
|
|
}
|
|
})
|
|
}
|
|
}
|