11// Benchmark to measure CPU efficiency
12func BenchmarkShellQuickCommands(b *testing.B) {
13 shell := NewShell(&Options{WorkingDir: b.TempDir()})
18 _, _, err := shell.Exec(context.Background(), "echo test")
19 exitCode := ExitCode(err)
20 if err != nil || exitCode != 0 {
21 b.Fatalf("Command failed: %v, exit code: %d", err, exitCode)
26func TestTestTimeout(t *testing.T) {
27 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
30 shell := NewShell(&Options{WorkingDir: t.TempDir()})
31 _, _, err := shell.Exec(ctx, "sleep 10")
32 if status := ExitCode(err); status == 0 {
33 t.Fatalf("Expected non-zero exit status, got %d", status)
35 if !IsInterrupt(err) {
36 t.Fatalf("Expected command to be interrupted, but it was not")
39 t.Fatalf("Expected an error due to timeout, but got none")
43func TestTestCancel(t *testing.T) {
44 ctx, cancel := context.WithCancel(t.Context())
45 cancel() // immediately cancel the context
47 shell := NewShell(&Options{WorkingDir: t.TempDir()})
48 _, _, err := shell.Exec(ctx, "sleep 10")
49 if status := ExitCode(err); status == 0 {
50 t.Fatalf("Expected non-zero exit status, got %d", status)
52 if !IsInterrupt(err) {
53 t.Fatalf("Expected command to be interrupted, but it was not")
56 t.Fatalf("Expected an error due to cancel, but got none")
60func TestRunCommandError(t *testing.T) {
61 shell := NewShell(&Options{WorkingDir: t.TempDir()})
62 _, _, err := shell.Exec(t.Context(), "nopenopenope")
63 if status := ExitCode(err); status == 0 {
64 t.Fatalf("Expected non-zero exit status, got %d", status)
67 t.Fatalf("Expected command to not be interrupted, but it was")
70 t.Fatalf("Expected an error, got nil")
74func TestRunContinuity(t *testing.T) {
75 shell := NewShell(&Options{WorkingDir: t.TempDir()})
76 shell.Exec(t.Context(), "export FOO=bar")
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
88func TestShellTypeDetection(t *testing.T) {
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)
139func 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
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 {
166 t.Errorf("Command %q should have failed", test.command)
170 t.Errorf("Command %q failed: %v", test.command, err)
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)
185func TestCrossPlatformExecution(t *testing.T) {
186 shell := NewShell(&Options{WorkingDir: "."})
187 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
190 // Test a simple command that should work on all platforms
191 stdout, stderr, err := shell.Exec(ctx, "echo hello")
193 t.Fatalf("Echo command failed: %v, stderr: %s", err, stderr)
197 t.Error("Echo command produced no output")
200 // The output should contain "hello" regardless of platform
201 if !strings.Contains(strings.ToLower(stdout), "hello") {
202 t.Errorf("Echo output should contain 'hello', got: %q", stdout)
206func 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")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list