X Tutup
Skip to content

Commit dadcf4b

Browse files
committed
add prompt.StubConfirm
1 parent 4c57566 commit dadcf4b

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

command/repo_test.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,7 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
294294
cs.Stub("") // git clone
295295
cs.Stub("") // git remote add
296296

297-
oldConfirm := prompt.Confirm
298-
prompt.Confirm = func(_ string, result *bool) error {
299-
*result = true
300-
return nil
301-
}
302-
defer func() { prompt.Confirm = oldConfirm }()
297+
defer prompt.StubConfirm(true)()
303298

304299
output, err := RunCommand("repo fork OWNER/REPO")
305300
if err != nil {
@@ -329,12 +324,7 @@ func TestRepoFork_outside_survey_no(t *testing.T) {
329324
return &test.OutputStub{}
330325
})()
331326

332-
oldConfirm := prompt.Confirm
333-
prompt.Confirm = func(_ string, result *bool) error {
334-
*result = false
335-
return nil
336-
}
337-
defer func() { prompt.Confirm = oldConfirm }()
327+
defer prompt.StubConfirm(false)()
338328

339329
output, err := RunCommand("repo fork OWNER/REPO")
340330
if err != nil {
@@ -367,12 +357,7 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
367357
return &test.OutputStub{}
368358
})()
369359

370-
oldConfirm := prompt.Confirm
371-
prompt.Confirm = func(_ string, result *bool) error {
372-
*result = true
373-
return nil
374-
}
375-
defer func() { prompt.Confirm = oldConfirm }()
360+
defer prompt.StubConfirm(true)()
376361

377362
output, err := RunCommand("repo fork")
378363
if err != nil {
@@ -411,12 +396,7 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) {
411396
return &test.OutputStub{}
412397
})()
413398

414-
oldConfirm := prompt.Confirm
415-
prompt.Confirm = func(_ string, result *bool) error {
416-
*result = false
417-
return nil
418-
}
419-
defer func() { prompt.Confirm = oldConfirm }()
399+
defer prompt.StubConfirm(false)()
420400

421401
output, err := RunCommand("repo fork")
422402
if err != nil {

pkg/prompt/prompt.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ package prompt
22

33
import "github.com/AlecAivazis/survey/v2"
44

5+
func StubConfirm(result bool) func() {
6+
orig := Confirm
7+
Confirm = func(_ string, r *bool) error {
8+
*r = result
9+
return nil
10+
}
11+
return func() {
12+
Confirm = orig
13+
}
14+
}
15+
516
var Confirm = func(prompt string, result *bool) error {
617
p := &survey.Confirm{
718
Message: prompt,

0 commit comments

Comments
 (0)
X Tutup