-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
519 lines (455 loc) · 13.4 KB
/
main.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/dpakach/gorkin/lexer"
"github.com/dpakach/gorkin/object"
"github.com/dpakach/gorkin/parser"
"github.com/dpakach/gorkin/token"
)
type scenarioType string
const version = "1.0.0"
const (
// Outline is scenario Outline type
Outline scenarioType = "Outline"
// Normal is regular scenario type
Normal = "Normal"
)
// Feature is gherkin Feature
type Feature struct {
Type scenarioType `json:"type"`
LineNumber int `json:"line_number"`
Title string `json:"title"`
DataRow []object.TableData `json:"data_row"`
FilePath string `json:"file_path"`
Scenario *object.Scenario `json:"-"`
Background *object.Background `json:"-"`
}
type shift struct {
oldPath string
newPath string
}
func getShifts(out io.Writer) int {
latestFeatures := getFeatures()
oldFeatureData, err := ioutil.ReadFile("output.json")
if err != nil {
panic(err)
}
var oldFeatures []Feature
var shifts []shift
_ = json.Unmarshal(oldFeatureData, &oldFeatures)
for _, l := range latestFeatures {
found := false
for _, o := range oldFeatures {
if o.Type == Outline {
if o.Title == l.Title && dataRowSame(l.DataRow, o.DataRow) && getTestSuite(o) == getTestSuite(l) {
found = true
}
} else {
if o.Title == l.Title && getTestSuite(o) == getTestSuite(l) {
found = true
}
}
if found {
if o.LineNumber != l.LineNumber {
_, _ = io.WriteString(out, "Found Shift\n")
_, _ = io.WriteString(out, "New: "+getTestPath(l)+"\n")
_, _ = io.WriteString(out, "\n\n")
shifts = append(shifts, shift{getTestPath(o), getTestPath(l)})
}
break
}
}
if !found {
_, _ = io.WriteString(out, "new scenario found\n")
_, _ = io.WriteString(out, "New: "+getTestPath(l))
_, _ = io.WriteString(out, "\n\n")
}
}
expectedFailuresDir := os.Getenv("EXPECTED_FAILURES_DIR")
if expectedFailuresDir == "" {
_, _ = io.WriteString(out, "Expected Failures directory not provided\n")
_, _ = io.WriteString(out, "Please use EXPECTED_FAILURES_DIR env variable to provide path to directory where expected failure files are\n")
_, _ = io.WriteString(out, "\n")
return 1
}
expectedFailuresPrefix := os.Getenv("EXPECTED_FAILURES_PREFIX")
if expectedFailuresPrefix == "" {
_, _ = io.WriteString(out, "Expected Failures prefix not provided\n")
_, _ = io.WriteString(out, "using 'expected-failures' as prefix of expected failure files by default\n")
expectedFailuresPrefix = "expected-failures"
}
files, err := ioutil.ReadDir(expectedFailuresDir)
if err != nil {
_, _ = io.WriteString(out, err.Error())
return 1
}
for _, f := range files {
if strings.HasPrefix(f.Name(), expectedFailuresPrefix) {
err = replaceOccurrenceInFile(shifts, filepath.Join(expectedFailuresDir, f.Name()))
if err != nil {
_, _ = io.WriteString(out, err.Error())
return 1
}
}
}
return 0
}
func inspect(out io.Writer) bool {
latestFeatures := getFeatures()
atLeastOneFound := false
for _, l := range latestFeatures {
found := false
for _, o := range latestFeatures {
if o.Type == Outline {
if o.Title == l.Title && dataRowSame(l.DataRow, o.DataRow) && getTestSuite(o) == getTestSuite(l) {
found = true
}
} else {
if o.Title == l.Title && getTestSuite(o) == getTestSuite(l) {
found = true
}
}
if found {
if o.LineNumber != l.LineNumber {
atLeastOneFound = true
_, _ = io.WriteString(out, "-> Found Scenarios with same title on same file\n")
_, _ = io.WriteString(out, fmt.Sprintf("\tScenario 1: %s\n", getTestPath(o)))
_, _ = io.WriteString(out, fmt.Sprintf("\tScenario 2: %s\n", getTestPath(l)))
}
break
}
}
}
return atLeastOneFound
}
func checkDuplicates(out io.Writer) int {
hasDuplicates := inspect(out)
_, _ = io.WriteString(out, "\n")
if hasDuplicates {
_, _ = io.WriteString(out, "\n")
_, _ = io.WriteString(out, "Duplicate scenarios found\n")
return 1
}
return 0
}
//lint:ignore U1000 Ignore unused function deleteEmpty
func deleteEmpty(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}
type update struct {
token string
linenumber int
}
func getUpdatesFromSteps(steps []object.Step) []update {
var lastToken token.Type
updates := []update{}
for _, s := range steps {
if lastToken == token.GIVEN || lastToken == token.WHEN || lastToken == token.THEN {
if lastToken == s.Token.Type {
updates = append(updates, update{
s.Token.Type.String(),
s.Token.LineNumber,
})
}
}
if s.Token.Type != token.AND {
lastToken = s.Token.Type
}
}
return updates
}
func checkAnd(out io.Writer) int {
latestFeatures := getFeatures()
var lastFeaturePath string
for _, feature := range latestFeatures {
updates := []update{}
updates = append(updates, getUpdatesFromSteps(feature.Scenario.Steps)...)
if lastFeaturePath != feature.FilePath && feature.Background != nil {
updates = append(updates, getUpdatesFromSteps(feature.Background.Steps)...)
}
input, err := ioutil.ReadFile(feature.FilePath)
if err != nil {
_, _ = io.WriteString(out, err.Error())
_, _ = io.WriteString(out, "\n")
return 1
}
content := strings.Split(string(input), "\n")
for _, u := range updates {
_, _ = io.WriteString(out, fmt.Sprintf("Replacing %s from %s -> \"And\"\n\n", getTestPath(feature), u.token))
content[u.linenumber-1] = strings.Replace(content[u.linenumber-1], u.token, "And", 1) // strings.Join(replaceString, " ")
}
err = ioutil.WriteFile(feature.FilePath, []byte(strings.Join(content, "\n")), 0644)
if err != nil {
_, _ = io.WriteString(out, err.Error())
_, _ = io.WriteString(out, "\n")
return 1
}
lastFeaturePath = feature.FilePath
}
return 0
}
func main() {
out := new(bytes.Buffer)
exitStatus := 0
if len(os.Args) < 2 {
help(out)
exitStatus++
} else {
switch os.Args[1] {
case "help":
exitStatus += help(out)
case "cache":
exitStatus += checkDuplicates(out)
exitStatus += cacheFeaturesData(out)
case "shift":
exitStatus += checkDuplicates(out)
exitStatus += getShifts(out)
case "inspect":
exitStatus += checkDuplicates(out)
case "check-and":
exitStatus += checkAnd(out)
case "scan":
exitStatus += scanForNewScenarios(out)
exitStatus += scanForRemovedScenarios(out)
default:
_, _ = io.WriteString(out, "opps! seems like you forgot to provide the path of the feature file")
exitStatus++
}
}
_, _= io.Copy(os.Stdout, out)
os.Exit(exitStatus)
}
func dataRowSame(d1, d2 []object.TableData) bool {
if len(d1) != len(d2) {
return false
}
for i := range d1 {
if d1[i].Literal != d2[i].Literal {
return false
}
}
return true
}
func getFeatures() []Feature {
featuresPath := os.Getenv("FEATURES_PATH")
if featuresPath == "" {
log.Fatal(fmt.Errorf("error: setup features path with FEATURES_PATH env variable"))
}
path := featuresPath
abs, err := filepath.Abs(path)
if err != nil {
log.Fatal(fmt.Errorf("error: invalid path provided, make sure the path %q is correct", path))
}
fi, err := os.Stat(abs)
if os.IsNotExist(err) {
log.Fatal(fmt.Errorf("error: make sure the path %q exists", abs))
}
var features []Feature
switch mode := fi.Mode(); {
case mode.IsDir():
err := filepath.Walk(abs, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
var fileFeatures []Feature
if !info.IsDir() {
if ext := filepath.Ext(info.Name()); ext == ".feature" {
fileFeatures = getFeaturesFromFile(path)
}
}
features = append(features, fileFeatures...)
return nil
})
if err != nil {
log.Println(err)
}
case mode.IsRegular():
features = getFeaturesFromFile(abs)
}
return features
}
func cacheFeaturesData(out io.Writer) int {
features := getFeatures()
bolB, _ := json.Marshal(features)
err := ioutil.WriteFile("output.json", bolB, 0644)
if err != nil {
_, _ = io.WriteString(out, err.Error())
_, _ = io.WriteString(out, "\n")
return 1
}
return 0
}
func getFeaturesFromFile(filePath string) []Feature {
var features []Feature
l := lexer.NewFromFile(filePath)
p := parser.New(l)
res := p.Parse()
if len(p.Errors()) != 0 {
for _, err := range p.Errors() {
panic(err)
}
} else {
for _, feature := range res.Features {
for _, scenario := range feature.Scenarios {
outlineObj, ok := scenario.(*object.ScenarioOutline)
isOutline := ok
if isOutline {
fullTables := object.Table{}
for _, table := range outlineObj.Tables {
fullTables.Append(table)
}
for i, s := range outlineObj.GetScenarios() {
features = append(features, Feature{Outline, s.LineNumber, s.ScenarioText, fullTables[i+1], filePath, &s, feature.Background})
}
} else {
s, _ := scenario.(*object.Scenario)
features = append(features, Feature{Normal, s.LineNumber, s.ScenarioText, []object.TableData{}, filePath, s, feature.Background})
}
}
}
}
return features
}
func getTestPath(feature Feature) string {
dir := filepath.Dir(feature.FilePath)
base := filepath.Base(dir)
filename := filepath.Base(feature.FilePath)
return fmt.Sprintf("%s:%d", filepath.Join(base, filename), feature.LineNumber)
}
func getTestSuite(feature Feature) string {
dir := filepath.Dir(feature.FilePath)
base := filepath.Base(dir)
filename := filepath.Base(feature.FilePath)
return filepath.Join(base, filename)
}
func getGithubLinkPath(path string) string {
parts := strings.Split(path, ":")
if len(parts) != 2 {
log.Fatal("Could not parse path")
}
return fmt.Sprintf("%s#L%s", parts[0], parts[1])
}
func replaceOccurrenceInFile(shifts []shift, filePath string) error {
input, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
newContents := string(input)
for _, shift := range shifts {
newContents = strings.Replace(
newContents,
fmt.Sprintf("[%s]", shift.oldPath),
// adding extra line to avoid changing already changed line
fmt.Sprintf("[%s]", shift.newPath+"आफ्नै बादल"),
-1,
)
}
for _, shift := range shifts {
newContents = strings.Replace(
newContents,
fmt.Sprintf("%s)", getGithubLinkPath(shift.oldPath)),
// adding extra line to avoid changing already changed line
fmt.Sprintf("%s%s)", getGithubLinkPath(shift.newPath), "आफ्नै बादल"),
-1,
)
}
// Remove added extra line
newContents = strings.Replace(newContents, "आफ्नै बादल", "", -1)
err = ioutil.WriteFile(filePath, []byte(newContents), 0644)
if err != nil {
return err
}
return nil
}
// scanForNewScenarios scans for new scenarios added after caching
func scanForNewScenarios(out io.Writer) int {
latestFeatures := getFeatures()
oldFeaturesData, err := ioutil.ReadFile("output.json")
if err != nil {
_, _ = io.WriteString(out, err.Error())
return 1
}
var oldFeatures []Feature
_ = json.Unmarshal(oldFeaturesData, &oldFeatures)
for _, l := range latestFeatures {
found := false
for _, o := range oldFeatures {
if o.Type == Outline {
if o.Title == l.Title && dataRowSame(o.DataRow, l.DataRow) && getTestSuite(o) == getTestSuite(l) {
found = true
}
} else {
if o.Title == l.Title && getTestSuite(o) == getTestSuite(l) {
found = true
}
}
}
if !found {
_, _ = io.WriteString(out, "found new scenario\n")
_, _ = io.WriteString(out, "New: "+getTestPath(l))
_, _ = io.WriteString(out, "\n\n")
}
}
return 0
}
// scanForRemovedScenarios scans for old scenarios removed after caching
func scanForRemovedScenarios(out io.Writer) int {
latestFeatures := getFeatures()
oldFeaturesData, err := ioutil.ReadFile("output.json")
if err != nil {
_, _ = io.WriteString(out, err.Error())
return 1
}
var oldFeatures []Feature
_ = json.Unmarshal(oldFeaturesData, &oldFeatures)
for _, o := range oldFeatures {
found := false
for _, l := range latestFeatures {
if o.Type == Outline {
if o.Title == l.Title && dataRowSame(o.DataRow, l.DataRow) && getTestSuite(o) == getTestSuite(l) {
found = true
}
} else {
if o.Title == l.Title && getTestSuite(o) == getTestSuite(l) {
found = true
}
}
}
if !found {
_, _ = io.WriteString(out, "scenario got removed\n")
_, _ = io.WriteString(out, "Deleted: "+getTestPath(o))
_, _ = io.WriteString(out, "\n\n")
}
}
return 0
}
func help(out io.Writer) int {
_, _ = io.WriteString(out, "ocBddKit " + version + ", A tool to manage feature files for ownCloud\n")
_, _ = io.WriteString(out, "Usage: ocBddKit <option>\n\n")
_, _ = io.WriteString(out, "Available Options:\n")
_, _ = io.WriteString(out, "\thelp - to display this help message\n")
_, _ = io.WriteString(out, "\tinspect - to inspect the feature file/dir if acceptable for caching\n")
_, _ = io.WriteString(out, "\tcache - to cache a feature file/dir\n")
_, _ = io.WriteString(out, "\tshift - to update the expected failures files\n")
_, _ = io.WriteString(out, "\tscan - to scan if new scenarios were added or old scenarios were deleted\n")
_, _ = io.WriteString(out, "\n")
_, _ = io.WriteString(out, "Instructions:\n")
_, _ = io.WriteString(out, "\t Use the existing commitID in .drone.env from respective projects to 'inspect' and 'cache'\n")
_, _ = io.WriteString(out, "\t Then, checkout to the latest version of that project.\n")
_, _ = io.WriteString(out, "\t After that, update the expected failures files with 'shift' command or 'scan' for new or removed scenarios\n")
return 0
}