返回 DeepSeek-Reasonix
recursive_search_test.go
根目录 / internal / tool / builtin / recursive_search_test.go
1 package builtin
2
3 import (
4 "context"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 )
11
12 func TestGlobBareNameFallsBackToRecursiveWithWorkDir(t *testing.T) {
13 root := t.TempDir()
14 if err := os.MkdirAll(filepath.Join(root, "sub", "deep"), 0o755); err != nil {
15 t.Fatal(err)
16 }
17 if err := os.WriteFile(filepath.Join(root, "sub", "deep", "target.go"), []byte("x"), 0o644); err != nil {
18 t.Fatal(err)
19 }
20 if err := os.MkdirAll(filepath.Join(root, "node_modules", "pkg"), 0o755); err != nil {
21 t.Fatal(err)
22 }
23 if err := os.WriteFile(filepath.Join(root, "node_modules", "pkg", "target.go"), []byte("x"), 0o644); err != nil {
24 t.Fatal(err)
25 }
26 t.Chdir(t.TempDir())
27
28 out := runTool(t, globTool{workDir: root}, map[string]any{"pattern": "target.go"})
29 if !strings.Contains(filepath.ToSlash(out), "sub/deep/target.go") {
30 t.Fatalf("bare filename should fall back to a recursive walk; got:\n%s", out)
31 }
32 if strings.Contains(out, "node_modules") {
33 t.Fatalf("recursive fallback should skip node_modules; got:\n%s", out)
34 }
35 }
36
37 func TestLsRecursive(t *testing.T) {
38 root := t.TempDir()
39 if err := os.MkdirAll(filepath.Join(root, "a", "b"), 0o755); err != nil {
40 t.Fatal(err)
41 }
42 if err := os.WriteFile(filepath.Join(root, "top.txt"), []byte("x"), 0o644); err != nil {
43 t.Fatal(err)
44 }
45 if err := os.WriteFile(filepath.Join(root, "a", "b", "nested.txt"), []byte("x"), 0o644); err != nil {
46 t.Fatal(err)
47 }
48 if err := os.MkdirAll(filepath.Join(root, ".git"), 0o755); err != nil {
49 t.Fatal(err)
50 }
51 if err := os.WriteFile(filepath.Join(root, ".git", "HEAD"), []byte("ref"), 0o644); err != nil {
52 t.Fatal(err)
53 }
54
55 l := listDir{workDir: root}
56
57 flat := runTool(t, l, map[string]any{"path": "."})
58 if strings.Contains(flat, "nested.txt") {
59 t.Fatalf("flat ls must not recurse; got:\n%s", flat)
60 }
61
62 rec, err := l.Execute(context.Background(), json.RawMessage(`{"path":".","recursive":true}`))
63 if err != nil {
64 t.Fatalf("recursive ls: %v", err)
65 }
66 s := filepath.ToSlash(rec)
67 if !strings.Contains(s, "a/b/nested.txt") {
68 t.Fatalf("recursive ls should list nested files; got:\n%s", rec)
69 }
70 if strings.Contains(rec, "HEAD") {
71 t.Fatalf("recursive ls should skip .git; got:\n%s", rec)
72 }
73 }
74
74 lines GO