@@ -38,11 +38,47 @@ func TestConfirmBaseOnCountPromptDisabled(t *testing.T) {
38
38
assert .True (t , result2 )
39
39
}
40
40
41
- func TestConfirmBaseOnCountZeroApps (t * testing.T ) {
42
- p := & Prompt {enabled : true }
43
- result1 , result2 := p .ConfirmBaseOnCount ("Proceed?" , "Process all?" , 0 )
44
- assert .True (t , result1 )
45
- assert .True (t , result2 )
41
+ func TestConfirmBaseOnCount (t * testing.T ) {
42
+ tests := []struct {
43
+ input string
44
+ output bool
45
+ count int
46
+ }{
47
+ {
48
+ input : "y\n " ,
49
+ output : true ,
50
+ count : 0 ,
51
+ },
52
+ {
53
+ input : "y\n " ,
54
+ output : true ,
55
+ count : 1 ,
56
+ },
57
+ {
58
+ input : "n\n " ,
59
+ output : false ,
60
+ count : 1 ,
61
+ },
62
+ }
63
+
64
+ origStdin := os .Stdin
65
+
66
+ for _ , tt := range tests {
67
+ tmpFile , err := writeToStdin (tt .input )
68
+ require .NoError (t , err )
69
+ p := & Prompt {enabled : true }
70
+ result1 , result2 := p .ConfirmBaseOnCount ("Proceed?" , "Proceed all?" , tt .count )
71
+ assert .Equal (t , tt .output , result1 )
72
+ if tt .count == 1 {
73
+ assert .False (t , result2 )
74
+ } else {
75
+ assert .Equal (t , tt .output , result2 )
76
+ }
77
+ _ = tmpFile .Close ()
78
+ os .Remove (tmpFile .Name ())
79
+ }
80
+
81
+ os .Stdin = origStdin
46
82
}
47
83
48
84
func TestConfirmPrompt (t * testing.T ) {
@@ -62,8 +98,8 @@ func TestConfirmPrompt(t *testing.T) {
62
98
p := & Prompt {enabled : true }
63
99
result := p .Confirm ("Are you sure you want to run this command? (y/n) \n " )
64
100
assert .Equal (t , c .output , result )
65
- os .Remove (tmpFile .Name ())
66
101
_ = tmpFile .Close ()
102
+ os .Remove (tmpFile .Name ())
67
103
}
68
104
69
105
os .Stdin = origStdin
@@ -89,8 +125,8 @@ func TestConfirmAllPrompt(t *testing.T) {
89
125
confirm , confirmAll := p .ConfirmAll ("Are you sure you want to run this command? (y/n) \n " )
90
126
assert .Equal (t , c .confirm , confirm )
91
127
assert .Equal (t , c .confirmAll , confirmAll )
92
- os .Remove (tmpFile .Name ())
93
128
_ = tmpFile .Close ()
129
+ os .Remove (tmpFile .Name ())
94
130
}
95
131
96
132
os .Stdin = origStdin
0 commit comments