-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
pickle_filter.ts
146 lines (130 loc) · 3.95 KB
/
pickle_filter.ts
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
import path from 'path'
import parse from '@cucumber/tag-expressions'
import { getGherkinScenarioLocationMap } from './formatter/helpers/gherkin_document_parser'
import { doesHaveValue, doesNotHaveValue } from './value_checker'
import * as messages from '@cucumber/messages'
import IGherkinDocument = messages.GherkinDocument
import IPickle = messages.Pickle
const FEATURE_LINENUM_REGEXP = /^(.*?)((?::[\d]+)+)?$/
export interface IPickleFilterOptions {
cwd: string
featurePaths?: string[]
names?: string[]
tagExpression?: string
}
export interface IMatchesAnyLineRequest {
gherkinDocument: messages.GherkinDocument
pickle: messages.Pickle
}
export default class PickleFilter {
private readonly lineFilter: PickleLineFilter
private readonly nameFilter: PickleNameFilter
private readonly tagFilter: PickleTagFilter
constructor({
cwd,
featurePaths,
names,
tagExpression,
}: IPickleFilterOptions) {
this.lineFilter = new PickleLineFilter(cwd, featurePaths)
this.nameFilter = new PickleNameFilter(names)
this.tagFilter = new PickleTagFilter(tagExpression)
}
matches({
gherkinDocument,
pickle,
}: {
gherkinDocument: IGherkinDocument
pickle: IPickle
}): boolean {
return (
this.lineFilter.matchesAnyLine({ gherkinDocument, pickle }) &&
this.nameFilter.matchesAnyName(pickle) &&
this.tagFilter.matchesAllTagExpressions(pickle)
)
}
}
export class PickleLineFilter {
private readonly featureUriToLinesMapping: Record<string, number[]>
constructor(cwd: string, featurePaths: string[] = []) {
this.featureUriToLinesMapping = this.getFeatureUriToLinesMapping({
cwd,
featurePaths,
})
}
getFeatureUriToLinesMapping({
cwd,
featurePaths,
}: {
cwd: string
featurePaths: string[]
}): Record<string, number[]> {
const mapping: Record<string, number[]> = {}
featurePaths.forEach((featurePath) => {
const match = FEATURE_LINENUM_REGEXP.exec(featurePath)
if (doesHaveValue(match)) {
let uri = match[1]
if (path.isAbsolute(uri)) {
uri = path.relative(cwd, uri)
} else {
uri = path.normalize(uri)
}
const linesExpression = match[2]
if (doesHaveValue(linesExpression)) {
if (doesNotHaveValue(mapping[uri])) {
mapping[uri] = []
}
linesExpression
.slice(1)
.split(':')
.forEach((line) => {
mapping[uri].push(parseInt(line))
})
}
}
})
return mapping
}
matchesAnyLine({ gherkinDocument, pickle }: IMatchesAnyLineRequest): boolean {
const uri = path.normalize(pickle.uri)
const linesToMatch = this.featureUriToLinesMapping[uri]
if (doesHaveValue(linesToMatch)) {
const gherkinScenarioLocationMap =
getGherkinScenarioLocationMap(gherkinDocument)
const pickleLines = new Set(
pickle.astNodeIds.map(
(sourceId) => gherkinScenarioLocationMap[sourceId].line
)
)
const linesIntersection = linesToMatch.filter((x) => pickleLines.has(x))
return linesIntersection.length > 0
}
return true
}
}
export class PickleNameFilter {
private readonly names: string[]
constructor(names: string[] = []) {
this.names = names
}
matchesAnyName(pickle: messages.Pickle): boolean {
if (this.names.length === 0) {
return true
}
return this.names.some((name) => pickle.name.match(name))
}
}
export class PickleTagFilter {
private readonly tagExpressionNode: any // cucumber-tag-expressions does not export interface
constructor(tagExpression: string) {
if (doesHaveValue(tagExpression) && tagExpression !== '') {
this.tagExpressionNode = parse(tagExpression)
}
}
matchesAllTagExpressions(pickle: messages.Pickle): boolean {
if (doesNotHaveValue(this.tagExpressionNode)) {
return true
}
return this.tagExpressionNode.evaluate(pickle.tags.map((x) => x.name))
}
}