Skip to content

Commit 6049858

Browse files
seanpoultercpojer
authored andcommitted
Escape Windows path separator in testPathPattern CLI arguments (#5054)
* Add tests for current testPathPattern behavior * Escape Windows path separator in testPathPattern CLI arg This fixes #3793. * Add PR to CHANGELOG.md * Insert ';'
1 parent 3ea2e10 commit 6049858

4 files changed

Lines changed: 78 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Fixes
44

5+
* `[jest-config]` Escape Windows path separator in testPathPattern CLI arguments
6+
([#5054](https://github.com/facebook/jest/pull/5054)
57
* `[jest-jasmine]` Register sourcemaps as node environment to improve performance with jsdom ([#5045](https://github.com/facebook/jest/pull/5045))
68
* `[pretty-format]` Do not call toJSON recursively
79
([#5044](https://github.com/facebook/jest/pull/5044))

packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ exports[`testMatch throws if testRegex and testMatch are both specified 1`] = `
6767
<red> https://facebook.github.io/jest/docs/configuration.html</>
6868
<red></>"
6969
`;
70+
71+
exports[`testPathPattern <regexForTestFiles> ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;
72+
73+
exports[`testPathPattern --testPathPattern ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;

packages/jest-config/src/__tests__/normalize.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,3 +1026,73 @@ describe('watchPlugins', () => {
10261026
]);
10271027
});
10281028
});
1029+
1030+
describe('testPathPattern', () => {
1031+
const initialOptions = {rootDir: '/root'};
1032+
const consoleLog = console.log;
1033+
1034+
function testWindowsPathSeparator(argv, expected) {
1035+
jest.resetModules();
1036+
jest.mock('path', () => require.requireActual('path').win32);
1037+
require('jest-resolve').findNodeModule = findNodeModule;
1038+
1039+
const {options} = require('../normalize').default(initialOptions, argv);
1040+
expect(options.testPathPattern).toBe(expected);
1041+
}
1042+
1043+
beforeEach(() => {
1044+
console.log = jest.fn();
1045+
});
1046+
1047+
afterEach(() => {
1048+
console.log = consoleLog;
1049+
});
1050+
1051+
it('defaults to empty', () => {
1052+
const {options} = normalize(initialOptions, {});
1053+
expect(options.testPathPattern).toBe('');
1054+
});
1055+
1056+
describe('--testPathPattern', () => {
1057+
it('uses testPathPattern if set', () => {
1058+
const {options} = normalize(initialOptions, {testPathPattern: 'a/b'});
1059+
expect(options.testPathPattern).toBe('a/b');
1060+
});
1061+
1062+
it('ignores invalid regular expressions and logs a warning', () => {
1063+
const {options} = normalize(initialOptions, {testPathPattern: 'a('});
1064+
expect(options.testPathPattern).toBe('');
1065+
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
1066+
});
1067+
1068+
it('escapes Windows path separators', () => {
1069+
testWindowsPathSeparator({testPathPattern: 'a\\b'}, 'a\\\\b');
1070+
});
1071+
});
1072+
1073+
describe('<regexForTestFiles>', () => {
1074+
it('uses <regexForTestFiles> if set', () => {
1075+
const {options} = normalize(initialOptions, {_: ['a/b']});
1076+
expect(options.testPathPattern).toBe('a/b');
1077+
});
1078+
1079+
it('ignores invalid regular expressions and logs a warning', () => {
1080+
const {options} = normalize(initialOptions, {_: ['a(']});
1081+
expect(options.testPathPattern).toBe('');
1082+
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
1083+
});
1084+
1085+
it('escapes Windows path separators', () => {
1086+
testWindowsPathSeparator({_: ['a\\b']}, 'a\\\\b');
1087+
});
1088+
1089+
it('joins multiple <regexForTestFiles> if set', () => {
1090+
const {options} = normalize(initialOptions, {_: ['a/b', 'c/d']});
1091+
expect(options.testPathPattern).toBe('a/b|c/d');
1092+
});
1093+
1094+
it('escapes Windows path separators in multiple args', () => {
1095+
testWindowsPathSeparator({_: ['a\\b', 'c\\d']}, 'a\\\\b|c\\\\d');
1096+
});
1097+
});
1098+
});

packages/jest-config/src/normalize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ const normalizeReporters = (options: InitialOptions, basedir) => {
281281
const buildTestPathPattern = (argv: Argv): string => {
282282
if (argv.testPathPattern) {
283283
if (validatePattern(argv.testPathPattern)) {
284-
return argv.testPathPattern;
284+
return replacePathSepForRegex(argv.testPathPattern);
285285
} else {
286286
showTestPathPatternError(argv.testPathPattern);
287287
}
288288
}
289289

290290
if (argv._ && argv._.length) {
291-
const testPathPattern = argv._.join('|');
291+
const testPathPattern = argv._.map(replacePathSepForRegex).join('|');
292292

293293
if (validatePattern(testPathPattern)) {
294294
return testPathPattern;

0 commit comments

Comments
 (0)