Skip to content

Commit e81ed5d

Browse files
author
Benjamin E. Coe
authored
fix(config): support configuration inheritance (#343)
1 parent ff01cd8 commit e81ed5d

File tree

5 files changed

+35
-50
lines changed

5 files changed

+35
-50
lines changed

lib/parse-args.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const defaultExtension = require('@istanbuljs/schema/default-extension')
33
const findUp = require('find-up')
44
const { readFileSync } = require('fs')
55
const Yargs = require('yargs/yargs')
6+
const { applyExtends } = require('yargs/helpers')
67
const parser = require('yargs-parser')
78
const { resolve } = require('path')
89

@@ -13,7 +14,10 @@ function buildYargs (withCommands = false) {
1314
alias: 'c',
1415
config: true,
1516
describe: 'path to JSON configuration file',
16-
configParser: (path) => JSON.parse(readFileSync(path)),
17+
configParser: (path) => {
18+
const config = JSON.parse(readFileSync(path))
19+
return applyExtends(config, process.cwd(), true)
20+
},
1721
default: () => findUp.sync(['.c8rc', '.c8rc.json', '.nycrc', '.nycrc.json'])
1822
})
1923
.option('reporter', {

test/fixtures/config/.c8rc-base.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./test/fixtures/config/.c8rc.json",
3+
"branches": 55
4+
}

test/fixtures/config/.c8rc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"temp-directory": "./foo",
3-
"lines": 101
3+
"lines": 101,
4+
"functions": 24
45
}

test/integration.js_10.snap

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,6 @@ All files | 83.33 | 85.71 | 66.66 | 83.33 |
144144
,"
145145
`;
146146

147-
exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
148-
",----------|---------|----------|---------|---------|-------------------
149-
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
150-
----------|---------|----------|---------|---------|-------------------
151-
All files | 0 | 0 | 0 | 0 |
152-
----------|---------|----------|---------|---------|-------------------
153-
,Error [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension: /Users/bencoe/oss/c8/test/fixtures/export.cjs
154-
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:93:13)
155-
at Loader.resolve (internal/modules/esm/loader.js:58:33)
156-
at Loader.getModuleJob (internal/modules/esm/loader.js:113:40)
157-
at ModuleWrap.promises.module.link (internal/modules/esm/module_job.js:32:40)
158-
at link (internal/modules/esm/module_job.js:31:36)
159-
"
160-
`;
161-
162147
exports[`c8 check-coverage --100 1`] = `
163148
",hey
164149
i am a line of code
@@ -194,12 +179,12 @@ hey
194179
--------------------------|---------|----------|---------|---------|--------------------------------
195180
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
196181
--------------------------|---------|----------|---------|---------|--------------------------------
197-
All files | 73.6 | 59.03 | 62.5 | 73.6 |
182+
All files | 73.72 | 59.03 | 62.5 | 73.72 |
198183
bin | 78.84 | 60 | 66.66 | 78.84 |
199184
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
200-
lib | 78.19 | 54.38 | 72 | 78.19 |
185+
lib | 78.32 | 54.38 | 72 | 78.32 |
201186
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
202-
parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192
187+
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
203188
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
204189
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
205190
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
@@ -209,9 +194,9 @@ All files | 73.6 | 59.03 | 62.5 | 73.6 |
209194
async.js | 100 | 100 | 100 | 100 |
210195
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
211196
--------------------------|---------|----------|---------|---------|--------------------------------
212-
,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%)
197+
,ERROR: Coverage for lines (73.72%) does not meet global threshold (101%)
213198
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
214-
ERROR: Coverage for statements (73.6%) does not meet global threshold (95%)
199+
ERROR: Coverage for statements (73.72%) does not meet global threshold (95%)
215200
"
216201
`;
217202

@@ -227,7 +212,7 @@ ERROR: Coverage for statements (80.48%) does not meet threshold (95%) for lib/co
227212
ERROR: Coverage for lines (90%) does not meet threshold (101%) for lib/is-cjs-esm-bridge.js
228213
ERROR: Coverage for branches (25%) does not meet threshold (82%) for lib/is-cjs-esm-bridge.js
229214
ERROR: Coverage for statements (90%) does not meet threshold (95%) for lib/is-cjs-esm-bridge.js
230-
ERROR: Coverage for lines (97.19%) does not meet threshold (101%) for lib/parse-args.js
215+
ERROR: Coverage for lines (97.24%) does not meet threshold (101%) for lib/parse-args.js
231216
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/parse-args.js
232217
ERROR: Coverage for lines (75.47%) does not meet threshold (101%) for lib/report.js
233218
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/report.js
@@ -242,19 +227,19 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
242227
`;
243228

244229
exports[`c8 check-coverage check-coverage command with --100 1`] = `
245-
",,ERROR: Coverage for lines (77.4%) does not meet global threshold (100%)
230+
",,ERROR: Coverage for lines (77.51%) does not meet global threshold (100%)
246231
ERROR: Coverage for functions (66.66%) does not meet global threshold (100%)
247232
ERROR: Coverage for branches (62.35%) does not meet global threshold (100%)
248-
ERROR: Coverage for statements (77.4%) does not meet global threshold (100%)
233+
ERROR: Coverage for statements (77.51%) does not meet global threshold (100%)
249234
"
250235
`;
251236

252237
exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;
253238

254239
exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
255-
",,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%)
240+
",,ERROR: Coverage for lines (73.72%) does not meet global threshold (101%)
256241
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
257-
ERROR: Coverage for statements (73.6%) does not meet global threshold (95%)
242+
ERROR: Coverage for statements (73.72%) does not meet global threshold (95%)
258243
"
259244
`;
260245

@@ -337,12 +322,12 @@ exports[`c8 report generates report from existing temporary files 1`] = `
337322
",--------------------------|---------|----------|---------|---------|--------------------------------
338323
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
339324
--------------------------|---------|----------|---------|---------|--------------------------------
340-
All files | 73.6 | 59.03 | 62.5 | 73.6 |
325+
All files | 73.72 | 59.03 | 62.5 | 73.72 |
341326
bin | 78.84 | 60 | 66.66 | 78.84 |
342327
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
343-
lib | 78.19 | 54.38 | 72 | 78.19 |
328+
lib | 78.32 | 54.38 | 72 | 78.32 |
344329
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
345-
parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192
330+
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
346331
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
347332
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
348333
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
@@ -359,12 +344,12 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
359344
",--------------------------|---------|----------|---------|---------|--------------------------------
360345
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
361346
--------------------------|---------|----------|---------|---------|--------------------------------
362-
All files | 73.6 | 59.03 | 62.5 | 73.6 |
347+
All files | 73.72 | 59.03 | 62.5 | 73.72 |
363348
bin | 78.84 | 60 | 66.66 | 78.84 |
364349
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
365-
lib | 78.19 | 54.38 | 72 | 78.19 |
350+
lib | 78.32 | 54.38 | 72 | 78.32 |
366351
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
367-
parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192
352+
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
368353
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
369354
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
370355
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
@@ -374,9 +359,9 @@ All files | 73.6 | 59.03 | 62.5 | 73.6 |
374359
async.js | 100 | 100 | 100 | 100 |
375360
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
376361
--------------------------|---------|----------|---------|---------|--------------------------------
377-
,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%)
362+
,ERROR: Coverage for lines (73.72%) does not meet global threshold (101%)
378363
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
379-
ERROR: Coverage for statements (73.6%) does not meet global threshold (95%)
364+
ERROR: Coverage for statements (73.72%) does not meet global threshold (95%)
380365
"
381366
`;
382367
@@ -612,18 +597,3 @@ All files | 83.33 | 85.71 | 66.66 | 83.33 |
612597
-----------|---------|----------|---------|---------|-------------------
613598
,"
614599
`;
615-
616-
exports[`c8 ts-node reads source-map from cache, and applies to coverage 1`] = `
617-
",covered
618-
covered
619-
covered
620-
covered
621-
covered
622-
------------------|---------|----------|---------|---------|-------------------
623-
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
624-
------------------|---------|----------|---------|---------|-------------------
625-
All files | 79.41 | 85.71 | 66.67 | 79.41 |
626-
ts-node-basic.ts | 79.41 | 85.71 | 66.67 | 79.41 | 15-17,29-32
627-
------------------|---------|----------|---------|---------|-------------------
628-
,"
629-
`;

test/parse-args.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@ describe('parse-args', () => {
5757
const argv = buildYargs().parse(['node', 'c8', '--lines', '100', '--config', require.resolve('./fixtures/config/.c8rc.json')])
5858
argv.lines.should.be.equal(100)
5959
})
60+
it('should allow config files to extend each other', () => {
61+
const argv = buildYargs().parse(['node', 'c8', '--lines', '100', '--config', require.resolve('./fixtures/config/.c8rc-base.json')])
62+
argv.branches.should.be.equal(55)
63+
argv.lines.should.be.equal(100)
64+
argv.functions.should.be.equal(24)
65+
})
6066
})
6167
})

0 commit comments

Comments
 (0)