From ff01cd832a155494892b24c30c5a1c8f0169fd8e Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Wed, 29 Dec 2021 21:19:30 -0700 Subject: [PATCH] feat: add --extension option (#331) --- README.md | 3 +- index.d.ts | 1 + lib/commands/check-coverage.js | 1 + lib/commands/report.js | 1 + lib/parse-args.js | 7 +++ lib/report.js | 2 + package-lock.json | 2 +- test/fixtures/custom-ext.special | 24 +++++++++ test/integration.js | 16 ++++++ test/integration.js.snap | 19 +++++++ test/integration.js_10.snap | 91 +++++++++++++++++++------------- 11 files changed, 129 insertions(+), 38 deletions(-) create mode 100644 test/fixtures/custom-ext.special diff --git a/README.md b/README.md index f20523e7..58748b83 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,8 @@ Here is a list of common options. Run `c8 --help` for the full list and document | `--all` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `boolean` | `false` | | `--src` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array` | `[process.cwd()]`| | `-n`, `--include` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array` | `[]` (include all files) | -| `-x`, `--exclude` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array` | [list](https://github.com/istanbuljs/schema/blob/main/default-exclude.js) | +| `-x`, `--exclude` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array` | [list](https://github.com/istanbuljs/schema/blob/master/default-exclude.js) | +| `-e`, `--extension` | only files matching these extensions will show coverage | `string | Array` | [list](https://github.com/istanbuljs/schema/blob/master/default-extension.js) | | `--skip-full` | do not show files with 100% statement, branch, and function coverage | `boolean` | `false` | | `--check-coverage` | check whether coverage is within thresholds provided | `boolean` | `false` | | `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` | diff --git a/index.d.ts b/index.d.ts index fc93511c..b6610188 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,6 +3,7 @@ export type Watermark = [number, number]; export declare class Report { constructor(opts: { exclude?: string | string[], + extension?: string | string[], excludeAfterRemap?: boolean, include?: string | string[], reporter: string[], diff --git a/lib/commands/check-coverage.js b/lib/commands/check-coverage.js index 24adaadf..e2e73cc9 100644 --- a/lib/commands/check-coverage.js +++ b/lib/commands/check-coverage.js @@ -22,6 +22,7 @@ exports.handler = function (argv) { const report = Report({ include: argv.include, exclude: argv.exclude, + extension: argv.extension, reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter], reportsDirectory: argv['reports-dir'], tempDirectory: argv.tempDirectory, diff --git a/lib/commands/report.js b/lib/commands/report.js index 568a9428..cacf88b4 100644 --- a/lib/commands/report.js +++ b/lib/commands/report.js @@ -21,6 +21,7 @@ exports.outputReport = async function (argv) { const report = Report({ include: argv.include, exclude: argv.exclude, + extension: argv.extension, excludeAfterRemap: argv.excludeAfterRemap, reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter], reportsDirectory: argv['reports-dir'], diff --git a/lib/parse-args.js b/lib/parse-args.js index 72eb7b7d..9e5ceade 100644 --- a/lib/parse-args.js +++ b/lib/parse-args.js @@ -1,4 +1,5 @@ const defaultExclude = require('@istanbuljs/schema/default-exclude') +const defaultExtension = require('@istanbuljs/schema/default-extension') const findUp = require('find-up') const { readFileSync } = require('fs') const Yargs = require('yargs/yargs') @@ -58,6 +59,12 @@ function buildYargs (withCommands = false) { group: 'Reporting options', describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)' }) + .option('extension', { + alias: 'e', + default: defaultExtension, + group: 'Reporting options', + describe: 'a list of specific file extensions that should be covered' + }) .option('exclude-after-remap', { alias: 'a', type: 'boolean', diff --git a/lib/report.js b/lib/report.js index 9c5835ff..d3c8806d 100644 --- a/lib/report.js +++ b/lib/report.js @@ -15,6 +15,7 @@ const debuglog = util.debuglog('c8') class Report { constructor ({ exclude, + extension, excludeAfterRemap, include, reporter, @@ -38,6 +39,7 @@ class Report { this.exclude = new Exclude({ exclude: exclude, include: include, + extension: extension, relativePath: !allowExternal, excludeNodeModules: excludeNodeModules }) diff --git a/package-lock.json b/package-lock.json index b97dc913..3bf245ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7032,4 +7032,4 @@ "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==" } } -} +} \ No newline at end of file diff --git a/test/fixtures/custom-ext.special b/test/fixtures/custom-ext.special new file mode 100644 index 00000000..ad544629 --- /dev/null +++ b/test/fixtures/custom-ext.special @@ -0,0 +1,24 @@ +require('./async') + +console.info('i am a line of code') + +function apple (awesome) { + if (false || true) { + console.info('what') + } + if (true || false) { + console.log('hey') + } +} + +function missed () { + +} + +function missed2 () { + +} + +apple() +apple() +apple() diff --git a/test/integration.js b/test/integration.js index 9e6eac31..2e06db34 100644 --- a/test/integration.js +++ b/test/integration.js @@ -630,4 +630,20 @@ describe('c8', () => { output.toString('utf8').should.matchSnapshot() }) }) + + describe('--extension', () => { + it('includes coverage when extensions specified', () => { + const { output } = spawnSync(nodePath, [ + c8Path, + '--exclude="test/*.js"', + '--extension=.js', + '--extension=.special', + '--temp-directory=tmp/extension', + '--clean=false', + nodePath, + require.resolve('./fixtures/custom-ext.special') + ]) + output.toString('utf8').should.matchSnapshot() + }) + }) }) diff --git a/test/integration.js.snap b/test/integration.js.snap index 9b7fe463..c3616a41 100644 --- a/test/integration.js.snap +++ b/test/integration.js.snap @@ -125,6 +125,25 @@ All files | 100 | 100 | 100 | 100 | ," `; +exports[`c8 --extension includes coverage when extensions specified 1`] = ` +",hey +i am a line of code +what +hey +what +hey +what +hey +--------------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +--------------------|---------|----------|---------|---------|------------------- +All files | 83.33 | 85.71 | 60 | 83.33 | + async.js | 100 | 100 | 100 | 100 | + custom-ext.special | 75 | 66.66 | 33.33 | 75 | 14-16,18-20 +--------------------|---------|----------|---------|---------|------------------- +," +`; + exports[`c8 ESM Modules collects coverage for ESM modules 1`] = ` ",bar foo ------------|---------|----------|---------|---------|------------------- diff --git a/test/integration.js_10.snap b/test/integration.js_10.snap index ad3a0e01..5e69d488 100644 --- a/test/integration.js_10.snap +++ b/test/integration.js_10.snap @@ -125,6 +125,25 @@ All files | 100 | 100 | 100 | 100 | ," `; +exports[`c8 --extension includes coverage when extensions specified 1`] = ` +",hey +i am a line of code +what +hey +what +hey +what +hey +--------------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +--------------------|---------|----------|---------|---------|------------------- +All files | 83.33 | 85.71 | 66.66 | 83.33 | + async.js | 100 | 100 | 100 | 100 | + custom-ext.special | 75 | 66.66 | 33.33 | 75 | 14-16,18-20 +--------------------|---------|----------|---------|---------|------------------- +," +`; + exports[`c8 ESM Modules collects coverage for ESM modules 1`] = ` ",----------|---------|----------|---------|---------|------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s @@ -175,24 +194,24 @@ hey --------------------------|---------|----------|---------|---------|-------------------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s --------------------------|---------|----------|---------|---------|-------------------------------- -All files | 73.37 | 59.03 | 62.5 | 73.37 | +All files | 73.6 | 59.03 | 62.5 | 73.6 | bin | 78.84 | 60 | 66.66 | 78.84 | c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51 - lib | 77.88 | 54.38 | 72 | 77.88 | + lib | 78.19 | 54.38 | 72 | 78.19 | is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9 - parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185 - report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311 + parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192 + report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313 source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98 - lib/commands | 41.28 | 66.66 | 16.66 | 41.28 | - check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69 - report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20 + lib/commands | 41.44 | 66.66 | 16.66 | 41.44 | + check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70 + report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20 test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 | async.js | 100 | 100 | 100 | 100 | normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20 --------------------------|---------|----------|---------|---------|-------------------------------- -,ERROR: Coverage for lines (73.37%) does not meet global threshold (101%) +,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%) ERROR: Coverage for branches (59.03%) does not meet global threshold (82%) -ERROR: Coverage for statements (73.37%) does not meet global threshold (95%) +ERROR: Coverage for statements (73.6%) does not meet global threshold (95%) " `; @@ -200,19 +219,19 @@ exports[`c8 check-coverage allows threshold to be applied on per-file basis 1`] ",,ERROR: Coverage for lines (78.84%) does not meet threshold (101%) for bin/c8.js ERROR: Coverage for branches (60%) does not meet threshold (82%) for bin/c8.js ERROR: Coverage for statements (78.84%) does not meet threshold (95%) for bin/c8.js -ERROR: Coverage for lines (18.84%) does not meet threshold (101%) for lib/commands/check-coverage.js -ERROR: Coverage for statements (18.84%) does not meet threshold (95%) for lib/commands/check-coverage.js -ERROR: Coverage for lines (80%) does not meet threshold (101%) for lib/commands/report.js +ERROR: Coverage for lines (18.57%) does not meet threshold (101%) for lib/commands/check-coverage.js +ERROR: Coverage for statements (18.57%) does not meet threshold (95%) for lib/commands/check-coverage.js +ERROR: Coverage for lines (80.48%) does not meet threshold (101%) for lib/commands/report.js ERROR: Coverage for branches (62.5%) does not meet threshold (82%) for lib/commands/report.js -ERROR: Coverage for statements (80%) does not meet threshold (95%) for lib/commands/report.js +ERROR: Coverage for statements (80.48%) does not meet threshold (95%) for lib/commands/report.js ERROR: Coverage for lines (90%) does not meet threshold (101%) for lib/is-cjs-esm-bridge.js ERROR: Coverage for branches (25%) does not meet threshold (82%) for lib/is-cjs-esm-bridge.js ERROR: Coverage for statements (90%) does not meet threshold (95%) for lib/is-cjs-esm-bridge.js -ERROR: Coverage for lines (97.1%) does not meet threshold (101%) for lib/parse-args.js +ERROR: Coverage for lines (97.19%) does not meet threshold (101%) for lib/parse-args.js ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/parse-args.js -ERROR: Coverage for lines (75.31%) does not meet threshold (101%) for lib/report.js +ERROR: Coverage for lines (75.47%) does not meet threshold (101%) for lib/report.js ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/report.js -ERROR: Coverage for statements (75.31%) does not meet threshold (95%) for lib/report.js +ERROR: Coverage for statements (75.47%) does not meet threshold (95%) for lib/report.js ERROR: Coverage for lines (45%) does not meet threshold (101%) for lib/source-map-from-file.js ERROR: Coverage for statements (45%) does not meet threshold (95%) for lib/source-map-from-file.js ERROR: Coverage for lines (100%) does not meet threshold (101%) for test/fixtures/async.js @@ -223,19 +242,19 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt `; exports[`c8 check-coverage check-coverage command with --100 1`] = ` -",,ERROR: Coverage for lines (77.22%) does not meet global threshold (100%) +",,ERROR: Coverage for lines (77.4%) does not meet global threshold (100%) ERROR: Coverage for functions (66.66%) does not meet global threshold (100%) ERROR: Coverage for branches (62.35%) does not meet global threshold (100%) -ERROR: Coverage for statements (77.22%) does not meet global threshold (100%) +ERROR: Coverage for statements (77.4%) does not meet global threshold (100%) " `; 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 (73.37%) does not meet global threshold (101%) +",,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%) ERROR: Coverage for branches (59.03%) does not meet global threshold (82%) -ERROR: Coverage for statements (73.37%) does not meet global threshold (95%) +ERROR: Coverage for statements (73.6%) does not meet global threshold (95%) " `; @@ -318,17 +337,17 @@ exports[`c8 report generates report from existing temporary files 1`] = ` ",--------------------------|---------|----------|---------|---------|-------------------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s --------------------------|---------|----------|---------|---------|-------------------------------- -All files | 73.37 | 59.03 | 62.5 | 73.37 | +All files | 73.6 | 59.03 | 62.5 | 73.6 | bin | 78.84 | 60 | 66.66 | 78.84 | c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51 - lib | 77.88 | 54.38 | 72 | 77.88 | + lib | 78.19 | 54.38 | 72 | 78.19 | is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9 - parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185 - report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311 + parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192 + report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313 source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98 - lib/commands | 41.28 | 66.66 | 16.66 | 41.28 | - check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69 - report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20 + lib/commands | 41.44 | 66.66 | 16.66 | 41.44 | + check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70 + report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20 test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 | async.js | 100 | 100 | 100 | 100 | normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20 @@ -340,24 +359,24 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = ` ",--------------------------|---------|----------|---------|---------|-------------------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s --------------------------|---------|----------|---------|---------|-------------------------------- -All files | 73.37 | 59.03 | 62.5 | 73.37 | +All files | 73.6 | 59.03 | 62.5 | 73.6 | bin | 78.84 | 60 | 66.66 | 78.84 | c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51 - lib | 77.88 | 54.38 | 72 | 77.88 | + lib | 78.19 | 54.38 | 72 | 78.19 | is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9 - parse-args.js | 97.1 | 58.33 | 100 | 97.1 | 148-149,170-171,184-185 - report.js | 75.31 | 58.33 | 78.57 | 75.31 | ...249,276-277,283-285,306-311 + parse-args.js | 97.19 | 58.33 | 100 | 97.19 | 155-156,177-178,191-192 + report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313 source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98 - lib/commands | 41.28 | 66.66 | 16.66 | 41.28 | - check-coverage.js | 18.84 | 100 | 0 | 18.84 | 9-11,14-35,38-52,54-69 - report.js | 80 | 62.5 | 50 | 80 | 9-10,15-20 + lib/commands | 41.44 | 66.66 | 16.66 | 41.44 | + check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70 + report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20 test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 | async.js | 100 | 100 | 100 | 100 | normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20 --------------------------|---------|----------|---------|---------|-------------------------------- -,ERROR: Coverage for lines (73.37%) does not meet global threshold (101%) +,ERROR: Coverage for lines (73.6%) does not meet global threshold (101%) ERROR: Coverage for branches (59.03%) does not meet global threshold (82%) -ERROR: Coverage for statements (73.37%) does not meet global threshold (95%) +ERROR: Coverage for statements (73.6%) does not meet global threshold (95%) " `;