Skip to content

Commit 5ff3f6a

Browse files
committed
fix: prefix on direct match
Signed-off-by: Carlos Alexandro Becker <[email protected]>
1 parent 975308f commit 5ff3f6a

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

glob.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,20 @@ func Glob(pattern string, opts ...OptFunc) ([]string, error) { // nolint:funlen,
130130
// glob contains no dynamic matchers so prefix is the file name that
131131
// the glob references directly. When the glob explicitly references
132132
// a single non-existing file, return an error for the user to check.
133-
return []string{}, fmt.Errorf(`matching "%s": %w`, prefix, fs.ErrNotExist)
133+
return []string{}, fmt.Errorf(`matching "%s%s": %w`, options.prefix, prefix, fs.ErrNotExist)
134134
}
135135

136136
return []string{}, nil
137137
}
138138
if err != nil {
139-
return nil, fmt.Errorf("stat static prefix %q: %w", prefix, err)
139+
return nil, fmt.Errorf("stat static prefix %s%s: %w", options.prefix, prefix, err)
140140
}
141141

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

149149
return []string{}, nil

glob_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ func TestGlob(t *testing.T) { // nolint:funlen
5151
require.Equal(t, fmt.Sprintf("&{fs:%s matchDirectoriesDirectly:false prefix:%s pattern:%s}", prefix, prefix, pattern), w.String())
5252
})
5353

54+
t.Run("real with rootfs direct file", func(t *testing.T) {
55+
t.Parallel()
56+
57+
wd, err := os.Getwd()
58+
require.NoError(t, err)
59+
60+
pattern := toNixPath(filepath.Join(wd, "prefix.go"))
61+
62+
matches, err := Glob(pattern, MaybeRootFS)
63+
require.NoError(t, err)
64+
require.Equal(t, []string{
65+
toNixPath(filepath.Join(wd, "prefix.go")),
66+
}, matches)
67+
})
68+
5469
t.Run("real with rootfs on relative path to parent", func(t *testing.T) {
5570
t.Parallel()
5671

@@ -258,15 +273,15 @@ func TestGlob(t *testing.T) { // nolint:funlen
258273
"./a/nope.txt",
259274
"./a/b/dc",
260275
}, nil)))
261-
require.EqualError(t, err, "matching \"a/b/d\": file does not exist")
276+
require.EqualError(t, err, "matching \"./a/b/d\": file does not exist")
262277
require.True(t, errors.Is(err, os.ErrNotExist))
263278
require.Empty(t, matches)
264279
})
265280

266281
t.Run("escaped direct no match", func(t *testing.T) {
267282
t.Parallel()
268283
matches, err := Glob("a/\\{b\\}", WithFs(testFs(t, nil, nil)))
269-
require.EqualError(t, err, "matching \"a/{b}\": file does not exist")
284+
require.EqualError(t, err, "matching \"./a/{b}\": file does not exist")
270285
require.True(t, errors.Is(err, os.ErrNotExist))
271286
require.Empty(t, matches)
272287
})
@@ -277,7 +292,7 @@ func TestGlob(t *testing.T) { // nolint:funlen
277292
"./a/nope.txt",
278293
"./a/b/dc",
279294
}, nil)))
280-
require.EqualError(t, err, "matching \"a/b/c{a\": file does not exist")
295+
require.EqualError(t, err, "matching \"./a/b/c{a\": file does not exist")
281296
require.Empty(t, matches)
282297
})
283298

0 commit comments

Comments
 (0)