diff --git a/lib/report.js b/lib/report.js index d3c8806d..92b86e2f 100644 --- a/lib/report.js +++ b/lib/report.js @@ -127,6 +127,15 @@ class Report { map.merge(converter.toIstanbul()) } } + + if (!this.all) { + for (const fileURL in map.data) { + if (!this.exclude.shouldInstrument(fileURL)) { + delete map.data[fileURL] + } + } + } + this._allCoverageFiles = map return this._allCoverageFiles } diff --git a/test/fixtures/bundle.js b/test/fixtures/bundle.js new file mode 100644 index 00000000..76086096 --- /dev/null +++ b/test/fixtures/bundle.js @@ -0,0 +1,58 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); +var __commonJS = (cb, mod) => function __require() { + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; +}; +var __reExport = (target, module2, copyDefault, desc) => { + if (module2 && typeof module2 === "object" || typeof module2 === "function") { + for (let key of __getOwnPropNames(module2)) + if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) + __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); + } + return target; +}; +var __toESM = (module2, isNodeMode) => { + return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); +}; + +// node_modules/path-exists/index.js +var require_path_exists = __commonJS({ + "node_modules/path-exists/index.js"(exports, module2) { + "use strict"; + var fs = require("fs"); + var { promisify } = require("util"); + var pAccess = promisify(fs.access); + module2.exports = async (path) => { + try { + await pAccess(path); + return true; + } catch (_) { + return false; + } + }; + module2.exports.sync = (path) => { + try { + fs.accessSync(path); + return true; + } catch (_) { + return false; + } + }; + } +}); + +// index.js +var { sync } = require_path_exists(); +try { + sync({ + cwd: require.resolve("aaaa") + }); +} catch (err) { + console.log("reachable"); +} +//# sourceMappingURL=bundle.js.map diff --git a/test/fixtures/bundle.js.map b/test/fixtures/bundle.js.map new file mode 100644 index 00000000..0e96fd13 --- /dev/null +++ b/test/fixtures/bundle.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["node_modules/path-exists/index.js", "index.js"], + "sourcesContent": ["'use strict';\nconst fs = require('fs');\nconst {promisify} = require('util');\n\nconst pAccess = promisify(fs.access);\n\nmodule.exports = async path => {\n\ttry {\n\t\tawait pAccess(path);\n\t\treturn true;\n\t} catch (_) {\n\t\treturn false;\n\t}\n};\n\nmodule.exports.sync = path => {\n\ttry {\n\t\tfs.accessSync(path);\n\t\treturn true;\n\t} catch (_) {\n\t\treturn false;\n\t}\n};\n", "const {sync} = require('path-exists')\n\ntry {\n\tsync({\n\t\tcwd: require.resolve('aaaa')\n\t})\n} catch (err) {\n\tconsole.log('reachable')\n}\n"], + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AACA,QAAM,KAAK,QAAQ;AACnB,QAAM,EAAC,cAAa,QAAQ;AAE5B,QAAM,UAAU,UAAU,GAAG;AAE7B,YAAO,UAAU,OAAM,SAAQ;AAC9B,UAAI;AACH,cAAM,QAAQ;AACd,eAAO;AAAA,eACC,GAAP;AACD,eAAO;AAAA;AAAA;AAIT,YAAO,QAAQ,OAAO,UAAQ;AAC7B,UAAI;AACH,WAAG,WAAW;AACd,eAAO;AAAA,eACC,GAAP;AACD,eAAO;AAAA;AAAA;AAAA;AAAA;;;ACpBT,IAAM,EAAC,SAAQ;AAEf,IAAI;AACH,OAAK;AAAA,IACJ,KAAqB;AAAA;AAAA,SAEd,KAAP;AACD,UAAQ,IAAI;AAAA;", + "names": [] +} diff --git a/test/integration.js b/test/integration.js index 2e06db34..19389326 100644 --- a/test/integration.js +++ b/test/integration.js @@ -646,4 +646,16 @@ describe('c8', () => { output.toString('utf8').should.matchSnapshot() }) }) + + it('exclude unexpected file in result', () => { + const { output } = spawnSync(nodePath, [ + c8Path, + '--exclude="test/*.js"', + '--temp-directory=tmp/bundle', + '--clean=false', + nodePath, + require.resolve('./fixtures/bundle.js') + ]) + output.toString('utf8').should.matchSnapshot() + }) }) diff --git a/test/integration.js.snap b/test/integration.js.snap index c3616a41..bcc25c02 100644 --- a/test/integration.js.snap +++ b/test/integration.js.snap @@ -330,6 +330,17 @@ All files | 75 | 50 | 100 | 75 | ," `; +exports[`c8 exclude unexpected file in result 1`] = ` +",reachable +----------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 100 | 100 | + index.js | 100 | 100 | 100 | 100 | +----------|---------|----------|---------|---------|------------------- +," +`; + exports[`c8 merges reports from subprocesses together 1`] = ` ",first