diff --git a/kyaml/filesys/fsnode.go b/kyaml/filesys/fsnode.go index 58bfe72bd75..983815fa90f 100644 --- a/kyaml/filesys/fsnode.go +++ b/kyaml/filesys/fsnode.go @@ -612,6 +612,7 @@ func (n *fsNode) RegExpGlob(pattern string) ([]string, error) { // This is how /bin/ls behaves. func (n *fsNode) Glob(pattern string) ([]string, error) { var result []string + var allFiles []string err := n.WalkMe(func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -622,7 +623,7 @@ func (n *fsNode) Glob(pattern string) ([]string, error) { return err } if match { - result = append(result, path) + allFiles = append(allFiles, path) } } return nil @@ -630,6 +631,11 @@ func (n *fsNode) Glob(pattern string) ([]string, error) { if err != nil { return nil, err } + if IsHiddenFilePath(pattern) { + result = allFiles + } else { + result = RemoveHiddenFiles(allFiles) + } sort.Strings(result) return result, nil } diff --git a/kyaml/filesys/fsnode_test.go b/kyaml/filesys/fsnode_test.go index 00b0c2ca6e1..dcf0406bd4b 100644 --- a/kyaml/filesys/fsnode_test.go +++ b/kyaml/filesys/fsnode_test.go @@ -463,6 +463,13 @@ var bunchOfFiles = []struct { { path: filepath.Join("b", "d", "a", "c", "u"), }, + { + path: filepath.Join("b", "d", ".hidden_file"), + }, + { + path: filepath.Join("b", "d", ".hidden_dir"), + addAsDir: true, + }, } func makeLoadedFileTree(t *testing.T) *fsNode { @@ -579,6 +586,7 @@ func TestExists(t *testing.T) { func TestRegExpGlob(t *testing.T) { n := makeLoadedFileTree(t) expected := []string{ + filepath.Join("b", "d", ".hidden_file"), filepath.Join("b", "d", "a", "c", "i", "beans"), filepath.Join("b", "d", "a", "c", "m"), filepath.Join("b", "d", "a", "c", "u"), @@ -598,16 +606,47 @@ func TestRegExpGlob(t *testing.T) { func TestGlob(t *testing.T) { n := makeLoadedFileTree(t) - expected := []string{ - filepath.Join("b", "d", "x"), - filepath.Join("b", "d", "y"), - filepath.Join("b", "d", "z"), + + tests := map[string]struct { + globPattern string + expectedFiles []string + }{ + "VisibleFiles": { + globPattern: "b/d/*", + expectedFiles: []string{ + filepath.Join("b", "d", "x"), + filepath.Join("b", "d", "y"), + filepath.Join("b", "d", "z"), + }, + }, + "HiddenFiles": { + globPattern: "b/d/.*", + expectedFiles: []string{ + filepath.Join("b", "d", ".hidden_file"), + }, + }, } - paths, err := n.Glob("b/d/*") - if err != nil { - t.Fatalf("glob error: %v", err) + + for test, c := range tests { + t.Run(test, func(t *testing.T) { + paths, err := n.Glob(c.globPattern) + if err != nil { + t.Fatalf("glob error: %v", err) + } + assertEqualStringSlices(t, c.expectedFiles, paths, "glob test") + }) } - assertEqualStringSlices(t, expected, paths, "glob test") + + // expected := []string{ + // filepath.Join("b", "d", "x"), + // filepath.Join("b", "d", "y"), + // filepath.Join("b", "d", "z"), + // } + // paths, err := n.Glob("b/d/*") + // if err != nil { + // t.Fatalf("glob error: %v", err) + // } + // assertEqualStringSlices(t, expected, paths, "glob test") } func assertEqualStringSlices(t *testing.T, expected, actual []string, message string) { diff --git a/kyaml/filesys/fsondisk.go b/kyaml/filesys/fsondisk.go index ad26ea58b9d..7fd02a255a1 100644 --- a/kyaml/filesys/fsondisk.go +++ b/kyaml/filesys/fsondisk.go @@ -90,10 +90,10 @@ func (fsOnDisk) Exists(name string) bool { func (fsOnDisk) Glob(pattern string) ([]string, error) { var result []string allFilePaths, err := filepath.Glob(pattern) - if (err != nil) { + if err != nil { return nil, err } - if (IsHiddenFilePath(pattern)) { + if IsHiddenFilePath(pattern) { result = allFilePaths } else { result = RemoveHiddenFiles(allFilePaths) diff --git a/kyaml/filesys/fsondisk_test.go b/kyaml/filesys/fsondisk_test.go index 3678e80f61e..d00baf18066 100644 --- a/kyaml/filesys/fsondisk_test.go +++ b/kyaml/filesys/fsondisk_test.go @@ -6,7 +6,6 @@ package filesys import ( - "fmt" "io/ioutil" "os" "path" @@ -166,11 +165,11 @@ func TestReadFilesRealFS(t *testing.T) { } // adding all files in every directory that we had defined - for _,d := range dirs { + for _, d := range dirs { if !fSys.IsDir(d) { t.Fatalf("Expected %s to be a dir\n", d) } - for _,f := range files { + for _, f := range files { err = fSys.WriteFile(path.Join(d, f), []byte(f)) if err != nil { t.Fatalf("unexpected error %s", err) @@ -181,7 +180,7 @@ func TestReadFilesRealFS(t *testing.T) { } } - tests := map[string]struct{ + tests := map[string]struct { globPattern string expectedFiles []string expectedDirs map[string][]string // glob returns directories as well, so we need to add those to expected files @@ -195,7 +194,7 @@ func TestReadFilesRealFS(t *testing.T) { }, expectedDirs: map[string][]string{ testDir: []string{dir}, - dir: []string{nestedDir}, + dir: []string{nestedDir}, }, }, "AllHiddenFiles": { @@ -224,11 +223,11 @@ func TestReadFilesRealFS(t *testing.T) { }, } - for n,c := range tests { + for n, c := range tests { t.Run(n, func(t *testing.T) { - for _,d := range dirs { + for _, d := range dirs { var expectedPaths []string - for _,f := range c.expectedFiles { + for _, f := range c.expectedFiles { expectedPaths = append(expectedPaths, path.Join(d, f)) } if c.expectedDirs != nil { @@ -246,4 +245,4 @@ func TestReadFilesRealFS(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/kyaml/filesys/util_test.go b/kyaml/filesys/util_test.go index cb520961443..266444c113f 100644 --- a/kyaml/filesys/util_test.go +++ b/kyaml/filesys/util_test.go @@ -378,8 +378,8 @@ func TestStripLeadingSeps(t *testing.T) { } func TestIsHiddenFilePath(t *testing.T) { - tests := map[string]struct{ - paths []string + tests := map[string]struct { + paths []string expectHidden bool }{ "hiddenGlobs": {