Skip to content

Commit 57a7181

Browse files
committed
WIP: --changedFilesSinceCommit=origin/master + hg support
1 parent f845792 commit 57a7181

5 files changed

Lines changed: 68 additions & 11 deletions

File tree

docs/CLI.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ 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+
### `--changedFilesSinceCommit`
106+
107+
When used together with `--onlyChanged` or `--watch`, it runs tests related the
108+
changes since the provided revision. If the current branch is not a child of the
109+
given commit, then only changes made locally will be tested.
110+
105111
### `--changedFilesWithAncestor`
106112

107113
When used together with `--onlyChanged` or `--watch`, it runs tests related to

integration_tests/__tests__/jest_changed_files.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,23 @@ test('gets changed files for git', async () => {
204204
.map(filePath => path.basename(filePath))
205205
.sort(),
206206
).toEqual(['file1.txt', 'file4.txt']);
207+
208+
run(`${GIT} checkout HEAD^^ -b feature-branch`, DIR);
209+
210+
writeFiles(DIR, {
211+
'file5.txt': 'file5',
212+
});
213+
run(`${GIT} commit -am "test5"`, DIR);
214+
215+
({changedFiles: files} = await getChangedFilesForRoots(roots, {
216+
sinceCommit: 'master',
217+
}));
218+
// Returns files from this branch but not ones that only exist on master
219+
expect(
220+
Array.from(files)
221+
.map(filePath => path.basename(filePath))
222+
.sort(),
223+
).toEqual(['file5.txt']);
207224
});
208225

209226
test('gets changed files for hg', async () => {
@@ -274,4 +291,35 @@ test('gets changed files for hg', async () => {
274291
.map(filePath => path.basename(filePath))
275292
.sort(),
276293
).toEqual(['file1.txt', 'file4.txt']);
294+
295+
run(`${HG} commit -m "test3"`, DIR);
296+
297+
({changedFiles: files} = await getChangedFilesForRoots(roots, {
298+
sinceCommit: '-2',
299+
}));
300+
// Returns files from the last 2 commits
301+
expect(
302+
Array.from(files)
303+
.map(filePath => path.basename(filePath))
304+
.sort(),
305+
).toEqual(['file1.txt', 'file4.txt']);
306+
307+
run(`${HG} bookmark master`, DIR);
308+
// Back up and develop on a different branch
309+
run(`${HG} checkout --rev=-2`, DIR);
310+
311+
writeFiles(DIR, {
312+
'file5.txt': 'file5',
313+
});
314+
run(`${HG} commit -am "test5"`, DIR);
315+
316+
({changedFiles: files} = await getChangedFilesForRoots(roots, {
317+
sinceCommit: 'master',
318+
}));
319+
// Returns files from this branch but not ones that only exist on master
320+
expect(
321+
Array.from(files)
322+
.map(filePath => path.basename(filePath))
323+
.sort(),
324+
).toEqual(['file5.txt']);
277325
});

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,19 @@ const adapter: SCMAdapter = {
5757
cwd,
5858
);
5959
} else if (sinceCommit) {
60-
const changed = await findChangedFilesUsingCommand(
61-
['diff', '--name-only', '--diff-filter=ACMR', sinceCommit],
60+
const committed = await findChangedFilesUsingCommand(
61+
['log', '--name-only', '--pretty=%b', 'HEAD', `^${sinceCommit}`],
6262
cwd,
6363
);
64-
const untracked = await findChangedFilesUsingCommand(
65-
['ls-files', '--other', '--exclude-standard'],
64+
const staged = await findChangedFilesUsingCommand(
65+
['diff', '--cached', '--name-only'],
6666
cwd,
6767
);
68-
return changed.concat(untracked);
68+
const unstaged = await findChangedFilesUsingCommand(
69+
['ls-files', '--other', '--modified', '--exclude-standard'],
70+
cwd,
71+
);
72+
return [...committed, ...staged, ...unstaged];
6973
} else {
7074
return await findChangedFilesUsingCommand(
7175
['ls-files', '--other', '--modified', '--exclude-standard'],

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ const adapter: SCMAdapter = {
2323
options: Options,
2424
): Promise<Array<Path>> => {
2525
return new Promise((resolve, reject) => {
26-
if (options && options.sinceCommit) {
27-
throw new Error(
28-
'`changedFilesSinceCommit` is not supported in hg repos.',
29-
);
30-
}
3126
let args = ['status', '-amnu'];
3227
if (options && options.withAncestor) {
3328
args.push('--rev', 'ancestor(.^)');
29+
} else if (options && options.sinceCommit) {
30+
args.push(`--rev', 'ancestor(., ${options.sinceCommit})`);
3431
} else if (options && options.lastCommit === true) {
3532
args = ['tip', '--template', '{files%"{file}\n"}'];
3633
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ export const options = {
105105
changedFilesSinceCommit: {
106106
description:
107107
'When used together with `--onlyChanged` or `--watch`, it runs tests ' +
108-
'related the changes since the provided revision.',
108+
'related the changes since the provided revision. If the current ' +
109+
'branch is not a child of the given commit, then only changes made ' +
110+
'locally will be tested.',
109111
type: 'string',
110112
},
111113
changedFilesWithAncestor: {

0 commit comments

Comments
 (0)