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