-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
index.ts
298 lines (276 loc) · 7.39 KB
/
index.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
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
import {
BaseOpts,
env,
Extra,
parseTestArgs,
PromiseWithSubtest,
TapPlugin,
TestArgs,
TestBase,
TestBaseOpts,
} from '@tapjs/core'
import type { Test, TestOpts } from '@tapjs/test'
const reRegExpLiteral = /^\/(.*)\/([a-z]*)$/
/**
* Options added by this plugin
*
* @group Test Options
*/
export interface FilterOptions extends TestBaseOpts {
/**
* Set to true to run in contexts where `runOnly` is set
*/
only?: boolean
/**
* Only run subtests marked with { only: true }
*/
runOnly?: boolean
/**
* only run subtests whose name matches the supplied pattern.
* Provide an array of strings/regexps to match down the test
* heirarchy.
*/
grep?: string | RegExp | (string | RegExp)[]
/**
* only run tests that DON'T match the pattern provided in `grep`
*/
grepInvert?: boolean
}
/**
* Class that provides the {@link @tapjs/filter!Filter#only} method, and
* extends the {@link @tapjs/core!test-base.TestBase#shouldSkipChild} method to
* respect the grep option
*/
export class Filter {
#t: TestBase
#grep: (string | RegExp)[] = []
#grepInvert: boolean
#runOnly: boolean
#filterQuietly: boolean
get runOnly() {
return this.#runOnly
}
set runOnly(o: boolean) {
this.#runOnly = o
}
constructor(t: TestBase, opts: FilterOptions) {
this.#t = t
const eq = env?.TAP_FILTER_QUIETLY
this.#filterQuietly =
eq === '1' || (!!t.options.failSkip && eq !== '0')
// don't filter test files when we're the cli test runner
const { grep, grepInvert, runOnly } =
opts.context === Symbol.for('tap.isRunner') ?
({
grep: [],
grepInvert: false,
runOnly: false,
} as FilterOptions)
: opts
if (grep !== undefined) {
this.#grep = !Array.isArray(grep) ? [grep] : grep
} else if (env.TAP_GREP !== undefined) {
this.#grep = env.TAP_GREP.split('\n').map(g => {
const p = g.match(reRegExpLiteral)
g = p && p[1] ? p[1] : g
const flags = p ? p[2] : ''
return new RegExp(g, flags)
})
}
if (grepInvert !== undefined) {
this.#grepInvert = !!grepInvert
} else {
this.#grepInvert = env.TAP_INVERT === '1'
}
if (runOnly !== undefined) {
this.#runOnly = !!runOnly
} else {
this.#runOnly = env.TAP_ONLY === '1'
}
if (this.#grep?.length) t.options.grep = this.#grep
const { shouldSkipChild } = t
t.shouldSkipChild = extra =>
this.#shouldSkipChild(extra, shouldSkipChild)
}
#shouldSkipChild(
extra: TestBaseOpts | BaseOpts | TestOpts,
shouldSkipChild: (
extra: TestBaseOpts | BaseOpts | TestOpts,
) => boolean,
) {
const opts = extra as FilterOptions & Extra & TestBaseOpts
const [pattern, ...rest] = this.#grep
if (pattern !== undefined) {
/* c8 ignore start */
const name = opts.name || ''
/* c8 ignore stop */
const m =
typeof pattern === 'string' ?
name.includes(pattern)
: pattern.test(name)
const match = this.#grepInvert ? !m : m
if (!match) {
if (!this.#filterQuietly) {
opts.skip = `filter${
this.#grepInvert ? ' out' : ''
}: ${pattern}`
}
shouldSkipChild(opts)
return true
} else {
opts.grep = rest
}
} else if (this.#grep) {
// just set to an empty array so it doesn't try to load from the
// env once we've exhausted the pattern parts.
opts.grep = []
}
if (this.#runOnly && !opts.only) {
if (!this.#filterQuietly) {
opts.skip = 'filter: only'
}
shouldSkipChild(opts)
return true
}
if (opts.only && !this.#runOnly) {
this.#t.comment(
'%j has `only` set but all tests run',
extra.name,
)
}
if (typeof opts.runOnly === 'undefined') {
opts.runOnly = this.#runOnly
}
return shouldSkipChild(opts)
}
/**
* Run a child test that will run when the `--only` config is set,
* or `{ runOnly: true }` is set in the parent test options.
*
* @group Subtest Methods
*/
only(
name: string,
extra: TestOpts,
cb: (t: Test) => any,
): PromiseWithSubtest<Test>
only(name: string, cb: (t: Test) => any): PromiseWithSubtest<Test>
only(
extra: TestOpts,
cb: (t: Test) => any,
): PromiseWithSubtest<Test>
only(cb: (t: Test) => any): PromiseWithSubtest<Test>
only(...args: TestArgs<Test>): PromiseWithSubtest<Test> {
const extra = parseTestArgs(...args)
/* c8 ignore start */
const name = extra.name || ''
/* c8 ignore stop */
return this.#t.t.test(name, { ...extra, only: true }, extra.cb)
}
}
/**
* plugin method that instantiates a {@link @tapjs/filter!Filter} object
*/
export const plugin: TapPlugin<Filter, FilterOptions> = (t, opts) =>
new Filter(t, opts)
/**
* Configuration options added by this plugin
*
* @group Configuration
*/
export const config = {
/**
* flag
*
* Only run tests with `{only: true}` option, or created with the
* `t.only(...)` function.
*
* @group Configuration
*/
only: {
type: 'boolean',
short: 'O',
description: `Only run tests with \`{only: true}\` option, or created with
the \`t.only(...)\` function.`,
nodeArgs: (v: boolean) => (v ? ['--test-only'] : []),
},
/**
* string option
*
* Only run subtests tests matching the specified pattern.
*
* Patterns are matched against top-level subtests in each
* file. To filter tests at subsequent levels, specify this
* option multiple times.
*
* To specify regular expression flags, format pattern like a
* JavaScript RegExp literal. For example: `/xyz/i` for
* case-insensitive matching.
*
* @group Configuration
*/
grep: {
type: 'string',
multiple: true,
hint: 'pattern',
short: 'g',
description: `Only run subtests tests matching the specified pattern.
Patterns are matched against top-level subtests in each
file. To filter tests at subsequent levels, specify this
option multiple times.
To specify regular expression flags, format pattern like a
JavaScript RegExp literal. For example: \`/xyz/i\` for
case-insensitive matching.`,
nodeArgs: (value: string[]) =>
value.map(g => `--test-name-pattern=${g}`),
},
/**
* flag
*
* Invert the matches to --grep patterns. (Like grep -v)
*
* @group Configuration
*/
invert: {
type: 'boolean',
short: 'i',
description:
'Invert the matches to --grep patterns. (Like grep -v)',
},
/**
* flag
*
* Do not invert the matches to --grep patterns. (default)
*
* @group Configuration
*/
'no-invert': {
type: 'boolean',
short: 'I',
description:
'Do not invert the matches to --grep patterns. (default)',
},
/**
* flag
*
* Do not apply a skip message to tests filtered using `--grep`
* and `--only`.
*
* Enabled by default if `--fail-skip` is set.
*/
'filter-quietly': {
type: 'boolean',
description: `
Do not apply a skip message to tests filtered using \`--grep\`
and \`--only\`.
Defaults to true if \`--fail-skip\` is set.
`,
},
'no-filter-quietly': {
type: 'boolean',
description: `
Always set a skip message on filtered tests, even if \`--fail-skip\`
is enabled.
`,
},
}