test-exclude
Advanced tools
Comparing version 5.0.0 to 5.0.1
@@ -6,2 +6,10 @@ # Change Log | ||
<a name="5.0.1"></a> | ||
## [5.0.1](https://github.com/istanbuljs/istanbuljs/compare/test-exclude@5.0.0...test-exclude@5.0.1) (2018-12-25) | ||
**Note:** Version bump only for package test-exclude | ||
<a name="5.0.0"></a> | ||
@@ -8,0 +16,0 @@ # [5.0.0](https://github.com/istanbuljs/istanbuljs/compare/test-exclude@4.2.2...test-exclude@5.0.0) (2018-06-26) |
223
index.js
@@ -1,43 +0,45 @@ | ||
const arrify = require('arrify') | ||
const minimatch = require('minimatch') | ||
const path = require('path') | ||
const readPkgUp = require('read-pkg-up') | ||
const requireMainFilename = require('require-main-filename') | ||
const arrify = require('arrify'); | ||
const minimatch = require('minimatch'); | ||
const path = require('path'); | ||
const readPkgUp = require('read-pkg-up'); | ||
const requireMainFilename = require('require-main-filename'); | ||
function TestExclude (opts) { | ||
Object.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 | ||
}, opts) | ||
function TestExclude(opts) { | ||
Object.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 | ||
}, | ||
opts | ||
); | ||
if (typeof this.include === 'string') this.include = [this.include] | ||
if (typeof this.exclude === 'string') this.exclude = [this.exclude] | ||
if (typeof this.include === 'string') this.include = [this.include]; | ||
if (typeof this.exclude === 'string') this.exclude = [this.exclude]; | ||
if (!this.include && !this.exclude && this.configKey) { | ||
Object.assign(this, this.pkgConf(this.configKey, this.configPath)) | ||
} | ||
if (!this.include && !this.exclude && this.configKey) { | ||
Object.assign(this, this.pkgConf(this.configKey, this.configPath)); | ||
} | ||
if (!this.exclude || !Array.isArray(this.exclude)) { | ||
this.exclude = exportFunc.defaultExclude | ||
} | ||
if (!this.exclude || !Array.isArray(this.exclude)) { | ||
this.exclude = exportFunc.defaultExclude; | ||
} | ||
if (this.include && this.include.length > 0) { | ||
this.include = prepGlobPatterns(arrify(this.include)) | ||
} else { | ||
this.include = false | ||
} | ||
if (this.include && this.include.length > 0) { | ||
this.include = prepGlobPatterns(arrify(this.include)); | ||
} else { | ||
this.include = false; | ||
} | ||
if (this.exclude.indexOf('**/node_modules/**') === -1) { | ||
this.exclude.push('**/node_modules/**') | ||
} | ||
if (this.exclude.indexOf('**/node_modules/**') === -1) { | ||
this.exclude.push('**/node_modules/**'); | ||
} | ||
this.exclude = prepGlobPatterns( | ||
[].concat(arrify(this.exclude)) | ||
) | ||
this.exclude = prepGlobPatterns([].concat(arrify(this.exclude))); | ||
this.handleNegation() | ||
this.handleNegation(); | ||
} | ||
@@ -49,88 +51,99 @@ | ||
// move excluded include rules into this.excludes. | ||
TestExclude.prototype.handleNegation = function () { | ||
if (Array.isArray(this.include)) { | ||
const includeNegated = this.include.filter(function (e) { | ||
return e.charAt(0) === '!' | ||
}).map(function (e) { | ||
return e.slice(1) | ||
}) | ||
this.exclude.push.apply(this.exclude, prepGlobPatterns(includeNegated)) | ||
this.include = this.include.filter(function (e) { | ||
return e.charAt(0) !== '!' | ||
}) | ||
} | ||
TestExclude.prototype.handleNegation = function() { | ||
if (Array.isArray(this.include)) { | ||
const includeNegated = this.include | ||
.filter(function(e) { | ||
return e.charAt(0) === '!'; | ||
}) | ||
.map(function(e) { | ||
return e.slice(1); | ||
}); | ||
this.exclude.push.apply(this.exclude, prepGlobPatterns(includeNegated)); | ||
this.include = this.include.filter(function(e) { | ||
return e.charAt(0) !== '!'; | ||
}); | ||
} | ||
this.excludeNegated = this.exclude.filter(function (e) { | ||
return e.charAt(0) === '!' | ||
}).map(function (e) { | ||
return e.slice(1) | ||
}) | ||
this.exclude = this.exclude.filter(function (e) { | ||
return e.charAt(0) !== '!' | ||
}) | ||
this.excludeNegated = prepGlobPatterns(this.excludeNegated) | ||
} | ||
this.excludeNegated = this.exclude | ||
.filter(function(e) { | ||
return e.charAt(0) === '!'; | ||
}) | ||
.map(function(e) { | ||
return e.slice(1); | ||
}); | ||
this.exclude = this.exclude.filter(function(e) { | ||
return e.charAt(0) !== '!'; | ||
}); | ||
this.excludeNegated = prepGlobPatterns(this.excludeNegated); | ||
}; | ||
TestExclude.prototype.shouldInstrument = function (filename, relFile) { | ||
var pathToCheck = filename | ||
TestExclude.prototype.shouldInstrument = function(filename, relFile) { | ||
var pathToCheck = filename; | ||
if (this.relativePath) { | ||
relFile = relFile || path.relative(this.cwd, filename) | ||
if (this.relativePath) { | ||
relFile = relFile || path.relative(this.cwd, filename); | ||
// Don't instrument files that are outside of the current working directory. | ||
if (/^\.\./.test(path.relative(this.cwd, filename))) return false | ||
// 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 '.\'. | ||
} | ||
pathToCheck = relFile.replace(/^\.[\\/]/, ''); // remove leading './' or '.\'. | ||
} | ||
return ( | ||
!this.include || | ||
this.include.some(include => minimatch(pathToCheck, include, {dot: true}))) && | ||
(!this.exclude.some(exclude => minimatch(pathToCheck, exclude, {dot: true})) || | ||
this.excludeNegated.some(exclude => minimatch(pathToCheck, exclude, {dot: true}))) | ||
} | ||
return ( | ||
(!this.include || | ||
this.include.some(include => | ||
minimatch(pathToCheck, include, { dot: true }) | ||
)) && | ||
(!this.exclude.some(exclude => | ||
minimatch(pathToCheck, exclude, { dot: true }) | ||
) || | ||
this.excludeNegated.some(exclude => | ||
minimatch(pathToCheck, exclude, { dot: true }) | ||
)) | ||
); | ||
}; | ||
TestExclude.prototype.pkgConf = function (key, path) { | ||
const obj = readPkgUp.sync({ | ||
cwd: path || requireMainFilename(require) | ||
}) | ||
TestExclude.prototype.pkgConf = function(key, path) { | ||
const obj = readPkgUp.sync({ | ||
cwd: path || requireMainFilename(require) | ||
}); | ||
if (obj.pkg && obj.pkg[key] && typeof obj.pkg[key] === 'object') { | ||
this.configFound = true | ||
return obj.pkg[key] | ||
} else { | ||
return {} | ||
} | ||
} | ||
function prepGlobPatterns (patterns) { | ||
return patterns.reduce(function (result, pattern) { | ||
// Allow gitignore style of directory exclusion | ||
if (!/\/\*\*$/.test(pattern)) { | ||
result = result.concat(pattern.replace(/\/$/, '') + '/**') | ||
if (obj.pkg && obj.pkg[key] && typeof obj.pkg[key] === 'object') { | ||
this.configFound = true; | ||
return obj.pkg[key]; | ||
} else { | ||
return {}; | ||
} | ||
}; | ||
// Any rules of the form **/foo.js, should also match foo.js. | ||
if (/^\*\*\//.test(pattern)) { | ||
result = result.concat(pattern.replace(/^\*\*\//, '')) | ||
} | ||
function prepGlobPatterns(patterns) { | ||
return patterns.reduce(function(result, pattern) { | ||
// Allow gitignore style of directory exclusion | ||
if (!/\/\*\*$/.test(pattern)) { | ||
result = result.concat(pattern.replace(/\/$/, '') + '/**'); | ||
} | ||
return result.concat(pattern) | ||
}, []) | ||
} | ||
// Any rules of the form **/foo.js, should also match foo.js. | ||
if (/^\*\*\//.test(pattern)) { | ||
result = result.concat(pattern.replace(/^\*\*\//, '')); | ||
} | ||
var exportFunc = function (opts) { | ||
return new TestExclude(opts) | ||
return result.concat(pattern); | ||
}, []); | ||
} | ||
var exportFunc = function(opts) { | ||
return new TestExclude(opts); | ||
}; | ||
exportFunc.defaultExclude = [ | ||
'coverage/**', | ||
'packages/*/test/**', | ||
'test/**', | ||
'test{,-*}.js', | ||
'**/*{.,-}test.js', | ||
'**/__tests__/**', | ||
'**/node_modules/**' | ||
] | ||
'coverage/**', | ||
'packages/*/test/**', | ||
'test/**', | ||
'test{,-*}.js', | ||
'**/*{.,-}test.js', | ||
'**/__tests__/**', | ||
'**/node_modules/**' | ||
]; | ||
module.exports = exportFunc | ||
module.exports = exportFunc; |
{ | ||
"name": "test-exclude", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"description": "test for inclusion or exclusion of paths using pkg-conf and globs", | ||
@@ -10,3 +10,2 @@ "main": "index.js", | ||
"scripts": { | ||
"pretest": "standard", | ||
"test": "mocha" | ||
@@ -33,4 +32,3 @@ }, | ||
"chai": "^4.1.2", | ||
"mocha": "^5.2.0", | ||
"standard": "^11.0.1" | ||
"mocha": "^5.2.0" | ||
}, | ||
@@ -37,0 +35,0 @@ "dependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14979
2
125