add test helpers
This commit is contained in:
parent
8bba7f9624
commit
2d093c1741
4 changed files with 51 additions and 26 deletions
22
internal/prompter/test.go
Normal file
22
internal/prompter/test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package prompter
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Test helpers
|
||||
|
||||
func IndexFor(options []string, answer string) (int, error) {
|
||||
for ix, a := range options {
|
||||
if a == answer {
|
||||
return ix, nil
|
||||
}
|
||||
}
|
||||
return -1, NoSuchAnswerErr(answer)
|
||||
}
|
||||
|
||||
func NoSuchAnswerErr(answer string) error {
|
||||
return fmt.Errorf("no such answer '%s'", answer)
|
||||
}
|
||||
|
||||
func NoSuchPromptErr(prompt string) error {
|
||||
return fmt.Errorf("no such prompt '%s'", prompt)
|
||||
}
|
||||
|
|
@ -385,8 +385,11 @@ func Test_loginRun_Survey(t *testing.T) {
|
|||
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org"))
|
||||
},
|
||||
prompterStubs: func(pm *prompter.PrompterMock) {
|
||||
pm.SelectFunc = func(_, _ string, _ []string) (int, error) {
|
||||
return 0, nil
|
||||
pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) {
|
||||
if prompt == "What account do you want to log into?" {
|
||||
return prompter.IndexFor(opts, "GitHub.com")
|
||||
}
|
||||
return -1, prompter.NoSuchPromptErr(prompt)
|
||||
}
|
||||
},
|
||||
wantHosts: "",
|
||||
|
|
@ -405,14 +408,14 @@ func Test_loginRun_Survey(t *testing.T) {
|
|||
git_protocol: https
|
||||
`),
|
||||
prompterStubs: func(pm *prompter.PrompterMock) {
|
||||
pm.SelectFunc = func(prompt, _ string, _ []string) (int, error) {
|
||||
pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) {
|
||||
switch prompt {
|
||||
case "What is your preferred protocol for Git operations?":
|
||||
return 0, nil
|
||||
return prompter.IndexFor(opts, "HTTPS")
|
||||
case "How would you like to authenticate GitHub CLI?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "Paste an authentication token")
|
||||
}
|
||||
return -1, nil
|
||||
return -1, prompter.NoSuchPromptErr(prompt)
|
||||
}
|
||||
},
|
||||
runStubs: func(rs *run.CommandStubber) {
|
||||
|
|
@ -439,16 +442,16 @@ func Test_loginRun_Survey(t *testing.T) {
|
|||
Interactive: true,
|
||||
},
|
||||
prompterStubs: func(pm *prompter.PrompterMock) {
|
||||
pm.SelectFunc = func(prompt, _ string, _ []string) (int, error) {
|
||||
pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) {
|
||||
switch prompt {
|
||||
case "What account do you want to log into?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "GitHub Enterprise Server")
|
||||
case "What is your preferred protocol for Git operations?":
|
||||
return 0, nil
|
||||
return prompter.IndexFor(opts, "HTTPS")
|
||||
case "How would you like to authenticate GitHub CLI?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "Paste an authentication token")
|
||||
}
|
||||
return -1, nil
|
||||
return -1, prompter.NoSuchPromptErr(prompt)
|
||||
}
|
||||
pm.InputHostnameFunc = func() (string, error) {
|
||||
return "brad.vickers", nil
|
||||
|
|
@ -478,16 +481,16 @@ func Test_loginRun_Survey(t *testing.T) {
|
|||
Interactive: true,
|
||||
},
|
||||
prompterStubs: func(pm *prompter.PrompterMock) {
|
||||
pm.SelectFunc = func(prompt, _ string, _ []string) (int, error) {
|
||||
pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) {
|
||||
switch prompt {
|
||||
case "What account do you want to log into?":
|
||||
return 0, nil
|
||||
return prompter.IndexFor(opts, "GitHub.com")
|
||||
case "What is your preferred protocol for Git operations?":
|
||||
return 0, nil
|
||||
return prompter.IndexFor(opts, "HTTPS")
|
||||
case "How would you like to authenticate GitHub CLI?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "Paste an authentication token")
|
||||
}
|
||||
return -1, nil
|
||||
return -1, prompter.NoSuchPromptErr(prompt)
|
||||
}
|
||||
},
|
||||
runStubs: func(rs *run.CommandStubber) {
|
||||
|
|
@ -508,16 +511,16 @@ func Test_loginRun_Survey(t *testing.T) {
|
|||
Interactive: true,
|
||||
},
|
||||
prompterStubs: func(pm *prompter.PrompterMock) {
|
||||
pm.SelectFunc = func(prompt, _ string, _ []string) (int, error) {
|
||||
pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) {
|
||||
switch prompt {
|
||||
case "What account do you want to log into?":
|
||||
return 0, nil
|
||||
return prompter.IndexFor(opts, "GitHub.com")
|
||||
case "What is your preferred protocol for Git operations?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "SSH")
|
||||
case "How would you like to authenticate GitHub CLI?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "Paste an authentication token")
|
||||
}
|
||||
return -1, nil
|
||||
return -1, prompter.NoSuchPromptErr(prompt)
|
||||
}
|
||||
},
|
||||
wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens"),
|
||||
|
|
|
|||
|
|
@ -48,14 +48,14 @@ func TestLogin_ssh(t *testing.T) {
|
|||
httpmock.StringResponse(`{}`))
|
||||
|
||||
pm := &prompter.PrompterMock{}
|
||||
pm.SelectFunc = func(prompt, _ string, _ []string) (int, error) {
|
||||
pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) {
|
||||
switch prompt {
|
||||
case "What is your preferred protocol for Git operations?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "SSH")
|
||||
case "How would you like to authenticate GitHub CLI?":
|
||||
return 1, nil
|
||||
return prompter.IndexFor(opts, "Paste an authentication token")
|
||||
}
|
||||
return -1, nil
|
||||
return -1, prompter.NoSuchPromptErr(prompt)
|
||||
}
|
||||
pm.PasswordFunc = func(_ string) (string, error) {
|
||||
return "monkey", nil
|
||||
|
|
|
|||
|
|
@ -395,7 +395,7 @@ func TestNewCmdExtension(t *testing.T) {
|
|||
return "", nil
|
||||
}
|
||||
pm.SelectFunc = func(prompt, defVal string, opts []string) (int, error) {
|
||||
return 0, nil
|
||||
return prompter.IndexFor(opts, "Script (Bash, Ruby, Python, etc)")
|
||||
}
|
||||
},
|
||||
wantStdout: heredoc.Doc(`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue