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