gunk-dev/the-valley

model_test.go

source60ad80d5a5a5main's published tip history
22.4 KiB
1package derive
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "reflect"
10 "slices"
11 "strings"
12 "testing"
13
14 "the-valley/composed"
15)
16
17// TestReadReadsTheWholePolicy holds composed.Read to what a policy says:
18// each check's runner, attribute or command, validity and cost; each
19// class's paths, requirements, pins and carry mode, with what a project
20// turned off left out; and the digest Compose gives the same layers.
21func TestReadReadsTheWholePolicy(t *testing.T) {
22 r := newRig(t)
23 l := newLayers(t, r.glob(t, "examples/policy/instance/*.cue"), r.glob(t, "examples/policy/project/*.cue"), map[string]string{
24 "live.cue": `package verification
25project: checks: "smoke-live": {runner: "command", command: "curl -fsS https://example.org/health", validity: "15m", cost: "slow"}
26project: classes: live: {paths: "deploy/**": true, requires: "smoke-live": true, carry: exact: true}
27`})
28 m, err := r.read(l)
29 if err != nil {
30 t.Fatal(err)
31 }
32 checks := map[string]composed.Check{
33 "prose-format": {Runner: "nix", Attribute: "prose-format", Cost: "quick"},
34 "link-check": {Runner: "nix", Attribute: "link-check", Cost: "quick"},
35 "knowledge-lint": {Runner: "nix", Attribute: "knowledge-lint", Cost: "quick"},
36 "shellcheck": {Runner: "nix", Attribute: "shellcheck", Cost: "quick"},
37 "install-vm": {Runner: "nix", Attribute: "install-vm", Cost: "slow"},
38 "smoke-live": {Runner: "command", Command: "curl -fsS https://example.org/health", Validity: "15m", Cost: "slow"},
39 }
40 if !reflect.DeepEqual(m.Checks, checks) {
41 t.Errorf("checks = %+v", m.Checks)
42 }
43 classes := map[string]composed.Class{
44 "prose": {Paths: []string{"**/*.md"}, Requires: map[string]composed.Pin{"prose-format": composed.Mandatory, "link-check": composed.Mandatory}, Carry: "same-paths"},
45 // The template offers the lint as a default and the project turns
46 // it off, so the class requires nothing.
47 "knowledge": {Paths: []string{".the-valley/**"}, Requires: map[string]composed.Pin{}, Carry: "same-paths"},
48 "scripts": {Paths: []string{"bin/**"}, Requires: map[string]composed.Pin{"shellcheck": composed.Mandatory, "install-vm": composed.Mandatory}, Carry: "same-paths"},
49 "live": {Paths: []string{"deploy/**"}, Requires: map[string]composed.Pin{"smoke-live": composed.Mandatory}, Carry: "exact"},
50 }
51 if !reflect.DeepEqual(m.Classes, classes) {
52 t.Errorf("classes = %+v", m.Classes)
53 }
54 if want := map[string]composed.Pin{"prose-format": composed.Mandatory}; !reflect.DeepEqual(m.Unclassified, want) {
55 t.Errorf("unclassified = %+v", m.Unclassified)
56 }
57 pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project)
58 if err != nil {
59 t.Fatal(err)
60 }
61 if m.Digest != pol.Digest || len(m.Digest) != 64 {
62 t.Errorf("Read's digest %q is not Compose's %q", m.Digest, pol.Digest)
63 }
64
65 // The template's default, left alone, is a default.
66 tmpl := newLayers(t, r.glob(t, "examples/policy/deriver/[ft]*.cue"), []string{r.path("examples/policy/deriver/project.cue")}, nil)
67 if m, err = r.read(tmpl); err != nil {
68 t.Fatal(err)
69 }
70 if got := m.Classes["docs"].Requires; got["c-tmpl"] != composed.Default || got["c-docs"] != composed.Mandatory {
71 t.Errorf("docs requires %v", got)
72 }
73}
74
75// TestReadRefuses holds composed.Read to refusing what it cannot read
76// whole: no schema, a layer with no documents, and a layer that does not
77// compose.
78func TestReadRefuses(t *testing.T) {
79 r := newRig(t)
80 good := newLayers(t, r.glob(t, "examples/policy/smoke/instance/*.cue"), nil, nil)
81 empty := layers{instance: t.TempDir(), project: good.project}
82 broken := newLayers(t, r.glob(t, "examples/policy/smoke/instance/*.cue"), nil, map[string]string{"broken.cue": "package verification\nproject: classes: x: requires: nothing: true\n"})
83 for _, c := range []struct {
84 name, schema string
85 l layers
86 says string
87 }{
88 {"no schema", r.path("schema/absent.cue"), good, "no policy schema at"},
89 {"an empty floor", r.schema, empty, "no policy layer at"},
90 {"a layer that does not compose", r.schema, broken, "do not compose"},
91 } {
92 if _, err := composed.Read(t.Context(), r.cue, c.schema, c.l.instance, c.l.project); err == nil || !strings.Contains(err.Error(), c.says) {
93 t.Errorf("%s: %v, not %q", c.name, err, c.says)
94 }
95 }
96}
97
98// TestTheHardwareRequirementIsRead holds Compose and Read to
99// approval.hardware as cue composes it, over the fixtures under
100// examples/policy/approval/. A floor's `true` and a project's `true` both
101// require a hardware key. A template default the project turned off and a
102// policy that never mentions approval do not. A project's `false` against
103// the floor's `true` is refused, rather than read as either.
104func TestTheHardwareRequirementIsRead(t *testing.T) {
105 r := newRig(t)
106 fixture := func(name string) layers {
107 return newLayers(t, []string{r.path("examples/policy/approval/" + name + ".cue")}, nil, nil)
108 }
109 for _, c := range []struct {
110 fixture string
111 want bool
112 }{
113 {"floor-requires-hardware", true},
114 {"project-requires-hardware", true},
115 {"template-default-declined", false},
116 {"no-requirement", false},
117 } {
118 l := fixture(c.fixture)
119 pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project)
120 if err != nil {
121 t.Fatalf("%s: %v", c.fixture, err)
122 }
123 m, err := r.read(l)
124 if err != nil {
125 t.Fatalf("%s: %v", c.fixture, err)
126 }
127 if pol.HardwareApproval != c.want || m.HardwareApproval != c.want {
128 t.Errorf("%s: Compose reads HardwareApproval %v and Read %v, not %v", c.fixture, pol.HardwareApproval, m.HardwareApproval, c.want)
129 }
130 }
131
132 l := fixture("rejected/project-drops-floor-hardware")
133 if pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project); err == nil || !strings.Contains(err.Error(), "policy.approval.hardware") {
134 t.Errorf("a project dropping the floor's hardware requirement composed to %+v, %v", pol, err)
135 }
136 if m, err := r.read(l); err == nil || !strings.Contains(err.Error(), "policy.approval.hardware") {
137 t.Errorf("a project dropping the floor's hardware requirement was read as %+v, %v", m, err)
138 }
139}
140
141// TestTheApprovalRequirementIsRead holds Compose and Read to
142// approval.required as cue composes it, over the fixtures under
143// examples/policy/approval/. A floor's `true` requires an approval. A
144// project's `false` and a policy that never mentions approval do not, and
145// the two digest differently: a field written out is in the export, and a
146// field left out is not. A project's `false` against the floor's `true` is
147// refused, rather than read as either.
148func TestTheApprovalRequirementIsRead(t *testing.T) {
149 r := newRig(t)
150 fixture := func(name string) layers {
151 return newLayers(t, []string{r.path("examples/policy/approval/" + name + ".cue")}, nil, nil)
152 }
153 digests := map[string]string{}
154 for _, c := range []struct {
155 fixture string
156 required bool
157 }{
158 {"floor-requires-approval", true},
159 {"project-writes-not-required", false},
160 {"no-requirement", false},
161 {"floor-requires-hardware", false},
162 } {
163 l := fixture(c.fixture)
164 pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project)
165 if err != nil {
166 t.Fatalf("%s: %v", c.fixture, err)
167 }
168 m, err := r.read(l)
169 if err != nil {
170 t.Fatalf("%s: %v", c.fixture, err)
171 }
172 if pol.RequiredApproval != c.required || m.RequiredApproval != c.required {
173 t.Errorf("%s: Compose reads RequiredApproval %v and Read %v, not %v", c.fixture, pol.RequiredApproval, m.RequiredApproval, c.required)
174 }
175 digests[c.fixture] = pol.Digest
176 }
177 if digests["project-writes-not-required"] == digests["no-requirement"] {
178 t.Error("a policy writing required: false digests as one leaving the field out")
179 }
180
181 l := fixture("rejected/project-drops-floor-required")
182 if pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project); err == nil || !strings.Contains(err.Error(), "policy.approval.required") {
183 t.Errorf("a project dropping the floor's approval requirement composed to %+v, %v", pol, err)
184 }
185 if m, err := r.read(l); err == nil || !strings.Contains(err.Error(), "policy.approval.required") {
186 t.Errorf("a project dropping the floor's approval requirement was read as %+v, %v", m, err)
187 }
188}
189
190// TestApprovalRequiredChangesNoPolicyThatLeavesItOut composes every policy
191// this repository carries, and every approval case that does not write
192// approval.required, under the schema and under the same schema with the
193// field's declaration taken out, and holds the two digests equal. The
194// digest is SHA-256 of the export, so equal digests are equal export
195// bytes: declaring the optional field changes no policy that leaves it
196// out. The schema without the field is made from the schema itself, so
197// the comparison stays exactly this field's however the rest of the
198// schema changes.
199func TestApprovalRequiredChangesNoPolicyThatLeavesItOut(t *testing.T) {
200 r := newRig(t)
201 schema, err := os.ReadFile(r.schema)
202 if err != nil {
203 t.Fatal(err)
204 }
205 const declaration = "\t\trequired?: bool\n"
206 if n := bytes.Count(schema, []byte(declaration)); n != 1 {
207 t.Fatalf("the schema declares approval.required %d times, not once as %q", n, declaration)
208 }
209 without := filepath.Join(t.TempDir(), "verification.cue")
210 if err := os.WriteFile(without, bytes.Replace(schema, []byte(declaration), nil, 1), 0o644); err != nil {
211 t.Fatal(err)
212 }
213 cases := fixtures(t, r)
214 for _, f := range r.glob(t, "examples/policy/approval/*.cue") {
215 body, err := os.ReadFile(f)
216 if err != nil {
217 t.Fatal(err)
218 }
219 if !bytes.Contains(body, []byte("required:")) {
220 cases = append(cases, fixture{name: "approval-" + strings.TrimSuffix(filepath.Base(f), ".cue"), instance: []string{f}})
221 }
222 }
223 for _, f := range cases {
224 l := newLayers(t, f.instance, f.project, nil)
225 with, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project)
226 if err != nil {
227 t.Fatalf("%s: %v", f.name, err)
228 }
229 was, err := composed.Compose(t.Context(), r.cue, without, l.instance, l.project)
230 if err != nil {
231 t.Fatalf("%s, without approval.required: %v", f.name, err)
232 }
233 if with.Digest != was.Digest {
234 t.Errorf("%s digests %s with approval.required declared and %s without", f.name, with.Digest, was.Digest)
235 }
236 }
237 if len(cases) < 12 {
238 t.Errorf("only %d policies were compared", len(cases))
239 }
240}
241
242// TestAReopenedDefinitionIsRefused holds Compose and Read to refusing an
243// export with a field the schema does not give. Every document of a policy
244// is in one package, so any of them may reopen a schema definition and add
245// a field to it. In the cross-family review's case, the floor writes
246// `hardware: true`, and a project adds `Hardware` to #Policy's approval and
247// writes it false. cue exports both keys, and a reader matching names
248// without regard to case would read whichever comes last.
249//
250// cue unifies the documents in any order, and cannot tell which layer a
251// field came from. The order changes only the order of the keys in the
252// export. So the three documents are composed in every order: the first
253// as the floor layer's document, the other two as the project layer's.
254func TestAReopenedDefinitionIsRefused(t *testing.T) {
255 r := newRig(t)
256 const floor = `package verification
257floor: checks: "c-build": {runner: "nix", attribute: "c-build"}
258floor: classes: code: {paths: "src/**": true, requires: "c-build": true}
259floor: approval: hardware: true
260`
261 docs := []string{floor, "package verification\n#Policy: approval: {Hardware?: bool}\n",
262 "package verification\nproject: approval: {hardware: true, Hardware: false}\n"}
263 layered := func(first string, rest ...string) layers {
264 extra := map[string]string{}
265 for i, doc := range rest {
266 extra[string(rune('a'+i))+".cue"] = doc
267 }
268 l := newLayers(t, nil, nil, extra)
269 if err := os.WriteFile(filepath.Join(l.instance, "a.cue"), []byte(first), 0o644); err != nil {
270 t.Fatal(err)
271 }
272 return l
273 }
274 refused := func(name string, l layers, says string) {
275 t.Helper()
276 if pol, err := composed.Compose(t.Context(), r.cue, r.schema, l.instance, l.project); err == nil || !strings.Contains(err.Error(), says) {
277 t.Errorf("%s: Compose read %+v, %v, not %q", name, pol, err, says)
278 }
279 if m, err := r.read(l); err == nil || !strings.Contains(err.Error(), says) {
280 t.Errorf("%s: Read read %+v, %v, not %q", name, m, err, says)
281 }
282 }
283 for _, o := range [][3]int{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}} {
284 refused("documents in order "+fmt.Sprint(o), layered(docs[o[0]], docs[o[1]], docs[o[2]]),
285 `policy.approval has a field "Hardware" the schema does not give it`)
286 }
287
288 // The same trick at the other objects the reader reads into a struct.
289 for _, c := range []struct{ name, doc, says string }{
290 {"an Approval beside approval", "#Policy: Approval?: {hardware?: bool}\nproject: Approval: hardware: false\n", `policy has a field "Approval"`},
291 {"a check's Runner", "#Check: Runner?: string\nproject: checks: \"c-build\": {runner: \"nix\", attribute: \"c-build\", Runner: \"command\"}\n", `policy.checks.c-build has a field "Runner"`},
292 {"a class's Requires", "#Class: Requires?: [string]: bool\nproject: classes: code: Requires: \"c-build\": false\n", `policy.classes.code has a field "Requires"`},
293 } {
294 refused(c.name, layered(floor, "package verification\n"+c.doc), c.says)
295 }
296
297 // cue cannot export a value of the wrong kind under the schema, or a
298 // key twice in one object, so a stand-in for cue prints each.
299 // encoding/json would read the cap written as a string as a number, and
300 // keep the second of two equal keys.
301 bash, err := exec.LookPath("bash")
302 if err != nil {
303 t.Fatal(err)
304 }
305 l := layered(floor)
306 for _, c := range []struct{ name, export, says string }{
307 {"a requirement written as a string", `"approval":{"hardware":"false"}`, "policy.approval.hardware is not a bool"},
308 {"a cap written as a string", `"restack":{"maxAutomaticVersions":"64"}`, "policy.restack.maxAutomaticVersions is not a number"},
309 {"approval written twice", `"approval":{"hardware":true},"approval":{"hardware":false}`, `policy holds the key "approval" twice`},
310 {"a requirement written twice", `"approval":{"hardware":true,"hardware":false}`, `policy.approval holds the key "hardware" twice`},
311 } {
312 cue := filepath.Join(t.TempDir(), "cue")
313 script := "#!" + bash + "\nprintf '%s' '{\"checks\":{},\"classes\":{},\"unclassified\":{}," + c.export + "}'\n"
314 if err := os.WriteFile(cue, []byte(script), 0o755); err != nil {
315 t.Fatal(err)
316 }
317 if pol, err := composed.Compose(t.Context(), cue, r.schema, l.instance, l.project); err == nil || !strings.Contains(err.Error(), c.says) {
318 t.Errorf("%s: Compose read %+v, %v, not %q", c.name, pol, err, c.says)
319 }
320 }
321}
322
323// TestDeriveOverAModel holds Derive's own rules, over a policy written
324// here rather than composed: the strictest carry mode among the matched
325// classes, a pin that is mandatory where any requirement of it is, a
326// class named unclassified kept apart from the unclassified set, and a
327// requirement of a check the policy does not define refused.
328func TestDeriveOverAModel(t *testing.T) {
329 nix := composed.Check{Runner: "nix", Cost: "quick"}
330 m := &composed.Model{
331 Policy: composed.Policy{Checks: map[string]composed.Check{"a": nix, "b": nix, "u": nix}},
332 Classes: map[string]composed.Class{
333 "loose": {Paths: []string{"loose/**"}, Requires: map[string]composed.Pin{"a": composed.Default}, Carry: "same-paths"},
334 "strict": {Paths: []string{"strict/*"}, Requires: map[string]composed.Pin{"a": composed.Mandatory, "b": composed.Default}, Carry: "same-class"},
335 "unclassified": {Paths: []string{"odd"}, Requires: map[string]composed.Pin{"b": composed.Default}, Carry: "exact"},
336 },
337 Unclassified: map[string]composed.Pin{"u": composed.Mandatory},
338 }
339 res, err := Derive(m, []string{"strict/x", "loose/y/z", "strict/x", "elsewhere"})
340 if err != nil {
341 t.Fatal(err)
342 }
343 want := &Result{
344 Paths: []string{"elsewhere", "loose/y/z", "strict/x"},
345 Classes: []string{"loose", "strict"},
346 Unclassified: []string{"elsewhere"},
347 Carry: "same-class",
348 Required: []Required{
349 {Name: "a", Classes: []string{"loose", "strict"}, Pin: composed.Mandatory, Check: nix},
350 {Name: "b", Classes: []string{"strict"}, Pin: composed.Default, Check: nix},
351 {Name: "u", Classes: []string{}, Unclassified: true, Pin: composed.Mandatory, Check: nix},
352 }, Follows: []Follow{},
353 }
354 if !reflect.DeepEqual(res, want) {
355 t.Errorf("Derive = %+v\nwant %+v", res, want)
356 }
357 if res, err = Derive(m, []string{"odd"}); err != nil || res.Carry != "exact" || len(res.Required) != 1 || res.Required[0].Name != "b" || res.Required[0].Unclassified {
358 t.Errorf("the class named unclassified derives %+v, %v", res, err)
359 }
360 if res, err = Derive(m, nil); err != nil || res.Carry != "same-paths" || len(res.Required) != 0 {
361 t.Errorf("no paths derive %+v, %v", res, err)
362 }
363 m.Unclassified["ghost"] = composed.Mandatory
364 if _, err := Derive(m, []string{"elsewhere"}); err == nil || !strings.Contains(err.Error(), "defines no check") {
365 t.Errorf("a requirement of an undefined check derived: %v", err)
366 }
367}
368
369// TestFollowChecksAreNotRequired holds the schema, composed.Read and Derive
370// to `follows`: a check a class follows is set due and not required, a
371// check the floor requires stays required whatever follows it, a follow
372// of a check the policy does not define does not compose, and
373// composed.ReadUnpinned reads the same policy with its pins unknown.
374func TestFollowChecksAreNotRequired(t *testing.T) {
375 r := newRig(t)
376 follows := func(extra string) layers {
377 return newLayers(t, r.glob(t, "examples/policy/instance/*.cue"), r.glob(t, "examples/policy/project/*.cue"), map[string]string{
378 "follows.cue": "package verification\n" + extra,
379 })
380 }
381 l := follows(`project: classes: live: {paths: "deploy/**": true, follows: "install-vm": true, carry: exact: true}
382project: classes: scripts: follows: "install-vm": true
383`)
384 m, err := r.read(l)
385 if err != nil {
386 t.Fatal(err)
387 }
388 if got := m.Classes["live"].Follows; !reflect.DeepEqual(got, []string{"install-vm"}) {
389 t.Errorf("live follows %v", got)
390 }
391 vm := m.Checks["install-vm"]
392 res, err := Derive(m, []string{"deploy/x"})
393 if err != nil {
394 t.Fatal(err)
395 }
396 if want := []Follow{{Name: "install-vm", Classes: []string{"live"}, Check: vm}}; len(res.Required) != 0 || !reflect.DeepEqual(res.Follows, want) {
397 t.Errorf("deploy/x requires %+v and follows %+v", res.Required, res.Follows)
398 }
399 // The floor requires install-vm of scripts, so it gates the change.
400 if res, err = Derive(m, []string{"deploy/x", "bin/y"}); err != nil {
401 t.Fatal(err)
402 }
403 if len(res.Follows) != 0 || len(res.Required) != 2 || res.Required[0].Name != "install-vm" || res.Required[0].Pin != composed.Mandatory {
404 t.Errorf("deploy/x and bin/y require %+v and follow %+v", res.Required, res.Follows)
405 }
406 u, err := composed.ReadUnpinned(t.Context(), r.cue, r.schema, l.instance, l.project)
407 if err != nil {
408 t.Fatal(err)
409 }
410 if u.Digest != m.Digest || !reflect.DeepEqual(u.Classes["live"], m.Classes["live"]) || u.Classes["scripts"].Requires["install-vm"] != composed.Unknown {
411 t.Errorf("an unpinned read gives digest %s and classes %+v", u.Digest, u.Classes)
412 }
413 if res, err = Derive(u, []string{"deploy/x", "bin/y"}); err != nil || len(res.Required) != 2 || res.Required[0].Pin != composed.Unknown || len(res.Follows) != 0 {
414 t.Errorf("deploy/x and bin/y unpinned require %+v and follow %+v: %v", res.Required, res.Follows, err)
415 }
416 if _, err := r.read(follows(`project: classes: scripts: follows: ghost: true` + "\n")); err == nil {
417 t.Error("a follow of an undefined check composed")
418 }
419}
420
421// TestChangedRefusesWhatGitDoesNotWrite holds Changed to a record cut
422// anywhere, and to the records git never writes.
423func TestChangedRefusesWhatGitDoesNotWrite(t *testing.T) {
424 got, err := Changed([]byte("R087\x00old\x00new\x00D\x00gone\nwith a newline\x00M\x00caf\xe9\x00"))
425 if want := []string{"old", "new", "gone\nwith a newline", "caf\xe9"}; err != nil || !reflect.DeepEqual(got, want) {
426 t.Errorf("Changed = %q, %v", got, err)
427 }
428 for _, out := range []string{"M", "M\x00", "M\x00a", "R100\x00old\x00", "M\x00a\x00R1", "Z\x00a\x00", "M\x00\x00", "\x00a\x00", "R1x\x00a\x00b\x00"} {
429 if got, err := Changed([]byte(out)); err == nil {
430 t.Errorf("Changed(%q) = %q", out, got)
431 }
432 }
433}
434
435// TestChangedReadsOnlyWholeRecords holds Changed to a stream of records of
436// every status git writes, cut at every byte: a cut at the end of a record
437// reads the paths of the records before it, in order, and every other cut
438// is refused.
439func TestChangedReadsOnlyWholeRecords(t *testing.T) {
440 var stream []byte
441 var paths []string
442 whole := map[int][]string{0: nil}
443 for _, rec := range []struct {
444 raw string
445 paths []string
446 }{
447 {"A\x00a b\x00", []string{"a b"}},
448 {"M\x00x.md\tq\x00", []string{"x.md\tq"}},
449 {"R075\x00old\nname\x00caf\xe9/new\x00", []string{"old\nname", "caf\xe9/new"}},
450 {"D\x00\xff*\x00", []string{"\xff*"}},
451 {"T\x00link\x00", []string{"link"}},
452 {"C050\x00src\x00dst\x00", []string{"src", "dst"}},
453 } {
454 stream = append(stream, rec.raw...)
455 paths = append(paths, rec.paths...)
456 whole[len(stream)] = slices.Clone(paths)
457 }
458 for cut := range len(stream) + 1 {
459 got, err := Changed(stream[:cut])
460 if want, ok := whole[cut]; !ok && err == nil {
461 t.Errorf("Changed(%q) read %q from a record cut short", stream[:cut], got)
462 } else if ok && (err != nil || !slices.Equal(got, want)) {
463 t.Errorf("Changed(%q) = %q, %v; want %q", stream[:cut], got, err, want)
464 }
465 }
466}
467
468// TestMatch is the glob language by example.
469func TestMatch(t *testing.T) {
470 for _, c := range []struct {
471 glob, path string
472 want bool
473 }{
474 {"src/*.rs", "src/one.rs", true},
475 {"src/*.rs", "src/deep/two.rs", false},
476 {"src/**/*.rs", "src/one.rs", true},
477 {"src/**/*.rs", "src/deep/two.rs", true},
478 {"a/**/b", "a/b", true},
479 {"a/**/b", "a/x/y/b", true},
480 {"a/**/b", "ab", false},
481 {"docs/**", "docs/x/y", true},
482 {"docs/**", "docs", false},
483 {"**/*.md", "README.md", true},
484 {"**/*.md", "a/b/c.md", true},
485 {"**", "", true},
486 {"a**b", "a/b", false},
487 {"a**b", "axxb", true},
488 {"?", "é", false}, // two bytes
489 {"??", "é", true},
490 {"?", "/", false},
491 {"*", "a\nb", true},
492 {"c.(x)", "c.(x)", true},
493 {"c.(x)", "cx(x)", false},
494 {"docs/", "docs", true}, // one trailing slash is dropped
495 {"docs//", "docs/", true},
496 {"**/**/x", "x", true},
497 {"*", "\xff", true},
498 {"**", "a\nb/c", true},
499 // Every byte but `*`, `?` and `/` matches itself, a backslash and
500 // what a regular expression treats specially included.
501 {"a[b]", "a[b]", true},
502 {"a[b]", "ab", false},
503 {"x+", "xx", false},
504 {"a|b", "a", false},
505 {"^a$", "^a$", true},
506 {"{x}", "{x}", true},
507 {`a\b`, `a\b`, true},
508 {`\*`, `\x`, true},
509 } {
510 if got := Match(c.glob, c.path); got != c.want {
511 t.Errorf("Match(%q, %q) = %v", c.glob, c.path, got)
512 }
513 }
514}