diff --git a/bin/c8.js b/bin/c8.js index 719272c80..6d7719617 100755 --- a/bin/c8.js +++ b/bin/c8.js @@ -26,7 +26,10 @@ let argv = buildYargs().parse(instrumenterArgs) await promisify(fs.mkdir)(argv.tempDirectory, { recursive: true }) } - process.env.NODE_V8_COVERAGE = argv.tempDirectory + if (!process.env.NODE_V8_COVERAGE) { + process.env.NODE_V8_COVERAGE = argv.tempDirectory + } + foreground(hideInstrumenterArgs(argv), () => { outputReport(argv) }) diff --git a/lib/parse-args.js b/lib/parse-args.js index bafe9d4f9..d5ade0346 100644 --- a/lib/parse-args.js +++ b/lib/parse-args.js @@ -61,7 +61,7 @@ function buildYargs (withCommands = false) { type: 'boolean' }) .option('temp-directory', { - default: './coverage/tmp', + default: process.env.NODE_V8_COVERAGE || './coverage/tmp', describe: 'directory V8 coverage data is written to and read from' }) .option('resolve', { diff --git a/test/integration.js b/test/integration.js index 924db054a..f8d1900d0 100644 --- a/test/integration.js +++ b/test/integration.js @@ -48,6 +48,19 @@ describe('c8', () => { ) }) + it('supports exeternally set NODE_V8_COVERAGE', () => { + process.env.NODE_V8_COVERAGE = './coverage/tmp_' + const { output } = spawnSync(nodePath, [ + c8Path, + '--exclude="test/*.js"', + '--clean=false', + nodePath, + require.resolve('./fixtures/normal') + ]) + output.toString('utf8').should.matchSnapshot() + process.env.NODE_V8_COVERAGE = '' + }) + describe('check-coverage', () => { it('exits with 0 if coverage within threshold', () => { const { output, status } = spawnSync(nodePath, [ diff --git a/test/integration.js.snap b/test/integration.js.snap index f21cf1607..43dde86fd 100644 --- a/test/integration.js.snap +++ b/test/integration.js.snap @@ -5,15 +5,15 @@ exports[`c8 ESM Modules collects coverage for ESM modules 1`] = ` --------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 96.2 | 86.49 | 90.32 | 96.2 | | - bin | 91.18 | 83.33 | 100 | 91.18 | | - c8.js | 91.18 | 83.33 | 100 | 91.18 | 25,26,27 | - lib | 97.51 | 82.35 | 100 | 97.51 | | - parse-args.js | 98.46 | 60 | 100 | 98.46 | 107,108 | - report.js | 96.69 | 91.67 | 100 | 96.69 |... 10,136,137,138 | - lib/commands | 100 | 88.89 | 100 | 100 | | - check-coverage.js | 100 | 92.31 | 100 | 100 | 17 | - report.js | 100 | 80 | 100 | 100 | 16 | +All files | 84.91 | 74.58 | 72.41 | 84.91 | | + bin | 83.78 | 50 | 100 | 83.78 | | + c8.js | 83.78 | 50 | 100 | 83.78 | 22,25,26,27,30,31 | + lib | 96.8 | 71.88 | 100 | 96.8 | | + parse-args.js | 96.92 | 40 | 100 | 96.92 | 93,94,107,108 | + report.js | 96.69 | 86.36 | 100 | 96.69 |... 10,136,137,138 | + lib/commands | 43.02 | 60 | 16.67 | 43.02 | | + check-coverage.js | 21.67 | 100 | 0 | 21.67 |... 56,57,58,59,60 | + report.js | 92.31 | 50 | 50 | 92.31 | 9,10 | test/fixtures | 89.04 | 93.75 | 70 | 89.04 | | async.js | 100 | 100 | 100 | 100 | | export.mjs | 71.43 | 100 | 50 | 71.43 | 2,3 | @@ -37,14 +37,14 @@ hey --------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 96.12 | 82.09 | 89.66 | 96.12 | | - bin | 91.18 | 80 | 100 | 91.18 | | - c8.js | 91.18 | 80 | 100 | 91.18 | 25,26,27 | - lib | 97.51 | 78.13 | 100 | 97.51 | | - parse-args.js | 98.46 | 60 | 100 | 98.46 | 107,108 | +All files | 85.01 | 73.21 | 74.07 | 85.01 | | + bin | 83.78 | 50 | 100 | 83.78 | | + c8.js | 83.78 | 50 | 100 | 83.78 | 22,25,26,27,30,31 | + lib | 96.8 | 71.88 | 100 | 96.8 | | + parse-args.js | 96.92 | 40 | 100 | 96.92 | 93,94,107,108 | report.js | 96.69 | 86.36 | 100 | 96.69 |... 10,136,137,138 | - lib/commands | 97.67 | 82.35 | 87.5 | 97.67 | | - check-coverage.js | 100 | 92.31 | 100 | 100 | 17 | + lib/commands | 43.02 | 60 | 16.67 | 43.02 | | + check-coverage.js | 21.67 | 100 | 0 | 21.67 |... 56,57,58,59,60 | report.js | 92.31 | 50 | 50 | 92.31 | 9,10 | test/fixtures | 90.48 | 92.31 | 75 | 90.48 | | async.js | 100 | 100 | 100 | 100 | | @@ -52,12 +52,12 @@ All files | 96.12 | 82.09 | 89.66 | 96.12 | normal.js | 75 | 66.67 | 33.33 | 75 | 14,15,16,18,19,20 | subprocess.js | 100 | 100 | 100 | 100 | | --------------------|----------|----------|----------|----------|-------------------| -,ERROR: Coverage for lines (96.12%) does not meet global threshold (101%) +,ERROR: Coverage for lines (85.01%) does not meet global threshold (101%) " `; exports[`c8 check-coverage allows threshold to be applied on per-file basis 1`] = ` -",,ERROR: Coverage for lines (78.33%) does not meet threshold (80%) for lib/commands/check-coverage.js +",,ERROR: Coverage for lines (21.67%) does not meet threshold (80%) for lib/commands/check-coverage.js ERROR: Coverage for lines (75%) does not meet threshold (80%) for test/fixtures/normal.js " `; @@ -65,7 +65,7 @@ ERROR: Coverage for lines (75%) does not meet threshold (80%) for test/fixtures/ exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`; exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = ` -",,ERROR: Coverage for lines (95.69%) does not meet global threshold (101%) +",,ERROR: Coverage for lines (85.01%) does not meet global threshold (101%) " `; @@ -77,11 +77,11 @@ second --------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 84.91 | 72 | 74.07 | 84.91 | | - bin | 88.24 | 60 | 100 | 88.24 | | - c8.js | 88.24 | 60 | 100 | 88.24 | 22,25,26,27 | - lib | 96.09 | 66.67 | 100 | 96.09 | | - parse-args.js | 96.92 | 44.44 | 100 | 96.92 | 93,94,107,108 | +All files | 84.58 | 69.23 | 74.07 | 84.58 | | + bin | 83.78 | 50 | 100 | 83.78 | | + c8.js | 83.78 | 50 | 100 | 83.78 | 22,25,26,27,30,31 | + lib | 96.09 | 64.29 | 100 | 96.09 | | + parse-args.js | 96.92 | 40 | 100 | 96.92 | 93,94,107,108 | report.js | 95.36 | 77.78 | 100 | 95.36 |... 10,136,137,138 | lib/commands | 43.02 | 60 | 16.67 | 43.02 | | check-coverage.js | 21.67 | 100 | 0 | 21.67 |... 56,57,58,59,60 | @@ -99,15 +99,15 @@ exports[`c8 report generates report from existing temporary files 1`] = ` ",--------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 96.12 | 85.51 | 89.66 | 96.12 | | - bin | 91.18 | 83.33 | 100 | 91.18 | | - c8.js | 91.18 | 83.33 | 100 | 91.18 | 25,26,27 | - lib | 97.51 | 82.35 | 100 | 97.51 | | - parse-args.js | 98.46 | 60 | 100 | 98.46 | 107,108 | - report.js | 96.69 | 91.67 | 100 | 96.69 |... 10,136,137,138 | - lib/commands | 97.67 | 87.5 | 87.5 | 97.67 | | - check-coverage.js | 100 | 91.67 | 100 | 100 | 17 | - report.js | 92.31 | 75 | 50 | 92.31 | 9,10 | +All files | 85.01 | 73.21 | 74.07 | 85.01 | | + bin | 83.78 | 50 | 100 | 83.78 | | + c8.js | 83.78 | 50 | 100 | 83.78 | 22,25,26,27,30,31 | + lib | 96.8 | 71.88 | 100 | 96.8 | | + parse-args.js | 96.92 | 40 | 100 | 96.92 | 93,94,107,108 | + report.js | 96.69 | 86.36 | 100 | 96.69 |... 10,136,137,138 | + lib/commands | 43.02 | 60 | 16.67 | 43.02 | | + check-coverage.js | 21.67 | 100 | 0 | 21.67 |... 56,57,58,59,60 | + report.js | 92.31 | 50 | 50 | 92.31 | 9,10 | test/fixtures | 90.48 | 92.31 | 75 | 90.48 | | async.js | 100 | 100 | 100 | 100 | | multiple-spawn.js | 100 | 100 | 100 | 100 | | @@ -121,22 +121,22 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = ` ",--------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 96.55 | 85.51 | 93.1 | 96.55 | | - bin | 91.18 | 80 | 100 | 91.18 | | - c8.js | 91.18 | 80 | 100 | 91.18 | 25,26,27 | - lib | 97.51 | 82.35 | 100 | 97.51 | | - parse-args.js | 98.46 | 60 | 100 | 98.46 | 107,108 | - report.js | 96.69 | 91.67 | 100 | 96.69 |... 10,136,137,138 | - lib/commands | 100 | 88.24 | 100 | 100 | | - check-coverage.js | 100 | 91.67 | 100 | 100 | 17 | - report.js | 100 | 80 | 100 | 100 | 16 | +All files | 85.01 | 73.21 | 74.07 | 85.01 | | + bin | 83.78 | 50 | 100 | 83.78 | | + c8.js | 83.78 | 50 | 100 | 83.78 | 22,25,26,27,30,31 | + lib | 96.8 | 71.88 | 100 | 96.8 | | + parse-args.js | 96.92 | 40 | 100 | 96.92 | 93,94,107,108 | + report.js | 96.69 | 86.36 | 100 | 96.69 |... 10,136,137,138 | + lib/commands | 43.02 | 60 | 16.67 | 43.02 | | + check-coverage.js | 21.67 | 100 | 0 | 21.67 |... 56,57,58,59,60 | + report.js | 92.31 | 50 | 50 | 92.31 | 9,10 | test/fixtures | 90.48 | 92.31 | 75 | 90.48 | | async.js | 100 | 100 | 100 | 100 | | multiple-spawn.js | 100 | 100 | 100 | 100 | | normal.js | 75 | 66.67 | 33.33 | 75 | 14,15,16,18,19,20 | subprocess.js | 100 | 100 | 100 | 100 | | --------------------|----------|----------|----------|----------|-------------------| -,ERROR: Coverage for lines (96.55%) does not meet global threshold (101%) +,ERROR: Coverage for lines (85.01%) does not meet global threshold (101%) " `; @@ -158,3 +158,33 @@ All files | 83.33 | 85.71 | 66.67 | 83.33 | | -----------|----------|----------|----------|----------|-------------------| ," `; + +exports[`c8 supports exeternally set NODE_V8_COVERAGE 1`] = ` +",hey +i am a line of code +what +hey +what +hey +what +hey +--------------------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +--------------------|----------|----------|----------|----------|-------------------| +All files | 95.11 | 80.88 | 89.66 | 95.11 | | + bin | 86.49 | 71.43 | 100 | 86.49 | | + c8.js | 86.49 | 71.43 | 100 | 86.49 | 25,26,27,30,31 | + lib | 96.8 | 75.76 | 100 | 96.8 | | + parse-args.js | 98.46 | 54.55 | 100 | 98.46 | 107,108 | + report.js | 95.36 | 86.36 | 100 | 95.36 |... 10,136,137,138 | + lib/commands | 100 | 88.89 | 100 | 100 | | + check-coverage.js | 100 | 92.31 | 100 | 100 | 17 | + report.js | 100 | 80 | 100 | 100 | 16 | + test/fixtures | 82.61 | 90 | 62.5 | 82.61 | | + async.js | 100 | 100 | 100 | 100 | | + export.mjs | 71.43 | 100 | 50 | 71.43 | 2,3 | + import.mjs | 100 | 100 | 100 | 100 | | + normal.js | 75 | 66.67 | 33.33 | 75 | 14,15,16,18,19,20 | +--------------------|----------|----------|----------|----------|-------------------| +," +`; diff --git a/test/parse-args.js b/test/parse-args.js index 04f291944..7411a4788 100644 --- a/test/parse-args.js +++ b/test/parse-args.js @@ -21,6 +21,17 @@ describe('parse-args', () => { const argv = buildYargs().parse(hideInstrumenteeArgs()) const instrumenteeArgs = hideInstrumenterArgs(argv) instrumenteeArgs.should.eql(['my-app', '--help']) + argv.tempDirectory.endsWith('/coverage/tmp').should.be.equal(true) + }) + }) + + describe('parse-args with NODE_V8_COVERAGE already set', () => { + it('hides arguments passed to c8 bin', () => { + process.env.NODE_V8_COVERAGE = './coverage/tmp_' + process.argv = ['node', 'c8', '--foo=99', 'my-app', '--help'] + const argv = buildYargs().parse(hideInstrumenteeArgs()) + argv.tempDirectory.endsWith('/coverage/tmp_').should.be.equal(true) + process.env.NODE_V8_COVERAGE = '' }) }) })