this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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í <mvdan@mvdan.cc>
Change-Id: I521e896c7349f0320bbe5afc63c45b78a7d8a9eb
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198635
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+44 -4
+6
pkg/path/match_test.go
··· 86 86 {"a[", "x", false, ErrBadPattern}, 87 87 {"a/b[", "x", false, ErrBadPattern}, 88 88 {"*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}, 89 95 } 90 96 91 97 func errp(e error) string {
+38 -4
pkg/tool/file/file_test.go
··· 19 19 "os" 20 20 "path/filepath" 21 21 "reflect" 22 + "runtime" 22 23 "testing" 23 24 24 25 "cuelang.org/go/cue" ··· 26 27 "cuelang.org/go/internal/task" 27 28 "cuelang.org/go/internal/value" 28 29 "cuelang.org/go/pkg/internal" 30 + "github.com/go-quicktest/qt" 29 31 ) 30 32 31 33 func parse(t *testing.T, kind, expr string) cue.Value { ··· 114 116 } 115 117 116 118 func TestGlob(t *testing.T) { 119 + // Simple globbing against testdata. 117 120 v := parse(t, "tool/file.Glob", `{ 118 121 glob: "testdata/input.*" 119 122 }`) 120 123 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)) 123 147 } 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}}))) 126 160 } 127 161 } 128 162
pkg/tool/file/testdata/glob1/glob.leaf

This is a binary file and will not be displayed.

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

This is a binary file and will not be displayed.