@@ -2,6 +2,7 @@ package shell
@@ -24,6 +25,11 @@ func BenchmarkShellQuickCommands(b *testing.B) {
26func TestTestTimeout(t *testing.T) {
27func TestTestTimeout(t *testing.T) {
28+ // XXX(@andreynering): This fails on Windows. Address once possible.
29+ if runtime.GOOS == "windows" {
30+ t.Skip("Skipping test on Windows")
27 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
33 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
28 t.Cleanup(cancel)
34 t.Cleanup(cancel)
@@ -72,113 +78,23 @@ func TestRunCommandError(t *testing.T) {
74func TestRunContinuity(t *testing.T) {
80func TestRunContinuity(t *testing.T) {
75− shell := NewShell(&Options{WorkingDir: t.TempDir()})
81+ tempDir1 := t.TempDir()
76− shell.Exec(t.Context(), "export FOO=bar")
77− dst := t.TempDir()
82+ tempDir2 := t.TempDir()
78− shell.Exec(t.Context(), "cd "+dst)
79− out, _, _ := shell.Exec(t.Context(), "echo $FOO ; pwd")
80− expect := "bar\n" + dst + "\n"
82− t.Fatalf("Expected output %q, got %q", expect, out)
86−// New tests for Windows shell support
84+ shell := NewShell(&Options{WorkingDir: tempDir1})
87−
85+ if _, _, err := shell.Exec(t.Context(), "export FOO=bar"); err != nil {
88−func TestShellTypeDetection(t *testing.T) {
86+ t.Fatalf("failed to set env: %v", err)
89− shell := &PersistentShell{}
96− // Windows-specific commands
97− {"dir", ShellTypeCmd, true},
98− {"type file.txt", ShellTypeCmd, true},
99− {"copy file1.txt file2.txt", ShellTypeCmd, true},
100− {"del file.txt", ShellTypeCmd, true},
101− {"md newdir", ShellTypeCmd, true},
102− {"tasklist", ShellTypeCmd, true},
104− // PowerShell commands
105− {"Get-Process", ShellTypePowerShell, true},
106− {"Get-ChildItem", ShellTypePowerShell, true},
107− {"Set-Location C:\\", ShellTypePowerShell, true},
108− {"Get-Content file.txt | Where-Object {$_ -match 'pattern'}", ShellTypePowerShell, true},
109− {"$files = Get-ChildItem", ShellTypePowerShell, true},
111− // Unix/cross-platform commands
112− {"ls -la", ShellTypePOSIX, false},
113− {"cat file.txt", ShellTypePOSIX, false},
114− {"grep pattern file.txt", ShellTypePOSIX, false},
115− {"echo hello", ShellTypePOSIX, false},
116− {"git status", ShellTypePOSIX, false},
117− {"go build", ShellTypePOSIX, false},
120− for _, test := range tests {
121− t.Run(test.command, func(t *testing.T) {
122− result := shell.determineShellType(test.command)
124− if test.windowsOnly && runtime.GOOS != "windows" {
125− // On non-Windows systems, everything should use POSIX
126− if result != ShellTypePOSIX {
127− t.Errorf("On non-Windows, command %q should use POSIX shell, got %v", test.command, result)
129− } else if runtime.GOOS == "windows" {
130− // On Windows, check the expected shell type
131− if result != test.expected {
132− t.Errorf("Command %q should use %v shell, got %v", test.command, test.expected, result)
137−}
88+ if _, _, err := shell.Exec(t.Context(), "cd "+filepath.ToSlash(tempDir2)); err != nil {
138−
89+ t.Fatalf("failed to change directory: %v", err)
139−func TestWindowsCDHandling(t *testing.T) {
140− if runtime.GOOS != "windows" {
141− t.Skip("Windows CD handling test only runs on Windows")
144− shell := NewShell(&Options{
145− WorkingDir: "C:\\Users",
153− {"cd ..", "C:\\", false},
154− {"cd Documents", "C:\\Users\\Documents", false},
155− {"cd C:\\Windows", "C:\\Windows", false},
156− {"cd", "", true}, // Missing argument
158−
91+ out, _, err := shell.Exec(t.Context(), "echo $FOO ; pwd")
159− for _, test := range tests {
160− t.Run(test.command, func(t *testing.T) {
161− originalCwd := shell.GetWorkingDir()
162− stdout, stderr, err := shell.handleWindowsCD(test.command)
164− if test.shouldError {
165− if err == nil {
92+ if err != nil {
166− t.Errorf("Command %q should have failed", test.command)
93+ t.Fatalf("failed to echo: %v", err)
168− } else {
95+ expect := "bar\n" + tempDir2 + "\n"
169− if err != nil {
96+ if out != expect {
170− t.Errorf("Command %q failed: %v", test.command, err)
97+ t.Fatalf("expected output %q, got %q", expect, out)
172− if shell.GetWorkingDir() != test.expectedCwd {
173− t.Errorf("Command %q: expected cwd %q, got %q", test.command, test.expectedCwd, shell.GetWorkingDir())
177− // Reset for next test
178− shell.SetWorkingDir(originalCwd)
@@ -202,23 +118,3 @@ func TestCrossPlatformExecution(t *testing.T) {
202 t.Errorf("Echo output should contain 'hello', got: %q", stdout)
118 t.Errorf("Echo output should contain 'hello', got: %q", stdout)
206−func TestWindowsNativeCommands(t *testing.T) {
207− if runtime.GOOS != "windows" {
208− t.Skip("Windows native command test only runs on Windows")
211− shell := NewShell(&Options{WorkingDir: "."})
212− ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
215− // Test Windows dir command
216− stdout, stderr, err := shell.Exec(ctx, "dir")
218− t.Fatalf("Dir command failed: %v, stderr: %s", err, stderr)
222− t.Error("Dir command produced no output")