From 5ff3f6a2beebe2aa07bb3e0403b22493f9d37336 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 28 Feb 2021 13:40:53 -0300 Subject: [PATCH] fix: prefix on direct match Signed-off-by: Carlos Alexandro Becker --- glob.go | 6 +++--- glob_test.go | 21 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/glob.go b/glob.go index 3d0c3ca..738f663 100644 --- a/glob.go +++ b/glob.go @@ -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 diff --git a/glob_test.go b/glob_test.go index 332f5d1..5283bd3 100644 --- a/glob_test.go +++ b/glob_test.go @@ -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() @@ -258,7 +273,7 @@ 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) }) @@ -266,7 +281,7 @@ func TestGlob(t *testing.T) { // nolint:funlen 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) }) @@ -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) })