Skip to content

Commit

Permalink
filter: ensure MapResult is followed for parent directories
Browse files Browse the repository at this point in the history
Similar to the previous commit, we weren't properly respecting MapResult
return values for parent directories. To resolve this, we just store
into `skipFn` just like previously, and return `SkipDir` instead of
`continue`ing on.

After these changes, the new test passes, and we don't end up including
entries that were in excluded directories, as was happening before:

	Error:      	Not equal:
			expected: "dir y\nfile y/b.txt\n"
			actual  : "file x/a.txt\ndir y\nfile y/b.txt\n"

Signed-off-by: Justin Chadwell <[email protected]>
  • Loading branch information
jedevc committed Feb 19, 2024
1 parent ab7803b commit d9cfebb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,11 @@ func (fs *filterFS) Walk(ctx context.Context, target string, fn gofs.WalkDirFunc
}
if fs.mapFn != nil {
result := fs.mapFn(parentStat.Path, parentStat)
if result == MapResultSkipDir || result == MapResultExclude {
if result == MapResultExclude {
continue
} else if result == MapResultSkipDir {
parentDirs[i].skipFn = true
return filepath.SkipDir
}
}

Expand Down
27 changes: 27 additions & 0 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,33 @@ file includeDir/a.txt
assert.Equal(t, []string{"excludeDir", "includeDir", filepath.FromSlash("includeDir/a.txt")}, walked)
}

func TestWalkerMapSkipDirWithPattern(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD x dir",
"ADD x/a.txt file",
"ADD y dir",
"ADD y/b.txt file",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)

b := &bytes.Buffer{}
err = Walk(context.Background(), d, &FilterOpt{
IncludePatterns: []string{"**/*.txt"},
Map: func(_ string, s *types.Stat) MapResult {
if filepath.Base(s.Path) == "x" {
return MapResultSkipDir
}
return MapResultKeep
},
}, bufWalk(b))
assert.NoError(t, err)

assert.Equal(t, filepath.FromSlash(`dir y
file y/b.txt
`), b.String())
}

func TestWalkerPermissionDenied(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("os.Chmod not fully supported on Windows")
Expand Down

0 comments on commit d9cfebb

Please sign in to comment.