Skip to content

Commit

Permalink
glob behaviour fixed in fsnode
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Bilal committed Oct 5, 2021
1 parent 440122b commit 04964b3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
8 changes: 7 additions & 1 deletion kyaml/filesys/fsnode.go
Expand Up @@ -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
Expand All @@ -622,14 +623,19 @@ func (n *fsNode) Glob(pattern string) ([]string, error) {
return err
}
if match {
result = append(result, path)
allFiles = append(allFiles, path)
}
}
return nil
})
if err != nil {
return nil, err
}
if IsHiddenFilePath(pattern) {
result = allFiles
} else {
result = RemoveHiddenFiles(allFiles)
}
sort.Strings(result)
return result, nil
}
Expand Down
55 changes: 47 additions & 8 deletions kyaml/filesys/fsnode_test.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"),
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions kyaml/filesys/fsondisk.go
Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions kyaml/filesys/fsondisk_test.go
Expand Up @@ -6,7 +6,6 @@
package filesys

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -195,7 +194,7 @@ func TestReadFilesRealFS(t *testing.T) {
},
expectedDirs: map[string][]string{
testDir: []string{dir},
dir: []string{nestedDir},
dir: []string{nestedDir},
},
},
"AllHiddenFiles": {
Expand Down Expand Up @@ -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 {
Expand All @@ -246,4 +245,4 @@ func TestReadFilesRealFS(t *testing.T) {
}
})
}
}
}
4 changes: 2 additions & 2 deletions kyaml/filesys/util_test.go
Expand Up @@ -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": {
Expand Down

0 comments on commit 04964b3

Please sign in to comment.