From 382e6271c85976a89354c9a70d680f31a0c12641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Wierci=C5=84ski?= Date: Tue, 5 Mar 2019 11:27:23 +0100 Subject: [PATCH 1/6] Change method of obtaining git root (#3214) On various environments like cygwin path returned by --show-toplevel is cygwin path, not windows path. Instead of that --show-cdup can be used and merged with root of npm project giving root of git repo --- packages/jest-changed-files/src/git.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index ed0d4a9b90a1..876c9cf956f9 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -72,12 +72,12 @@ const adapter: SCMAdapter = { }, getRoot: async cwd => { - const options = ['rev-parse', '--show-toplevel']; + const options = ['rev-parse', '--show-cdup']; try { const result = await execa('git', options, {cwd}); - return result.stdout; + return path.join(cwd, result.stdout); } catch (e) { return null; } From 3666f323dd0106af30a19dbd6a5e6dcf3c873c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Wierci=C5=84ski?= Date: Tue, 5 Mar 2019 11:40:05 +0100 Subject: [PATCH 2/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39d765fd161b..a035afb9f208 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - `[jest-circus]`: Throw explicit error when errors happen after test is considered complete ([#8005](https://github.com/facebook/jest/pull/8005)) - `[expect]` Remove duck typing and obsolete browser support code when comparing DOM nodes and use DOM-Level-3 API instead ([#7995](https://github.com/facebook/jest/pull/7995)) - `[jest-mock]` Adds a type check to `prototype` to allow mocks of objects with a primitive `prototype` property. ([#8040](https://github.com/facebook/jest/pull/8040)) +- `[jest-changed-files]` Change method of obtaining git root (fixes #3214) ([#8052](https://github.com/facebook/jest/pull/8052)) ### Chore & Maintenance From f864f7fa5d16fe77a291129e96879749ec2134c8 Mon Sep 17 00:00:00 2001 From: bwiercinski Date: Wed, 6 Mar 2019 17:16:20 +0100 Subject: [PATCH 3/6] Fix tests for #8052 For execa it does not matter if path is ended with folder separator --- e2e/__tests__/jestChangedFiles.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/e2e/__tests__/jestChangedFiles.test.ts b/e2e/__tests__/jestChangedFiles.test.ts index 5cd462fc3937..5f70987d0e08 100644 --- a/e2e/__tests__/jestChangedFiles.test.ts +++ b/e2e/__tests__/jestChangedFiles.test.ts @@ -54,8 +54,8 @@ test('gets hg SCM roots and dedups them', async () => { // NOTE: This test can break if you have a .hg repo initialized inside your // os tmp directory. expect(hgRepos).toHaveLength(2); - expect(hgRepos[0]).toMatch(/\/jest-changed-files-test-dir\/first-repo$/); - expect(hgRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo$/); + expect(hgRepos[0]).toMatch(/\/jest-changed-files-test-dir\/first-repo\/?$/); + expect(hgRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo\/?$/); }); test('gets git SCM roots and dedups them', async () => { @@ -88,8 +88,8 @@ test('gets git SCM roots and dedups them', async () => { // NOTE: This test can break if you have a .git repo initialized inside your // os tmp directory. expect(gitRepos).toHaveLength(2); - expect(gitRepos[0]).toMatch(/\/jest-changed-files-test-dir\/first-repo$/); - expect(gitRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo$/); + expect(gitRepos[0]).toMatch(/\/jest-changed-files-test-dir\/first-repo\/?$/); + expect(gitRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo\/?$/); }); test('gets mixed git and hg SCM roots and dedups them', async () => { @@ -121,8 +121,8 @@ test('gets mixed git and hg SCM roots and dedups them', async () => { // inside your os tmp directory. expect(gitRepos).toHaveLength(1); expect(hgRepos).toHaveLength(1); - expect(gitRepos[0]).toMatch(/\/jest-changed-files-test-dir\/first-repo$/); - expect(hgRepos[0]).toMatch(/\/jest-changed-files-test-dir\/second-repo$/); + expect(gitRepos[0]).toMatch(/\/jest-changed-files-test-dir\/first-repo\/?$/); + expect(hgRepos[0]).toMatch(/\/jest-changed-files-test-dir\/second-repo\/?$/); }); test('gets changed files for git', async () => { From eb0829df27231071782c582aeaf044af4e7b508f Mon Sep 17 00:00:00 2001 From: bwiercinski Date: Wed, 6 Mar 2019 17:17:24 +0100 Subject: [PATCH 4/6] Change method of defining path of npm root Instead of absolute path, use path relative to git root. Thanks to that it works on various windows enviroments etc. Cygwin which requires cygwin path if absolute --- packages/jest-changed-files/src/git.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index 876c9cf956f9..7c5e205152f3 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -29,8 +29,10 @@ const adapter: SCMAdapter = { const changedSince: string | undefined = options && (options.withAncestor ? 'HEAD^' : options.changedSince); - const includePaths: Array = - (options && options.includePaths) || []; + const includePaths: Array = ( + (options && options.includePaths) || + [] + ).map(absoluteRoot => path.relative(cwd, absoluteRoot)); if (options && options.lastCommit) { return findChangedFilesUsingCommand( From d155af23f9ed8dd532792153603e52894cb1a2ff Mon Sep 17 00:00:00 2001 From: bwiercinski Date: Wed, 6 Mar 2019 18:08:03 +0100 Subject: [PATCH 5/6] Normalize path to avoid empty string as path for case path.relative(dir, dir) returning '' --- packages/jest-changed-files/src/git.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index 7c5e205152f3..dd5a2b9d3067 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -32,7 +32,7 @@ const adapter: SCMAdapter = { const includePaths: Array = ( (options && options.includePaths) || [] - ).map(absoluteRoot => path.relative(cwd, absoluteRoot)); + ).map(absoluteRoot => path.normalize(path.relative(cwd, absoluteRoot))); if (options && options.lastCommit) { return findChangedFilesUsingCommand( From 96634282ea24caa93b5dff18129609017e4f6f16 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Mar 2019 11:41:17 +0100 Subject: [PATCH 6/6] Use resolve when obtaining absolute path of git's root directory Co-Authored-By: bwiercinski --- packages/jest-changed-files/src/git.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index dd5a2b9d3067..29cf3cf0ede7 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -79,7 +79,7 @@ const adapter: SCMAdapter = { try { const result = await execa('git', options, {cwd}); - return path.join(cwd, result.stdout); + return path.resolve(cwd, result.stdout); } catch (e) { return null; }