mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Add filesys.FileSystem to ignoreFileMatcher (#3994)
This commit is contained in:
@@ -4,12 +4,14 @@
|
||||
package kio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/monochromegane/go-gitignore"
|
||||
gitignore "github.com/monochromegane/go-gitignore"
|
||||
"sigs.k8s.io/kustomize/kyaml/ext"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
// ignoreFilesMatcher handles `.krmignore` files, which allows for ignoring
|
||||
@@ -32,6 +34,7 @@ import (
|
||||
// is set to true
|
||||
type ignoreFilesMatcher struct {
|
||||
matchers []matcher
|
||||
fs filesys.FileSystemOrOnDisk
|
||||
}
|
||||
|
||||
// readIgnoreFile checks whether there is a .krmignore file in the path, and
|
||||
@@ -39,9 +42,9 @@ type ignoreFilesMatcher struct {
|
||||
// we just add a matcher that match nothing.
|
||||
func (i *ignoreFilesMatcher) readIgnoreFile(path string) error {
|
||||
i.verifyPath(path)
|
||||
m, err := gitignore.NewGitIgnore(filepath.Join(path, ext.IgnoreFileName()))
|
||||
f, err := i.fs.Open(filepath.Join(path, ext.IgnoreFileName()))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
i.matchers = append(i.matchers, matcher{
|
||||
matcher: gitignore.DummyIgnoreMatcher(false),
|
||||
basePath: path,
|
||||
@@ -50,8 +53,10 @@ func (i *ignoreFilesMatcher) readIgnoreFile(path string) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
i.matchers = append(i.matchers, matcher{
|
||||
matcher: m,
|
||||
matcher: gitignore.NewGitIgnoreFromReader(path, f),
|
||||
basePath: path,
|
||||
})
|
||||
return nil
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
func TestIgnoreFilesMatcher_readIgnoreFile(t *testing.T) {
|
||||
@@ -30,35 +32,52 @@ func TestIgnoreFilesMatcher_readIgnoreFile(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for i := range testCases {
|
||||
test := testCases[i]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
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) {
|
||||
dir, err := ioutil.TempDir("", "kyaml-test")
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if test.writeIgnoreFile {
|
||||
ignoreFilePath := filepath.Join(dir, ".krmignore")
|
||||
err = ioutil.WriteFile(ignoreFilePath, []byte(`
|
||||
testfile.yaml
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
}
|
||||
}
|
||||
testFilePath := filepath.Join(dir, "testfile.yaml")
|
||||
err = ioutil.WriteFile(testFilePath, []byte{}, 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
assert.FailNow(t, err.Error())
|
||||
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
|
||||
},
|
||||
|
||||
ignoreFilesMatcher := ignoreFilesMatcher{}
|
||||
err = ignoreFilesMatcher.readIgnoreFile(dir)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
// 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)))
|
||||
})
|
||||
}
|
||||
assert.Equal(t, test.isMatch, ignoreFilesMatcher.matchFile(testFilePath))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user