Repository navigation
Expand file tree
/
Copy pathconformance_test.go
More file actions
499 lines (459 loc) · 14.3 KB
/
Copy pathconformance_test.go
File metadata and controls
499 lines (459 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package gitignore_test
import (
"bytes"
"math/rand"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"testing"
"github.com/git-pkgs/gitignore"
)
// TestConformance compares Match against git check-ignore for every case
// under testdata/conformance. Each case directory contains:
//
// gitignore pattern file
// paths one query path per line; a trailing slash marks a directory
// skip optional; when present, divergences are logged instead of failed
//
// The test builds a temporary git repository per case, materialises every
// path, asks git check-ignore for all of them in one call, and checks the
// library returns the same answer.
func TestConformance(t *testing.T) {
requireGit(t)
isolateGitEnv(t)
cases, err := filepath.Glob("testdata/conformance/*")
if err != nil {
t.Fatal(err)
}
if len(cases) == 0 {
t.Fatal("no conformance cases found under testdata/conformance")
}
for _, dir := range cases {
info, err := os.Stat(dir)
if err != nil || !info.IsDir() {
continue
}
name := filepath.Base(dir)
t.Run(name, func(t *testing.T) {
runConformanceCase(t, dir)
})
}
}
func runConformanceCase(t *testing.T, dir string) {
patterns, err := os.ReadFile(filepath.Join(dir, "gitignore"))
if err != nil {
t.Fatalf("read gitignore: %v", err)
}
rawPaths, err := os.ReadFile(filepath.Join(dir, "paths"))
if err != nil {
t.Fatalf("read paths: %v", err)
}
skipReason, _ := os.ReadFile(filepath.Join(dir, "skip"))
paths := parsePathList(string(rawPaths))
if len(paths) == 0 {
t.Fatal("no paths to check")
}
root := buildRepo(t, string(patterns), paths)
m := gitignore.New(root)
want := gitCheckIgnore(t, root, paths)
report := func(format string, args ...any) {
if len(skipReason) > 0 {
t.Logf("(known divergence) "+format, args...)
} else {
t.Errorf(format, args...)
}
}
diverged := 0
for _, p := range paths {
got := m.Match(p.query())
if got != want[p.rel] {
diverged++
report("path %q (isDir=%v): library=%v git=%v", p.rel, p.isDir, got, want[p.rel])
}
}
if diverged == 0 && len(skipReason) > 0 {
t.Errorf("case is marked skip (%s) but no longer diverges; remove the skip file",
strings.TrimSpace(string(skipReason)))
}
}
// TestConformanceHarnessSelfCheck verifies the batched gitCheckIgnore parse
// by re-asking git for each path individually with the simple exit-code form
// and comparing answers. It exercises every case under testdata/conformance.
// If this fails and TestConformance passes, the batched harness is wrong.
func TestConformanceHarnessSelfCheck(t *testing.T) {
requireGit(t)
isolateGitEnv(t)
cases, _ := filepath.Glob("testdata/conformance/*")
for _, dir := range cases {
info, err := os.Stat(dir)
if err != nil || !info.IsDir() {
continue
}
t.Run(filepath.Base(dir), func(t *testing.T) {
patterns, _ := os.ReadFile(filepath.Join(dir, "gitignore"))
rawPaths, _ := os.ReadFile(filepath.Join(dir, "paths"))
paths := parsePathList(string(rawPaths))
root := buildRepo(t, string(patterns), paths)
batched := gitCheckIgnore(t, root, paths)
for _, p := range paths {
cmd := exec.Command("git", "check-ignore", "-q", "--no-index", p.rel)
cmd.Dir = root
single := cmd.Run() == nil
if single != batched[p.rel] {
t.Errorf("path %q: batched=%v single-call=%v", p.rel, batched[p.rel], single)
}
}
})
}
}
func TestConformanceGitStartupFailure(t *testing.T) {
requireGit(t)
if os.Getenv("GITIGNORE_TEST_STARTUP_FAILURE") == "1" {
gitCheckIgnore(t, filepath.Join(t.TempDir(), "missing"), []conformancePath{{rel: "file"}})
return
}
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(executable, "-test.run=^TestConformanceGitStartupFailure$")
cmd.Env = append(os.Environ(), "GITIGNORE_TEST_STARTUP_FAILURE=1")
out, err := cmd.CombinedOutput()
if ee, ok := err.(*exec.ExitError); !ok || ee.ExitCode() != 1 {
t.Fatalf("expected test failure, got %v\n%s", err, out)
}
if !bytes.Contains(out, []byte("git check-ignore:")) || !bytes.Contains(out, []byte("missing")) {
t.Fatalf("missing startup error diagnostic:\n%s", out)
}
if bytes.Contains(out, []byte("panic:")) {
t.Fatalf("startup failure caused a panic:\n%s", out)
}
}
func TestConformanceNestedWalk(t *testing.T) {
requireGit(t)
isolateGitEnv(t)
paths := parsePathList("a/cache/x\na/keep.log\na/drop.log\na/sub/keep.log\na/sub/drop.log\nb/drop.log\nb/keep.log\nb/cache/x\nblocked/sub/file\n")
root := buildRepo(t, "*.log\nblocked/\n", paths)
for dir, patterns := range map[string]string{
"a": "!keep.log\ncache/\n", "a/sub": "!drop.log\n",
"b": "!drop.log\n", "blocked/sub": "!file\n",
} {
if err := os.WriteFile(filepath.Join(root, dir, ".gitignore"), []byte(patterns), 0o644); err != nil {
t.Fatal(err)
}
}
want := gitCheckIgnore(t, root, paths)
m := gitignore.NewFromDirectory(root)
for _, p := range paths {
if got := m.Match(p.query()); got != want[p.rel] {
t.Errorf("NewFromDirectory Match(%q) = %v, git = %v", p.rel, got, want[p.rel])
}
}
for _, start := range []string{"", "a", "a/sub", "b", "blocked/sub"} {
visited := make(map[string]bool)
visit := func(path string, _ os.DirEntry) error {
visited[filepath.ToSlash(path)] = true
return nil
}
var err error
if start == "" {
err = gitignore.Walk(root, visit)
} else {
err = gitignore.WalkFrom(root, start, visit)
}
if err != nil {
t.Fatal(err)
}
for _, p := range paths {
inScope := start == "" || strings.HasPrefix(p.rel, start+"/")
if expected := inScope && !want[p.rel]; visited[p.rel] != expected {
t.Errorf("walk from %q: visited %q = %v, want %v", start, p.rel, visited[p.rel], expected)
}
}
}
}
// TestConformanceFuzz generates random pattern sets and paths, then compares
// the library against git check-ignore. It is skipped under -short.
func TestConformanceFuzz(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuzz comparison in short mode")
}
requireGit(t)
isolateGitEnv(t)
const (
maxPatterns = 6
pathsPerRound = 30
)
rounds := envInt("GITIGNORE_FUZZ_ROUNDS", 40)
seed := int64(envInt("GITIGNORE_FUZZ_SEED", 1))
rng := rand.New(rand.NewSource(seed))
for r := 0; r < rounds; r++ {
patterns := randomPatterns(rng, 1+rng.Intn(maxPatterns))
paths := randomPaths(rng, pathsPerRound)
root := buildRepo(t, patterns, paths)
m := gitignore.New(root)
want := gitCheckIgnore(t, root, paths)
for _, p := range paths {
got := m.Match(p.query())
if got != want[p.rel] {
t.Errorf("round %d\npatterns:\n%s\npath %q (isDir=%v): library=%v git=%v",
r, indent(patterns), p.rel, p.isDir, got, want[p.rel])
}
}
_ = os.RemoveAll(root)
}
}
type conformancePath struct {
rel string
isDir bool
}
func (p conformancePath) query() string {
if p.isDir {
return p.rel + "/"
}
return p.rel
}
func parsePathList(s string) []conformancePath {
var out []conformancePath
for _, line := range strings.Split(s, "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
isDir := strings.HasSuffix(line, "/")
out = append(out, conformancePath{
rel: strings.TrimSuffix(line, "/"),
isDir: isDir,
})
}
return out
}
// buildRepo creates a temporary git repository containing the given
// .gitignore and materialises each path as a file or directory so that
// git check-ignore has real filesystem entries to consult.
func buildRepo(t *testing.T, patterns string, paths []conformancePath) string {
t.Helper()
root := t.TempDir()
cmd := exec.Command("git", "-c", "init.defaultBranch=main", "init", "-q")
cmd.Dir = root
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git init: %v\n%s", err, out)
}
if err := os.WriteFile(filepath.Join(root, ".gitignore"), []byte(patterns), 0o644); err != nil {
t.Fatal(err)
}
// Create directories first, deepest last is fine since MkdirAll handles
// intermediates. Create files afterwards so a file path does not get
// turned into a directory by a later entry that lives under it. Sort
// files shortest-first for the same reason in the other direction.
var files, dirs []conformancePath
for _, p := range paths {
if p.isDir {
dirs = append(dirs, p)
} else {
files = append(files, p)
}
}
sort.Slice(files, func(i, j int) bool { return len(files[i].rel) < len(files[j].rel) })
for _, p := range dirs {
if err := os.MkdirAll(filepath.Join(root, filepath.FromSlash(p.rel)), 0o755); err != nil {
t.Fatal(err)
}
}
for _, p := range files {
full := filepath.Join(root, filepath.FromSlash(p.rel))
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
t.Fatal(err)
}
if info, err := os.Stat(full); err == nil && info.IsDir() {
continue
}
if err := os.WriteFile(full, nil, 0o644); err != nil {
t.Fatal(err)
}
}
// Verify what was materialised matches what the caller declared, so
// git and the library see the same isDir for every path. A mismatch
// means the paths file lists the same name as both a file and (part
// of) a directory, which cannot be represented on disk.
for _, p := range paths {
info, err := os.Stat(filepath.Join(root, filepath.FromSlash(p.rel)))
if err != nil {
t.Fatalf("materialise %q: %v", p.rel, err)
}
if info.IsDir() != p.isDir {
t.Fatalf("path %q declared isDir=%v but is a %v on disk; fix the paths file",
p.rel, p.isDir, kind(info.IsDir()))
}
}
return root
}
func kind(isDir bool) string {
if isDir {
return "directory"
}
return "file"
}
// gitCheckIgnore asks git which of the given paths are ignored, in a single
// batched call. It returns a map from relative path to the ignore result.
// --no-index avoids the tracked-file exemption; -n prints unmatched paths
// too; -z uses NUL separators so patterns containing colons or tabs do not
// break parsing.
func gitCheckIgnore(t *testing.T, root string, paths []conformancePath) map[string]bool {
t.Helper()
var stdin bytes.Buffer
for _, p := range paths {
stdin.WriteString(p.rel)
stdin.WriteByte(0)
}
cmd := exec.Command("git", "-c", "core.excludesFile=", "check-ignore",
"--no-index", "-z", "-v", "-n", "--stdin")
cmd.Dir = root
cmd.Stdin = &stdin
out, err := cmd.Output()
if err != nil {
// check-ignore exits 1 when no path is ignored; that is not an error
// for our purposes. Any other failure is.
ee, ok := err.(*exec.ExitError)
if !ok {
t.Fatalf("git check-ignore: %v", err)
}
if ee.ExitCode() != 1 {
t.Fatalf("git check-ignore: %v\n%s", err, ee.Stderr)
}
}
result := make(map[string]bool, len(paths))
fields := strings.Split(strings.TrimSuffix(string(out), "\x00"), "\x00")
if len(fields) != 4*len(paths) {
t.Fatalf("git check-ignore returned %d fields for %d paths (want %d)",
len(fields), len(paths), 4*len(paths))
}
// Output is groups of four fields: source, linenum, pattern, path.
for i := 0; i+3 < len(fields); i += 4 {
pattern := fields[i+2]
path := fields[i+3]
ignored := pattern != "" && !strings.HasPrefix(pattern, "!")
result[path] = ignored
}
if len(result) != len(paths) {
t.Fatalf("git check-ignore returned %d distinct paths for %d inputs", len(result), len(paths))
}
return result
}
func requireGit(t *testing.T) {
t.Helper()
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
}
// isolateGitEnv points HOME, XDG_CONFIG_HOME and the git global/system config
// locations at an empty temporary directory so neither the git subprocess nor
// gitignore.New picks up the user's global excludes.
func isolateGitEnv(t *testing.T) {
t.Helper()
empty := t.TempDir()
t.Setenv("HOME", empty)
t.Setenv("USERPROFILE", empty)
t.Setenv("XDG_CONFIG_HOME", empty)
t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(empty, "gitconfig"))
t.Setenv("GIT_CONFIG_SYSTEM", filepath.Join(empty, "gitconfig"))
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
}
func envInt(name string, def int) int {
if v, err := strconv.Atoi(os.Getenv(name)); err == nil && v > 0 {
return v
}
return def
}
func indent(s string) string {
lines := strings.Split(strings.TrimRight(s, "\n"), "\n")
for i, l := range lines {
lines[i] = " " + l
}
return strings.Join(lines, "\n")
}
// randomPatterns builds a small .gitignore from a fixed vocabulary of
// segments and pattern shapes. It is deterministic for a given rng state.
func randomPatterns(rng *rand.Rand, n int) string {
segs := []string{
"a", "b", "c", "*", "**", "*.log", "x*", "*x",
"?", "??", "a?", "?b",
"[ab]", "[!ab]", "[a-c]", "[[:lower:]]", "[a*]b", "[a?]",
"a*b", "\\*", "\\!a", "***", "*a*",
}
var b strings.Builder
for i := 0; i < n; i++ {
depth := 1 + rng.Intn(4)
parts := make([]string, depth)
for j := range parts {
parts[j] = segs[rng.Intn(len(segs))]
}
p := strings.Join(parts, "/")
// Reject patterns git refuses or that have no useful effect.
if p == "" || p == "**" || p == "/" || strings.Contains(p, "**/**") {
i--
continue
}
if rng.Intn(4) == 0 {
p = "/" + p
}
if rng.Intn(4) == 0 {
p += "/"
}
if i > 0 && rng.Intn(3) == 0 {
p = "!" + p
}
b.WriteString(p)
b.WriteByte('\n')
}
return b.String()
}
// randomPaths builds a set of query paths from the same segment vocabulary
// as randomPatterns so they have a reasonable chance of matching. It avoids
// producing a file path that is also a parent of another path, so that
// everything can be materialised on disk consistently.
func randomPaths(rng *rand.Rand, n int) []conformancePath {
segs := []string{"a", "b", "c", "d", "ab", "ax", "xb", "x1", "app.log", "keep", "!a"}
asDir := make(map[string]bool)
asFile := make(map[string]bool)
seen := make(map[string]bool)
var out []conformancePath
tries:
for len(out) < n {
depth := 1 + rng.Intn(4)
parts := make([]string, depth)
for j := range parts {
parts[j] = segs[rng.Intn(len(segs))]
}
rel := strings.Join(parts, "/")
if seen[rel] {
continue
}
isDir := rng.Intn(3) == 0
// Every proper prefix must be a directory; reject if any is
// already a file. The full path must not already be a directory
// if we picked file, or a file if we picked directory.
for i := 1; i < depth; i++ {
if asFile[strings.Join(parts[:i], "/")] {
continue tries
}
}
if isDir && asFile[rel] || !isDir && asDir[rel] {
continue
}
for i := 1; i < depth; i++ {
asDir[strings.Join(parts[:i], "/")] = true
}
if isDir {
asDir[rel] = true
} else {
asFile[rel] = true
}
seen[rel] = true
out = append(out, conformancePath{rel: rel, isDir: isDir})
}
return out
}