-
Notifications
You must be signed in to change notification settings - Fork 25
/
resources_test.go
298 lines (257 loc) · 7.92 KB
/
resources_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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"bytes"
"os"
"reflect"
"regexp"
"testing"
"github.com/rainforestapp/rainforest-cli/rainforest"
"github.com/urfave/cli"
)
func regexMatchOut(pattern string, t *testing.T) {
matched, err := regexp.Match(pattern, tablesOut.(*bytes.Buffer).Bytes())
if err != nil {
t.Error("Error with pattern match:", err)
}
if !matched {
t.Errorf("Printed out %v, want %v", tablesOut, pattern)
}
}
func TestPrintResourceTable(t *testing.T) {
tablesOut = &bytes.Buffer{}
defer func() {
tablesOut = os.Stdout
}()
testBody := [][]string{{"1337", "Dyer"}, {"42", "Situation"}}
printResourceTable([]string{"Column 1", "Column 2"}, testBody)
regexMatchOut(`\| +COLUMN 1 +\| +COLUMN 2 +\|`, t)
regexMatchOut(`\| +1337 +\| +Dyer +\|`, t)
}
type testResourceAPI struct {
Folders []rainforest.Folder
Platforms []rainforest.Platform
Sites []rainforest.Site
Environments []rainforest.Environment
Features []rainforest.Feature
RunGroups []rainforest.RunGroup
Junit string
}
func (api testResourceAPI) GetFolders() ([]rainforest.Folder, error) {
return api.Folders, nil
}
func (api testResourceAPI) GetPlatforms() ([]rainforest.Platform, error) {
return api.Platforms, nil
}
func (api testResourceAPI) GetSites() ([]rainforest.Site, error) {
return api.Sites, nil
}
func (api testResourceAPI) GetEnvironments() ([]rainforest.Environment, error) {
return api.Environments, nil
}
func (api testResourceAPI) GetFeatures() ([]rainforest.Feature, error) {
return api.Features, nil
}
func (api testResourceAPI) GetRunGroups() ([]rainforest.RunGroup, error) {
return api.RunGroups, nil
}
func (api testResourceAPI) GetRunJunit(run_id int) (*string, error) {
return &api.Junit, nil
}
func TestPrintFolders(t *testing.T) {
tablesOut = &bytes.Buffer{}
defer func() {
tablesOut = os.Stdout
}()
testAPI := testResourceAPI{
Folders: []rainforest.Folder{
{ID: 123, Title: "First Folder Title"},
{ID: 456, Title: "Second Folder Title"},
},
}
printFolders(testAPI)
regexMatchOut(`\| +FOLDER ID +\| +FOLDER NAME +\|`, t)
regexMatchOut(`\| +123 +\| +First Folder Title +\|`, t)
regexMatchOut(`\| +456 +\| +Second Folder Title +\|`, t)
}
func TestPrintPlatforms(t *testing.T) {
tablesOut = &bytes.Buffer{}
defer func() {
tablesOut = os.Stdout
}()
testAPI := testResourceAPI{
Platforms: []rainforest.Platform{
{Name: "chrome", Description: "Google Chrome"},
{Name: "firefox", Description: "Mozilla Firefox"},
},
}
printPlatforms(testAPI)
regexMatchOut(`\| +PLATFORM ID +\| +PLATFORM NAME +\|`, t)
regexMatchOut(`\| +chrome +\| +Google Chrome +\|`, t)
regexMatchOut(`\| +firefox +\| +Mozilla Firefox +\|`, t)
}
func TestPrintSites(t *testing.T) {
tablesOut = &bytes.Buffer{}
defer func() {
tablesOut = os.Stdout
}()
testAPI := testResourceAPI{
Sites: []rainforest.Site{
{ID: 123, Name: "My favorite site", Category: "site"},
{ID: 456, Name: "My favorite app URL", Category: "ios"},
{ID: 789, Name: "Site with unknown platform", Category: "unknown_platform"},
},
}
printSites(testAPI)
regexMatchOut(`\| +SITE ID +\| +SITE NAME +\| +CATEGORY +\|`, t)
regexMatchOut(`\| +123 +\| +My favorite site +\| +Site +\|`, t)
regexMatchOut(`\| +456 +\| +My favorite app URL +\| +iOS +\|`, t)
regexMatchOut(`\| +789 +\| +Site with unknown platform +\| +unknown_platform +\|`, t)
}
func TestPrintEnvironments(t *testing.T) {
tablesOut = &bytes.Buffer{}
defer func() {
tablesOut = os.Stdout
}()
testAPI := testResourceAPI{
Environments: []rainforest.Environment{
{ID: 123, Name: "QA"},
{ID: 456, Name: "Staging 1"},
{ID: 789, Name: "Staging 2"},
},
}
printEnvironments(testAPI)
regexMatchOut(`\| +ENVIRONMENT ID +\| +ENVIRONMENT NAME +\|`, t)
regexMatchOut(`\| +123 +\| +QA +\|`, t)
regexMatchOut(`\| +456 +\| +Staging 1 +\|`, t)
regexMatchOut(`\| +789 +\| +Staging 2 +\|`, t)
}
func TestPrintFeatures(t *testing.T) {
tablesOut = &bytes.Buffer{}
defer func() {
tablesOut = os.Stdout
}()
testAPI := testResourceAPI{
Features: []rainforest.Feature{
{ID: 123, Title: "My favorite feature"},
{ID: 456, Title: "My least favorite feature"},
{ID: 789, Title: "An OK feature"},
},
}
printFeatures(testAPI)
regexMatchOut(`\| +FEATURE ID +\| +FEATURE TITLE +\|`, t)
regexMatchOut(`\| +123 +\| +My favorite feature +\|`, t)
regexMatchOut(`\| +456 +\| +My least favorite feature +\|`, t)
regexMatchOut(`\| +789 +\| +An OK feature +\|`, t)
}
func TestPrintRunGroups(t *testing.T) {
tablesOut = &bytes.Buffer{}
defer func() {
tablesOut = os.Stdout
}()
testAPI := testResourceAPI{
RunGroups: []rainforest.RunGroup{
{ID: 123, Title: "My favorite run group"},
{ID: 456, Title: "My least favorite run group"},
{ID: 789, Title: "An OK run group"},
},
}
printRunGroups(testAPI)
regexMatchOut(`\| +RUN GROUP ID +\| +RUN GROUP TITLE +\|`, t)
regexMatchOut(`\| +123 +\| +My favorite run group +\|`, t)
regexMatchOut(`\| +456 +\| +My least favorite run group +\|`, t)
regexMatchOut(`\| +789 +\| +An OK run group +\|`, t)
}
func TestWriteJunit(t *testing.T) {
fakeContext := newFakeContext(map[string]interface{}{"junit-file": "junit.xml"}, cli.Args{"1"})
testAPI := testResourceAPI{
Junit: "<xml>hai</xml>",
}
err := writeJunit(fakeContext, testAPI, 0)
if err != nil {
t.Errorf("writeJunit returned %+v", err)
}
data, _ := os.ReadFile("junit.xml")
if !reflect.DeepEqual(testAPI.Junit, string(data)) {
t.Errorf("writeJunit wrote %+v, want %+v", data, testAPI.Junit)
}
// uses the passed runID and doesn't error if there are none in the context
fakeContext = newFakeContext(map[string]interface{}{"junit-file": "junit.xml"}, cli.Args{})
err = writeJunit(fakeContext, testAPI, 1)
if err != nil {
t.Errorf("writeJunit returned %+v", err)
}
data, _ = os.ReadFile("junit.xml")
if !reflect.DeepEqual(testAPI.Junit, string(data)) {
t.Errorf("writeJunit wrote %+v, want %+v", data, testAPI.Junit)
}
// errors when the junit file is not properly provided
fakeContext = newFakeContext(map[string]interface{}{"junit-file": ""}, cli.Args{"1"})
err = writeJunit(fakeContext, testAPI, 0)
expected := "JUnit output file not specified"
if !reflect.DeepEqual(expected, err.Error()) {
t.Errorf("writeJunit should have errored: expected '%v', got '%v'", expected, err.Error())
}
// errors when the junit file is not provided
fakeContext = newFakeContext(map[string]interface{}{}, cli.Args{"1"})
err = writeJunit(fakeContext, testAPI, 0)
expected = "JUnit output file not specified"
if !reflect.DeepEqual(expected, err.Error()) {
t.Errorf("writeJunit should have errored: expected '%v', got '%v'", expected, err.Error())
}
// errors when no runID is specified
fakeContext = newFakeContext(map[string]interface{}{"junit-file": "junit.xml"}, cli.Args{})
err = writeJunit(fakeContext, testAPI, 0)
expected = "No run ID argument found."
if !reflect.DeepEqual(expected, err.Error()) {
t.Errorf("writeJunit should have errored: expected '%v', got '%v'", expected, err.Error())
}
}
func TestAugmentJunitFileName(t *testing.T) {
testCases := []struct {
OriginalName string
RerunAttempt uint
Want string
}{
{
OriginalName: "output",
RerunAttempt: 1,
Want: "output.1",
},
{
OriginalName: "output.xml",
RerunAttempt: 1,
Want: "output.xml.1",
},
{
OriginalName: "output.xml",
RerunAttempt: 2,
Want: "output.xml.2",
},
{
OriginalName: "output.xml.1",
RerunAttempt: 3,
Want: "output.xml.1.3",
},
{
OriginalName: "some.output.xml.1.2.3",
RerunAttempt: 2,
Want: "some.output.xml.1.2.3.2",
},
{
OriginalName: "output.html",
RerunAttempt: 1,
Want: "output.html.1",
},
{
OriginalName: "output.1",
RerunAttempt: 2,
Want: "output.1.2",
},
}
for _, testCase := range testCases {
got := augmentJunitFileName(testCase.OriginalName, testCase.RerunAttempt)
if got != testCase.Want {
t.Errorf("Wrong name, wanted '%v', got '%v'", testCase.Want, got)
}
}
}