-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix --testPathPattern escaping for '\\' on Windows (#5230)
PR #5054 introduced a regression when handling escaped Windows path separators in the `--testPathPattern`. The PR applied the same escaping as the "Watch Usage" prompt, which incorrectly escapes `path\\.*file` as `path\\\.*file`. This commit fixes the regular expression used in "Watch Usage" and the `--testPathPattern` CLI argument with unit tests.
- Loading branch information
1 parent
6896ee6
commit f3fce7b
Showing
4 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/jest-regex-util/src/__tests__/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`replacePathSepForRegex() posix should match the expected output from #5216 1`] = ` | ||
Array [ | ||
"jest-config\\\\/.*normalize", | ||
"jest-config/.*normalize", | ||
"jest-config\\\\.*normalize", | ||
"jest-config\\\\\\\\.*normalize", | ||
] | ||
`; | ||
|
||
exports[`replacePathSepForRegex() win32 should match the expected output from #5216 1`] = ` | ||
Array [ | ||
"jest-config\\\\\\\\\\\\\\\\.*normalize", | ||
"jest-config\\\\\\\\.*normalize", | ||
"jest-config\\\\.*normalize", | ||
"jest-config\\\\\\\\.*normalize", | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
jest.mock('path'); | ||
|
||
import {replacePathSepForRegex} from '../index'; | ||
import path from 'path'; | ||
|
||
describe('replacePathSepForRegex()', () => { | ||
const testPatternsFrom5216 = [ | ||
'jest-config\\/.*normalize', | ||
'jest-config/.*normalize', | ||
'jest-config\\.*normalize', | ||
'jest-config\\\\.*normalize', | ||
]; | ||
|
||
describe('posix', () => { | ||
beforeEach(() => (path.sep = '/')); | ||
|
||
it('should return the path', () => { | ||
const expected = {}; | ||
expect(replacePathSepForRegex(expected)).toBe(expected); | ||
}); | ||
|
||
// Confirming existing behavior; could be changed to improve cross-platform support | ||
it('should not replace Windows path separators', () => { | ||
expect(replacePathSepForRegex('a\\.*b')).toBe('a\\.*b'); | ||
expect(replacePathSepForRegex('a\\\\.*b')).toBe('a\\\\.*b'); | ||
}); | ||
|
||
// Bonus: Test cases from https://github.com/facebook/jest#5216 | ||
it('should match the expected output from #5216', () => { | ||
expect( | ||
testPatternsFrom5216.map(replacePathSepForRegex), | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('win32', () => { | ||
beforeEach(() => (path.sep = '\\')); | ||
|
||
it('should escape Windows path separators', () => { | ||
expect(replacePathSepForRegex('a\\b\\c')).toBe('a\\\\b\\\\c'); | ||
}); | ||
|
||
it('should replace POSIX path separators', () => { | ||
expect(replacePathSepForRegex('a/b/c')).toBe('a\\\\b\\\\c'); | ||
}); | ||
|
||
it('should not escape an escaped period', () => { | ||
expect(replacePathSepForRegex('a\\.dotfile')).toBe('a\\.dotfile'); | ||
expect(replacePathSepForRegex('a\\\\\\.dotfile')).toBe('a\\\\\\.dotfile'); | ||
}); | ||
|
||
it('should not escape an escaped Windows path separator', () => { | ||
expect(replacePathSepForRegex('a\\\\b')).toBe('a\\\\b'); | ||
expect(replacePathSepForRegex('a\\\\.dotfile')).toBe('a\\\\.dotfile'); | ||
}); | ||
|
||
// Confirming existing behavior; could be changed to improve cross-platform support | ||
it('should not replace escaped POSIX separators', () => { | ||
expect(replacePathSepForRegex('a\\/b')).toBe('a\\\\\\\\b'); | ||
}); | ||
|
||
// Bonus: Test cases from https://github.com/facebook/jest#5216 | ||
it('should match the expected output from #5216', () => { | ||
expect( | ||
testPatternsFrom5216.map(replacePathSepForRegex), | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters