Skip to content

Commit

Permalink
fix: use Array#reduce and remove unneeded branch in prepGlobPatterns (#5
Browse files Browse the repository at this point in the history
)

since arrify is called wherever this function is used.
Also add extra test case for which more accurately reflects a real project.
  • Loading branch information
JaKXz authored and bcoe committed Jun 27, 2016
1 parent df4d88c commit c0f0f59
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
21 changes: 4 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,13 @@ TestExclude.prototype.pkgConf = function (key, path) {
}

function prepGlobPatterns (patterns) {
if (!patterns) return patterns

const result = []

function add (pattern) {
if (result.indexOf(pattern) === -1) {
result.push(pattern)
}
}

patterns.forEach(function (pattern) {
return patterns.reduce(function (result, pattern) {
// Allow gitignore style of directory exclusion
if (!/\/\*\*$/.test(pattern)) {
add(pattern.replace(/\/$/, '') + '/**')
result = result.concat(pattern.replace(/\/$/, '') + '/**')
}

add(pattern)
})

return result
return result.concat(pattern)
}, [])
}

module.exports = function (opts) {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/include-src-only/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"c": {
"include": [
"src/**/*.js"
]
}
}
13 changes: 13 additions & 0 deletions test/test-exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ describe('testExclude', function () {
e.configFound.should.equal(true)
})

it('should only instrument files that are included in subdirs', function () {
const e = exclude({
configPath: './test/fixtures/include-src-only',
configKey: 'c'
})
e.shouldInstrument('bar/baz.js').should.equal(false)
e.shouldInstrument('bad/file.js').should.equal(false)
e.shouldInstrument('foo.js').should.equal(false)

e.shouldInstrument('src/app.test.js').should.equal(false)
e.shouldInstrument('src/app.js').should.equal(true)
})

it('should not throw if a key is missing', function () {
var e = exclude({
configPath: './test/fixtures/include',
Expand Down

0 comments on commit c0f0f59

Please sign in to comment.