Skip to content

Commit 19e79b1

Browse files
authored
fix(confirm): --timeout was being ignored (#697)
* fix(confirm) Options.Timeout was ignored, now works as documented * Streamlines command.go per PR feedback
1 parent 93da22d commit 19e79b1

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

confirm/command.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package confirm
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67

@@ -26,12 +27,15 @@ func (o Options) Run() error {
2627
Value(&choice),
2728
),
2829
).
30+
WithTimeout(o.Timeout).
2931
WithTheme(theme).
3032
WithShowHelp(o.ShowHelp).
3133
Run()
3234

3335
if err != nil {
34-
return fmt.Errorf("unable to run confirm: %w", err)
36+
if !o.errIsValidTimeout(err) {
37+
return fmt.Errorf("unable to run confirm: %w", err)
38+
}
3539
}
3640

3741
if !choice {
@@ -40,3 +44,11 @@ func (o Options) Run() error {
4044

4145
return nil
4246
}
47+
48+
// errIsValidTimeout returns false unless 1) the user has specified a nonzero timeout and 2) the error is a huh.ErrTimeout.
49+
func (o Options) errIsValidTimeout(err error) bool {
50+
errWasTimeout := errors.Is(err, huh.ErrTimeout)
51+
timeoutsExpected := o.Timeout > 0
52+
53+
return errWasTimeout && timeoutsExpected
54+
}

confirm/command_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package confirm
2+
3+
import (
4+
"fmt"
5+
"github.com/charmbracelet/huh"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestOptions_errIsValidTimeout(t *testing.T) {
11+
type testCase struct {
12+
Description string
13+
GivenTimeout time.Duration
14+
GivenError error
15+
ExpectedResult bool
16+
}
17+
18+
cases := []testCase{
19+
{
20+
Description: "timeout is positive, err is a timeout",
21+
GivenTimeout: time.Second,
22+
GivenError: huh.ErrTimeout,
23+
ExpectedResult: true,
24+
},
25+
{
26+
Description: "timeout is zero, err is a timeout",
27+
GivenTimeout: 0,
28+
GivenError: huh.ErrTimeout,
29+
ExpectedResult: false,
30+
},
31+
{
32+
Description: "timeout is positive, err is not a timeout",
33+
GivenTimeout: 1,
34+
GivenError: fmt.Errorf("i'm not a timeout"),
35+
ExpectedResult: false,
36+
},
37+
{
38+
Description: "timeout is zero, err is not a timeout",
39+
GivenTimeout: 0,
40+
GivenError: fmt.Errorf("i'm not a timeout"),
41+
ExpectedResult: false,
42+
},
43+
}
44+
45+
for _, testCase := range cases {
46+
t.Run(testCase.Description, func(t *testing.T) {
47+
sut := Options{Timeout: testCase.GivenTimeout}
48+
actualResult := sut.errIsValidTimeout(testCase.GivenError)
49+
if actualResult != testCase.ExpectedResult {
50+
t.Errorf("got: %v, want: %v", actualResult, testCase.ExpectedResult)
51+
}
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)