Skip to content

Commit 25bb3d4

Browse files
committed
pkg: add test cases for ** in file patterns or globs
Right now we follow the Go semantics, where it is interpreted as two `*` symbols next to each other, thus behaving like a single star symbol as the second one will never match anything. The following commit will change this behavior, to be shown in the changes made to these added test cases. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I521e896c7349f0320bbe5afc63c45b78a7d8a9eb Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198635 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent a4abb05 commit 25bb3d4

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

pkg/path/match_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ var matchTests = []MatchTest{
8686
{"a[", "x", false, ErrBadPattern},
8787
{"a/b[", "x", false, ErrBadPattern},
8888
{"*x", "xxx", true, nil},
89+
// TODO(mvdan): this should fail; right now "**" happens to behave like "*".
90+
{"**", "ab/c", false, nil},
91+
{"**/c", "ab/c", true, nil},
92+
{"a/b/**", "", false, nil},
93+
{"\\**", "*ab", true, nil},
94+
{"[x**y]", "*", true, nil},
8995
}
9096

9197
func errp(e error) string {

pkg/tool/file/file_test.go

+38-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import (
1919
"os"
2020
"path/filepath"
2121
"reflect"
22+
"runtime"
2223
"testing"
2324

2425
"cuelang.org/go/cue"
2526
"cuelang.org/go/cue/parser"
2627
"cuelang.org/go/internal/task"
2728
"cuelang.org/go/internal/value"
2829
"cuelang.org/go/pkg/internal"
30+
"github.com/go-quicktest/qt"
2931
)
3032

3133
func parse(t *testing.T, kind, expr string) cue.Value {
@@ -114,15 +116,47 @@ func TestCreate(t *testing.T) {
114116
}
115117

116118
func TestGlob(t *testing.T) {
119+
// Simple globbing against testdata.
117120
v := parse(t, "tool/file.Glob", `{
118121
glob: "testdata/input.*"
119122
}`)
120123
got, err := (*cmdGlob).Run(nil, &task.Context{Obj: v})
121-
if err != nil {
122-
t.Fatal(err)
124+
qt.Assert(t, qt.IsNil(err))
125+
qt.Assert(t, qt.DeepEquals(got, any(map[string]any{"files": []string{"testdata/input.foo"}})))
126+
127+
// globstar or recursive globbing is not supported.
128+
// TODO(mvdan): this should fail; right now "**" happens to behave like "*".
129+
v = parse(t, "tool/file.Glob", `{
130+
glob: "testdata/**/glob.leaf"
131+
}`)
132+
got, err = (*cmdGlob).Run(nil, &task.Context{Obj: v})
133+
qt.Assert(t, qt.IsNil(err))
134+
qt.Assert(t, qt.DeepEquals(got, any(map[string]any{"files": []string{"testdata/glob1/glob.leaf"}})))
135+
}
136+
137+
func TestGlobEscapeStar(t *testing.T) {
138+
// `\**` is disallowed in a pattern on Windows, as the backslash is a path separator,
139+
// hence `**` is treated as a globstar which is not yet supported.
140+
// `\**` is allowed on other OSes as the first star is escaped, and only the second
141+
// is treated as a wildcard. Thus such a pattern should match a file like `*.test`.
142+
dir := t.TempDir()
143+
leafFile := filepath.Join(dir, "*.test")
144+
if runtime.GOOS != "windows" {
145+
err := os.WriteFile(leafFile, nil, 0o666)
146+
qt.Assert(t, qt.IsNil(err))
123147
}
124-
if want := map[string]interface{}{"files": []string{"testdata/input.foo"}}; !reflect.DeepEqual(got, want) {
125-
t.Errorf("got %v; want %v", got, want)
148+
149+
v := parse(t, "tool/file.Glob", `{
150+
glob: "`+filepath.ToSlash(dir)+`/\\**"
151+
}`)
152+
got, err := (*cmdGlob).Run(nil, &task.Context{Obj: v})
153+
if runtime.GOOS == "windows" {
154+
// TODO(mvdan): this should fail; right now "**" happens to behave like "*".
155+
qt.Assert(t, qt.IsNil(err))
156+
qt.Assert(t, qt.DeepEquals(got, any(map[string]any{"files": []string(nil)})))
157+
} else {
158+
qt.Assert(t, qt.IsNil(err))
159+
qt.Assert(t, qt.DeepEquals(got, any(map[string]any{"files": []string{leafFile}})))
126160
}
127161
}
128162

pkg/tool/file/testdata/glob1/glob.leaf

Whitespace-only changes.

pkg/tool/file/testdata/glob1/glob2/glob.leaf

Whitespace-only changes.

0 commit comments

Comments
 (0)