返回 DeepSeek-Reasonix
walk_cancel_test.go
根目录 / internal / tool / builtin / walk_cancel_test.go
1 package builtin
2
3 import (
4 "context"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11 )
12
13 // TestGrepWalkInterruptible proves the native (no-ripgrep) grep walk aborts on a
14 // cancelled context instead of scanning the whole tree.
15 func TestGrepWalkInterruptible(t *testing.T) {
16 dir := t.TempDir()
17 if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("FINDME here\n"), 0o644); err != nil {
18 t.Fatal(err)
19 }
20 ctx, cancel := context.WithCancel(context.Background())
21 cancel() // pre-cancelled: the walk must stop before searching
22 args, _ := json.Marshal(map[string]any{"pattern": "FINDME", "path": dir})
23 out, _ := grepTool{}.Execute(ctx, args)
24 if strings.Contains(out, "FINDME") {
25 t.Fatalf("cancelled grep kept scanning and matched: %q", out)
26 }
27 }
28
29 // TestGlobWalkInterruptible proves the recursive glob walk aborts on cancel.
30 func TestGlobWalkInterruptible(t *testing.T) {
31 dir := t.TempDir()
32 if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o755); err != nil {
33 t.Fatal(err)
34 }
35 if err := os.WriteFile(filepath.Join(dir, "sub", "a.go"), []byte("x"), 0o644); err != nil {
36 t.Fatal(err)
37 }
38 ctx, cancel := context.WithCancel(context.Background())
39 cancel()
40 args, _ := json.Marshal(map[string]any{"pattern": filepath.Join(dir, "**", "*.go")})
41 if _, err := (globTool{}).Execute(ctx, args); err == nil {
42 t.Fatal("cancelled glob should surface a context error, not finish the walk")
43 }
44 }
45
46 // TestGlobDeadlineReportsIncomplete proves an expired walk budget degrades to a
47 // labelled partial result instead of an error, so a deep tree can't hang a turn.
48 func TestGlobDeadlineReportsIncomplete(t *testing.T) {
49 dir := t.TempDir()
50 if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o755); err != nil {
51 t.Fatal(err)
52 }
53 if err := os.WriteFile(filepath.Join(dir, "sub", "a.go"), []byte("x"), 0o644); err != nil {
54 t.Fatal(err)
55 }
56 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
57 defer cancel()
58 args, _ := json.Marshal(map[string]any{"pattern": filepath.Join(dir, "**", "*.go")})
59 out, err := (globTool{}).Execute(ctx, args)
60 if err != nil {
61 t.Fatalf("expired glob budget should return partial results, got error: %v", err)
62 }
63 if !strings.Contains(out, "timed out") {
64 t.Fatalf("expired glob budget should label the result, got %q", out)
65 }
66 }
67
68 func TestGlobTimeoutClamp(t *testing.T) {
69 if got := globTimeout(0); got != globDefaultTimeout {
70 t.Errorf("globTimeout(0) = %s, want %s", got, globDefaultTimeout)
71 }
72 if got := globTimeout(5); got != 5*time.Second {
73 t.Errorf("globTimeout(5) = %s, want 5s", got)
74 }
75 if got := globTimeout(100000); got != globMaxTimeout {
76 t.Errorf("globTimeout(100000) = %s, want %s", got, globMaxTimeout)
77 }
78 }
79
79 lines GO