Skip to content

Commit

Permalink
fix: prefix on direct match
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Feb 28, 2021
1 parent 975308f commit 5ff3f6a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
6 changes: 3 additions & 3 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,20 @@ func Glob(pattern string, opts ...OptFunc) ([]string, error) { // nolint:funlen,
// glob contains no dynamic matchers so prefix is the file name that
// the glob references directly. When the glob explicitly references
// a single non-existing file, return an error for the user to check.
return []string{}, fmt.Errorf(`matching "%s": %w`, prefix, fs.ErrNotExist)
return []string{}, fmt.Errorf(`matching "%s%s": %w`, options.prefix, prefix, fs.ErrNotExist)
}

return []string{}, nil
}
if err != nil {
return nil, fmt.Errorf("stat static prefix %q: %w", prefix, err)
return nil, fmt.Errorf("stat static prefix %s%s: %w", options.prefix, prefix, err)
}

if !prefixInfo.IsDir() {
// if the prefix is a file, it either has to be
// the only match, or nothing matches at all
if matcher.Match(prefix) {
return []string{prefix}, nil
return cleanFilepaths([]string{prefix}, options.prefix), nil
}

return []string{}, nil
Expand Down
21 changes: 18 additions & 3 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ func TestGlob(t *testing.T) { // nolint:funlen
require.Equal(t, fmt.Sprintf("&{fs:%s matchDirectoriesDirectly:false prefix:%s pattern:%s}", prefix, prefix, pattern), w.String())
})

t.Run("real with rootfs direct file", func(t *testing.T) {
t.Parallel()

wd, err := os.Getwd()
require.NoError(t, err)

pattern := toNixPath(filepath.Join(wd, "prefix.go"))

matches, err := Glob(pattern, MaybeRootFS)
require.NoError(t, err)
require.Equal(t, []string{
toNixPath(filepath.Join(wd, "prefix.go")),
}, matches)
})

t.Run("real with rootfs on relative path to parent", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -258,15 +273,15 @@ func TestGlob(t *testing.T) { // nolint:funlen
"./a/nope.txt",
"./a/b/dc",
}, nil)))
require.EqualError(t, err, "matching \"a/b/d\": file does not exist")
require.EqualError(t, err, "matching \"./a/b/d\": file does not exist")
require.True(t, errors.Is(err, os.ErrNotExist))
require.Empty(t, matches)
})

t.Run("escaped direct no match", func(t *testing.T) {
t.Parallel()
matches, err := Glob("a/\\{b\\}", WithFs(testFs(t, nil, nil)))
require.EqualError(t, err, "matching \"a/{b}\": file does not exist")
require.EqualError(t, err, "matching \"./a/{b}\": file does not exist")
require.True(t, errors.Is(err, os.ErrNotExist))
require.Empty(t, matches)
})
Expand All @@ -277,7 +292,7 @@ func TestGlob(t *testing.T) { // nolint:funlen
"./a/nope.txt",
"./a/b/dc",
}, nil)))
require.EqualError(t, err, "matching \"a/b/c{a\": file does not exist")
require.EqualError(t, err, "matching \"./a/b/c{a\": file does not exist")
require.Empty(t, matches)
})

Expand Down

0 comments on commit 5ff3f6a

Please sign in to comment.