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

Use git diff instead of git log for --changedSince #10155

Merged
merged 11 commits into from Jul 3, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,7 @@
- `[docs]` Add param to `setSystemTime` docs and remove preceding period from it and `getRealSystemTime` ([#10169](https://github.com/facebook/jest/pull/10169))
- `[jest-snapshot, jest-util]` Replace `make-dir` with `fs.mkdir` ([#10136](https://github.com/facebook/jest/pull/10136))
- `[docs]` Added parcel-bundler documentation inside readme.md file
- `[jest-changed-files]` Use git diff instead of git log for --changedSince ([#10155](https://github.com/facebook/jest/pull/10155))

### Performance

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