From dadcf4ba960bcf7ee4df2f6d89ecef6f75caa2e7 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 27 Jul 2020 10:00:19 -0500 Subject: [PATCH] add prompt.StubConfirm --- command/repo_test.go | 28 ++++------------------------ pkg/prompt/prompt.go | 11 +++++++++++ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/command/repo_test.go b/command/repo_test.go index 18e6ae304..904d4c6bb 100644 --- a/command/repo_test.go +++ b/command/repo_test.go @@ -294,12 +294,7 @@ func TestRepoFork_outside_survey_yes(t *testing.T) { cs.Stub("") // git clone cs.Stub("") // git remote add - oldConfirm := prompt.Confirm - prompt.Confirm = func(_ string, result *bool) error { - *result = true - return nil - } - defer func() { prompt.Confirm = oldConfirm }() + defer prompt.StubConfirm(true)() output, err := RunCommand("repo fork OWNER/REPO") if err != nil { @@ -329,12 +324,7 @@ func TestRepoFork_outside_survey_no(t *testing.T) { return &test.OutputStub{} })() - oldConfirm := prompt.Confirm - prompt.Confirm = func(_ string, result *bool) error { - *result = false - return nil - } - defer func() { prompt.Confirm = oldConfirm }() + defer prompt.StubConfirm(false)() output, err := RunCommand("repo fork OWNER/REPO") if err != nil { @@ -367,12 +357,7 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) { return &test.OutputStub{} })() - oldConfirm := prompt.Confirm - prompt.Confirm = func(_ string, result *bool) error { - *result = true - return nil - } - defer func() { prompt.Confirm = oldConfirm }() + defer prompt.StubConfirm(true)() output, err := RunCommand("repo fork") if err != nil { @@ -411,12 +396,7 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) { return &test.OutputStub{} })() - oldConfirm := prompt.Confirm - prompt.Confirm = func(_ string, result *bool) error { - *result = false - return nil - } - defer func() { prompt.Confirm = oldConfirm }() + defer prompt.StubConfirm(false)() output, err := RunCommand("repo fork") if err != nil { diff --git a/pkg/prompt/prompt.go b/pkg/prompt/prompt.go index 03bb194fa..76c58a11e 100644 --- a/pkg/prompt/prompt.go +++ b/pkg/prompt/prompt.go @@ -2,6 +2,17 @@ package prompt import "github.com/AlecAivazis/survey/v2" +func StubConfirm(result bool) func() { + orig := Confirm + Confirm = func(_ string, r *bool) error { + *r = result + return nil + } + return func() { + Confirm = orig + } +} + var Confirm = func(prompt string, result *bool) error { p := &survey.Confirm{ Message: prompt,