-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers_test.go
158 lines (132 loc) · 2.9 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestExists(t *testing.T) {
does, _ := exists("./docker")
assert.True(t, does)
does, _ = exists("./not-exists")
assert.False(t, does)
}
func TestDepInjDefaultAction(t *testing.T) {
result := depInjDefaultAction()
assert.Equal(t, reflect.ValueOf(placeholderDefaultActionMock), reflect.ValueOf(result))
complete := make(chan bool, 1)
fmt.Println("The following errors can be ignored:")
result(complete, dep{}, perform{})
fmt.Println("Okay, resume caring about errors.")
<-complete
}
func TestAskForConfirmationYes(t *testing.T) {
in, err := setupMockInput("Yes")
if err != nil {
t.Fatal(err)
}
result := askForConfirmation("Test Yes", in)
assert.True(t, result)
in.Close()
fmt.Println()
in, err = setupMockInput("y")
if err != nil {
t.Fatal(err)
}
result = askForConfirmation("Test y", in)
assert.True(t, result)
in.Close()
fmt.Println()
in, err = setupMockInput("YES")
if err != nil {
t.Fatal(err)
}
result = askForConfirmation("Test YES", in)
assert.True(t, result)
in.Close()
fmt.Println()
in, err = setupMockInput("yes")
if err != nil {
t.Fatal(err)
}
result = askForConfirmation("Test yes", in)
assert.True(t, result)
in.Close()
fmt.Println()
}
func TestAskForConfirmationNo(t *testing.T) {
in, err := setupMockInput("No")
if err != nil {
t.Fatal(err)
}
result := askForConfirmation("Test No", in)
assert.False(t, result)
in.Close()
fmt.Println()
in, err = setupMockInput("n")
if err != nil {
t.Fatal(err)
}
result = askForConfirmation("Test n", in)
assert.False(t, result)
in.Close()
fmt.Println()
in, err = setupMockInput("NO")
if err != nil {
t.Fatal(err)
}
result = askForConfirmation("Test NO", in)
assert.False(t, result)
in.Close()
fmt.Println()
in, err = setupMockInput("no")
if err != nil {
t.Fatal(err)
}
result = askForConfirmation("Test no", in)
assert.False(t, result)
in.Close()
fmt.Println()
}
func TestAskForConfirmationAgain(t *testing.T) {
in, err := setupMockInput("Nope \n n")
if err != nil {
t.Fatal(err)
}
result := askForConfirmation("", in)
assert.False(t, result)
in.Close()
fmt.Println()
}
func TestPosString(t *testing.T) {
haystack := []string{"Hello", "World", "Needle"}
pos := posString(haystack, "Needle")
assert.Equal(t, 2, pos)
pos = posString(haystack, "Goose")
assert.Equal(t, -1, pos)
}
func TestContainsString(t *testing.T) {
haystack := []string{"Hello", "World", "Needle"}
found := containsString(haystack, "Needle")
assert.True(t, found)
found = containsString(haystack, "Goose")
assert.False(t, found)
}
func setupMockInput(response string) (in *os.File, err error) {
in, err = ioutil.TempFile("", "")
if err != nil {
return
}
_, err = io.WriteString(in, response)
if err != nil {
return
}
_, err = in.Seek(0, os.SEEK_SET)
if err != nil {
return
}
return
}