Skip to content

Commit

Permalink
fix: --only-changed should include staged files (#9799)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 12, 2020
1 parent 832f85b commit 0924baf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[expect]` Restore support for passing functions to `toHaveLength` matcher ([#9796](https://github.com/facebook/jest/pull/9796))
- `[jest-changed-files]` `--only-changed` should include staged files ([#9799](https://github.com/facebook/jest/pull/9799))

### Chore & Maintenance

Expand Down
16 changes: 13 additions & 3 deletions e2e/__tests__/jestChangedFiles.test.ts
Expand Up @@ -32,7 +32,7 @@ if (!hgIsInstalled) {
console.warn('Mercurial (hg) is not installed - skipping some tests');
}

testIfHg('gets hg SCM roots and dedups them', async () => {
testIfHg('gets hg SCM roots and dedupes them', async () => {
writeFiles(DIR, {
'first-repo/file1.txt': 'file1',
'first-repo/nested-dir/file2.txt': 'file2',
Expand Down Expand Up @@ -67,7 +67,7 @@ testIfHg('gets hg SCM roots and dedups them', async () => {
expect(hgRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo\/?$/);
});

test('gets git SCM roots and dedups them', async () => {
test('gets git SCM roots and dedupes them', async () => {
writeFiles(DIR, {
'first-repo/file1.txt': 'file1',
'first-repo/nested-dir/file2.txt': 'file2',
Expand Down Expand Up @@ -101,7 +101,7 @@ test('gets git SCM roots and dedups them', async () => {
expect(gitRepos[1]).toMatch(/\/jest-changed-files-test-dir\/second-repo\/?$/);
});

testIfHg('gets mixed git and hg SCM roots and dedups them', async () => {
testIfHg('gets mixed git and hg SCM roots and dedupes them', async () => {
writeFiles(DIR, {
'first-repo/file1.txt': 'file1',
'first-repo/nested-dir/file2.txt': 'file2',
Expand Down Expand Up @@ -186,6 +186,16 @@ test('gets changed files for git', async () => {
.sort(),
).toEqual(['file1.txt']);

run(`${GIT} add -A`, DIR);

// staged files should be included
({changedFiles: files} = await getChangedFilesForRoots(roots, {}));
expect(
Array.from(files)
.map(filePath => path.basename(filePath))
.sort(),
).toEqual(['file1.txt']);

run(`${GIT} commit --no-gpg-sign -am "test2"`, DIR);

writeFiles(DIR, {
Expand Down
57 changes: 33 additions & 24 deletions packages/jest-changed-files/src/git.ts
Expand Up @@ -50,36 +50,45 @@ const adapter: SCMAdapter = {
),
cwd,
);
} else if (changedSince) {
const committed = await findChangedFilesUsingCommand(
[
'log',
'--name-only',
'--pretty=format:',
'HEAD',
`^${changedSince}`,
].concat(includePaths),
cwd,
);
const staged = await findChangedFilesUsingCommand(
['diff', '--cached', '--name-only'].concat(includePaths),
cwd,
);
const unstaged = await findChangedFilesUsingCommand(
['ls-files', '--other', '--modified', '--exclude-standard'].concat(
includePaths,
}
if (changedSince) {
const [committed, staged, unstaged] = await Promise.all([
findChangedFilesUsingCommand(
[
'log',
'--name-only',
'--pretty=format:',
'HEAD',
`^${changedSince}`,
].concat(includePaths),
cwd,
),
cwd,
);
findChangedFilesUsingCommand(
['diff', '--cached', '--name-only'].concat(includePaths),
cwd,
),
findChangedFilesUsingCommand(
['ls-files', '--other', '--modified', '--exclude-standard'].concat(
includePaths,
),
cwd,
),
]);
return [...committed, ...staged, ...unstaged];
} else {
return findChangedFilesUsingCommand(
}
const [staged, unstaged] = await Promise.all([
findChangedFilesUsingCommand(
['diff', '--cached', '--name-only'].concat(includePaths),
cwd,
),
findChangedFilesUsingCommand(
['ls-files', '--other', '--modified', '--exclude-standard'].concat(
includePaths,
),
cwd,
);
}
),
]);
return [...staged, ...unstaged];
},

getRoot: async cwd => {
Expand Down

0 comments on commit 0924baf

Please sign in to comment.