-
Notifications
You must be signed in to change notification settings - Fork 1
/
gherkinize.go
179 lines (166 loc) · 4.43 KB
/
gherkinize.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
package main
import (
"os"
"fmt"
"sort"
"container/list"
"github.com/wesovilabs/gherkinize/path"
"github.com/wesovilabs/gherkinize/gherkin"
"github.com/wesovilabs/gherkinize/util"
"github.com/wesovilabs/gherkinize/config"
"github.com/fatih/color"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "Gherkinize"
app.Usage = "A Gherkin utility command-line tool."
app.Version = "0.0.1"
app.Authors = []cli.Author{
cli.Author{
Name: "Iván Corrales Solera",
Email: "developer@wesovilabs.com",
},
}
app.Copyright = "(c) 2017 Wesovilabs"
app.EnableBashCompletion = true
var configurationFile string
var scenariosPath string
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load toml configuration from `FILE`",
Destination: &configurationFile,
},
cli.StringFlag{
Name: "input, i",
Usage: "Scenarios directory path",
Destination: &scenariosPath,
},
}
app.Commands = []cli.Command{
{
Name: "validate",
Aliases: []string{"v"},
Usage: "validate the scenarios",
Action: func(c *cli.Context) error {
if( scenariosPath == ""){
return cli.NewExitError("Please specify the scenarios path by using option -i", 86)
}
if(configurationFile == ""){
configurationFile = "./config/gherkin-rules.toml"
}
validateFiles(scenariosPath,configurationFile)
return nil
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
app.Run(os.Args)
}
func showInvalidStructureMessage(feature string, lineNumber int){
fmt.Println()
util.Print_subtitle(feature)
fmt.Println()
util.Print_error("Line: %d", lineNumber)
util.Print_error("Missing scenario keyword.")
color.White("[ USAGE ]")
util.Print_message("\tScenario:")
util.Print_message("\t Given ...")
util.Print_message("\t When ...")
util.Print_message("\t Then ...")
}
func validateFiles(scenarios_path string, config_path string) error {
gherkin.InitializeTokenMap()
list_files := path.ReadDirectory(scenarios_path)
configFile, err := os.Open(config_path)
if err != nil {
return nil
}
config := config.GetConfig(configFile)
for _, filePath := range list_files {
file, err := os.Open(filePath)
if err != nil {
return nil
}
defer file.Close()
scanner := gherkin.NewGherkinScanner(file)
lineNumber :=1
tokens := list.New()
fmt.Println()
fmt.Println()
util.Print_title("-------------------------------------------------- ")
util.Print_title("File " + filePath)
util.Print_title("-------------------------------------------------- ")
var feature = &gherkin.Feature{}
var invalidFeature = false
for {
token := scanner.Scan(lineNumber)
switch token.Kind {
case gherkin.EOF:
feature.Validate(*config)
case gherkin.NEW_LINE:
if(feature.LineNumber == 0) {
util.Print_error("Missing feature tag.")
invalidFeature = true
break
}
case gherkin.TOKEN_FEATURE:
*feature = token.ToFeature()
case gherkin.TOKEN_SCENARIO:
if(feature.LineNumber == 0) {
break
}
scenario := token.ToScenario()
feature.Scenarios.PushBack(scenario)
case gherkin.TOKEN_GIVEN:
step := token.ToStep()
if(feature.Scenarios.Back() == nil){
showInvalidStructureMessage(feature.Text, token.LineNumber)
invalidFeature = true
break
} else {
feature.Scenarios.Back().Value.(gherkin.Scenario).Steps.PushBack(step)
}
case gherkin.TOKEN_WHEN:
step := token.ToStep()
if(feature.Scenarios.Back() == nil){
showInvalidStructureMessage(feature.Text, token.LineNumber)
invalidFeature = true
break
} else {
feature.Scenarios.Back().Value.(gherkin.Scenario).Steps.PushBack(step)
}
case gherkin.TOKEN_THEN:
step := token.ToStep()
if(feature.Scenarios.Back() == nil){
showInvalidStructureMessage(feature.Text, token.LineNumber)
invalidFeature = true
break
} else {
feature.Scenarios.Back().Value.(gherkin.Scenario).Steps.PushBack(step)
}
case gherkin.TOKEN_AND:
step := token.ToStep()
if(feature.Scenarios.Back() == nil){
showInvalidStructureMessage(feature.Text, token.LineNumber)
invalidFeature = true
break
} else {
feature.Scenarios.Back().Value.(gherkin.Scenario).Steps.PushBack(step)
}
}
if(invalidFeature){
break
}
if(token.IsKeyword()){
tokens.PushBack(token)
lineNumber++
}
if(token.Kind == gherkin.EOF) {
break
}
}
}
return nil
}