Skip to content

Commit

Permalink
Add filesys.FileSystem to ignoreFileMatcher (#3994)
Browse files Browse the repository at this point in the history
  • Loading branch information
campoy authored Jun 17, 2021
1 parent 24d06f8 commit 60038d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
13 changes: 9 additions & 4 deletions kyaml/kio/ignorefilesmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,16 +34,17 @@ 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
// if it is, reads it in and turns it into a matcher. If we can't find a file,
// 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,
Expand All @@ -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
Expand Down
65 changes: 42 additions & 23 deletions kyaml/kio/ignorefilesmatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
})
}
}
Expand Down

0 comments on commit 60038d4

Please sign in to comment.