From 60038d44f9f591b4fd13eb7d572365ad1d8216b1 Mon Sep 17 00:00:00 2001 From: Francesc Campoy Date: Wed, 16 Jun 2021 17:16:20 -0700 Subject: [PATCH] Add filesys.FileSystem to ignoreFileMatcher (#3994) --- kyaml/kio/ignorefilesmatcher.go | 13 ++++-- kyaml/kio/ignorefilesmatcher_test.go | 65 ++++++++++++++++++---------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/kyaml/kio/ignorefilesmatcher.go b/kyaml/kio/ignorefilesmatcher.go index 9a6ce924bd..0ba3d83821 100644 --- a/kyaml/kio/ignorefilesmatcher.go +++ b/kyaml/kio/ignorefilesmatcher.go @@ -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 diff --git a/kyaml/kio/ignorefilesmatcher_test.go b/kyaml/kio/ignorefilesmatcher_test.go index a808a586ff..91e9dc15c6 100644 --- a/kyaml/kio/ignorefilesmatcher_test.go +++ b/kyaml/kio/ignorefilesmatcher_test.go @@ -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()) - } + if writeIgnoreFile { + ignoreFilePath := filepath.Join(dir, ignoreFileName) + require.NoError(t, ioutil.WriteFile(ignoreFilePath, []byte(ignoreFileBody), 0600)) } - 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 + }, + + // 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 + }, + } - ignoreFilesMatcher := ignoreFilesMatcher{} - err = ignoreFilesMatcher.readIgnoreFile(dir) - if !assert.NoError(t, err) { - t.FailNow() + 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)) }) } }