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: normalize path separator for rootDir replacement #7814

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-config]` fix: normalize path separator for rootDir replacement ([#7814](https://github.com/facebook/jest/pull/7814))

### Chore & Maintenance

- `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808))
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"jest-validate": "^24.0.0",
"micromatch": "^3.1.10",
"pretty-format": "^24.0.0",
"realpath-native": "^1.0.2"
"realpath-native": "^1.0.2",
"slash": "^2.0.0"
},
"devDependencies": {
"@types/babel__core": "^7.0.4",
Expand Down
57 changes: 44 additions & 13 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import crypto from 'crypto';
import path from 'path';
import {escapeStrForRegex} from 'jest-regex-util';
import normalize from '../normalize';
import Defaults from '../Defaults';

import {DEFAULT_JS_PATTERN} from '../constants';
Expand All @@ -18,6 +17,8 @@ const DEFAULT_CSS_PATTERN = '^.+\\.(css)$';

jest.mock('jest-resolve').mock('path', () => jest.requireActual('path').posix);

let normalize;

let root;
let expectedPathFooBar;
let expectedPathFooQux;
Expand All @@ -39,6 +40,7 @@ function joinForPattern() {
}

beforeEach(() => {
normalize = require('../normalize').default;
root = path.resolve('/');
expectedPathFooBar = path.join(root, 'root', 'path', 'foo', 'bar', 'baz');
expectedPathFooQux = path.join(root, 'root', 'path', 'foo', 'qux', 'quux');
Expand Down Expand Up @@ -924,6 +926,39 @@ describe('moduleDirectories', () => {
'/root/node_modules',
]);
});

describe('win32', () => {
beforeEach(() => {
jest.resetModules();
jest.mock('path', () => jest.requireActual('path').win32);

normalize = require('../normalize').default;
require('jest-resolve').findNodeModule = findNodeModule;
});

afterEach(() => {
jest.resetModules();
jest.mock('path', () => jest.requireActual('path').posix);

normalize = require('../normalize').default;
});

it('correctly handles globs', () => {
const {options} = normalize(
{
rootDir: '/root',
testMatch: [
'<rootDir>/test/{TestCasesNormal,StatsTestCases,ConfigTestCases}.test.js',
],
},
{},
);

expect(options.testMatch).toEqual([
'/root/test/{TestCasesNormal,StatsTestCases,ConfigTestCases}.test.js',
]);
});
});
});

describe('preset', () => {
Expand Down Expand Up @@ -1375,40 +1410,36 @@ describe('testPathPattern', () => {

describe('win32', () => {
beforeEach(() => {
jest.resetModules();
Copy link
Member Author

Choose a reason for hiding this comment

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

had to fight the mocking a bit to properly reset it.

jest.mock('path', () => jest.requireActual('path').win32);
normalize = require('../normalize').default;
require('jest-resolve').findNodeModule = findNodeModule;
});

afterEach(() => {
jest.resetModules();
jest.mock('path', () => jest.requireActual('path').posix);
normalize = require('../normalize').default;
require('jest-resolve').findNodeModule = findNodeModule;
});

it('preserves any use of "\\"', () => {
const argv = {[opt.property]: ['a\\b', 'c\\\\d']};
const {options} = require('../normalize').default(
initialOptions,
argv,
);
const {options} = normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('a\\b|c\\\\d');
});

it('replaces POSIX path separators', () => {
const argv = {[opt.property]: ['a/b']};
const {options} = require('../normalize').default(
initialOptions,
argv,
);
const {options} = normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('a\\\\b');
});

it('replaces POSIX paths in multiple args', () => {
const argv = {[opt.property]: ['a/b', 'c/d']};
const {options} = require('../normalize').default(
initialOptions,
argv,
);
const {options} = normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('a\\\\b|c\\\\d');
});
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-config/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import path from 'path';
import {ValidationError} from 'jest-validate';
import Resolver from 'jest-resolve';
import chalk from 'chalk';
import slash from 'slash';

type ResolveOptions = {|
rootDir: string,
Expand Down Expand Up @@ -66,9 +67,11 @@ export const replaceRootDirInPath = (
return filePath;
}

return path.resolve(
rootDir,
path.normalize('./' + filePath.substr('<rootDir>'.length)),
return slash(
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this could break glob patterns with escape sequences.

Like <rootDir>/test \(in brakets\)/*.js

Maybe add a test case for this.

path.resolve(
rootDir,
path.normalize('./' + filePath.substr('<rootDir>'.length)),
),
);
};

Expand Down