@plugjs/cov8 0.7.12 → 0.7.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SKILL.md +245 -0
- package/dist/analysis.d.ts +43 -36
- package/dist/analysis.d.ts.map +1 -1
- package/dist/analysis.js +110 -65
- package/dist/analysis.js.map +1 -1
- package/dist/cli.js +26 -21
- package/dist/cli.js.map +1 -1
- package/dist/coverage.d.ts.map +1 -1
- package/dist/coverage.js +37 -20
- package/dist/coverage.js.map +1 -1
- package/dist/index.d.ts +21 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/report.d.ts +26 -24
- package/dist/report.d.ts.map +1 -1
- package/dist/report.js +79 -36
- package/dist/report.js.map +1 -1
- package/package.json +6 -5
- package/src/analysis.ts +146 -99
- package/src/cli.ts +26 -21
- package/src/coverage.ts +44 -22
- package/src/index.ts +21 -21
- package/src/report.ts +103 -55
package/src/analysis.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
2
2
|
|
|
3
3
|
import { assert } from '@plugjs/plug/asserts'
|
|
4
|
-
import { readFile } from '@plugjs/plug/fs'
|
|
4
|
+
import { readFile, realpath } from '@plugjs/plug/fs'
|
|
5
5
|
import { $gry, $p } from '@plugjs/plug/logging'
|
|
6
6
|
import { SourceMapConsumer } from 'source-map'
|
|
7
7
|
|
|
@@ -13,21 +13,21 @@ import type { RawSourceMap } from 'source-map'
|
|
|
13
13
|
* V8 COVERAGE TYPES *
|
|
14
14
|
* ========================================================================== */
|
|
15
15
|
|
|
16
|
-
/** Coverage range */
|
|
16
|
+
/** Coverage range. */
|
|
17
17
|
export interface V8CoveredRange {
|
|
18
|
-
/** The offset
|
|
18
|
+
/** The inclusive start offset of the range, in UTF-16 code units. */
|
|
19
19
|
startOffset: number,
|
|
20
|
-
/** The
|
|
20
|
+
/** The exclusive end offset of the range, in UTF-16 code units. */
|
|
21
21
|
endOffset: number,
|
|
22
|
-
/** The
|
|
22
|
+
/** The execution count for this range. */
|
|
23
23
|
count: number,
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
/**
|
|
26
|
+
/** Per-function coverage reported by V8. */
|
|
27
27
|
export interface V8CoveredFunction {
|
|
28
|
-
/** The name of the function being covered */
|
|
28
|
+
/** The name of the function being covered. */
|
|
29
29
|
functionName: string,
|
|
30
|
-
/**
|
|
30
|
+
/** Whether the ranges include block-level coverage. */
|
|
31
31
|
isBlockCoverage: boolean,
|
|
32
32
|
/**
|
|
33
33
|
* The ranges covered.
|
|
@@ -37,37 +37,33 @@ export interface V8CoveredFunction {
|
|
|
37
37
|
ranges: V8CoveredRange[],
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
/** Coverage
|
|
40
|
+
/** Coverage for a script as recorded by Node.js. */
|
|
41
41
|
export interface V8CoverageResult {
|
|
42
|
-
/** The script ID, uniquely identifying the script within the Node process */
|
|
42
|
+
/** The script ID, uniquely identifying the script within the Node process. */
|
|
43
43
|
scriptId: string,
|
|
44
|
-
/** The URL of the script (might not be unique, if the script is loaded multiple times) */
|
|
44
|
+
/** The URL of the script (might not be unique, if the script is loaded multiple times). */
|
|
45
45
|
url: string,
|
|
46
|
-
/** Per-function report of coverage */
|
|
46
|
+
/** Per-function report of coverage. */
|
|
47
47
|
functions: V8CoveredFunction[]
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
/** Cached source map for a coverage result */
|
|
50
|
+
/** Cached source map for a coverage result. */
|
|
51
51
|
export interface V8SourceMapCache {
|
|
52
|
-
/**
|
|
52
|
+
/** Line lengths in the generated code, as cached by Node.js. */
|
|
53
53
|
lineLengths: number[],
|
|
54
|
-
/** The source map associated with the transpiled code */
|
|
54
|
+
/** The source map associated with the transpiled code. */
|
|
55
55
|
data: RawSourceMap | null,
|
|
56
|
-
/** The
|
|
56
|
+
/** The source-map URL, or null for an inline source map. */
|
|
57
57
|
url: string | null,
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
/**
|
|
60
|
+
/** Coverage data emitted by Node.js and parsed from JSON. */
|
|
61
61
|
export interface V8CoverageData {
|
|
62
|
-
/**
|
|
63
|
-
* Coverage results, per script.
|
|
64
|
-
*
|
|
65
|
-
* The first element in the array describes the coverage for the whole script.
|
|
66
|
-
*/
|
|
62
|
+
/** Coverage results, with one entry per recorded script. */
|
|
67
63
|
'result': V8CoverageResult[],
|
|
68
|
-
/** Timestamp when coverage was taken */
|
|
64
|
+
/** Timestamp when coverage was taken. */
|
|
69
65
|
'timestamp'?: number,
|
|
70
|
-
/** Source
|
|
66
|
+
/** Source-map cache entries keyed by script URL. */
|
|
71
67
|
'source-map-cache'?: Record<string, V8SourceMapCache>
|
|
72
68
|
}
|
|
73
69
|
|
|
@@ -76,41 +72,49 @@ export interface V8CoverageData {
|
|
|
76
72
|
* ========================================================================== */
|
|
77
73
|
|
|
78
74
|
/**
|
|
79
|
-
*
|
|
75
|
+
* Bias for mapping original source positions to generated code.
|
|
80
76
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
77
|
+
* The Coverage plug defaults to `least_upper_bound`. Passing `undefined`
|
|
78
|
+
* directly to createAnalyser leaves the source-map library's default in effect.
|
|
79
|
+
* Use `none` to require exact original positions and sum the execution counts
|
|
80
|
+
* of all generated locations mapped to each position.
|
|
84
81
|
*/
|
|
85
82
|
export type SourceMapBias = 'greatest_lower_bound' | 'least_upper_bound' | 'none' | undefined
|
|
86
83
|
|
|
87
|
-
/** Interface providing coverage data */
|
|
84
|
+
/** Interface providing coverage data. */
|
|
88
85
|
export interface CoverageAnalyser {
|
|
89
|
-
/** Return the
|
|
86
|
+
/** Return the execution count for a source URL, one-based line, and zero-based column. */
|
|
90
87
|
coverage(source: string, line: number, column: number): number
|
|
91
|
-
/**
|
|
88
|
+
/**
|
|
89
|
+
* Whether a source URL has an exact mapping at the given one-based line and
|
|
90
|
+
* zero-based column, regardless of execution count. Direct script analysers
|
|
91
|
+
* treat every position in their source as mapped.
|
|
92
|
+
*/
|
|
93
|
+
hasMapping(source: string, line: number, column: number): boolean
|
|
94
|
+
/** Release resources held by this analyser. */
|
|
92
95
|
destroy(): void
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
/* ========================================================================== */
|
|
96
99
|
|
|
97
|
-
/**
|
|
100
|
+
/** Base class for coverage analysers with asynchronous initialization. */
|
|
98
101
|
abstract class CoverageAnalyserImpl implements CoverageAnalyser {
|
|
99
102
|
constructor(protected readonly _log: Logger) {}
|
|
100
103
|
|
|
101
104
|
abstract init(): Promise<this>
|
|
102
105
|
abstract destroy(): void
|
|
103
106
|
abstract coverage(source: string, line: number, column: number): number
|
|
107
|
+
abstract hasMapping(source: string, line: number, column: number): boolean
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
|
|
107
111
|
/* ========================================================================== */
|
|
108
112
|
|
|
109
|
-
/** Return coverage data from a V8 {@link V8CoverageResult} structure */
|
|
113
|
+
/** Return coverage data from a V8 {@link V8CoverageResult} structure. */
|
|
110
114
|
class CoverageResultAnalyser extends CoverageAnalyserImpl {
|
|
111
|
-
/**
|
|
115
|
+
/** Execution counts indexed by UTF-16 offset in the recorded script. */
|
|
112
116
|
protected readonly _coverage: readonly (number | undefined)[]
|
|
113
|
-
/**
|
|
117
|
+
/** Line lengths used to convert line and column positions to script offsets. */
|
|
114
118
|
protected _lineLengths?: readonly number[]
|
|
115
119
|
|
|
116
120
|
constructor(
|
|
@@ -143,7 +147,11 @@ class CoverageResultAnalyser extends CoverageAnalyserImpl {
|
|
|
143
147
|
// Nothing to do
|
|
144
148
|
}
|
|
145
149
|
|
|
146
|
-
|
|
150
|
+
hasMapping(source: string, _line: number, _column: number): boolean {
|
|
151
|
+
return source === this._result.url
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Return the execution count for a source URL, one-based line, and zero-based column. */
|
|
147
155
|
coverage(source: string, line: number, column: number): number {
|
|
148
156
|
assert(this._lineLengths, 'Analyser not initialized')
|
|
149
157
|
assert(source === this._result.url, `Wrong source ${source} (should be ${this._result.url})`)
|
|
@@ -161,9 +169,9 @@ class CoverageResultAnalyser extends CoverageAnalyserImpl {
|
|
|
161
169
|
|
|
162
170
|
/* ========================================================================== */
|
|
163
171
|
|
|
164
|
-
/**
|
|
172
|
+
/** Map original source positions to coverage in a generated V8 script. */
|
|
165
173
|
class CoverageSitemapAnalyser extends CoverageResultAnalyser {
|
|
166
|
-
private _preciseMappings = new Map<string, { line: number, column: number }>()
|
|
174
|
+
private _preciseMappings = new Map<string, { line: number, column: number }[]>()
|
|
167
175
|
private _sourceMap?: SourceMapConsumer
|
|
168
176
|
|
|
169
177
|
constructor(
|
|
@@ -185,13 +193,13 @@ class CoverageSitemapAnalyser extends CoverageResultAnalyser {
|
|
|
185
193
|
assert(sourceMap, 'Missing source map data from cache')
|
|
186
194
|
this._sourceMap = await new SourceMapConsumer(sourceMap)
|
|
187
195
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
196
|
+
this._sourceMap.eachMapping((m) => {
|
|
197
|
+
const location = { line: m.generatedLine, column: m.generatedColumn }
|
|
198
|
+
const key = this._key(m.source, m.originalLine, m.originalColumn)
|
|
199
|
+
const locations = this._preciseMappings.get(key) || []
|
|
200
|
+
locations.push(location)
|
|
201
|
+
this._preciseMappings.set(key, locations)
|
|
202
|
+
})
|
|
195
203
|
|
|
196
204
|
return this
|
|
197
205
|
}
|
|
@@ -200,17 +208,25 @@ class CoverageSitemapAnalyser extends CoverageResultAnalyser {
|
|
|
200
208
|
this._sourceMap?.destroy()
|
|
201
209
|
}
|
|
202
210
|
|
|
211
|
+
override hasMapping(source: string, line: number, column: number): boolean {
|
|
212
|
+
return this._preciseMappings.has(this._key(source, line, column))
|
|
213
|
+
}
|
|
214
|
+
|
|
203
215
|
override coverage(source: string, line: number, column: number): number {
|
|
204
216
|
assert(this._sourceMap, 'Analyser not initialized')
|
|
205
217
|
|
|
206
218
|
if (this._sourceMapBias === 'none') {
|
|
207
219
|
const key = this._key(source, line, column)
|
|
208
|
-
const
|
|
209
|
-
if (!
|
|
220
|
+
const locations = this._preciseMappings.get(key)
|
|
221
|
+
if (! locations) {
|
|
210
222
|
this._log.debug(`No precise mapping for ${source}:${line}:${column}`)
|
|
211
223
|
return 0
|
|
212
224
|
} else {
|
|
213
|
-
|
|
225
|
+
let coverage = 0
|
|
226
|
+
for (const location of locations) {
|
|
227
|
+
coverage += super.coverage(this._result.url, location.line, location.column)
|
|
228
|
+
}
|
|
229
|
+
return coverage
|
|
214
230
|
}
|
|
215
231
|
}
|
|
216
232
|
|
|
@@ -249,17 +265,15 @@ function mapBias(bias?: 'greatest_lower_bound' | 'least_upper_bound'): number |
|
|
|
249
265
|
return undefined
|
|
250
266
|
}
|
|
251
267
|
|
|
252
|
-
/** Combine (add) all coverage data from all analysers */
|
|
268
|
+
/** Combine (add) all coverage data from all analysers. */
|
|
253
269
|
function combineCoverage(
|
|
254
|
-
analysers: Set<CoverageAnalyser
|
|
270
|
+
analysers: Set<CoverageAnalyser>,
|
|
255
271
|
source: string,
|
|
256
272
|
line: number,
|
|
257
273
|
column: number,
|
|
258
274
|
): number {
|
|
259
275
|
let coverage = 0
|
|
260
276
|
|
|
261
|
-
if (! analysers) return coverage
|
|
262
|
-
|
|
263
277
|
for (const analyser of analysers) {
|
|
264
278
|
coverage += analyser.coverage(source, line, column)
|
|
265
279
|
}
|
|
@@ -269,9 +283,10 @@ function combineCoverage(
|
|
|
269
283
|
|
|
270
284
|
/* ========================================================================== */
|
|
271
285
|
|
|
272
|
-
/** Associate
|
|
286
|
+
/** Associate requested source URLs with analysers and their recorded source URLs. */
|
|
273
287
|
export class SourcesCoverageAnalyser extends CoverageAnalyserImpl {
|
|
274
|
-
private readonly _mappings = new Map<string,
|
|
288
|
+
private readonly _mappings = new Map<string, Map<CoverageAnalyserImpl, string>>()
|
|
289
|
+
private readonly _analysers = new Set<CoverageAnalyserImpl>()
|
|
275
290
|
|
|
276
291
|
constructor(log: Logger, private readonly _filename: AbsolutePath) {
|
|
277
292
|
super(log)
|
|
@@ -281,37 +296,40 @@ export class SourcesCoverageAnalyser extends CoverageAnalyserImpl {
|
|
|
281
296
|
return this._mappings.size > 0
|
|
282
297
|
}
|
|
283
298
|
|
|
284
|
-
add(source: string, analyser: CoverageAnalyserImpl): void {
|
|
285
|
-
const analysers = this._mappings.get(source) || new
|
|
286
|
-
analysers.
|
|
299
|
+
add(source: string, analyser: CoverageAnalyserImpl, mappedSource = source): void {
|
|
300
|
+
const analysers = this._mappings.get(source) || new Map()
|
|
301
|
+
analysers.set(analyser, mappedSource)
|
|
287
302
|
this._mappings.set(source, analysers)
|
|
303
|
+
this._analysers.add(analyser)
|
|
288
304
|
}
|
|
289
305
|
|
|
290
306
|
async init(): Promise<this> {
|
|
291
307
|
this._log.debug('SourcesCoverageAnalyser', $p(this._filename), $gry(`(${this._mappings.size} mappings)`))
|
|
292
|
-
for (const
|
|
293
|
-
for (const analyser of analysers) {
|
|
294
|
-
await analyser.init()
|
|
295
|
-
}
|
|
296
|
-
}
|
|
308
|
+
for (const analyser of this._analysers) await analyser.init()
|
|
297
309
|
return this
|
|
298
310
|
}
|
|
299
311
|
|
|
300
312
|
destroy(): void {
|
|
301
|
-
for (const
|
|
302
|
-
for (const analyser of analysers) {
|
|
303
|
-
analyser.destroy()
|
|
304
|
-
}
|
|
305
|
-
}
|
|
313
|
+
for (const analyser of this._analysers) analyser.destroy()
|
|
306
314
|
}
|
|
307
315
|
|
|
308
316
|
coverage(source: string, line: number, column: number): number {
|
|
309
|
-
|
|
310
|
-
|
|
317
|
+
let coverage = 0
|
|
318
|
+
for (const [ analyser, mappedSource ] of this._mappings.get(source) || []) {
|
|
319
|
+
coverage += analyser.coverage(mappedSource, line, column)
|
|
320
|
+
}
|
|
321
|
+
return coverage
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
hasMapping(source: string, line: number, column: number): boolean {
|
|
325
|
+
for (const [ analyser, mappedSource ] of this._mappings.get(source) || []) {
|
|
326
|
+
if (analyser.hasMapping(mappedSource, line, column)) return true
|
|
327
|
+
}
|
|
328
|
+
return false
|
|
311
329
|
}
|
|
312
330
|
}
|
|
313
331
|
|
|
314
|
-
/** Combine multiple {@link CoverageAnalyser} instances together */
|
|
332
|
+
/** Combine multiple {@link CoverageAnalyser} instances together. */
|
|
315
333
|
export class CombiningCoverageAnalyser extends CoverageAnalyserImpl {
|
|
316
334
|
private readonly _analysers = new Set<CoverageAnalyserImpl>()
|
|
317
335
|
|
|
@@ -337,13 +355,30 @@ export class CombiningCoverageAnalyser extends CoverageAnalyserImpl {
|
|
|
337
355
|
coverage(source: string, line: number, column: number): number {
|
|
338
356
|
return combineCoverage(this._analysers, source, line, column)
|
|
339
357
|
}
|
|
358
|
+
|
|
359
|
+
hasMapping(source: string, line: number, column: number): boolean {
|
|
360
|
+
for (const analyser of this._analysers) {
|
|
361
|
+
if (analyser.hasMapping(source, line, column)) return true
|
|
362
|
+
}
|
|
363
|
+
return false
|
|
364
|
+
}
|
|
340
365
|
}
|
|
341
366
|
|
|
342
367
|
/* ========================================================================== */
|
|
343
368
|
|
|
369
|
+
/** Resolve file aliases without requiring every recorded source to still exist. */
|
|
370
|
+
async function canonicalUrl(url: string): Promise<string> {
|
|
371
|
+
if (! url.startsWith('file:')) return url
|
|
372
|
+
try {
|
|
373
|
+
return pathToFileURL(await realpath(fileURLToPath(url))).toString()
|
|
374
|
+
} catch {
|
|
375
|
+
return url
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
344
379
|
/**
|
|
345
|
-
*
|
|
346
|
-
*
|
|
380
|
+
* Create and initialize an analyser for the requested sources using the supplied
|
|
381
|
+
* V8 coverage files. The caller must destroy the analyser when finished.
|
|
347
382
|
*/
|
|
348
383
|
export async function createAnalyser(
|
|
349
384
|
sourceFiles: AbsolutePath[],
|
|
@@ -352,14 +387,32 @@ export async function createAnalyser(
|
|
|
352
387
|
log: Logger,
|
|
353
388
|
): Promise<CoverageAnalyser> {
|
|
354
389
|
/* Internally V8 coverage uses URLs for everything */
|
|
355
|
-
const
|
|
390
|
+
const sources = new Map<string, Set<string>>()
|
|
391
|
+
const canonicalUrls = new Map<string, string>()
|
|
392
|
+
for (const path of sourceFiles) {
|
|
393
|
+
const url = pathToFileURL(path).toString()
|
|
394
|
+
const canonical = await canonicalUrl(url)
|
|
395
|
+
canonicalUrls.set(url, canonical)
|
|
396
|
+
const aliases = sources.get(canonical) || new Set<string>()
|
|
397
|
+
aliases.add(url)
|
|
398
|
+
sources.set(canonical, aliases)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const matches = async (url: string): Promise<Set<string>> => {
|
|
402
|
+
let canonical = canonicalUrls.get(url)
|
|
403
|
+
if (! canonical) {
|
|
404
|
+
canonical = await canonicalUrl(url)
|
|
405
|
+
canonicalUrls.set(url, canonical)
|
|
406
|
+
}
|
|
407
|
+
return sources.get(canonical) || new Set()
|
|
408
|
+
}
|
|
356
409
|
|
|
357
410
|
/* The coverage analyser combining all coverage files in the directory */
|
|
358
411
|
const analyser = new CombiningCoverageAnalyser(log)
|
|
359
412
|
|
|
360
|
-
/*
|
|
413
|
+
/* Read the coverage files supplied by the caller. */
|
|
361
414
|
for await (const coverageFile of coverageFiles) {
|
|
362
|
-
/*
|
|
415
|
+
/* Collect source mappings for this coverage file. */
|
|
363
416
|
const coverageFileAnalyser = new SourcesCoverageAnalyser(log, coverageFile)
|
|
364
417
|
|
|
365
418
|
/* Parse our coverage file from JSON */
|
|
@@ -374,35 +427,29 @@ export async function createAnalyser(
|
|
|
374
427
|
}
|
|
375
428
|
|
|
376
429
|
/*
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
* between the file on disk, and what's been used by Node.JS.
|
|
381
|
-
*/
|
|
430
|
+
* A script may have a source map, including one that maps its URL back
|
|
431
|
+
* to itself after a loader has transformed the code.
|
|
432
|
+
*/
|
|
382
433
|
const mapping = coverage['source-map-cache']?.[result.url]
|
|
383
|
-
if (mapping) {
|
|
434
|
+
if (mapping?.data) {
|
|
384
435
|
log.debug('Found source mapping for', result.url, mapping.data)
|
|
385
436
|
|
|
386
|
-
/*
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (matches.length) {
|
|
394
|
-
log.debug('Found source mapping matches', matches)
|
|
395
|
-
|
|
396
|
-
const sourceAnalyser = new CoverageSitemapAnalyser(log, result, mapping, sourceMapBias)
|
|
397
|
-
for (const match of matches) coverageFileAnalyser.add(match, sourceAnalyser)
|
|
437
|
+
/* Match source-map entries to requested sources, resolving aliases. */
|
|
438
|
+
let sourceAnalyser: CoverageSitemapAnalyser | undefined
|
|
439
|
+
for (const source of mapping.data.sources) {
|
|
440
|
+
for (const match of await matches(source)) {
|
|
441
|
+
sourceAnalyser ||= new CoverageSitemapAnalyser(log, result, mapping, sourceMapBias)
|
|
442
|
+
coverageFileAnalyser.add(match, sourceAnalyser, source)
|
|
443
|
+
}
|
|
398
444
|
}
|
|
399
445
|
|
|
400
|
-
/*
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
446
|
+
/* Without usable source-map data, match the recorded script directly. */
|
|
447
|
+
} else {
|
|
448
|
+
const aliases = await matches(result.url)
|
|
449
|
+
if (aliases.size) {
|
|
450
|
+
const sourceAnalyser = new CoverageResultAnalyser(log, result)
|
|
451
|
+
for (const alias of aliases) coverageFileAnalyser.add(alias, sourceAnalyser, result.url)
|
|
452
|
+
}
|
|
406
453
|
}
|
|
407
454
|
}
|
|
408
455
|
|
package/src/cli.ts
CHANGED
|
@@ -15,7 +15,7 @@ const $gnd = (s: string): string => $gry($und(s))
|
|
|
15
15
|
const $bnd = (s: string): string => $blu($und(s))
|
|
16
16
|
const $wnd = (s: string): string => $wht($und(s))
|
|
17
17
|
|
|
18
|
-
/** Version resolved from "package.json" */
|
|
18
|
+
/** Version resolved from "package.json". */
|
|
19
19
|
function version(): string {
|
|
20
20
|
try {
|
|
21
21
|
const packageJsonPath = _path.resolve(import.meta.dirname, '..', 'package.json')
|
|
@@ -32,7 +32,7 @@ function version(): string {
|
|
|
32
32
|
* HELP SCREEN *
|
|
33
33
|
* ========================================================================== */
|
|
34
34
|
|
|
35
|
-
/** Show help screen */
|
|
35
|
+
/** Show help screen. */
|
|
36
36
|
function help(): void {
|
|
37
37
|
console.log(`${$blu($und('Usage:'))}
|
|
38
38
|
|
|
@@ -46,35 +46,34 @@ function help(): void {
|
|
|
46
46
|
${$wht('-s --sources')} ${$gnd('glob')} A glob for the sources to be reported upon
|
|
47
47
|
${$wht('-m --minimum')} ${$gnd('num')} The desired minimum coverage level to achieve
|
|
48
48
|
${$wht('-o --optimal')} ${$gnd('num')} The desired optimal coverage level to achieve
|
|
49
|
-
${$wht('-
|
|
49
|
+
${$wht('-l --lines')} Report uncovered lines
|
|
50
|
+
${$wht('-h --help')} Help! You're reading it now!
|
|
50
51
|
${$wht(' --version')} Version! This one: ${version()}!
|
|
51
52
|
|
|
52
53
|
|
|
53
|
-
${$bnd('
|
|
54
|
+
${$bnd('Usage details:')}
|
|
54
55
|
|
|
55
56
|
This utility can process and create reports for ${$und('existing')} Node.js coverage
|
|
56
57
|
data, or execute a command and verify the execution's coverage.
|
|
57
58
|
|
|
58
59
|
When a command is not specified, the ${$wht('--coverage-dir')} option must be
|
|
59
|
-
specified
|
|
60
|
-
coverage files.
|
|
60
|
+
specified and point to a directory containing one or more Node.js JSON
|
|
61
|
+
coverage files. These can be generated by running Node.js with the
|
|
61
62
|
${$wnd('NODE_V8_COVERAGE')} environment variable.
|
|
62
63
|
|
|
63
64
|
When a command is specified, the command will be executed, and coverage
|
|
64
65
|
will be collected automatically in a temporary directory (or in the
|
|
65
66
|
directory specified by ${$wht('--coverage-dir')}).
|
|
66
67
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
directory) and the ${$wht('--sources')} option (can be specified multiple times)
|
|
70
|
-
indicating the globs matching the source files to report upon.
|
|
68
|
+
Use ${$wht('--source-dir')} to select the source directory (defaults to ${$wnd('./src')})
|
|
69
|
+
and ${$wht('--sources')} to specify a glob matching the source files to analyse.
|
|
71
70
|
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
By default, matching includes .js, .cjs, .mjs, .ts, .cts, and .mts files.
|
|
72
|
+
TypeScript declaration files (.d.ts, .d.cts, and .d.mts) are excluded.
|
|
74
73
|
|
|
75
74
|
${$bnd('Environment Variables:')}
|
|
76
75
|
|
|
77
|
-
${$wht('LOG_LEVEL
|
|
76
|
+
${$wht('LOG_LEVEL ')} Logging level; defaults to ${$wnd('notice')}.
|
|
78
77
|
|
|
79
78
|
${$bnd('TypeScript module format:')}
|
|
80
79
|
|
|
@@ -83,17 +82,17 @@ function help(): void {
|
|
|
83
82
|
|
|
84
83
|
To force a specific module format use one of the following flags:
|
|
85
84
|
|
|
86
|
-
${$wht('--force-esm')} Force transpilation of ${$wnd('.ts')} files to
|
|
85
|
+
${$wht('--force-esm')} Force transpilation of ${$wnd('.ts')} files to ECMAScript modules
|
|
87
86
|
${$wht('--force-cjs')} Force transpilation of ${$wnd('.ts')} files to CommonJS modules
|
|
88
87
|
`)
|
|
89
88
|
process.exit(0)
|
|
90
89
|
}
|
|
91
90
|
|
|
92
91
|
/* ========================================================================== *
|
|
93
|
-
*
|
|
92
|
+
* COVERAGE COMMAND *
|
|
94
93
|
* ========================================================================== */
|
|
95
94
|
|
|
96
|
-
/** Parse command line
|
|
95
|
+
/** Parse the command line, optionally run a command, and analyse coverage. */
|
|
97
96
|
main(import.meta.url, async (args): Promise<void> => {
|
|
98
97
|
logging.logOptions.spinner = false
|
|
99
98
|
|
|
@@ -103,6 +102,7 @@ main(import.meta.url, async (args): Promise<void> => {
|
|
|
103
102
|
const globs: string[] = [] // empty globs list
|
|
104
103
|
let minimumCoverage: number | undefined = undefined
|
|
105
104
|
let optimalCoverage: number | undefined = undefined
|
|
105
|
+
let reportUncoveredLines: boolean = false
|
|
106
106
|
let coverageDir: string | undefined
|
|
107
107
|
let reportDir: string | undefined
|
|
108
108
|
let sourceDir: string | undefined
|
|
@@ -122,12 +122,13 @@ main(import.meta.url, async (args): Promise<void> => {
|
|
|
122
122
|
'minimum': [ 'm' ],
|
|
123
123
|
'optimal': [ 'o' ],
|
|
124
124
|
'sources': [ 's' ],
|
|
125
|
+
'lines': [ 'l' ],
|
|
125
126
|
'help': [ 'h' ],
|
|
126
127
|
},
|
|
127
128
|
|
|
128
129
|
string: [ 'coverage-dir', 'report-dir', 'source-dir', 'sources' ],
|
|
129
130
|
number: [ 'minimum', 'optimal' ],
|
|
130
|
-
boolean: [ 'help', 'version' ],
|
|
131
|
+
boolean: [ 'help', 'version', 'lines' ],
|
|
131
132
|
})
|
|
132
133
|
|
|
133
134
|
for (const [ key, value ] of Object.entries(parsed)) {
|
|
@@ -153,6 +154,9 @@ main(import.meta.url, async (args): Promise<void> => {
|
|
|
153
154
|
case 'optimal':
|
|
154
155
|
optimalCoverage = value
|
|
155
156
|
break
|
|
157
|
+
case 'lines':
|
|
158
|
+
reportUncoveredLines = value
|
|
159
|
+
break
|
|
156
160
|
case 'help':
|
|
157
161
|
return help()
|
|
158
162
|
case 'version':
|
|
@@ -165,15 +169,15 @@ main(import.meta.url, async (args): Promise<void> => {
|
|
|
165
169
|
|
|
166
170
|
// Extra check for command or coverage dir requirement
|
|
167
171
|
if ((command.length === 0) && (! coverageDir)) {
|
|
168
|
-
context.log.error(`Either ${$wnd('--coverage-dir')} or a command must
|
|
172
|
+
context.log.error(`Either ${$wnd('--coverage-dir')} or a command must be specified`)
|
|
169
173
|
process.exit(1)
|
|
170
174
|
}
|
|
171
175
|
|
|
172
|
-
// Default glob
|
|
176
|
+
// Default glob for .js, .cjs, .mjs, .ts, .cts, and .mts source files.
|
|
173
177
|
const glob = globs.shift() || '**/*.([cm])?[jt]s'
|
|
174
|
-
const ignore = '**/*.d.([cm])?ts' //
|
|
178
|
+
const ignore = '**/*.d.([cm])?ts' // Exclude .d.ts, .d.cts, and .d.mts declaration files.
|
|
175
179
|
|
|
176
|
-
//
|
|
180
|
+
// Collect coverage when a command is supplied, then run the Coverage plug.
|
|
177
181
|
try {
|
|
178
182
|
process.exitCode = 0
|
|
179
183
|
await async.runAsync(context, async () => {
|
|
@@ -197,6 +201,7 @@ main(import.meta.url, async (args): Promise<void> => {
|
|
|
197
201
|
await find(glob, ...globs, { directory: sourceDir || 'src', ignore })
|
|
198
202
|
.plug(new Coverage(coverageDir, {
|
|
199
203
|
reportDir: reportDir as string,
|
|
204
|
+
reportUncoveredLines,
|
|
200
205
|
minimumCoverage,
|
|
201
206
|
optimalCoverage,
|
|
202
207
|
}))
|