/
transformer.go
328 lines (299 loc) · 9.5 KB
/
transformer.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
package ghokin
import (
"bytes"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/cucumber/gherkin/go/v28"
)
// CmdErr is thrown when an error occurred when calling
// a command on an input, both stdout and stderr are stored
type CmdErr struct {
output string
}
// Error outputs both stdout and stderr
func (e CmdErr) Error() string {
return e.output
}
func extractSections(content []byte) (*section, error) {
section := §ion{}
builder := &tokenGenerator{section: section}
matcher := gherkin.NewMatcher(gherkin.DialectsBuiltin())
scanner := gherkin.NewScanner(bytes.NewBuffer(content))
parser := gherkin.NewParser(builder)
parser.StopAtFirstError(true)
return section, parser.Parse(scanner, matcher)
}
func transform(section *section, indent int, aliases aliases) ([]byte, error) {
paddings := map[gherkin.TokenType]int{
gherkin.TokenTypeFeatureLine: 0,
gherkin.TokenTypeBackgroundLine: indent,
gherkin.TokenTypeScenarioLine: indent,
gherkin.TokenTypeDocStringSeparator: 3 * indent,
gherkin.TokenTypeStepLine: 2 * indent,
gherkin.TokenTypeExamplesLine: 2 * indent,
gherkin.TokenTypeOther: 3 * indent,
gherkin.TokenTypeTableRow: 3 * indent,
}
formats := map[gherkin.TokenType](func(values []*gherkin.Token) []string){
gherkin.TokenTypeFeatureLine: extractKeywordAndTextSeparatedWithAColon,
gherkin.TokenTypeBackgroundLine: extractKeywordAndTextSeparatedWithAColon,
gherkin.TokenTypeScenarioLine: extractKeywordAndTextSeparatedWithAColon,
gherkin.TokenTypeExamplesLine: extractKeywordAndTextSeparatedWithAColon,
gherkin.TokenTypeComment: extractTokensText,
gherkin.TokenTypeTagLine: extractTokensItemsText,
gherkin.TokenTypeDocStringSeparator: extractKeyword,
gherkin.TokenTypeRuleLine: extractKeywordAndTextSeparatedWithAColon,
gherkin.TokenTypeOther: extractTokensText,
gherkin.TokenTypeStepLine: extractTokensKeywordAndText,
gherkin.TokenTypeTableRow: extractTableRowsAndComments,
gherkin.TokenTypeEmpty: extractTokensItemsText,
gherkin.TokenTypeLanguage: extractLanguage,
}
var cmd *exec.Cmd
document := []string{}
optionalRulePadding := 0
accumulator := []*gherkin.Token{}
for sec := section; sec != nil; sec = sec.nex {
values := sec.values
if len(accumulator) > 0 &&
sec.kind == gherkin.TokenTypeTableRow &&
(sec.nex != nil && sec.nex.kind != gherkin.TokenTypeComment) || sec.nex == nil {
values = append(accumulator, sec.values...)
accumulator = []*gherkin.Token{}
}
if sec.kind == gherkin.TokenTypeTableRow &&
sec.nex != nil &&
sec.nex.kind == gherkin.TokenTypeComment &&
sec.nex.nex != nil &&
sec.nex.nex.kind == gherkin.TokenTypeTableRow ||
len(accumulator) > 0 && sec.kind == gherkin.TokenTypeComment ||
len(accumulator) > 0 && sec.kind == gherkin.TokenTypeTableRow {
accumulator = append(accumulator, sec.values...)
continue
}
if sec.kind == 0 {
continue
}
padding := paddings[sec.kind] + optionalRulePadding
lines := formats[sec.kind](values)
switch sec.kind {
case gherkin.TokenTypeRuleLine:
optionalRulePadding = indent
padding = indent
case gherkin.TokenTypeComment, gherkin.TokenTypeLanguage:
cmd = extractCommand(sec.values, aliases)
padding = getTagOrCommentPadding(paddings, indent, sec)
lines = trimLinesSpace(lines)
case gherkin.TokenTypeTagLine:
padding = getTagOrCommentPadding(paddings, indent, sec)
case gherkin.TokenTypeDocStringSeparator:
lines = extractKeyword(sec.values)
case gherkin.TokenTypeOther:
if isDescriptionFeature(sec) {
lines = trimLinesSpace(lines)
padding = indent
}
}
computed, lines, err := computeCommand(cmd, lines, sec)
if err != nil {
return []byte{}, err
}
if computed {
cmd = nil
}
document = append(document, trimExtraTrailingSpace(indentStrings(padding, lines))...)
}
return []byte(strings.Join(document, "\n") + "\n"), nil
}
func getTagOrCommentPadding(paddings map[gherkin.TokenType]int, indent int, sec *section) int {
var kind gherkin.TokenType
excluded := []gherkin.TokenType{gherkin.TokenTypeTagLine, gherkin.TokenTypeComment}
if sec.next(excluded) != nil {
if s := sec.next(excluded); s != nil {
kind = s.kind
}
}
if kind == 0 && sec.previous(excluded) != nil {
if s := sec.previous(excluded); s != nil {
kind = s.kind
}
}
// indent the last comment line at the same level than scenario and background
if sec.next([]gherkin.TokenType{gherkin.TokenTypeEmpty}) == nil {
return indent
}
return paddings[kind]
}
func computeCommand(cmd *exec.Cmd, lines []string, sec *section) (bool, []string, error) {
if sec.kind == gherkin.TokenTypeComment || sec.kind == gherkin.TokenTypeDocStringSeparator || cmd == nil {
return false, lines, nil
}
l, err := runCommand(cmd, lines)
if err != nil {
return true, []string{}, err
}
return true, l, err
}
func isDescriptionFeature(sec *section) bool {
excluded := []gherkin.TokenType{gherkin.TokenTypeEmpty}
if sec.previous(excluded) != nil {
if s := sec.previous(excluded); s != nil && s.kind == gherkin.TokenTypeFeatureLine {
return true
}
}
return false
}
func trimLinesSpace(lines []string) []string {
content := []string{}
for _, line := range lines {
content = append(content, strings.TrimSpace(line))
}
return content
}
func trimExtraTrailingSpace(lines []string) []string {
content := []string{}
for _, line := range lines {
content = append(content, strings.TrimRight(line, " \t"))
}
return content
}
func indentStrings(padding int, lines []string) []string {
content := []string{}
for _, line := range lines {
content = append(content, strings.Repeat(" ", padding)+line)
}
return content
}
func extractLanguage(tokens []*gherkin.Token) []string {
return []string{fmt.Sprintf("# language: %s", tokens[0].Text)}
}
func extractTokensText(tokens []*gherkin.Token) []string {
content := []string{}
for _, token := range tokens {
content = append(content, token.Text)
}
return content
}
func extractTokensItemsText(tokens []*gherkin.Token) []string {
content := []string{}
for _, token := range tokens {
t := []string{}
for _, item := range token.Items {
t = append(t, item.Text)
}
content = append(content, strings.Join(t, " "))
}
return content
}
func extractTokensKeywordAndText(tokens []*gherkin.Token) []string {
content := []string{}
for _, token := range tokens {
content = append(content, fmt.Sprintf("%s%s", token.Keyword, token.Text))
}
return content
}
func extractKeywordAndTextSeparatedWithAColon(tokens []*gherkin.Token) []string {
content := []string{}
for _, token := range tokens {
content = append(content, fmt.Sprintf("%s: %s", token.Keyword, token.Text))
}
return content
}
func extractKeyword(tokens []*gherkin.Token) []string {
content := []string{}
for _, t := range tokens {
content = append(content, t.Keyword)
}
return content
}
func extractTableRowsAndComments(tokens []*gherkin.Token) []string {
type tableElement struct {
content []string
kind gherkin.TokenType
}
rows := [][]string{}
tableElements := []tableElement{}
for _, token := range tokens {
element := tableElement{}
if token.Type == gherkin.TokenTypeComment {
element.kind = token.Type
element.content = []string{token.Text}
} else {
row := []string{}
for _, data := range token.Items {
// A remaining pipe means it was escaped before to not be messed with pipe column delimiter
// so here we introduce the escaping sequence back
text := data.Text
text = strings.ReplaceAll(text, "\\", "\\\\")
text = strings.ReplaceAll(text, "\n", "\\n")
text = strings.ReplaceAll(text, "|", "\\|")
row = append(row, text)
}
element.kind = token.Type
element.content = row
rows = append(rows, row)
}
tableElements = append(tableElements, element)
}
var tableRows []string
lengths := calculateLonguestLineLengthPerColumn(rows)
for _, tableElement := range tableElements {
inputs := []interface{}{}
fmtDirective := ""
if tableElement.kind == gherkin.TokenTypeComment {
inputs = append(inputs, trimLinesSpace(tableElement.content)[0])
fmtDirective = "%s"
} else {
for i, str := range tableElement.content {
inputs = append(inputs, str)
fmtDirective += "| %-" + strconv.Itoa(lengths[i]) + "s "
}
fmtDirective += "|"
}
tableRows = append(tableRows, fmt.Sprintf(fmtDirective, inputs...))
}
return tableRows
}
func calculateLonguestLineLengthPerColumn(rows [][]string) []int {
lengths := []int{}
for i, row := range rows {
for j, str := range row {
switch true {
case i == 0:
lengths = append(lengths, utf8.RuneCountInString(str))
case i != 0 && len(lengths) > j && lengths[j] < utf8.RuneCountInString(str):
lengths[j] = utf8.RuneCountInString(str)
default:
lengths = append(lengths, 0)
}
}
}
return lengths
}
func extractCommand(tokens []*gherkin.Token, aliases map[string]string) *exec.Cmd {
re := regexp.MustCompile(`(\@[a-zA-Z0-9]+)`)
matches := re.FindStringSubmatch(tokens[0].Text)
if len(matches) == 0 {
return nil
}
/* #nosec */
if cmd, ok := aliases[matches[0][1:]]; ok {
return exec.Command("sh", "-c", cmd)
}
return nil
}
func runCommand(cmd *exec.Cmd, lines []string) ([]string, error) {
if len(lines) == 0 {
return lines, nil
}
cmd.Stdin = strings.NewReader(strings.Join(lines, "\n"))
o, err := cmd.CombinedOutput()
if err != nil {
return []string{}, CmdErr{strings.TrimRight(string(o), "\n")}
}
return strings.Split(strings.TrimRight(string(o), "\n"), "\n"), nil
}