返回 DeepSeek-Reasonix
bash_heredoc_test.go
根目录 / internal / tool / builtin / bash_heredoc_test.go
1 package builtin
2
3 import (
4 "context"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "reasonix/internal/sandbox"
13 )
14
15 func TestBashHereDocIssue5624CommandsReturnPromptly(t *testing.T) {
16 sh := requireHereDocBash(t)
17
18 root := t.TempDir()
19 if err := os.MkdirAll(filepath.Join(root, "app", "platform", "github"), 0o755); err != nil {
20 t.Fatalf("mkdir fixture: %v", err)
21 }
22
23 appendTarget := filepath.Join(root, "app", "platform", "github", "adapter.go")
24 redactTarget := filepath.Join(root, "test_redact.go")
25 tests := []struct {
26 name string
27 command string
28 target string
29 wantOut string
30 wantInFile []string
31 }{
32 {
33 name: "append pull request mapping heredoc",
34 command: strings.Join([]string{
35 "cat >> app/platform/github/adapter.go << 'EOF'",
36 "",
37 "// Pull request raw mapping (v0.6+).",
38 "type githubPullRaw struct {",
39 "\tNumber int `json:\"number\"`",
40 "\tTitle string `json:\"title\"`",
41 "}",
42 "",
43 "func githubPullToDetail(mergeable *bool) bool {",
44 "\treturn mergeable != nil && *mergeable",
45 "}",
46 "EOF",
47 "echo \"appended OK\"",
48 }, "\n"),
49 target: appendTarget,
50 wantOut: "appended OK",
51 wantInFile: []string{"type githubPullRaw struct", "`json:\"number\"`", "mergeable != nil && *mergeable"},
52 },
53 {
54 name: "cd then write redact repro heredoc",
55 command: strings.Join([]string{
56 "cd " + bashQuote(filepath.ToSlash(root)) + " && cat > " + bashQuote(filepath.ToSlash(redactTarget)) + " <<'EOF'",
57 "package main",
58 "",
59 "import (",
60 "\t\"encoding/json\"",
61 "\t\"fmt\"",
62 ")",
63 "",
64 "func main() {",
65 "\tdata := []byte(`{\"accounts\":[{\"id\":\"a1\",\"username\":\"alice\",\"token\":\"TOKEN_EXAMPLE\"}]}`)",
66 "\tvar v any",
67 "\tjson.Unmarshal(data, &v)",
68 "\tfmt.Printf(\"before: %v\\n\", v)",
69 "}",
70 "EOF",
71 "echo \"skip\"",
72 }, "\n"),
73 target: redactTarget,
74 wantOut: "skip",
75 wantInFile: []string{"package main", "TOKEN_EXAMPLE", "fmt.Printf(\"before: %v\\n\", v)"},
76 },
77 }
78
79 for _, tt := range tests {
80 t.Run(tt.name, func(t *testing.T) {
81 ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
82 defer cancel()
83 ctx = fullAccessBashTestContext(ctx)
84
85 done := make(chan struct {
86 out string
87 err error
88 elapsed time.Duration
89 }, 1)
90 go func() {
91 start := time.Now()
92 out, err := (bash{shell: sh, workDir: root, timeout: 3 * time.Second}).Execute(ctx, argsJSON(t, map[string]any{"command": tt.command}))
93 done <- struct {
94 out string
95 err error
96 elapsed time.Duration
97 }{out: out, err: err, elapsed: time.Since(start)}
98 }()
99
100 var got struct {
101 out string
102 err error
103 elapsed time.Duration
104 }
105 select {
106 case got = <-done:
107 case <-time.After(10 * time.Second):
108 t.Fatal("heredoc bash command did not return within 10s")
109 }
110 if got.err != nil {
111 t.Fatalf("bash heredoc failed after %v: %v (out=%q)", got.elapsed, got.err, got.out)
112 }
113 if got.elapsed > 2*time.Second {
114 t.Fatalf("bash heredoc returned too slowly: %v (out=%q)", got.elapsed, got.out)
115 }
116 if !strings.Contains(got.out, tt.wantOut) {
117 t.Fatalf("output = %q, want %q", got.out, tt.wantOut)
118 }
119 data, err := os.ReadFile(tt.target)
120 if err != nil {
121 t.Fatalf("read heredoc target: %v", err)
122 }
123 body := string(data)
124 for _, want := range tt.wantInFile {
125 if !strings.Contains(body, want) {
126 t.Fatalf("target missing %q:\n%s", want, body)
127 }
128 }
129 })
130 }
131 }
132
133 func requireHereDocBash(t *testing.T) sandbox.Shell {
134 t.Helper()
135 sh := sandbox.ResolveShell("bash", "", nil)
136 if sh.Kind != sandbox.ShellBash {
137 t.Skipf("bash heredoc regression requires bash, got %s", sh.Kind.String())
138 }
139 path := sh.Path
140 if path == "" {
141 path = "bash"
142 }
143 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
144 defer cancel()
145 if err := exec.CommandContext(ctx, path, "-c", "true").Run(); err != nil {
146 t.Skipf("bash heredoc regression requires a runnable bash: %v", err)
147 }
148 sh.Path = path
149 return sh
150 }
151
152 func bashQuote(s string) string {
153 return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
154 }
155
155 lines GO