forked from derekparker/go-pear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
278 lines (222 loc) · 6.08 KB
/
main_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/libgit2/git2go"
)
func TestPear(t *testing.T) {
mockHomeEnv("fixtures/integration")
tmpstdin := mockStdin(t, "Person B")
tmp, oldstdout := mockStdout(t)
pearrc := createPearrc(t, []byte("email: [email protected]\ndevs:\n deva: Full Name A"))
defer func() {
cleanupStdout(t, tmp, oldstdout)
closeFile(tmpstdin)
closeFile(pearrc)
}()
os.Args = []string{"pear", "DevB", "DevA", "--file", "fixtures/test.config"}
main()
conf, err := readPearrc("fixtures/integration/.pearrc")
if err != nil {
t.Error(err)
}
if len(conf.Devs) != 2 {
t.Error("Devs were not recorded")
}
expectedUser := "Full Name A and Person B"
gitconfig := initTestGitConfig("fixtures/test.config", t)
actualUser := username(gitconfig)
if actualUser != expectedUser {
t.Errorf("Expected %s got %s", expectedUser, actualUser)
}
expectedEmail := "[email protected]"
actualEmail := email(gitconfig)
if actualEmail != expectedEmail {
t.Errorf("Expected %s got %s", expectedEmail, actualEmail)
}
}
func TestPearOneDevNoSavedEmail(t *testing.T) {
mockHomeEnv("fixtures/integration")
tmpstdin := mockStdin(t, "[email protected]")
tmp, oldstdout := mockStdout(t)
pearrc := createPearrc(t, []byte("devs:\n dev1: Full Name A"))
defer func() {
cleanupStdout(t, tmp, oldstdout)
closeFile(tmpstdin)
closeFile(pearrc)
}()
os.Args = []string{"pear", "dev1", "--email", "[email protected]", "--file", "fixtures/test.config"}
main()
readConf, err := readPearrc("fixtures/integration/.pearrc")
if err != nil {
t.Fatal(err)
}
if readConf.Email != "[email protected]" {
t.Error("Email was not saved.")
}
}
func TestPearWithinSubdirectory(t *testing.T) {
mockHomeEnv("fixtures/integration")
pearrc := createPearrc(t, []byte("email: [email protected]\ndevs:\n deva: Full Name A\n devb: Full Name B"))
defer closeFile(pearrc)
withinStubRepo(t, "foo", func(conf *git.Config) {
err := os.MkdirAll("bar", os.ModePerm|os.ModeExclusive|os.ModeDir)
if err != nil {
t.Fatal(err)
}
err = os.Chdir("bar")
if err != nil {
t.Fatal(err)
}
os.Args = []string{"pear", "DevB", "DevA"}
main()
conf.Refresh()
expected := "Full Name A and Full Name B"
if usr := username(conf); usr != expected {
t.Errorf("Expected %s, got %s", expected, usr)
}
})
}
func TestCheckEmail(t *testing.T) {
conf := Config{}
mockHomeEnv("fixtures/integration")
tempstdin := mockStdin(t, "[email protected]")
tmp, oldstdout := mockStdout(t)
pearrc := createPearrc(t, []byte("devs:\n dev1: Full Name A"))
defer func() {
cleanupStdout(t, tmp, oldstdout)
closeFile(tempstdin)
closeFile(pearrc)
}()
checkEmail(&conf)
_, err := tmp.Seek(0, os.SEEK_SET)
if err != nil {
t.Error(err)
}
output, err := ioutil.ReadAll(tmp)
if err != nil {
t.Error("Could not read from temp file")
}
if string(output) != "Please provide base author email:\n" {
t.Errorf("Prompt was incorrect, got: %#v", string(output))
}
expected := "[email protected]"
if conf.Email != expected {
t.Errorf("Expected %s, got %s", expected, conf.Email)
}
}
func TestSetPairWithOneDev(t *testing.T) {
gitconfig := initTestGitConfig("fixtures/test.config", t)
setPair("[email protected]", []string{"user1"}, gitconfig)
expected := "user1"
actual := username(gitconfig)
if actual != expected {
t.Errorf("Expected %s got %s", expected, actual)
}
}
func TestSetPairWithTwoDevs(t *testing.T) {
pair := []string{"user1", "user2"}
formattedEmail := formatEmail("[email protected]", pair)
gitconfig := initTestGitConfig("fixtures/test.config", t)
setPair(formattedEmail, pair, gitconfig)
expectedUser := "user1 and user2"
actualUser := username(gitconfig)
expectedEmail := "[email protected]"
actualEmail := email(gitconfig)
if actualUser != expectedUser {
t.Errorf("Expected %s got %s", expectedUser, actualUser)
}
if actualEmail != expectedEmail {
t.Errorf("Expected %s got %s", expectedEmail, actualEmail)
}
}
func TestReadPearrc(t *testing.T) {
nonExistantPath := "fixtures/.fakepearrc"
readPearrc(nonExistantPath)
f, err := os.Open(nonExistantPath)
if err != nil {
t.Error(err)
}
defer func() {
f.Close()
os.Remove(nonExistantPath)
}()
}
func TestSavePearrc(t *testing.T) {
expected := map[string]string{
"dparker": "Derek Parker",
"chriserin": "Chris Erin",
}
conf := Config{
Devs: expected,
}
err := savePearrc(&conf, "fixtures/.pearrc")
if err != nil {
t.Fatal(err)
}
readConf, err := readPearrc("fixtures/.pearrc")
if err != nil {
t.Fatal(err)
}
actual := readConf.Devs
if len(actual) != len(expected) {
t.Error("Did not read devs")
}
for username, dev := range expected {
if actual[username] != dev {
t.Errorf("Expected %s got %s", dev, actual[username])
}
}
}
func TestCheckPairWithUnknownDev(t *testing.T) {
expectedFullName := "Person B"
pair := []string{"knowndev", "newdev"}
conf := &Config{
Devs: map[string]string{
"knowndev": "Known Dev",
},
}
tmpstdin := mockStdin(t, "Person B")
tmp, oldstdout := mockStdout(t)
defer func() {
cleanupStdout(t, tmp, oldstdout)
closeFile(tmpstdin)
}()
checkPair(pair, conf)
_, err := tmp.Seek(0, os.SEEK_SET)
if err != nil {
t.Error(err)
}
output, err := ioutil.ReadAll(tmp)
if err != nil {
t.Error("Could not read from temp file")
}
if string(output) != "Please enter a full name for newdev:\n" {
t.Errorf("Question output was incorrect, got: %v", string(output))
}
fullName, ok := conf.Devs["newdev"]
if !ok {
t.Error("Dev was not found in conf")
}
if fullName != expectedFullName {
t.Errorf("Expected %s got %s", expectedFullName, fullName)
}
}
func TestEmailFormat(t *testing.T) {
tests := []struct {
email string
devs []string
expected string
}{
{"[email protected]", []string{"dev1"}, "[email protected]"},
{"[email protected]", []string{"dev1", "dev2"}, "[email protected]"},
{"[email protected]", []string{"dev1", "dev2", "dev3"}, "[email protected]"},
}
for _, test := range tests {
actual := formatEmail(test.email, test.devs)
if actual != test.expected {
t.Errorf("Expected %s, got %s", test.expected, actual)
}
}
}