| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func mkfile(t *testing.T, path, content string) { |
| 13 | t.Helper() |
| 14 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // TestGrepSkipsVendorDirs proves a recursive grep prunes nested node_modules |
| 23 | // (perf + noise) but still searches one when it's the explicit target. |
| 24 | func TestGrepSkipsVendorDirs(t *testing.T) { |
| 25 | dir := t.TempDir() |
| 26 | mkfile(t, filepath.Join(dir, "app.go"), "the needle is here\n") |
| 27 | mkfile(t, filepath.Join(dir, "node_modules", "dep", "lib.go"), "needle in a dependency\n") |
| 28 | |
| 29 | grepIn := func(path string) string { |
| 30 | args, _ := json.Marshal(map[string]any{"pattern": "needle", "path": path}) |
| 31 | out, _ := grepTool{}.Execute(context.Background(), args) |
| 32 | return out |
| 33 | } |
| 34 | |
| 35 | root := grepIn(dir) |
| 36 | if !strings.Contains(root, "app.go") { |
| 37 | t.Errorf("grep should find app.go: %q", root) |
| 38 | } |
| 39 | if strings.Contains(root, "node_modules") { |
| 40 | t.Errorf("grep should skip nested node_modules, got: %q", root) |
| 41 | } |
| 42 | |
| 43 | nested := grepIn(filepath.Join(dir, "node_modules")) |
| 44 | if !strings.Contains(nested, "lib.go") { |
| 45 | t.Errorf("an explicit node_modules grep should still search it: %q", nested) |
| 46 | } |
| 47 | } |
| 48 |