返回 DeepSeek-Reasonix
shell_nul_test.go
根目录 / internal / sandbox / shell_nul_test.go
1 package sandbox
2
3 import "testing"
4
5 func TestNormalizeNullRedirects(t *testing.T) {
6 const bash = "/dev/null"
7 cases := []struct {
8 in, sink, want string
9 }{
10 {"echo hi 2>nul", bash, "echo hi 2>/dev/null"},
11 {"echo hi 2>nul", "$null", "echo hi 2>$null"},
12 {"echo hi 2>/dev/null", "$null", "echo hi 2>$null"},
13 {"echo hi 2>$null", bash, "echo hi 2>/dev/null"},
14 {"echo hi 2>$NULL", "$null", "echo hi 2>$null"},
15 {"build >nul 2>&1", bash, "build >/dev/null 2>&1"},
16 {"a 2>nul; b", bash, "a 2>/dev/null; b"},
17 {"go test 1>>NUL", bash, "go test 1>>/dev/null"},
18 {"x > nul", bash, "x >/dev/null"},
19 {"x >nul", "$null", "x >$null"},
20 {"probe &>nul", bash, "probe &>/dev/null"},
21 {"probe &>/dev/null", "$null", "probe &>$null"},
22 {"probe &>>$null", bash, "probe &>>/dev/null"},
23 // Not a nul redirect — leave untouched.
24 {"echo nul", bash, "echo nul"},
25 {"grep nul file.txt", bash, "grep nul file.txt"},
26 {"cat nul.txt", bash, "cat nul.txt"},
27 {"cat /dev/null.txt", "$null", "cat /dev/null.txt"},
28 {"echo '$null >nul '", bash, "echo '$null >nul '"},
29 {"echo \"quoted >/dev/null \"", "$null", "echo \"quoted >/dev/null \""},
30 {"echo \\>nul", bash, "echo \\>nul"},
31 {"run 2>&1", bash, "run 2>&1"},
32 {"rm nul", bash, "rm nul"},
33 {"echo nullish", bash, "echo nullish"},
34 }
35 for _, c := range cases {
36 if got := normalizeNullRedirects(c.in, c.sink); got != c.want {
37 t.Errorf("normalizeNullRedirects(%q, %q) = %q, want %q", c.in, c.sink, got, c.want)
38 }
39 }
40 }
41
42 func TestNormalizeNullRedirectsPreservesHereDocBody(t *testing.T) {
43 in := "cat > out.go <<'EOF'\n" +
44 "func main() {\n" +
45 "\tdata := []byte(`{\"token\":\"TOKEN_EXAMPLE\"}`)\n" +
46 "\tjson.Unmarshal(data, &v)\n" +
47 "}\n" +
48 "EOF\n"
49 if got := normalizeNullRedirects(in, "/dev/null"); got != in {
50 t.Fatalf("normalizeNullRedirects changed heredoc body:\n--- got ---\n%s\n--- want ---\n%s", got, in)
51 }
52 }
53
54 func TestArgvNormalizesNullRedirects(t *testing.T) {
55 bashArgv := Shell{Kind: ShellBash, Path: "bash"}.argv("echo hi 2>nul")
56 if last := bashArgv[len(bashArgv)-1]; last != "echo hi 2>/dev/null" {
57 t.Errorf("bash argv command = %q, want nul rewritten to /dev/null", last)
58 }
59 psArgv := Shell{Kind: ShellPowerShell, Path: "powershell"}.argv("echo hi 2>/dev/null")
60 if last := psArgv[len(psArgv)-1]; last != psUTF8Prologue+"echo hi 2>$null" {
61 t.Errorf("powershell argv command = %q, want /dev/null rewritten to $null", last)
62 }
63 }
64
64 lines GO