| 🛟 Updated. 28d5985 k33g 17h ago | 1 | package tools |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // The Windows half of the shell choice, testable on every platform because it |
| 6 | // is arithmetic on strings. The exec.Cmd it feeds is compiled with GOOS=windows |
| 7 | // and has never been run by this project's authors — see README.md. |
| 8 | |
| 9 | func TestWindowsShellIsComspecOrCmd(t *testing.T) { |
| 10 | if got := windowsShell(`C:\Windows\system32\cmd.exe`); got != `C:\Windows\system32\cmd.exe` { |
| 11 | t.Errorf("windowsShell(COMSPEC) = %q, want COMSPEC itself", got) |
| 12 | } |
| 13 | if got := windowsShell(""); got != "cmd.exe" { |
| 14 | t.Errorf("windowsShell(\"\") = %q, want cmd.exe", got) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | func TestCmdExeGetsTheCommandVerbatimInsideOnePairOfQuotes(t *testing.T) { |
| 19 | // cmd.exe /S /C strips the first and last quote and parses the rest by its |
| 20 | // own rules, so a quote inside the command must reach it as a quote and |
| 21 | // not as the C runtime's \". |
| 22 | got := cmdExeCommandLine(`C:\Windows\system32\cmd.exe`, `echo "hello world" && go test ./...`) |
| 23 | |
| 24 | want := `"C:\Windows\system32\cmd.exe" /S /C "echo "hello world" && go test ./..."` |
| 25 | if got != want { |
| 26 | t.Errorf("cmdExeCommandLine() = %s, want %s", got, want) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestTheShellRunsOneCommandLineAndExits(t *testing.T) { |
| 31 | // Whatever the platform, the arguments end with the command itself, which |
| 32 | // is what lets the terminal package compose the line. |
| 33 | args := ShellArgs("go test ./...") |
| 34 | |
| 35 | if len(args) < 2 || args[len(args)-1] != "go test ./..." { |
| 36 | t.Errorf("ShellArgs() = %q, want the command as the last argument after the shell's switch", args) |
| 37 | } |
| 38 | if Shell() == "" { |
| 39 | t.Error("Shell() is empty") |
| 40 | } |
| 41 | } |