From c57472193747ff96cdcc809cb6f35be6dd1de02e Mon Sep 17 00:00:00 2001 From: David Laban Date: Wed, 27 Dec 2017 17:09:27 +0000 Subject: [PATCH 1/8] refactor of findChangedFiles --- packages/jest-changed-files/src/git.js | 50 ++++++++++++++------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/jest-changed-files/src/git.js b/packages/jest-changed-files/src/git.js index 8057c5612462..36085ec155f2 100644 --- a/packages/jest-changed-files/src/git.js +++ b/packages/jest-changed-files/src/git.js @@ -13,6 +13,33 @@ import type {Options, SCMAdapter} from 'types/ChangedFiles'; import path from 'path'; import childProcess from 'child_process'; +const findChangedFilesUsingCommand = async (args, cwd) => { + return new Promise((resolve, reject) => { + const child = childProcess.spawn('git', args, {cwd}); + let stdout = ''; + let stderr = ''; + child.stdout.on('data', data => (stdout += data)); + child.stderr.on('data', data => (stderr += data)); + child.on('error', e => reject(e)); + child.on('close', code => { + if (code === 0) { + stdout = stdout.trim(); + if (stdout === '') { + resolve([]); + } else { + resolve( + stdout + .split('\n') + .map(changedPath => path.resolve(cwd, changedPath)), + ); + } + } else { + reject(code + ': ' + stderr); + } + }); + }); +}; + const adapter: SCMAdapter = { findChangedFiles: async ( cwd: string, @@ -28,28 +55,7 @@ const adapter: SCMAdapter = { options && options.lastCommit ? ['show', '--name-only', '--pretty=%b', 'HEAD'] : ['ls-files', '--other', '--modified', '--exclude-standard']; - const child = childProcess.spawn('git', args, {cwd}); - let stdout = ''; - let stderr = ''; - child.stdout.on('data', data => (stdout += data)); - child.stderr.on('data', data => (stderr += data)); - child.on('error', e => reject(e)); - child.on('close', code => { - if (code === 0) { - stdout = stdout.trim(); - if (stdout === '') { - resolve([]); - } else { - resolve( - stdout - .split('\n') - .map(changedPath => path.resolve(cwd, changedPath)), - ); - } - } else { - reject(code + ': ' + stderr); - } - }); + resolve(findChangedFilesUsingCommand(args, cwd)); }); }, From beefdbccbf5ebbc496cc87b31529c61e73a7c04e Mon Sep 17 00:00:00 2001 From: David Laban Date: Wed, 27 Dec 2017 17:21:23 +0000 Subject: [PATCH 2/8] make --changedFilesWithAncestor work with git --- packages/jest-changed-files/src/git.js | 29 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/jest-changed-files/src/git.js b/packages/jest-changed-files/src/git.js index 36085ec155f2..27fdce319c4a 100644 --- a/packages/jest-changed-files/src/git.js +++ b/packages/jest-changed-files/src/git.js @@ -45,18 +45,27 @@ const adapter: SCMAdapter = { cwd: string, options?: Options, ): Promise> => { - if (options && options.withAncestor) { - throw new Error( - '`changedFilesWithAncestor` is not supported in git repos.', + if (options && options.lastCommit) { + return await findChangedFilesUsingCommand( + ['diff', '--name-only', 'HEAD^', 'HEAD'], + cwd, + ); + } else if (options && options.withAncestor) { + const changed = await findChangedFilesUsingCommand( + ['diff', '--name-only', 'HEAD^'], + cwd, + ); + const untracked = await findChangedFilesUsingCommand( + ['ls-files', '--other', '--exclude-standard'], + cwd, + ); + return changed.concat(untracked); + } else { + return await findChangedFilesUsingCommand( + ['ls-files', '--other', '--modified', '--exclude-standard'], + cwd, ); } - return new Promise((resolve, reject) => { - const args = - options && options.lastCommit - ? ['show', '--name-only', '--pretty=%b', 'HEAD'] - : ['ls-files', '--other', '--modified', '--exclude-standard']; - resolve(findChangedFilesUsingCommand(args, cwd)); - }); }, getRoot: async (cwd: string): Promise => { From 0c20845c86b57e32b1e0bc5c76ceb8a2a615ed29 Mon Sep 17 00:00:00 2001 From: David Laban Date: Wed, 27 Dec 2017 20:20:39 +0000 Subject: [PATCH 3/8] fixup! refactor of findChangedFiles --- packages/jest-changed-files/src/git.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-changed-files/src/git.js b/packages/jest-changed-files/src/git.js index 27fdce319c4a..ef02aaea3dbc 100644 --- a/packages/jest-changed-files/src/git.js +++ b/packages/jest-changed-files/src/git.js @@ -47,7 +47,7 @@ const adapter: SCMAdapter = { ): Promise> => { if (options && options.lastCommit) { return await findChangedFilesUsingCommand( - ['diff', '--name-only', 'HEAD^', 'HEAD'], + ['show', '--name-only', '--pretty=%b', 'HEAD'], cwd, ); } else if (options && options.withAncestor) { From 007104e2713bbb70885a4148a378e107cda516cd Mon Sep 17 00:00:00 2001 From: David Laban Date: Wed, 27 Dec 2017 22:46:01 +0000 Subject: [PATCH 4/8] add withAncestor test to git --- .../__tests__/jest_changed_files.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/integration_tests/__tests__/jest_changed_files.test.js b/integration_tests/__tests__/jest_changed_files.test.js index e56cd6029dc5..759f246f49e8 100644 --- a/integration_tests/__tests__/jest_changed_files.test.js +++ b/integration_tests/__tests__/jest_changed_files.test.js @@ -175,6 +175,22 @@ test('gets changed files for git', async () => { .map(filePath => path.basename(filePath)) .sort(), ).toEqual(['file1.txt']); + + run(`${GIT} commit -am "test2"`, DIR); + + writeFiles(DIR, { + 'file4.txt': 'file4', + }); + + ({changedFiles: files} = await getChangedFilesForRoots(roots, { + withAncestor: true, + })); + // Returns files from current uncommitted state + the last commit + expect( + Array.from(files) + .map(filePath => path.basename(filePath)) + .sort(), + ).toEqual(['file1.txt', 'file4.txt']); }); test('gets changed files for hg', async () => { From 129d52d538832eb27c0a08d25c82eaf6718816e9 Mon Sep 17 00:00:00 2001 From: David Laban Date: Wed, 27 Dec 2017 23:27:30 +0000 Subject: [PATCH 5/8] docs: update changedFilesWithAncestor's description --- packages/jest-cli/src/cli/args.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 2c09cfce60d1..b558536bdf5b 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -105,8 +105,7 @@ export const options = { changedFilesWithAncestor: { description: 'When used together with `--onlyChanged`, it runs tests ' + - 'related to the current changes and the changes made in the last commit. ' + - '(NOTE: this only works for hg repos)', + 'related to the current changes and the changes made in the last commit. ', type: 'boolean', }, ci: { From f6f5f4781ef02b09520ac973d3265dd8bf95e4c7 Mon Sep 17 00:00:00 2001 From: David Laban Date: Wed, 27 Dec 2017 23:31:58 +0000 Subject: [PATCH 6/8] docs: add --changedFilesWithAncestor fix to changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44b83f2bd464..b49fa06a43bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Fixes +* `[jest-cli]` `jest --onlyChanged --changedFilesWithAncestor` now also works + with git. ([#5189](https://github.com/facebook/jest/pull/5189)) * `[jest-config]` fix unexpected condition to avoid infinite recursion in Windows platform. ([#5161](https://github.com/facebook/jest/pull/5161)) From 86451bac7d1be09fde83148e6e561501d01d9254 Mon Sep 17 00:00:00 2001 From: David Laban Date: Thu, 28 Dec 2017 11:24:04 +0000 Subject: [PATCH 7/8] docs: updated docs for --changedFilesWithAncestor and --lastCommit --- docs/CLI.md | 8 +++++++- packages/jest-cli/src/cli/args.js | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 929bc25cedc3..af43d66f69ef 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -102,6 +102,11 @@ two times slower._ If you want to inspect the cache, use `--showConfig` and look at the `cacheDirectory` value. If you need to clear the cache, use `--clearCache`. +### `--changedFilesWithAncestor` + +When used together with `--onlyChanged`, it runs tests related to the current +changes and the changes made in the last commit. + ### `--ci` 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. ### `--lastCommit` -Will run all tests affected by file changes in the last commit made. +When used together with `--onlyChanged`, it will run all tests affected by file +changes in the last commit made. ### `--listTests` diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index b558536bdf5b..efae34fbfbe8 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -267,8 +267,8 @@ export const options = { lastCommit: { default: undefined, description: - 'Will run all tests affected by file changes in the last ' + - 'commit made.', + 'When used together with `--onlyChanged`, it will run all tests ' + + 'affected by file changes in the last commit made.', type: 'boolean', }, listTests: { From 4b15f861bb6e93f3400776bac1ef74cb8c4e87be Mon Sep 17 00:00:00 2001 From: David Laban Date: Thu, 28 Dec 2017 11:42:59 +0000 Subject: [PATCH 8/8] docs: mention --watch in --changedFilesWithAncestor docs --- docs/CLI.md | 4 ++-- packages/jest-cli/src/cli/args.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index af43d66f69ef..dd8684517cc1 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -104,8 +104,8 @@ If you want to inspect the cache, use `--showConfig` and look at the ### `--changedFilesWithAncestor` -When used together with `--onlyChanged`, it runs tests related to the current -changes and the changes made in the last commit. +When used together with `--onlyChanged` or `--watch`, it runs tests related to +the current changes and the changes made in the last commit. ### `--ci` diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index efae34fbfbe8..f692493908ca 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -104,7 +104,7 @@ export const options = { }, changedFilesWithAncestor: { description: - 'When used together with `--onlyChanged`, it runs tests ' + + 'When used together with `--onlyChanged` or `--watch`, it runs tests ' + 'related to the current changes and the changes made in the last commit. ', type: 'boolean', },