|
5 | 5 | package syscall_test
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "bytes" |
8 | 9 | "fmt"
|
| 10 | + "io" |
9 | 11 | "os"
|
10 | 12 | "os/exec"
|
| 13 | + "path" |
11 | 14 | "path/filepath"
|
12 | 15 | "syscall"
|
13 | 16 | "testing"
|
@@ -47,6 +50,73 @@ func TestEscapeArg(t *testing.T) {
|
47 | 50 | }
|
48 | 51 | }
|
49 | 52 |
|
| 53 | +func TestStartProcessBatchFile(t *testing.T) { |
| 54 | + const batchFileContent = "@echo %*" |
| 55 | + |
| 56 | + dir, err := os.MkdirTemp("", "TestStartProcessBatchFile") |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + defer os.RemoveAll(dir) |
| 61 | + |
| 62 | + noSpacesInPath := path.Join(dir, "no-spaces-in-path.cmd") |
| 63 | + err = os.WriteFile(noSpacesInPath, []byte(batchFileContent), 0644) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + |
| 68 | + spacesInPath := path.Join(dir, "spaces in path.cmd") |
| 69 | + err = os.WriteFile(spacesInPath, []byte(batchFileContent), 0644) |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + |
| 74 | + tests := []struct { |
| 75 | + batchFile string |
| 76 | + args []string |
| 77 | + want string |
| 78 | + }{ |
| 79 | + {noSpacesInPath, []string{noSpacesInPath}, "ECHO is on."}, |
| 80 | + {spacesInPath, []string{spacesInPath}, "ECHO is on."}, |
| 81 | + {noSpacesInPath, []string{noSpacesInPath, "test-arg-no-spaces"}, "test-arg-no-spaces"}, |
| 82 | + {spacesInPath, []string{spacesInPath, "test-arg-no-spaces"}, "test-arg-no-spaces"}, |
| 83 | + {noSpacesInPath, []string{noSpacesInPath, "test arg spaces"}, `"test arg spaces"`}, |
| 84 | + {spacesInPath, []string{spacesInPath, "test arg spaces"}, `"test arg spaces"`}, |
| 85 | + {noSpacesInPath, []string{noSpacesInPath, "test arg spaces", "test-arg-no-spaces"}, `"test arg spaces" test-arg-no-spaces`}, |
| 86 | + {spacesInPath, []string{spacesInPath, "test arg spaces", "test-arg-no-spaces"}, `"test arg spaces" test-arg-no-spaces`}, |
| 87 | + } |
| 88 | + for _, test := range tests { |
| 89 | + pr, pw, err := os.Pipe() |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + defer pr.Close() |
| 94 | + defer pw.Close() |
| 95 | + |
| 96 | + attr := &os.ProcAttr{Files: []*os.File{nil, pw, pw}} |
| 97 | + p, err := os.StartProcess(test.batchFile, test.args, attr) |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + |
| 102 | + _, err = p.Wait() |
| 103 | + if err != nil { |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | + pw.Close() |
| 107 | + |
| 108 | + var buf bytes.Buffer |
| 109 | + _, err = io.Copy(&buf, pr) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + if got, want := string(buf.Bytes()), test.want+"\r\n"; got != want { |
| 115 | + t.Errorf("StartProcess(%#q, %#q) = %#q, want %#q", test.batchFile, test.args, got, want) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
50 | 120 | func TestChangingProcessParent(t *testing.T) {
|
51 | 121 | if os.Getenv("GO_WANT_HELPER_PROCESS") == "parent" {
|
52 | 122 | // in parent process
|
|
0 commit comments