Skip to content

Commit 31284e3

Browse files
refactor: extract surveyAskOne function variable for testing
Enables mocking of survey.AskOne in unit tests for prompt utilities
1 parent a0e2ba8 commit 31284e3

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

utils/prompt.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"github.com/AlecAivazis/survey/v2/terminal"
88
)
99

10+
// Function variable for testing - allows mocking survey.AskOne
11+
var surveyAskOne = survey.AskOne
12+
1013
// ==========
1114
// Useful APIs of survey library
1215
// ==========
@@ -21,7 +24,7 @@ func ConfirmFromUser(msg, helpText string) (bool, error) {
2124
Help: helpText,
2225
}
2326

24-
err := survey.AskOne(confirmPrompt, &response)
27+
err := surveyAskOne(confirmPrompt, &response)
2528
if err != nil {
2629
return true, checkInterrupt(err)
2730
}
@@ -42,7 +45,7 @@ func SelectFromOptions(msg, helpText string, opts []string) (string, error) {
4245
Default: nil,
4346
Help: helpText,
4447
}
45-
err := survey.AskOne(prompt, &result)
48+
err := surveyAskOne(prompt, &result)
4649
if err != nil {
4750
return "", checkInterrupt(err)
4851
}
@@ -60,7 +63,7 @@ func GetSingleLineInput(msg, helpText string) (string, error) {
6063
Help: helpText,
6164
}
6265

63-
err := survey.AskOne(prompt, &response)
66+
err := surveyAskOne(prompt, &response)
6467
if err != nil {
6568
return "", checkInterrupt(err)
6669
}
@@ -84,7 +87,7 @@ func SelectFromMultipleOptions(msg, helpText string, options []string) ([]string
8487
Options: options,
8588
Help: helpText,
8689
}
87-
err := survey.AskOne(analyzerPrompt, &response, survey.WithValidator(survey.Required))
90+
err := surveyAskOne(analyzerPrompt, &response, survey.WithValidator(survey.Required))
8891
if err != nil {
8992
return nil, checkInterrupt(err)
9093
}

0 commit comments

Comments
 (0)