From 2d093c1741aab1dbb28b774c90ec835f2c39254e Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 15 Aug 2022 13:53:43 -0500 Subject: [PATCH] add test helpers --- internal/prompter/test.go | 22 +++++++++++++ pkg/cmd/auth/login/login_test.go | 45 ++++++++++++++------------ pkg/cmd/auth/shared/login_flow_test.go | 8 ++--- pkg/cmd/extension/command_test.go | 2 +- 4 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 internal/prompter/test.go diff --git a/internal/prompter/test.go b/internal/prompter/test.go new file mode 100644 index 000000000..ae7574ea9 --- /dev/null +++ b/internal/prompter/test.go @@ -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) +} diff --git a/pkg/cmd/auth/login/login_test.go b/pkg/cmd/auth/login/login_test.go index c05d9b9de..65de3065c 100644 --- a/pkg/cmd/auth/login/login_test.go +++ b/pkg/cmd/auth/login/login_test.go @@ -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"), diff --git a/pkg/cmd/auth/shared/login_flow_test.go b/pkg/cmd/auth/shared/login_flow_test.go index 8bcecfc6a..40d045661 100644 --- a/pkg/cmd/auth/shared/login_flow_test.go +++ b/pkg/cmd/auth/shared/login_flow_test.go @@ -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 diff --git a/pkg/cmd/extension/command_test.go b/pkg/cmd/extension/command_test.go index 8cdb45d45..fb4707320 100644 --- a/pkg/cmd/extension/command_test.go +++ b/pkg/cmd/extension/command_test.go @@ -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(`