Skip to content

Commit

Permalink
Use git diff instead of git log for --changedSince (#10155)
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-orsini committed Jul 3, 2020
1 parent 6005d7e commit e936da2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[expect]` Match symbols and bigints in `any()` ([#10223](https://github.com/facebook/jest/pull/10223))
- `[jest-changed-files]` Use `git diff` instead of `git log` for `--changedSince` ([#10155](https://github.com/facebook/jest/pull/10155))
- `[jest-console]` Add missing console.timeLog for compatability with Node ([#10209](https://github.com/facebook/jest/pull/10209))
- `[jest-snapshot]` Strip added indentation for inline error snapshots ([#10217](https://github.com/facebook/jest/pull/10217))

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/jestChangedFiles.test.ts.snap
Expand Up @@ -5,7 +5,7 @@ exports[`handles a bad revision for "changedSince", for git 1`] = `
● Test suite failed to run
fatal: bad revision '^blablabla'
fatal: bad revision '^blablabla...HEAD'
`;

Expand Down
45 changes: 44 additions & 1 deletion e2e/__tests__/jestChangedFiles.test.ts
Expand Up @@ -254,6 +254,49 @@ test('monitors only root paths for git', async () => {
).toEqual(['file2.txt', 'file3.txt']);
});

it('does not find changes in files with no diff, for git', async () => {
const roots = [path.resolve(DIR)];

// create an empty file, commit it to "master"
writeFiles(DIR, {
'file1.txt': '',
});
run(`${GIT} init`, DIR);
run(`${GIT} add file1.txt`, DIR);
run(`${GIT} commit --no-gpg-sign -m "initial"`, DIR);

// check out a new branch, jestChangedFilesSpecBase, to use later in diff
run(`${GIT} checkout -b jestChangedFilesSpecBase`, DIR);

// check out second branch, jestChangedFilesSpecMod, modify file & commit
run(`${GIT} checkout -b jestChangedFilesSpecMod`, DIR);
writeFiles(DIR, {
'file1.txt': 'modified file1',
});
run(`${GIT} add file1.txt`, DIR);
run(`${GIT} commit --no-gpg-sign -m "modified"`, DIR);

// still on jestChangedFilesSpecMod branch, "revert" back to empty file and commit
writeFiles(DIR, {
'file1.txt': '',
});
run(`${GIT} add file1.txt`, DIR);
run(`${GIT} commit --no-gpg-sign -m "removemod"`, DIR);

// check that passing in no changedSince arg doesn't return any unstaged / other changes
const {changedFiles: files} = await getChangedFilesForRoots(roots, {});
expect(Array.from(files)).toEqual([]);

// check that in diff from `jestChangedFilesSpecBase` branch, no changed files are reported
const {changedFiles: filesExplicitBaseBranch} = await getChangedFilesForRoots(
roots,
{
changedSince: 'jestChangedFilesSpecBase',
},
);
expect(Array.from(filesExplicitBaseBranch)).toEqual([]);
});

test('handles a bad revision for "changedSince", for git', async () => {
writeFiles(DIR, {
'.watchmanconfig': '',
Expand All @@ -266,7 +309,7 @@ test('handles a bad revision for "changedSince", for git', async () => {
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

const {exitCode, stderr} = runJest(DIR, ['--changedSince=blablabla']);
const {exitCode, stderr} = runJest(DIR, ['--changedSince=^blablabla']);

expect(exitCode).toBe(1);
expect(wrap(stderr)).toMatchSnapshot();
Expand Down
10 changes: 3 additions & 7 deletions packages/jest-changed-files/src/git.ts
Expand Up @@ -54,13 +54,9 @@ const adapter: SCMAdapter = {
if (changedSince) {
const [committed, staged, unstaged] = await Promise.all([
findChangedFilesUsingCommand(
[
'log',
'--name-only',
'--pretty=format:',
'HEAD',
`^${changedSince}`,
].concat(includePaths),
['diff', '--name-only', `${changedSince}...HEAD`].concat(
includePaths,
),
cwd,
),
findChangedFilesUsingCommand(
Expand Down

0 comments on commit e936da2

Please sign in to comment.