Skip to content

Commit

Permalink
Escape Windows path separator in testPathPattern CLI arguments (#5054)
Browse files Browse the repository at this point in the history
* Add tests for current testPathPattern behavior

* Escape Windows path separator in testPathPattern CLI arg

This fixes #3793.

* Add PR to CHANGELOG.md

* Insert ';'
  • Loading branch information
seanpoulter authored and cpojer committed Dec 11, 2017
1 parent 3ea2e10 commit 6049858
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Fixes

* `[jest-config]` Escape Windows path separator in testPathPattern CLI arguments
([#5054](https://github.com/facebook/jest/pull/5054)
* `[jest-jasmine]` Register sourcemaps as node environment to improve performance with jsdom ([#5045](https://github.com/facebook/jest/pull/5045))
* `[pretty-format]` Do not call toJSON recursively
([#5044](https://github.com/facebook/jest/pull/5044))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ exports[`testMatch throws if testRegex and testMatch are both specified 1`] = `
<red> https://facebook.github.io/jest/docs/configuration.html</>
<red></>"
`;
exports[`testPathPattern <regexForTestFiles> ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;
exports[`testPathPattern --testPathPattern ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;
70 changes: 70 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,3 +1026,73 @@ describe('watchPlugins', () => {
]);
});
});

describe('testPathPattern', () => {
const initialOptions = {rootDir: '/root'};
const consoleLog = console.log;

function testWindowsPathSeparator(argv, expected) {
jest.resetModules();
jest.mock('path', () => require.requireActual('path').win32);
require('jest-resolve').findNodeModule = findNodeModule;

const {options} = require('../normalize').default(initialOptions, argv);
expect(options.testPathPattern).toBe(expected);
}

beforeEach(() => {
console.log = jest.fn();
});

afterEach(() => {
console.log = consoleLog;
});

it('defaults to empty', () => {
const {options} = normalize(initialOptions, {});
expect(options.testPathPattern).toBe('');
});

describe('--testPathPattern', () => {
it('uses testPathPattern if set', () => {
const {options} = normalize(initialOptions, {testPathPattern: 'a/b'});
expect(options.testPathPattern).toBe('a/b');
});

it('ignores invalid regular expressions and logs a warning', () => {
const {options} = normalize(initialOptions, {testPathPattern: 'a('});
expect(options.testPathPattern).toBe('');
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
});

it('escapes Windows path separators', () => {
testWindowsPathSeparator({testPathPattern: 'a\\b'}, 'a\\\\b');
});
});

describe('<regexForTestFiles>', () => {
it('uses <regexForTestFiles> if set', () => {
const {options} = normalize(initialOptions, {_: ['a/b']});
expect(options.testPathPattern).toBe('a/b');
});

it('ignores invalid regular expressions and logs a warning', () => {
const {options} = normalize(initialOptions, {_: ['a(']});
expect(options.testPathPattern).toBe('');
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
});

it('escapes Windows path separators', () => {
testWindowsPathSeparator({_: ['a\\b']}, 'a\\\\b');
});

it('joins multiple <regexForTestFiles> if set', () => {
const {options} = normalize(initialOptions, {_: ['a/b', 'c/d']});
expect(options.testPathPattern).toBe('a/b|c/d');
});

it('escapes Windows path separators in multiple args', () => {
testWindowsPathSeparator({_: ['a\\b', 'c\\d']}, 'a\\\\b|c\\\\d');
});
});
});
4 changes: 2 additions & 2 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ const normalizeReporters = (options: InitialOptions, basedir) => {
const buildTestPathPattern = (argv: Argv): string => {
if (argv.testPathPattern) {
if (validatePattern(argv.testPathPattern)) {
return argv.testPathPattern;
return replacePathSepForRegex(argv.testPathPattern);
} else {
showTestPathPatternError(argv.testPathPattern);
}
}

if (argv._ && argv._.length) {
const testPathPattern = argv._.join('|');
const testPathPattern = argv._.map(replacePathSepForRegex).join('|');

if (validatePattern(testPathPattern)) {
return testPathPattern;
Expand Down

0 comments on commit 6049858

Please sign in to comment.