Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { minimatch } = require('minimatch');
const { defaults } = require('@istanbuljs/schema');
const isOutsideDir = require('./is-outside-dir');

const minimatchOptions = { dot: true };
const patternToRegExp = pattern => minimatch.makeRe(pattern, minimatchOptions);

class TestExclude {
constructor(opts = {}) {
Object.assign(
Expand Down Expand Up @@ -50,6 +53,13 @@ class TestExclude {
this.exclude = prepGlobPatterns([].concat(this.exclude));

this.handleNegation();
this.compilePatterns();
}

compilePatterns() {
this.includeRegexps = this.include === false ? false : this.include.map(patternToRegExp);
this.excludeRegexps = this.exclude.map(patternToRegExp);
this.excludeNegatedRegexps = this.excludeNegated.map(patternToRegExp);
}

/* handle the special case of negative globs
Expand Down Expand Up @@ -94,11 +104,14 @@ class TestExclude {
pathToCheck = relFile.replace(/^\.[\\/]/, ''); // remove leading './' or '.\'.
}

const dot = { dot: true };
const matches = pattern => minimatch(pathToCheck, pattern, dot);
// minimatch.makeRe produces regexes that expect forward slashes;
// normalize any backslashes so the precompiled regex matches Windows paths.
const normalizedPath = pathToCheck.includes('\\') ? pathToCheck.split('\\').join('/') : pathToCheck;

const matches = re => re.test(normalizedPath);
return (
(!this.include || this.include.some(matches)) &&
(!this.exclude.some(matches) || this.excludeNegated.some(matches))
(!this.includeRegexps || this.includeRegexps.some(matches)) &&
(!this.excludeRegexps.some(matches) || this.excludeNegatedRegexps.some(matches))
);
}

Expand Down
15 changes: 15 additions & 0 deletions test/test-exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,18 @@ t.test('tolerates undefined exclude/include', t =>
yes: ['index.js']
})
);

// Patterns are precompiled to forward-slash regexes; verify Windows-style
// backslash paths still match. shouldInstrument accepts an explicit relFile,
// which is what we'd see on Windows.
t.test('matches Windows-style backslash paths against forward-slash globs', t => {
const e = new TestExclude({
include: ['src/**/*.js'],
exclude: ['src/**/*.spec.js']
});

strictEqual(e.shouldInstrument('src\\batman\\robin\\foo.js', 'src\\batman\\robin\\foo.js'), true);
strictEqual(e.shouldInstrument('src\\batman\\robin\\foo.spec.js', 'src\\batman\\robin\\foo.spec.js'), false);
strictEqual(e.shouldInstrument('src\\node_modules\\cat.js', 'src\\node_modules\\cat.js'), false);
t.end();
});