Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(confirm) --timeout was ignored, now works as documented #697

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion confirm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ func (o Options) Run() error {
Value(&choice),
),
).
WithTimeout(o.Timeout).
WithTheme(theme).
WithShowHelp(o.ShowHelp).
Run()

if err != nil {
return fmt.Errorf("unable to run confirm: %w", err)
allowErr := o.errIsValidTimeout(err)

if !allowErr {
return fmt.Errorf("unable to run confirm: %w", err)
}
}

if !choice {
Expand All @@ -40,3 +45,11 @@ func (o Options) Run() error {

return nil
}

// errIsValidTimeout returns false unless 1) the user has specified a nonzero timeout and 2) the error is a huh.ErrTimeout.
func (o Options) errIsValidTimeout(err error) bool {
errWasTimeout := err.Error() == huh.ErrTimeout.Error()
timeoutsExpected := o.Timeout > 0

return errWasTimeout && timeoutsExpected
}
54 changes: 54 additions & 0 deletions confirm/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package confirm

import (
"fmt"
"github.com/charmbracelet/huh"
"testing"
"time"
)

func TestOptions_errIsValidTimeout(t *testing.T) {
type testCase struct {
Description string
GivenTimeout time.Duration
GivenError error
ExpectedResult bool
}

cases := []testCase{
{
Description: "timeout is positive, err is a timeout",
GivenTimeout: time.Second,
GivenError: huh.ErrTimeout,
ExpectedResult: true,
},
{
Description: "timeout is zero, err is a timeout",
GivenTimeout: 0,
GivenError: huh.ErrTimeout,
ExpectedResult: false,
},
{
Description: "timeout is positive, err is not a timeout",
GivenTimeout: 1,
GivenError: fmt.Errorf("i'm not a timeout"),
ExpectedResult: false,
},
{
Description: "timeout is zero, err is not a timeout",
GivenTimeout: 0,
GivenError: fmt.Errorf("i'm not a timeout"),
ExpectedResult: false,
},
}

for _, testCase := range cases {
t.Run(testCase.Description, func(t *testing.T) {
sut := Options{Timeout: testCase.GivenTimeout}
actualResult := sut.errIsValidTimeout(testCase.GivenError)
if actualResult != testCase.ExpectedResult {
t.Errorf("got: %v, want: %v", actualResult, testCase.ExpectedResult)
}
})
}
}