Skip to content

Commit fbc5da3

Browse files
committed
pattern: add more test cases for globstar not used on its own
Bash documents globstar as follows: When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories That is, `/**/foo` or `bar/**` both result in globstar globbing where we can match files and nested directories, but `/**foo` or `bar**/` do not place the two star characters in a single pattern, so they behave like a single glob star instead. The interp and expand packages already deals with this correctly, because they first split the glob pattern into path elements, and they deal with one element being exactly "**" in a special way. The pattern package still aims to support "**" by itself, but it does not handle this edge case properly; it treats any occurrence of "**" as a globstar wildcard no matter whether or not it's on its own as a path element. Fix coming in the next commit. For #1149.
1 parent 1d64c22 commit fbc5da3

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

interp/interp_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,10 @@ done <<< 2`,
28772877
"shopt -s globstar; mkdir -p a/b/c a/d; echo ** | sed 's@\\\\@/@g'",
28782878
"a a/b a/b/c a/d\n",
28792879
},
2880+
{
2881+
"shopt -s globstar; mkdir -p a.x a/b.x a/b/c.x; echo **.x ./**.x | sed 's@\\\\@/@g'",
2882+
"a.x ./a.x\n",
2883+
},
28802884
{
28812885
"mkdir foo; touch foo/bar; echo */bar */bar/ | sed 's@\\\\@/@g'",
28822886
"foo/bar */bar/\n",

pattern/pattern_test.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ package pattern
55

66
import (
77
"fmt"
8+
"regexp"
89
"regexp/syntax"
910
"testing"
11+
12+
"github.com/go-quicktest/qt"
1013
)
1114

12-
var translateTests = []struct {
15+
var regexpTests = []struct {
1316
pat string
1417
mode Mode
1518
want string
1619
wantErr bool
20+
21+
mustMatch []string
22+
mustNotMatch []string
1723
}{
1824
{pat: ``, want: ``},
1925
{pat: `foo`, want: `foo`},
@@ -30,7 +36,23 @@ var translateTests = []struct {
3036
{pat: `/**/foo`, mode: Filenames, want: `(?s)/(.*/|)foo`},
3137
{pat: `/**/foo`, mode: Filenames | NoGlobStar, want: `/[^/]*/foo`},
3238
{pat: `/**/à`, mode: Filenames, want: `(?s)/(.*/|)à`},
33-
{pat: `/**foo`, mode: Filenames, want: `(?s)/.*foo`},
39+
{
40+
pat: `/**foo`, mode: Filenames, want: `(?s)/.*foo`,
41+
// These all match because without EntireString, we match substrings.
42+
mustMatch: []string{"/foo", "/prefix-foo", "/foo-suffix", "/sub/foo"},
43+
},
44+
{
45+
pat: `/**foo`, mode: Filenames | EntireString, want: `(?s)^/.*foo$`,
46+
// TODO: /sub/foo should not match; see issue 1149
47+
mustMatch: []string{"/foo", "/prefix-foo", "/sub/foo"},
48+
mustNotMatch: []string{"/foo-suffix"},
49+
},
50+
{
51+
pat: `/foo**`, mode: Filenames | EntireString, want: `(?s)^/foo.*$`,
52+
// TODO: /foo/sub should not match; see issue 1149
53+
mustMatch: []string{"/foo", "/foo-suffix", "/foo/sub"},
54+
mustNotMatch: []string{"/prefix-foo"},
55+
},
3456
{pat: `\*`, want: `\*`},
3557
{pat: `\`, wantErr: true},
3658
{pat: `?`, want: `(?s).`},
@@ -88,7 +110,7 @@ var translateTests = []struct {
88110

89111
func TestRegexp(t *testing.T) {
90112
t.Parallel()
91-
for i, tc := range translateTests {
113+
for i, tc := range regexpTests {
92114
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
93115
got, gotErr := Regexp(tc.pat, tc.mode)
94116
if tc.wantErr && gotErr == nil {
@@ -104,6 +126,13 @@ func TestRegexp(t *testing.T) {
104126
if gotErr == nil && rxErr != nil {
105127
t.Fatalf("regexp/syntax.Parse(%q) failed with %q", got, rxErr)
106128
}
129+
rx := regexp.MustCompile(got)
130+
for _, s := range tc.mustMatch {
131+
qt.Assert(t, qt.IsTrue(rx.MatchString(s)), qt.Commentf("must match: %q", s))
132+
}
133+
for _, s := range tc.mustNotMatch {
134+
qt.Assert(t, qt.IsFalse(rx.MatchString(s)), qt.Commentf("must not match: %q", s))
135+
}
107136
})
108137
}
109138
}

0 commit comments

Comments
 (0)