crap-ts 1.0.0 → 2.0.0
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/README.md +72 -28
- package/package.json +1 -1
- package/src/cli.js +22 -2
- package/src/crap-analysis.js +2 -1
- package/src/crap-runner.d.ts +7 -3
- package/src/crap-runner.js +57 -13
package/README.md
CHANGED
|
@@ -91,10 +91,14 @@ Scores are compared unrounded: with `max` 30, a score of exactly 30 passes and
|
|
|
91
91
|
|
|
92
92
|
`crap-ts score` walks the working directory for `.ts`, `.tsx`, `.mts` and
|
|
93
93
|
`.cts` files, skipping `node_modules`, directories whose name starts with `.`,
|
|
94
|
-
symbolic links and `.d.ts` declarations. `include` and `exclude`
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
symbolic links and `.d.ts` declarations. `include` and `exclude` match paths
|
|
95
|
+
relative to the working directory. A pattern ending in `/**` matches files
|
|
96
|
+
below a directory at that root. For example, `src/**` matches `src/a.ts`
|
|
97
|
+
and `src/lib/b.ts`. Plain patterns match substrings. For example, `src/`
|
|
98
|
+
also matches `tests/fixtures/src/c.ts`. An asterisk outside the trailing
|
|
99
|
+
`/**` is a usage error. Both forms accept `/` and `\` as
|
|
100
|
+
separators. The same filters apply to files named in the coverage JSON,
|
|
101
|
+
which can have other extensions.
|
|
98
102
|
|
|
99
103
|
An inventoried file with no coverage entry **fails the gate** if it contains a
|
|
100
104
|
function body or an executable module-level statement. Declaration-only files
|
|
@@ -130,9 +134,9 @@ Coverage is per function, from Istanbul statement counters:
|
|
|
130
134
|
entry, or the function was added after coverage ran.
|
|
131
135
|
- The exception is code under a coverage ignore comment, which coverage tools
|
|
132
136
|
leave without counters on purpose. Such a function is listed with
|
|
133
|
-
`evidence: "ignored"` and
|
|
134
|
-
|
|
135
|
-
reference only. crap-ts recognises `istanbul`, `v8` and `c8` comments
|
|
137
|
+
`evidence: "ignored"` and sorts after every other row. By default, its
|
|
138
|
+
level is `ignored` and it does not affect the gate. Its `coverage` (0) and
|
|
139
|
+
`crap` are shown for reference only. crap-ts recognises `istanbul`, `v8` and `c8` comments
|
|
136
140
|
(block or line):
|
|
137
141
|
- `ignore next` covers the next statement or declaration, including any
|
|
138
142
|
functions inside it.
|
|
@@ -140,13 +144,19 @@ Coverage is per function, from Istanbul statement counters:
|
|
|
140
144
|
- `ignore if` and `ignore else`, before an `if` statement, cover its `if`
|
|
141
145
|
branch or its `else` branch.
|
|
142
146
|
- `ignore start` … `ignore stop` covers the region between them.
|
|
143
|
-
- `ignore file` covers the whole file,
|
|
147
|
+
- `ignore file` covers the whole file. By default, the file needs no
|
|
148
|
+
coverage entry.
|
|
144
149
|
|
|
145
150
|
Comment-like text inside strings, template literals, regular expressions
|
|
146
151
|
and JSX text is not treated as a comment.
|
|
147
152
|
|
|
148
153
|
An ignore comment only exempts a function that has no counters. If the
|
|
149
154
|
coverage tool still measured it, its counters are used.
|
|
155
|
+
|
|
156
|
+
Set `failOnIgnored: true` or pass `--fail-on-ignored` to fail on audited
|
|
157
|
+
functions with `evidence: "ignored"`. The text report names these functions
|
|
158
|
+
even when they fall outside `--limit`. An executable file with an
|
|
159
|
+
`ignore file` comment also needs a coverage entry when this setting is on.
|
|
150
160
|
- Module-level statements belong to no function.
|
|
151
161
|
|
|
152
162
|
Pass `--coverage-file` more than once (or a `coverageFile` array) to merge
|
|
@@ -154,6 +164,11 @@ runs, such as Node and browser suites. Entries for the same file are merged by
|
|
|
154
164
|
each counter's original source position, never by counter id or by averaging
|
|
155
165
|
percentages.
|
|
156
166
|
|
|
167
|
+
Each listed coverage file must contain at least one source entry. The CLI
|
|
168
|
+
rejects `{}` before applying path filters, exits with code 1, and names the
|
|
169
|
+
empty file. This check also runs with `--report-only`. To accept empty files
|
|
170
|
+
as in 1.0, set `allowEmptyCoverage: true` or pass `--allow-empty-coverage`.
|
|
171
|
+
|
|
157
172
|
`crap-ts` refuses coverage whose statements start outside the current source
|
|
158
173
|
(a line that does not exist, or a column past the end of the line): re-run
|
|
159
174
|
coverage after changing the source. This is a cheap staleness guard, not a
|
|
@@ -195,7 +210,8 @@ abstract methods have no body and no row. Unnamed functions are reported as
|
|
|
195
210
|
|
|
196
211
|
`minLines` (default 10) hides functions shorter than that many lines from both
|
|
197
212
|
reports and the gate. A function fails when its score is above `max` or its
|
|
198
|
-
evidence is `missing`.
|
|
213
|
+
evidence is `missing`. With `failOnIgnored` enabled, it also fails when its
|
|
214
|
+
evidence is `ignored`.
|
|
199
215
|
|
|
200
216
|
When the CRAP check flags a hotspot:
|
|
201
217
|
|
|
@@ -222,10 +238,12 @@ error.
|
|
|
222
238
|
- `--coverage-file <path>`: Istanbul `coverage-final.json`. Repeat to merge
|
|
223
239
|
several runs. Defaults to `coverage/unit/coverage-final.json`.
|
|
224
240
|
- `--coverage-command <cmd>`: run a coverage command before scoring.
|
|
225
|
-
- `--include <
|
|
226
|
-
|
|
227
|
-
- `--exclude <
|
|
228
|
-
|
|
241
|
+
- `--include <pattern>`: only analyze matching paths. Repeatable. Quote
|
|
242
|
+
patterns like `'src/**'` to pass them directly to crap-ts.
|
|
243
|
+
- `--exclude <pattern>`: skip matching paths. Repeatable.
|
|
244
|
+
- `--fail-on-ignored`: fail on ignored functions and require coverage entries
|
|
245
|
+
for executable files with an `ignore file` comment.
|
|
246
|
+
- `--allow-empty-coverage`: accept listed coverage files containing `{}`.
|
|
229
247
|
- `--json`: print the JSON report instead of the text report.
|
|
230
248
|
- `--all`: print every analyzed function instead of applying `--limit`.
|
|
231
249
|
- `--limit <n>`: number of text report rows to print. Defaults to `20`.
|
|
@@ -237,9 +255,9 @@ error.
|
|
|
237
255
|
- `--skip-coverage`: do not run the coverage command.
|
|
238
256
|
|
|
239
257
|
Exit codes: `0` passed (or `--report-only`), `1` gate failed, coverage
|
|
240
|
-
command failed, or coverage input is missing, malformed
|
|
241
|
-
config error (unknown
|
|
242
|
-
|
|
258
|
+
command failed, or coverage input is missing, malformed, stale or rejected
|
|
259
|
+
for being empty, `2` usage or config error (unknown command or flag, invalid
|
|
260
|
+
value, unreadable or invalid config file).
|
|
243
261
|
|
|
244
262
|
When the coverage command fails, `crap-ts` exits `1` before scoring and
|
|
245
263
|
prints no report, not even with `--json`.
|
|
@@ -260,6 +278,8 @@ word, because you'd be mad at me if it didn't, and I'd also be mad at me.
|
|
|
260
278
|
| `coverageFile` | string or string[] | `--coverage-file` | `coverage/unit/coverage-final.json` |
|
|
261
279
|
| `include` | string or string[] | `--include` | everything |
|
|
262
280
|
| `exclude` | string or string[] | `--exclude` | nothing |
|
|
281
|
+
| `failOnIgnored` | boolean | `--fail-on-ignored` | `false` |
|
|
282
|
+
| `allowEmptyCoverage` | boolean | `--allow-empty-coverage` | `false` |
|
|
263
283
|
| `json` | boolean | `--json` | `false` |
|
|
264
284
|
| `all` | boolean | `--all` | `false` |
|
|
265
285
|
| `limit` | number | `--limit` | `20` |
|
|
@@ -272,7 +292,7 @@ word, because you'd be mad at me if it didn't, and I'd also be mad at me.
|
|
|
272
292
|
## JSON Report
|
|
273
293
|
|
|
274
294
|
`crap-ts score --json` prints one JSON document to stdout. Schema version `1`
|
|
275
|
-
is stable for crap-ts 1.x: fields are only added, never renamed, removed or
|
|
295
|
+
is stable for crap-ts 1.x and 2.x: fields are only added, never renamed, removed or
|
|
276
296
|
changed in meaning. A breaking change bumps `schemaVersion`.
|
|
277
297
|
|
|
278
298
|
```json
|
|
@@ -312,11 +332,13 @@ changed in meaning. A breaking change bumps `schemaVersion`.
|
|
|
312
332
|
|
|
313
333
|
- `thresholds.warn` is `null` when no warn threshold is set.
|
|
314
334
|
- `summary.files`: source files analyzed. `functions`: rows listed.
|
|
315
|
-
`aboveMax`: rows with `crap > max`,
|
|
335
|
+
`aboveMax`: rows with `crap > max`, excluding rows with evidence `ignored`.
|
|
336
|
+
`warning`: rows with level `warn`.
|
|
316
337
|
`missingEvidence`: rows with evidence `missing`. `ignored`: rows with
|
|
317
338
|
evidence `ignored`. `missingFiles`: length of `missingFiles`.
|
|
318
|
-
- `summary.passed` is
|
|
319
|
-
`missingFiles` all zero,
|
|
339
|
+
- `summary.passed` is true when `aboveMax`, `missingEvidence` and
|
|
340
|
+
`missingFiles` are all zero. With `failOnIgnored` enabled, `ignored` must
|
|
341
|
+
also be zero. The verdict is independent of `--report-only`.
|
|
320
342
|
- `functions` lists every function at or above `minLines`, never truncated by
|
|
321
343
|
`--limit`, sorted by `crap` descending, then `file`, `line`, `column`, with
|
|
322
344
|
`ignored` rows after all others.
|
|
@@ -326,25 +348,28 @@ changed in meaning. A breaking change bumps `schemaVersion`.
|
|
|
326
348
|
unrounded.
|
|
327
349
|
- `evidence` is `statements`, `function`, `ignored` or `missing` (see
|
|
328
350
|
[Coverage](#coverage)).
|
|
329
|
-
- `level` is `fail` (above `max`,
|
|
330
|
-
`
|
|
331
|
-
and `crap` is the score at 0% coverage,
|
|
351
|
+
- `level` is `fail` (above `max`, missing evidence, or ignored evidence with
|
|
352
|
+
`failOnIgnored` enabled), `warn` (above `warn`), `ignored` or `ok`. With
|
|
353
|
+
ignored evidence, `coverage` is 0 and `crap` is the score at 0% coverage,
|
|
354
|
+
for reference only.
|
|
332
355
|
- `missingFiles` lists inventoried files with executable code and no coverage
|
|
333
356
|
entry, sorted.
|
|
334
357
|
|
|
335
358
|
## Programmatic API
|
|
336
359
|
|
|
337
360
|
The package root is the only entry point. These exports, and the types in
|
|
338
|
-
`src/index.d.ts`, are the
|
|
361
|
+
`src/index.d.ts`, are the 2.x API:
|
|
339
362
|
|
|
340
363
|
- `runCrapAudit(options?: CrapAuditOptions): Promise<number>` runs the same
|
|
341
364
|
audit as the CLI and resolves to its exit code (`2` for invalid options,
|
|
342
365
|
including non-finite numbers; `1` for a failed gate, a failed coverage
|
|
343
366
|
command or a missing coverage file). It rejects with an `Error`
|
|
344
|
-
when coverage or source input is malformed or stale
|
|
345
|
-
|
|
346
|
-
`
|
|
347
|
-
`
|
|
367
|
+
when coverage or source input is malformed or stale, or when a required
|
|
368
|
+
coverage file is empty and `allowEmptyCoverage` is false. Options include
|
|
369
|
+
`coverageCommand`, `coveragePaths`, `cwd`, `includes`, `excludes`,
|
|
370
|
+
`failOnIgnored`, `allowEmptyCoverage`,
|
|
371
|
+
`json`, `all`, `limit`, `maxScore`, `warnScore`, `minLines`, `reportOnly`
|
|
372
|
+
and `skipCoverage`.
|
|
348
373
|
- `analyzeFileRisk({ filePath, sourceText, coverage?, minLines?, sourceFilePath? }): Promise<FunctionRisk[]>`
|
|
349
374
|
scores the functions in one source text. `coverage` is that file's entry
|
|
350
375
|
from `coverage-final.json`. A function without counters has
|
|
@@ -357,6 +382,25 @@ The package root is the only entry point. These exports, and the types in
|
|
|
357
382
|
|
|
358
383
|
Everything else under `src/` is internal and may change in any release.
|
|
359
384
|
|
|
385
|
+
## Upgrading From 1.x
|
|
386
|
+
|
|
387
|
+
2.0 has two breaking changes. Scores and the JSON schema do not change.
|
|
388
|
+
|
|
389
|
+
- A listed coverage file that contains `{}` now fails with exit code 1, also
|
|
390
|
+
with `--report-only`. Fix the coverage run so that it writes entries. To
|
|
391
|
+
accept empty files as in 1.0, pass `--allow-empty-coverage` or set
|
|
392
|
+
`allowEmptyCoverage: true`.
|
|
393
|
+
- An `include` or `exclude` pattern with `*` anywhere other than a trailing
|
|
394
|
+
`/**` now exits with code 2. Replace `src/*` or `src/**/*.ts` with `src/**`,
|
|
395
|
+
or use a plain substring pattern such as `src/`.
|
|
396
|
+
|
|
397
|
+
New options:
|
|
398
|
+
|
|
399
|
+
- `--fail-on-ignored` (`failOnIgnored`) fails the gate on functions that
|
|
400
|
+
coverage ignore comments exempt.
|
|
401
|
+
- A pattern ending in `/**` matches only below that directory at the working
|
|
402
|
+
directory root. Plain patterns still match substrings.
|
|
403
|
+
|
|
360
404
|
## Upgrading From 0.x
|
|
361
405
|
|
|
362
406
|
1.0 changes scores and removes an unused mode. Expect different numbers.
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -21,8 +21,12 @@ Options:
|
|
|
21
21
|
--coverage-command <cmd>
|
|
22
22
|
Command to generate coverage before scoring.
|
|
23
23
|
--all Print every analyzed function instead of applying --limit.
|
|
24
|
-
--include <
|
|
25
|
-
--exclude <
|
|
24
|
+
--include <pattern> Only analyze matching source paths. Use dir/** to anchor.
|
|
25
|
+
--exclude <pattern> Skip matching source paths. Use dir/** to anchor.
|
|
26
|
+
--fail-on-ignored
|
|
27
|
+
Fail when an audited function has coverage evidence marked ignored.
|
|
28
|
+
--allow-empty-coverage
|
|
29
|
+
Accept listed coverage JSON files with no entries.
|
|
26
30
|
--json Print the full JSON report (schema version 1) instead of text.
|
|
27
31
|
--limit <n> Number of rows to print in the text report. Defaults to 20.
|
|
28
32
|
--min-lines <n> Ignore functions shorter than this. Defaults to 10.
|
|
@@ -41,10 +45,12 @@ export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
|
|
|
41
45
|
args,
|
|
42
46
|
options: {
|
|
43
47
|
all: { type: "boolean" },
|
|
48
|
+
"allow-empty-coverage": { type: "boolean" },
|
|
44
49
|
config: { type: "string" },
|
|
45
50
|
"coverage-file": { type: "string", multiple: true },
|
|
46
51
|
"coverage-command": { type: "string" },
|
|
47
52
|
exclude: { type: "string", multiple: true },
|
|
53
|
+
"fail-on-ignored": { type: "boolean" },
|
|
48
54
|
help: { type: "boolean", short: "h" },
|
|
49
55
|
include: { type: "string", multiple: true },
|
|
50
56
|
json: { type: "boolean" },
|
|
@@ -62,10 +68,12 @@ export function parseCliArgs(argv, { cwd = process.cwd() } = {}) {
|
|
|
62
68
|
mergeConfigWithCli(config, {
|
|
63
69
|
command,
|
|
64
70
|
all: values.all,
|
|
71
|
+
allowEmptyCoverage: values["allow-empty-coverage"],
|
|
65
72
|
configPath: values.config,
|
|
66
73
|
coverageCommand: values["coverage-command"],
|
|
67
74
|
coveragePaths: values["coverage-file"],
|
|
68
75
|
excludes: values.exclude,
|
|
76
|
+
failOnIgnored: values["fail-on-ignored"],
|
|
69
77
|
help: values.help,
|
|
70
78
|
includes: values.include,
|
|
71
79
|
json: values.json,
|
|
@@ -158,9 +166,11 @@ function mergeConfigWithCli(config, cliOptions) {
|
|
|
158
166
|
|
|
159
167
|
const configKeys = new Set([
|
|
160
168
|
"all",
|
|
169
|
+
"allowEmptyCoverage",
|
|
161
170
|
"coverageCommand",
|
|
162
171
|
"coverageFile",
|
|
163
172
|
"exclude",
|
|
173
|
+
"failOnIgnored",
|
|
164
174
|
"include",
|
|
165
175
|
"json",
|
|
166
176
|
"limit",
|
|
@@ -186,6 +196,11 @@ function normalizeConfig(config) {
|
|
|
186
196
|
|
|
187
197
|
return stripUndefined({
|
|
188
198
|
all: parseConfigType(config.all, "all", "boolean"),
|
|
199
|
+
allowEmptyCoverage: parseConfigType(
|
|
200
|
+
config.allowEmptyCoverage,
|
|
201
|
+
"allowEmptyCoverage",
|
|
202
|
+
"boolean",
|
|
203
|
+
),
|
|
189
204
|
coverageCommand: parseConfigType(
|
|
190
205
|
config.coverageCommand,
|
|
191
206
|
"coverageCommand",
|
|
@@ -193,6 +208,11 @@ function normalizeConfig(config) {
|
|
|
193
208
|
),
|
|
194
209
|
coveragePaths: normalizeConfigList(config.coverageFile, "coverageFile"),
|
|
195
210
|
excludes: normalizeConfigList(config.exclude, "exclude"),
|
|
211
|
+
failOnIgnored: parseConfigType(
|
|
212
|
+
config.failOnIgnored,
|
|
213
|
+
"failOnIgnored",
|
|
214
|
+
"boolean",
|
|
215
|
+
),
|
|
196
216
|
includes: normalizeConfigList(config.include, "include"),
|
|
197
217
|
json: parseConfigType(config.json, "json", "boolean"),
|
|
198
218
|
limit: parseConfigNumber(config.limit, "limit"),
|
package/src/crap-analysis.js
CHANGED
|
@@ -33,6 +33,7 @@ export async function analyzeFileRisk({
|
|
|
33
33
|
|
|
34
34
|
export async function analyzeSource({
|
|
35
35
|
coverage,
|
|
36
|
+
failOnIgnored = false,
|
|
36
37
|
filePath,
|
|
37
38
|
sourceFilePath,
|
|
38
39
|
sourceText,
|
|
@@ -53,7 +54,7 @@ export async function analyzeSource({
|
|
|
53
54
|
|
|
54
55
|
return {
|
|
55
56
|
executable:
|
|
56
|
-
!ignore.file &&
|
|
57
|
+
(!ignore.file || failOnIgnored) &&
|
|
57
58
|
(functions.length > 0 || hasExecutableModuleStatements(adapter)),
|
|
58
59
|
functions: functions.map(({ ignored, risk }) => {
|
|
59
60
|
// No counters is only acceptable where an ignore comment removed them.
|
package/src/crap-runner.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
export interface CrapAuditOptions {
|
|
2
2
|
all?: boolean;
|
|
3
|
+
/** Accept listed coverage JSON files with no source entries. Defaults to false. */
|
|
4
|
+
allowEmptyCoverage?: boolean;
|
|
3
5
|
coverageCommand?: string;
|
|
4
6
|
/** Istanbul `coverage-final.json` files, merged by source position. Defaults to `["coverage/unit/coverage-final.json"]`. */
|
|
5
7
|
coveragePaths?: string[];
|
|
6
8
|
cwd?: string;
|
|
7
|
-
/** Skip
|
|
9
|
+
/** Skip files matching these path substrings or root-anchored `dir/**` patterns. */
|
|
8
10
|
excludes?: string[];
|
|
9
|
-
/**
|
|
11
|
+
/** Fail on functions with ignored evidence and require coverage entries for executable files with an ignore-file comment. Defaults to false. */
|
|
12
|
+
failOnIgnored?: boolean;
|
|
13
|
+
/** Analyze files matching these path substrings or root-anchored `dir/**` patterns. */
|
|
10
14
|
includes?: string[];
|
|
11
15
|
/** Print the JSON report (schema version 1) to stdout instead of the text report. */
|
|
12
16
|
json?: boolean;
|
|
@@ -21,6 +25,6 @@ export interface CrapAuditOptions {
|
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* Resolves to the process exit code: 0 pass, 1 gate failure, failed coverage command or missing coverage file, 2 usage error (including non-finite numeric options).
|
|
24
|
-
* Rejects when coverage or source input is malformed or stale.
|
|
28
|
+
* Rejects when coverage or source input is malformed or stale, or when a coverage file is empty and `allowEmptyCoverage` is false.
|
|
25
29
|
*/
|
|
26
30
|
export function runCrapAudit(options?: CrapAuditOptions): Promise<number>;
|
package/src/crap-runner.js
CHANGED
|
@@ -12,10 +12,12 @@ const declarationFilePattern = /\.d\.[cm]?ts$/;
|
|
|
12
12
|
|
|
13
13
|
export async function runCrapAudit({
|
|
14
14
|
all = false,
|
|
15
|
+
allowEmptyCoverage = false,
|
|
15
16
|
coverageCommand,
|
|
16
17
|
coveragePaths = ["coverage/unit/coverage-final.json"],
|
|
17
18
|
cwd = process.cwd(),
|
|
18
19
|
excludes = [],
|
|
20
|
+
failOnIgnored = false,
|
|
19
21
|
includes = [],
|
|
20
22
|
json = false,
|
|
21
23
|
limit = 20,
|
|
@@ -44,6 +46,18 @@ export async function runCrapAudit({
|
|
|
44
46
|
return 2;
|
|
45
47
|
}
|
|
46
48
|
|
|
49
|
+
const unsupportedPattern = [...includes, ...excludes].find((pattern) =>
|
|
50
|
+
normalizePath(pattern)
|
|
51
|
+
.replace(/\/\*\*$/, "")
|
|
52
|
+
.includes("*"),
|
|
53
|
+
);
|
|
54
|
+
if (unsupportedPattern !== undefined) {
|
|
55
|
+
console.error(
|
|
56
|
+
`Unsupported path pattern ${JSON.stringify(unsupportedPattern)}. Use a substring or a directory pattern such as src/**.`,
|
|
57
|
+
);
|
|
58
|
+
return 2;
|
|
59
|
+
}
|
|
60
|
+
|
|
47
61
|
if (!skipCoverage && coverageCommand) {
|
|
48
62
|
const result = spawnSync(coverageCommand, {
|
|
49
63
|
cwd,
|
|
@@ -78,7 +92,11 @@ export async function runCrapAudit({
|
|
|
78
92
|
// Coverage tools report real paths; compare everything in the file
|
|
79
93
|
// system's canonical form (`.native` also canonicalises letter case).
|
|
80
94
|
const root = realpathSync.native(cwd);
|
|
81
|
-
const coverageByFile = readCoverageFiles(
|
|
95
|
+
const coverageByFile = readCoverageFiles(
|
|
96
|
+
resolvedCoveragePaths,
|
|
97
|
+
root,
|
|
98
|
+
allowEmptyCoverage,
|
|
99
|
+
);
|
|
82
100
|
const isSelected = (path) => {
|
|
83
101
|
const relativePath = relative(root, path);
|
|
84
102
|
return (
|
|
@@ -102,6 +120,7 @@ export async function runCrapAudit({
|
|
|
102
120
|
const coverage = coverageByFile.get(sourceFilePath);
|
|
103
121
|
const analysis = await analyzeSource({
|
|
104
122
|
coverage,
|
|
123
|
+
failOnIgnored,
|
|
105
124
|
filePath,
|
|
106
125
|
sourceFilePath,
|
|
107
126
|
sourceText: readFileSync(sourceFilePath, "utf8"),
|
|
@@ -117,6 +136,7 @@ export async function runCrapAudit({
|
|
|
117
136
|
}
|
|
118
137
|
|
|
119
138
|
const report = buildReport({
|
|
139
|
+
failOnIgnored,
|
|
120
140
|
files: sourcePaths.size,
|
|
121
141
|
functions,
|
|
122
142
|
maxScore,
|
|
@@ -163,6 +183,7 @@ export async function runCrapAudit({
|
|
|
163
183
|
}
|
|
164
184
|
|
|
165
185
|
function buildReport({
|
|
186
|
+
failOnIgnored,
|
|
166
187
|
files,
|
|
167
188
|
functions,
|
|
168
189
|
maxScore,
|
|
@@ -173,7 +194,7 @@ function buildReport({
|
|
|
173
194
|
const rows = functions
|
|
174
195
|
.sort(
|
|
175
196
|
(left, right) =>
|
|
176
|
-
//
|
|
197
|
+
// Keep ignored scores below rows that use coverage to score the gate.
|
|
177
198
|
Number(left.evidence === "ignored") -
|
|
178
199
|
Number(right.evidence === "ignored") ||
|
|
179
200
|
right.crapScore - left.crapScore ||
|
|
@@ -194,13 +215,13 @@ function buildReport({
|
|
|
194
215
|
coveredStatements: risk.coveredStatements,
|
|
195
216
|
evidence: risk.evidence,
|
|
196
217
|
crap: risk.crapScore,
|
|
197
|
-
level: getRiskLevel(risk, { maxScore, warnScore }),
|
|
218
|
+
level: getRiskLevel(risk, { failOnIgnored, maxScore, warnScore }),
|
|
198
219
|
}));
|
|
199
220
|
const count = (predicate) => rows.filter(predicate).length;
|
|
200
221
|
const summary = {
|
|
201
222
|
files,
|
|
202
223
|
functions: rows.length,
|
|
203
|
-
aboveMax: count((row) => row.
|
|
224
|
+
aboveMax: count((row) => row.evidence !== "ignored" && row.crap > maxScore),
|
|
204
225
|
warning: count((row) => row.level === "warn"),
|
|
205
226
|
missingEvidence: count((row) => row.evidence === "missing"),
|
|
206
227
|
ignored: count((row) => row.evidence === "ignored"),
|
|
@@ -215,7 +236,8 @@ function buildReport({
|
|
|
215
236
|
passed:
|
|
216
237
|
summary.aboveMax === 0 &&
|
|
217
238
|
summary.missingEvidence === 0 &&
|
|
218
|
-
summary.missingFiles === 0
|
|
239
|
+
summary.missingFiles === 0 &&
|
|
240
|
+
(!failOnIgnored || summary.ignored === 0),
|
|
219
241
|
},
|
|
220
242
|
functions: rows,
|
|
221
243
|
missingFiles,
|
|
@@ -240,9 +262,15 @@ function printTextReport({ all, limit, maxScore, report, warnScore }) {
|
|
|
240
262
|
`CRAP report: ${summary.functions} functions checked, ${summary.aboveMax} above ${maxScore}${warningSummary}${missingSummary}${ignoredSummary}.`,
|
|
241
263
|
);
|
|
242
264
|
|
|
243
|
-
|
|
265
|
+
const visibleRows = all ? functions : functions.slice(0, limit);
|
|
266
|
+
for (const row of visibleRows) {
|
|
244
267
|
console.log(formatReportRow(row));
|
|
245
268
|
}
|
|
269
|
+
for (const row of functions.slice(visibleRows.length)) {
|
|
270
|
+
if (row.evidence === "ignored" && row.level === "fail") {
|
|
271
|
+
console.log(formatReportRow(row));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
246
274
|
}
|
|
247
275
|
|
|
248
276
|
const rowTags = { fail: "FAIL", ignored: "SKIP", ok: " ok", warn: "WARN" };
|
|
@@ -260,9 +288,9 @@ function formatReportRow(row) {
|
|
|
260
288
|
].join(" | ");
|
|
261
289
|
}
|
|
262
290
|
|
|
263
|
-
function getRiskLevel(risk, { maxScore, warnScore }) {
|
|
291
|
+
function getRiskLevel(risk, { failOnIgnored, maxScore, warnScore }) {
|
|
264
292
|
if (risk.evidence === "ignored") {
|
|
265
|
-
return "ignored";
|
|
293
|
+
return failOnIgnored ? "fail" : "ignored";
|
|
266
294
|
}
|
|
267
295
|
|
|
268
296
|
if (risk.crapScore > maxScore || risk.evidence === "missing") {
|
|
@@ -276,14 +304,19 @@ function getRiskLevel(risk, { maxScore, warnScore }) {
|
|
|
276
304
|
return "ok";
|
|
277
305
|
}
|
|
278
306
|
|
|
279
|
-
function readCoverageFiles(coveragePaths, root) {
|
|
307
|
+
function readCoverageFiles(coveragePaths, root, allowEmptyCoverage) {
|
|
280
308
|
const coverageByFile = new Map();
|
|
281
309
|
|
|
282
310
|
for (const coveragePath of coveragePaths) {
|
|
283
311
|
const coverageJson = parseCoverageJson(coveragePath);
|
|
284
312
|
assertObject(coverageJson, coveragePath);
|
|
313
|
+
const entries = Object.entries(coverageJson);
|
|
285
314
|
|
|
286
|
-
|
|
315
|
+
if (!allowEmptyCoverage && entries.length === 0) {
|
|
316
|
+
throw new Error(`Empty coverage JSON: ${coveragePath}`);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
for (const [key, entry] of entries) {
|
|
287
320
|
const label = `${coveragePath} (${key})`;
|
|
288
321
|
validateFileCoverage(entry, label);
|
|
289
322
|
|
|
@@ -445,7 +478,18 @@ function matchesAny(path, patterns, emptyResult) {
|
|
|
445
478
|
|
|
446
479
|
const normalizedPath = normalizePath(path);
|
|
447
480
|
|
|
448
|
-
return patterns.some((pattern) =>
|
|
449
|
-
|
|
450
|
-
|
|
481
|
+
return patterns.some((pattern) => {
|
|
482
|
+
const normalizedPattern = normalizePath(pattern);
|
|
483
|
+
|
|
484
|
+
if (normalizedPattern.endsWith("/**")) {
|
|
485
|
+
return (
|
|
486
|
+
!isAbsolute(path) &&
|
|
487
|
+
normalizedPath !== ".." &&
|
|
488
|
+
!normalizedPath.startsWith("../") &&
|
|
489
|
+
normalizedPath.startsWith(normalizedPattern.slice(0, -2))
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return normalizedPath.includes(normalizedPattern);
|
|
494
|
+
});
|
|
451
495
|
}
|