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

fix: --only-changed should include staged files #9799

Merged
merged 2 commits into from Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore whitespace

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