Skip to content

Commit 1a039f9

Browse files
alsurencpojer
authored andcommitted
Git support for --changedFilesWithAncestor (#5189)
* refactor of findChangedFiles * make --changedFilesWithAncestor work with git * fixup! refactor of findChangedFiles * add withAncestor test to git * docs: update changedFilesWithAncestor's description * docs: add --changedFilesWithAncestor fix to changelog * docs: updated docs for --changedFilesWithAncestor and --lastCommit * docs: mention --watch in --changedFilesWithAncestor docs
1 parent f3fce7b commit 1a039f9

5 files changed

Lines changed: 75 additions & 37 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-cli]` `jest --onlyChanged --changedFilesWithAncestor` now also works
6+
with git. ([#5189](https://github.com/facebook/jest/pull/5189))
57
* `[jest-config]` fix unexpected condition to avoid infinite recursion in
68
Windows platform. ([#5161](https://github.com/facebook/jest/pull/5161))
79
* `[jest-regex-util]` Fix breaking change in `--testPathPattern`

docs/CLI.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ two times slower._
102102
If you want to inspect the cache, use `--showConfig` and look at the
103103
`cacheDirectory` value. If you need to clear the cache, use `--clearCache`.
104104

105+
### `--changedFilesWithAncestor`
106+
107+
When used together with `--onlyChanged` or `--watch`, it runs tests related to
108+
the current changes and the changes made in the last commit.
109+
105110
### `--ci`
106111

107112
When this option is provided, Jest will assume it is running in a CI
@@ -182,7 +187,8 @@ Write test results to a file when the `--json` option is also specified.
182187

183188
### `--lastCommit`
184189

185-
Will run all tests affected by file changes in the last commit made.
190+
When used together with `--onlyChanged`, it will run all tests affected by file
191+
changes in the last commit made.
186192

187193
### `--listTests`
188194

integration_tests/__tests__/jest_changed_files.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ test('gets changed files for git', async () => {
175175
.map(filePath => path.basename(filePath))
176176
.sort(),
177177
).toEqual(['file1.txt']);
178+
179+
run(`${GIT} commit -am "test2"`, DIR);
180+
181+
writeFiles(DIR, {
182+
'file4.txt': 'file4',
183+
});
184+
185+
({changedFiles: files} = await getChangedFilesForRoots(roots, {
186+
withAncestor: true,
187+
}));
188+
// Returns files from current uncommitted state + the last commit
189+
expect(
190+
Array.from(files)
191+
.map(filePath => path.basename(filePath))
192+
.sort(),
193+
).toEqual(['file1.txt', 'file4.txt']);
178194
});
179195

180196
test('gets changed files for hg', async () => {

packages/jest-changed-files/src/git.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,59 @@ import type {Options, SCMAdapter} from 'types/ChangedFiles';
1313
import path from 'path';
1414
import childProcess from 'child_process';
1515

16+
const findChangedFilesUsingCommand = async (args, cwd) => {
17+
return new Promise((resolve, reject) => {
18+
const child = childProcess.spawn('git', args, {cwd});
19+
let stdout = '';
20+
let stderr = '';
21+
child.stdout.on('data', data => (stdout += data));
22+
child.stderr.on('data', data => (stderr += data));
23+
child.on('error', e => reject(e));
24+
child.on('close', code => {
25+
if (code === 0) {
26+
stdout = stdout.trim();
27+
if (stdout === '') {
28+
resolve([]);
29+
} else {
30+
resolve(
31+
stdout
32+
.split('\n')
33+
.map(changedPath => path.resolve(cwd, changedPath)),
34+
);
35+
}
36+
} else {
37+
reject(code + ': ' + stderr);
38+
}
39+
});
40+
});
41+
};
42+
1643
const adapter: SCMAdapter = {
1744
findChangedFiles: async (
1845
cwd: string,
1946
options?: Options,
2047
): Promise<Array<Path>> => {
21-
if (options && options.withAncestor) {
22-
throw new Error(
23-
'`changedFilesWithAncestor` is not supported in git repos.',
48+
if (options && options.lastCommit) {
49+
return await findChangedFilesUsingCommand(
50+
['show', '--name-only', '--pretty=%b', 'HEAD'],
51+
cwd,
52+
);
53+
} else if (options && options.withAncestor) {
54+
const changed = await findChangedFilesUsingCommand(
55+
['diff', '--name-only', 'HEAD^'],
56+
cwd,
57+
);
58+
const untracked = await findChangedFilesUsingCommand(
59+
['ls-files', '--other', '--exclude-standard'],
60+
cwd,
61+
);
62+
return changed.concat(untracked);
63+
} else {
64+
return await findChangedFilesUsingCommand(
65+
['ls-files', '--other', '--modified', '--exclude-standard'],
66+
cwd,
2467
);
2568
}
26-
return new Promise((resolve, reject) => {
27-
const args =
28-
options && options.lastCommit
29-
? ['show', '--name-only', '--pretty=%b', 'HEAD']
30-
: ['ls-files', '--other', '--modified', '--exclude-standard'];
31-
const child = childProcess.spawn('git', args, {cwd});
32-
let stdout = '';
33-
let stderr = '';
34-
child.stdout.on('data', data => (stdout += data));
35-
child.stderr.on('data', data => (stderr += data));
36-
child.on('error', e => reject(e));
37-
child.on('close', code => {
38-
if (code === 0) {
39-
stdout = stdout.trim();
40-
if (stdout === '') {
41-
resolve([]);
42-
} else {
43-
resolve(
44-
stdout
45-
.split('\n')
46-
.map(changedPath => path.resolve(cwd, changedPath)),
47-
);
48-
}
49-
} else {
50-
reject(code + ': ' + stderr);
51-
}
52-
});
53-
});
5469
},
5570

5671
getRoot: async (cwd: string): Promise<?string> => {

packages/jest-cli/src/cli/args.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ export const options = {
104104
},
105105
changedFilesWithAncestor: {
106106
description:
107-
'When used together with `--onlyChanged`, it runs tests ' +
108-
'related to the current changes and the changes made in the last commit. ' +
109-
'(NOTE: this only works for hg repos)',
107+
'When used together with `--onlyChanged` or `--watch`, it runs tests ' +
108+
'related to the current changes and the changes made in the last commit. ',
110109
type: 'boolean',
111110
},
112111
ci: {
@@ -268,8 +267,8 @@ export const options = {
268267
lastCommit: {
269268
default: undefined,
270269
description:
271-
'Will run all tests affected by file changes in the last ' +
272-
'commit made.',
270+
'When used together with `--onlyChanged`, it will run all tests ' +
271+
'affected by file changes in the last commit made.',
273272
type: 'boolean',
274273
},
275274
listTests: {

0 commit comments

Comments
 (0)