|
| 1 | +package shellpath |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "runtime" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestWindowsCmdExe_ComSpec(t *testing.T) { |
| 10 | + t.Setenv("ComSpec", `C:\Custom\cmd.exe`) |
| 11 | + got := WindowsCmdExe() |
| 12 | + if got != `C:\Custom\cmd.exe` { |
| 13 | + t.Errorf("WindowsCmdExe() = %q, want %q", got, `C:\Custom\cmd.exe`) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func TestWindowsCmdExe_SystemRoot(t *testing.T) { |
| 18 | + t.Setenv("ComSpec", "") |
| 19 | + t.Setenv("SystemRoot", `C:\Windows`) |
| 20 | + got := WindowsCmdExe() |
| 21 | + want := `C:\Windows` + string(filepath.Separator) + filepath.Join("System32", "cmd.exe") |
| 22 | + if got != want { |
| 23 | + t.Errorf("WindowsCmdExe() = %q, want %q", got, want) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestWindowsCmdExe_Fallback(t *testing.T) { |
| 28 | + t.Setenv("ComSpec", "") |
| 29 | + t.Setenv("SystemRoot", "") |
| 30 | + got := WindowsCmdExe() |
| 31 | + if got != "cmd.exe" { |
| 32 | + t.Errorf("WindowsCmdExe() = %q, want %q", got, "cmd.exe") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestDetectShell_Unix(t *testing.T) { |
| 37 | + if runtime.GOOS == "windows" { |
| 38 | + t.Skip("Unix-only test") |
| 39 | + } |
| 40 | + |
| 41 | + t.Setenv("SHELL", "/bin/zsh") |
| 42 | + shell, args := DetectShell() |
| 43 | + if shell != "/bin/zsh" { |
| 44 | + t.Errorf("DetectShell() shell = %q, want /bin/zsh", shell) |
| 45 | + } |
| 46 | + if len(args) != 1 || args[0] != "-c" { |
| 47 | + t.Errorf("DetectShell() args = %v, want [-c]", args) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestDetectShell_Unix_Fallback(t *testing.T) { |
| 52 | + if runtime.GOOS == "windows" { |
| 53 | + t.Skip("Unix-only test") |
| 54 | + } |
| 55 | + |
| 56 | + t.Setenv("SHELL", "") |
| 57 | + shell, _ := DetectShell() |
| 58 | + if shell != "/bin/sh" { |
| 59 | + t.Errorf("DetectShell() shell = %q, want /bin/sh", shell) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestDefaultUnixShell(t *testing.T) { |
| 64 | + t.Setenv("SHELL", "/usr/local/bin/fish") |
| 65 | + got := defaultUnixShell() |
| 66 | + if got != "/usr/local/bin/fish" { |
| 67 | + t.Errorf("defaultUnixShell() = %q, want /usr/local/bin/fish", got) |
| 68 | + } |
| 69 | + |
| 70 | + t.Setenv("SHELL", "") |
| 71 | + got = defaultUnixShell() |
| 72 | + if got != "/bin/sh" { |
| 73 | + t.Errorf("defaultUnixShell() = %q, want /bin/sh", got) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestWindowsCmdExe_PrefersComSpecOverSystemRoot(t *testing.T) { |
| 78 | + // When both are set, ComSpec should take priority |
| 79 | + t.Setenv("ComSpec", `D:\Tools\cmd.exe`) |
| 80 | + t.Setenv("SystemRoot", `C:\Windows`) |
| 81 | + got := WindowsCmdExe() |
| 82 | + if got != `D:\Tools\cmd.exe` { |
| 83 | + t.Errorf("WindowsCmdExe() = %q, want %q (ComSpec should take priority)", got, `D:\Tools\cmd.exe`) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestDetectWindowsShell_FallbackUsesAbsolutePath(t *testing.T) { |
| 88 | + // Simulate an environment where no PowerShell is found: |
| 89 | + // set PATH to empty so LookPath won't find pwsh.exe or powershell.exe |
| 90 | + t.Setenv("PATH", "") |
| 91 | + |
| 92 | + t.Setenv("ComSpec", "") |
| 93 | + t.Setenv("SystemRoot", `C:\Windows`) |
| 94 | + |
| 95 | + shell, args := DetectWindowsShell() |
| 96 | + want := `C:\Windows` + string(filepath.Separator) + filepath.Join("System32", "cmd.exe") |
| 97 | + if shell != want { |
| 98 | + t.Errorf("DetectWindowsShell() shell = %q, want %q", shell, want) |
| 99 | + } |
| 100 | + if len(args) != 1 || args[0] != "/C" { |
| 101 | + t.Errorf("DetectWindowsShell() args = %v, want [/C]", args) |
| 102 | + } |
| 103 | +} |
0 commit comments