Skip to content

Commit

Permalink
feat: add possibility to filter coverage maps when running reports po…
Browse files Browse the repository at this point in the history
…st-hoc (#24)
  • Loading branch information
schutm authored and bcoe committed Apr 8, 2017
1 parent b60a4ac commit e1c99d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 14 additions & 0 deletions packages/istanbul-lib-coverage/lib/coverage-map.js
Expand Up @@ -57,6 +57,20 @@ CoverageMap.prototype.merge = function (obj) {
}
});
};
/**
* filter the coveragemap based on the callback provided
* @param {Function (filename)} callback - Returns true if the path
* should be included in the coveragemap. False if it should be
* removed.
*/
CoverageMap.prototype.filter = function (callback) {
var that = this;
Object.keys(that.data).forEach(function (k) {
if (!callback(k)) {
delete that.data[k];
}
});
};
/**
* returns a JSON-serializable POJO for this coverage map
* @returns {Object}
Expand Down
16 changes: 11 additions & 5 deletions packages/test-exclude/index.js
Expand Up @@ -9,6 +9,7 @@ function TestExclude (opts) {
assign(this, {
cwd: process.cwd(),
include: false,
relativePath: true,
configKey: null, // the key to load config from in package.json.
configPath: null, // optionally override requireMainFilename.
configFound: false
Expand Down Expand Up @@ -55,13 +56,18 @@ TestExclude.prototype.removeNegatedModuleExclude = function () {
}

TestExclude.prototype.shouldInstrument = function (filename, relFile) {
relFile = relFile || path.relative(this.cwd, filename)
var pathToCheck = filename

// Don't instrument files that are outside of the current working directory.
if (/^\.\./.test(path.relative(this.cwd, filename))) return false
if (this.relativePath) {
relFile = relFile || path.relative(this.cwd, filename)

relFile = relFile.replace(/^\.[\\/]/, '') // remove leading './' or '.\'.
return (!this.include || micromatch.any(relFile, this.include, {dotfiles: true})) && !micromatch.any(relFile, this.exclude, {dotfiles: true})
// Don't instrument files that are outside of the current working directory.
if (/^\.\./.test(path.relative(this.cwd, filename))) return false

pathToCheck = relFile.replace(/^\.[\\/]/, '') // remove leading './' or '.\'.
}

return (!this.include || micromatch.any(pathToCheck, this.include, {dotfiles: true})) && !micromatch.any(pathToCheck, this.exclude, {dotfiles: true})
}

TestExclude.prototype.pkgConf = function (key, path) {
Expand Down

0 comments on commit e1c99d6

Please sign in to comment.