mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 09:02:53 +00:00
Add support for .krmignore file
This commit is contained in:
@@ -4,59 +4,14 @@
|
||||
package kio_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
. "sigs.k8s.io/kustomize/kyaml/kio"
|
||||
// "sigs.k8s.io/kustomize/kyaml/testutil"
|
||||
)
|
||||
|
||||
// setup creates directories and files for testing
|
||||
type setup struct {
|
||||
// root is the tmp directory
|
||||
root string
|
||||
}
|
||||
|
||||
// setupDirectories creates directories for reading test configuration from
|
||||
func setupDirectories(t *testing.T, dirs ...string) setup {
|
||||
d, err := ioutil.TempDir("", "kyaml-test")
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
err = os.Chdir(d)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
for _, s := range dirs {
|
||||
err = os.MkdirAll(s, 0700)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
return setup{root: d}
|
||||
}
|
||||
|
||||
// writeFile writes a file under the test directory
|
||||
func (s setup) writeFile(t *testing.T, path string, value []byte) {
|
||||
err := os.MkdirAll(filepath.Dir(filepath.Join(s.root, path)), 0700)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(s.root, path), value, 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// clean deletes the test config
|
||||
func (s setup) clean() {
|
||||
os.RemoveAll(s.root)
|
||||
}
|
||||
|
||||
var readFileA = []byte(`---
|
||||
a: b #first
|
||||
---
|
||||
@@ -94,18 +49,18 @@ func TestLocalPackageReader_Read_empty(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_pkg(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s.writeFile(t, filepath.Join("c_test.yaml"), readFileC)
|
||||
s.writeFile(t, filepath.Join("d_test.yaml"), readFileD)
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s.WriteFile(t, filepath.Join("c_test.yaml"), readFileC)
|
||||
s.WriteFile(t, filepath.Join("d_test.yaml"), readFileD)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.root},
|
||||
{path: s.Root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
rfr := LocalPackageReader{PackagePath: p.path}
|
||||
@@ -167,13 +122,13 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
|
||||
s.writeFile(t, filepath.Join("a_test.json"), []byte(`{
|
||||
s.WriteFile(t, filepath.Join("a_test.json"), []byte(`{
|
||||
"a": "b"
|
||||
}`))
|
||||
s.writeFile(t, filepath.Join("b_test.json"), []byte(`{
|
||||
s.WriteFile(t, filepath.Join("b_test.json"), []byte(`{
|
||||
"e": "f",
|
||||
"g": {
|
||||
"h": ["i", "j"]
|
||||
@@ -184,7 +139,7 @@ func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.root},
|
||||
{path: s.Root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
rfr := LocalPackageReader{PackagePath: p.path, MatchFilesGlob: []string{"*.json"}}
|
||||
@@ -217,16 +172,16 @@ func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_file(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.root},
|
||||
{path: s.Root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
rfr := LocalPackageReader{PackagePath: filepath.Join(p.path, "a_test.yaml")}
|
||||
@@ -265,16 +220,16 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_pkgOmitAnnotations(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.root},
|
||||
{path: s.Root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
// empty path
|
||||
@@ -313,16 +268,16 @@ g:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_nestedDirs(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
|
||||
paths := []struct {
|
||||
path string
|
||||
}{
|
||||
{path: "./"},
|
||||
{path: s.root},
|
||||
{path: s.Root},
|
||||
}
|
||||
for _, p := range paths {
|
||||
// empty path
|
||||
@@ -374,13 +329,13 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_matchRegex(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||
|
||||
// empty path
|
||||
rfr := LocalPackageReader{PackagePath: s.root, MatchFilesGlob: []string{`a*.yaml`}}
|
||||
rfr := LocalPackageReader{PackagePath: s.Root, MatchFilesGlob: []string{`a*.yaml`}}
|
||||
nodes, err := rfr.Read()
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
@@ -414,14 +369,14 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.writeFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
|
||||
// empty path
|
||||
rfr := LocalPackageReader{PackagePath: s.root, PackageFileName: "pkgFile"}
|
||||
rfr := LocalPackageReader{PackagePath: s.Root, PackageFileName: "pkgFile"}
|
||||
nodes, err := rfr.Read()
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
@@ -455,14 +410,14 @@ metadata:
|
||||
}
|
||||
|
||||
func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) {
|
||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.clean()
|
||||
s.writeFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.writeFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.writeFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||
defer s.Clean()
|
||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||
|
||||
// empty path
|
||||
rfr := LocalPackageReader{PackagePath: s.root, IncludeSubpackages: true, PackageFileName: "pkgFile"}
|
||||
rfr := LocalPackageReader{PackagePath: s.Root, IncludeSubpackages: true, PackageFileName: "pkgFile"}
|
||||
nodes, err := rfr.Read()
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
|
||||
Reference in New Issue
Block a user