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(findRelatedTests): edge cases with --findRelatedTests on Windows #11548

Merged
merged 6 commits into from Jun 14, 2021
Merged
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 CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

- `[jest-reporter]` Allow `node-notifier@10` as peer dependency ([#11523](https://github.com/facebook/jest/pull/11523))
- `[jest-reporter]` Update `v8-to-istanbul` ([#11523](https://github.com/facebook/jest/pull/11523))
- `[jest-core]` Support special characters like `@`, `+` and `()` on windows with `--findRelatedTests` ([#11548](https://github.com/facebook/jest/pull/11548))

### Chore & Maintenance

Expand Down
39 changes: 27 additions & 12 deletions packages/jest-core/src/SearchSource.ts
Expand Up @@ -288,18 +288,7 @@ export default class SearchSource {
let paths = globalConfig.nonFlagArgs;

if (globalConfig.findRelatedTests && 'win32' === os.platform()) {
const allFiles = this._context.hasteFS.getAllFiles();
const options = {nocase: true, windows: false};

paths = paths
.map(p => {
const relativePath = path
.resolve(this._context.config.cwd, p)
.replace(/\\/g, '\\\\');
const match = micromatch(allFiles, relativePath, options);
return match[0];
})
.filter(Boolean);
paths = this.filterPathsWin32(paths);
}

if (globalConfig.runTestsByPath && paths && paths.length) {
Expand All @@ -316,6 +305,32 @@ export default class SearchSource {
}
}

public filterPathsWin32(paths: Array<string>): Array<string> {
const allFiles = this._context.hasteFS.getAllFiles();
const options = {nocase: true, windows: false};

function normalizePosix(filePath: string) {
return filePath.replace(/\\/g, '/');
}

paths = paths
.map(p => {
// micromatch works with forward slashes: https://github.com/micromatch/micromatch#backslashes
const normalizedPath = normalizePosix(
path.resolve(this._context.config.cwd, p),
);
const match = micromatch(
allFiles.map(normalizePosix),
normalizedPath,
options,
);
return match[0];
})
.filter(Boolean)
.map(p => path.resolve(p));
return paths;
}

async getTestPaths(
globalConfig: Config.GlobalConfig,
changedFiles: ChangedFiles | undefined,
Expand Down
75 changes: 75 additions & 0 deletions packages/jest-core/src/__tests__/SearchSource.test.ts
Expand Up @@ -406,6 +406,81 @@ describe('SearchSource', () => {
});
});

describe('filterPathsWin32', () => {
beforeEach(async () => {
const config = (
await normalize(
{
name,
rootDir: '.',
roots: [],
},
{} as Config.Argv,
)
).options;
const context = await Runtime.createContext(config, {
maxWorkers,
watchman: false,
});

searchSource = new SearchSource(context);
context.hasteFS.getAllFiles = () => [
path.resolve('packages/lib/my-lib.ts'),
path.resolve('packages/@core/my-app.ts'),
path.resolve('packages/+cli/my-cli.ts'),
path.resolve('packages/.hidden/my-app-hidden.ts'),
path.resolve('packages/programs (x86)/my-program.ts'),
];
});

it('should allow a simple match', async () => {
const result = searchSource.filterPathsWin32(['packages/lib/my-lib.ts']);
expect(result).toEqual([path.resolve('packages/lib/my-lib.ts')]);
});
it('should allow to match a file inside a hidden directory', async () => {
const result = searchSource.filterPathsWin32([
'packages/.hidden/my-app-hidden.ts',
]);
expect(result).toEqual([
path.resolve('packages/.hidden/my-app-hidden.ts'),
]);
});
it('should allow to match a file inside a directory prefixed with a "@"', async () => {
const result = searchSource.filterPathsWin32([
'packages/@core/my-app.ts',
]);
expect(result).toEqual([path.resolve('packages/@core/my-app.ts')]);
});
it('should allow to match a file inside a directory prefixed with a "+"', async () => {
const result = searchSource.filterPathsWin32(['packages/+cli/my-cli.ts']);
expect(result).toEqual([path.resolve('packages/+cli/my-cli.ts')]);
});
it('should allow an @(pattern)', () => {
const result = searchSource.filterPathsWin32([
'packages/@(@core)/my-app.ts',
]);
expect(result).toEqual([path.resolve('packages/@core/my-app.ts')]);
});
it('should allow a +(pattern)', () => {
const result = searchSource.filterPathsWin32([
'packages/+(@core)/my-app.ts',
]);
expect(result).toEqual([path.resolve('packages/@core/my-app.ts')]);
});
it('should allow for (pattern) in file path', () => {
const result = searchSource.filterPathsWin32([
'packages/programs (x86)/my-program.ts',
]);
expect(result).toEqual([
path.resolve('packages/programs (x86)/my-program.ts'),
]);
});
it('should allow no results found', () => {
const result = searchSource.filterPathsWin32(['not/exists']);
expect(result).toHaveLength(0);
});
});

describe('findRelatedTests', () => {
const rootDir = path.join(
__dirname,
Expand Down