From e936da2ec5a175f18f1f32b22946cdf3d777840e Mon Sep 17 00:00:00 2001 From: Seraphina Orsini Date: Fri, 3 Jul 2020 05:41:00 -0400 Subject: [PATCH] Use `git diff` instead of `git log` for --changedSince (#10155) --- CHANGELOG.md | 1 + .../jestChangedFiles.test.ts.snap | 2 +- e2e/__tests__/jestChangedFiles.test.ts | 45 ++++++++++++++++++- packages/jest-changed-files/src/git.ts | 10 ++--- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73616e2902d9..ba03e914b1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/e2e/__tests__/__snapshots__/jestChangedFiles.test.ts.snap b/e2e/__tests__/__snapshots__/jestChangedFiles.test.ts.snap index cbbd571d60a3..3091b28da28d 100644 --- a/e2e/__tests__/__snapshots__/jestChangedFiles.test.ts.snap +++ b/e2e/__tests__/__snapshots__/jestChangedFiles.test.ts.snap @@ -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' `; diff --git a/e2e/__tests__/jestChangedFiles.test.ts b/e2e/__tests__/jestChangedFiles.test.ts index 480e1d966187..45e04eeda08d 100644 --- a/e2e/__tests__/jestChangedFiles.test.ts +++ b/e2e/__tests__/jestChangedFiles.test.ts @@ -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': '', @@ -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(); diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index 919eb19be98d..da1329a77bbb 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -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(