10 "github.com/stretchr/testify/require"
13func TestBackgroundShellManager_Start(t *testing.T) {
14 t.Skip("Skipping this until I figure out why its flaky")
18 workingDir := t.TempDir()
19 manager := newBackgroundShellManager()
21 bgShell, err := manager.Start(ctx, workingDir, nil, "echo 'hello world'", "")
23 t.Fatalf("failed to start background shell: %v", err)
27 t.Error("expected shell ID to be non-empty")
30 // Wait for the command to complete
33 stdout, stderr, done, err := bgShell.GetOutput()
35 t.Error("expected shell to be done")
39 t.Errorf("expected no error, got: %v", err)
42 if !strings.Contains(stdout, "hello world") {
43 t.Errorf("expected stdout to contain 'hello world', got: %s", stdout)
47 t.Errorf("expected empty stderr, got: %s", stderr)
51func TestBackgroundShellManager_Get(t *testing.T) {
55 workingDir := t.TempDir()
56 manager := newBackgroundShellManager()
58 bgShell, err := manager.Start(ctx, workingDir, nil, "echo 'test'", "")
60 t.Fatalf("failed to start background shell: %v", err)
64 retrieved, ok := manager.Get(bgShell.ID)
66 t.Error("expected to find the background shell")
69 if retrieved.ID != bgShell.ID {
70 t.Errorf("expected shell ID %s, got %s", bgShell.ID, retrieved.ID)
74 manager.Kill(bgShell.ID)
77func TestBackgroundShellManager_Kill(t *testing.T) {
81 workingDir := t.TempDir()
82 manager := newBackgroundShellManager()
84 // Start a long-running command
85 bgShell, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
87 t.Fatalf("failed to start background shell: %v", err)
91 err = manager.Kill(bgShell.ID)
93 t.Errorf("failed to kill background shell: %v", err)
96 // Verify it's no longer in the manager
97 _, ok := manager.Get(bgShell.ID)
99 t.Error("expected shell to be removed after kill")
102 // Verify the shell is done
103 if !bgShell.IsDone() {
104 t.Error("expected shell to be done after kill")
108func TestBackgroundShellManager_KillNonExistent(t *testing.T) {
111 manager := newBackgroundShellManager()
113 err := manager.Kill("non-existent-id")
115 t.Error("expected error when killing non-existent shell")
119func TestBackgroundShell_IsDone(t *testing.T) {
123 workingDir := t.TempDir()
124 manager := newBackgroundShellManager()
126 bgShell, err := manager.Start(ctx, workingDir, nil, "echo 'quick'", "")
128 t.Fatalf("failed to start background shell: %v", err)
131 // Wait a bit for the command to complete
132 time.Sleep(100 * time.Millisecond)
134 if !bgShell.IsDone() {
135 t.Error("expected shell to be done")
139 manager.Kill(bgShell.ID)
142func TestBackgroundShell_WithBlockFuncs(t *testing.T) {
146 workingDir := t.TempDir()
147 manager := newBackgroundShellManager()
149 blockFuncs := []BlockFunc{
150 CommandsBlocker([]string{"curl", "wget"}),
153 bgShell, err := manager.Start(ctx, workingDir, blockFuncs, "curl example.com", "")
155 t.Fatalf("failed to start background shell: %v", err)
158 // Wait for the command to complete
161 stdout, stderr, done, execErr := bgShell.GetOutput()
163 t.Error("expected shell to be done")
166 // The command should have been blocked
167 output := stdout + stderr
168 if !strings.Contains(output, "not allowed") && execErr == nil {
169 t.Errorf("expected command to be blocked, got stdout: %s, stderr: %s, err: %v", stdout, stderr, execErr)
173 manager.Kill(bgShell.ID)
176func TestBackgroundShellManager_List(t *testing.T) {
177 if runtime.GOOS == "windows" {
178 t.Skip("skipping flacky test on windows")
184 workingDir := t.TempDir()
185 manager := newBackgroundShellManager()
188 bgShell1, err := manager.Start(ctx, workingDir, nil, "sleep 1", "")
190 t.Fatalf("failed to start first background shell: %v", err)
193 bgShell2, err := manager.Start(ctx, workingDir, nil, "sleep 1", "")
195 t.Fatalf("failed to start second background shell: %v", err)
198 ids := manager.List()
200 // Check that both shells are in the list
203 for _, id := range ids {
204 if id == bgShell1.ID {
207 if id == bgShell2.ID {
213 t.Errorf("expected to find shell %s in list", bgShell1.ID)
216 t.Errorf("expected to find shell %s in list", bgShell2.ID)
220 manager.Kill(bgShell1.ID)
221 manager.Kill(bgShell2.ID)
224func TestBackgroundShellManager_KillAll(t *testing.T) {
228 workingDir := t.TempDir()
229 manager := newBackgroundShellManager()
231 // Start multiple long-running shells
232 shell1, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
234 t.Fatalf("failed to start shell 1: %v", err)
237 shell2, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
239 t.Fatalf("failed to start shell 2: %v", err)
242 shell3, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
244 t.Fatalf("failed to start shell 3: %v", err)
247 // Verify shells are running
248 if shell1.IsDone() || shell2.IsDone() || shell3.IsDone() {
249 t.Error("shells should not be done yet")
253 manager.KillAll(t.Context())
255 // Verify all shells are done
256 if !shell1.IsDone() {
257 t.Error("shell1 should be done after KillAll")
259 if !shell2.IsDone() {
260 t.Error("shell2 should be done after KillAll")
262 if !shell3.IsDone() {
263 t.Error("shell3 should be done after KillAll")
266 // Verify they're removed from the manager
267 if _, ok := manager.Get(shell1.ID); ok {
268 t.Error("shell1 should be removed from manager")
270 if _, ok := manager.Get(shell2.ID); ok {
271 t.Error("shell2 should be removed from manager")
273 if _, ok := manager.Get(shell3.ID); ok {
274 t.Error("shell3 should be removed from manager")
277 // Verify list is empty (or doesn't contain our shells)
278 ids := manager.List()
279 for _, id := range ids {
280 if id == shell1.ID || id == shell2.ID || id == shell3.ID {
281 t.Errorf("shell %s should not be in list after KillAll", id)
286func TestBackgroundShellManager_KillAll_Timeout(t *testing.T) {
289 // XXX: can't use synctest here - causes --race to trip.
291 workingDir := t.TempDir()
292 manager := newBackgroundShellManager()
294 // Start a shell that traps signals and ignores cancellation.
295 _, err := manager.Start(t.Context(), workingDir, nil, "trap '' TERM INT; sleep 60", "")
296 require.NoError(t, err)
298 // Short timeout to test the timeout path.
299 ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
305 elapsed := time.Since(start)
307 // Must return promptly after timeout, not hang for 60 seconds.
308 require.Less(t, elapsed, 2*time.Second)
311func TestBackgroundShell_WaitContext_Completed(t *testing.T) {
314 done := make(chan struct{})
317 bgShell := &BackgroundShell{done: done}
319 ctx, cancel := context.WithTimeout(t.Context(), time.Second)
322 require.True(t, bgShell.WaitContext(ctx))
325func TestBackgroundShell_WaitContext_Canceled(t *testing.T) {
328 bgShell := &BackgroundShell{done: make(chan struct{})}
330 ctx, cancel := context.WithCancel(t.Context())
333 require.False(t, bgShell.WaitContext(ctx))
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