Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure glob and minimatch are case insenitive on win32 #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules/*
coverage
.nyc_output
.idea
5 changes: 5 additions & 0 deletions extension-matcher-posix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use-strict'

module.exports = (filename) => {
return ext => filename.endsWith(ext);
};
5 changes: 5 additions & 0 deletions extension-matcher-win32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

module.exports = (filename) => {
return ext => filename.toLowerCase().endsWith(ext.toLowerCase());
};
8 changes: 8 additions & 0 deletions extension-matcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

if (process.platform === 'win32') {
module.exports = require("./extension-matcher-win32");
} else {
module.exports = require("./extension-matcher-posix");
}

13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const { promisify } = require('util');
const glob = promisify(require('glob'));
const minimatch = require('minimatch');
const { defaults } = require('@istanbuljs/schema');
const isOutsideDir = require('./is-outside-dir');
const { isOutsideDir, minimatchOptions } = require('./is-outside-dir');
const extensionMatcher = require("./extension-matcher");


class TestExclude {
constructor(opts = {}) {
Expand Down Expand Up @@ -77,7 +79,7 @@ class TestExclude {
shouldInstrument(filename, relFile) {
if (
this.extension &&
!this.extension.some(ext => filename.endsWith(ext))
!this.extension.some(extensionMatcher(filename))
) {
return false;
}
Expand All @@ -95,8 +97,7 @@ class TestExclude {
pathToCheck = relFile.replace(/^\.[\\/]/, ''); // remove leading './' or '.\'.
}

const dot = { dot: true };
const matches = pattern => minimatch(pathToCheck, pattern, dot);
const matches = pattern => minimatch(pathToCheck, pattern, minimatchOptions);
return (
(!this.include || this.include.some(matches)) &&
(!this.exclude.some(matches) || this.excludeNegated.some(matches))
Expand All @@ -105,7 +106,7 @@ class TestExclude {

globSync(cwd = this.cwd) {
const globPatterns = getExtensionPattern(this.extension || []);
const globOptions = { cwd, nodir: true, dot: true };
const globOptions = Object.assign({ cwd, nodir: true }, minimatchOptions);
/* If we don't have any excludeNegated then we can optimize glob by telling
* it to not iterate into unwanted directory trees (like node_modules). */
if (this.excludeNegated.length === 0) {
Expand All @@ -119,7 +120,7 @@ class TestExclude {

async glob(cwd = this.cwd) {
const globPatterns = getExtensionPattern(this.extension || []);
const globOptions = { cwd, nodir: true, dot: true };
const globOptions = Object.assign({ cwd, nodir: true }, minimatchOptions);
/* If we don't have any excludeNegated then we can optimize glob by telling
* it to not iterate into unwanted directory trees (like node_modules). */
if (this.excludeNegated.length === 0) {
Expand Down
9 changes: 7 additions & 2 deletions is-outside-dir-posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const path = require('path');

module.exports = function(dir, filename) {
return /^\.\./.test(path.relative(dir, filename));
module.exports = {
isOutsideDir(dir, filename) {
return /^\.\./.test(path.relative(dir, filename));
},
minimatchOptions: {
dot: true
}
};
15 changes: 10 additions & 5 deletions is-outside-dir-win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

const path = require('path');
const minimatch = require('minimatch');

const dot = { dot: true };

module.exports = function(dir, filename) {
return !minimatch(path.resolve(dir, filename), path.join(dir, '**'), dot);
const minimatchOptions = {
dot: true,
nocase: true // win32 should be case insensitive matches
};

module.exports = {
isOutsideDir(dir, filename) {
return !minimatch(path.resolve(dir, filename), path.join(dir, '**'), minimatchOptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, this still seems to the wrong result for me on Windows when dir is relative ... I think if we want to resolve one path, we should also do so for the other?

Suggested change
return !minimatch(path.resolve(dir, filename), path.join(dir, '**'), minimatchOptions)
return !minimatch(path.resolve(dir, filename), path.resolve(dir, '**'), minimatchOptions)

},
minimatchOptions
}
4 changes: 3 additions & 1 deletion nyc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = {
exclude: [
...defaultExclude,
'is-outside-dir.js',
isWindows ? 'is-outside-dir-posix.js' : 'is-outside-dir-win32.js'
'extension-matcher.js',
isWindows ? 'is-outside-dir-posix.js' : 'is-outside-dir-win32.js',
isWindows ? 'extension-matcher-posix.js' : 'extension-matcher-win32.js'
]
};
7 changes: 7 additions & 0 deletions tap-snapshots/test-glob.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ Array [
]
`

exports[`test/glob.js TAP handles case insensitive matches on windows > must match snapshot 1`] = `
Array [
"file1.js",
"file2.js",
]
`

exports[`test/glob.js TAP should exclude the node_modules folder by default > absolute constructor cwd 1`] = `
Array [
"file1.js",
Expand Down
12 changes: 12 additions & 0 deletions test/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ t.test('allows negated include patterns', t =>
}
})
);

if (process.platform === 'win32') {
t.test('handles case insensitive matches on windows', t =>
testHelper(t, {
options: {
cwd,
extension: extension.toUpperCase(),
include: ['file1.js', 'FILE2.js']
}
})
);
}
20 changes: 20 additions & 0 deletions test/test-exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ if (process.platform === 'win32') {
no: ['D:\\project\\foo.js']
})
);

t.test('should handle case insensitive drives on win32', t =>
testHelper(t, {
options: {
cwd: 'C:\\project'
},
yes: ['c:\\project\\foo.js']
})
);

t.test('should handle case insensitive paths when on win32', t =>
testHelper(t, {
options: {
exclude: ['foo.js'],
include: ['boo.js']
},
no: ['FOO.js'],
yes: ['BOO.js']
})
);
}

t.test('can instrument files outside cwd if relativePath=false', t =>
Expand Down