Skip to content

Commit f1a8726

Browse files
sj26ampcode-com
andcommitted
Fix SpinWhile to handle non-TTY environments
- Invert TTY detection logic to run action directly when no TTY available - Add comprehensive test coverage for SpinWhile function - Prevents crashes in CI/CD and non-interactive environments Fixes logic from PR #499 Amp-Thread: https://ampcode.com/threads/T-c5d06564-9dec-49ca-9134-0733756f1e98 Co-authored-by: Amp <[email protected]>
1 parent e2fb0fb commit f1a8726

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

internal/io/spinner.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
)
99

1010
func SpinWhile(name string, action func()) error {
11-
if isatty.IsTerminal(os.Stdout.Fd()) {
11+
if !isatty.IsTerminal(os.Stdout.Fd()) {
12+
// No TTY available, just run the action without spinner
1213
action()
13-
1414
return nil
1515
}
1616

17+
// TTY is available, use the spinner
1718
return spinner.New().
1819
Title(name).
1920
Action(action).

internal/io/spinner_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSpinWhileWithoutTTY(t *testing.T) {
8+
// Test that SpinWhile works without TTY
9+
actionCalled := false
10+
err := SpinWhile("Test action", func() {
11+
actionCalled = true
12+
})
13+
14+
if err != nil {
15+
t.Errorf("SpinWhile should not return error: %v", err)
16+
}
17+
18+
if !actionCalled {
19+
t.Error("Action should have been called")
20+
}
21+
}

0 commit comments

Comments
 (0)