Skip to content

Commit

Permalink
--changedFilesToContributeTo=$BRANCH
Browse files Browse the repository at this point in the history
make changedFilesToContributeTo require an arg, and imply onlyChanged
  • Loading branch information
alsuren committed Jan 15, 2018
1 parent 31fd272 commit 0ba2431
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* `[jest-cli]` Make Jest exit without an error when no tests are found in the
case of `--lastCommit`, `--findRelatedTests`, or `--onlyChanged` options
having been passed to the CLI
* `[jest-cli]` Allow selectively running tests for code changed since arbitrary
revisions. ([#5188](https://github.com/facebook/jest/pull/5188))

### Fixes
* `[jest-cli]` Use `import-local` to support global Jest installations.
Expand Down
6 changes: 6 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ 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`.

### `--changedFilesToContributeTo`

When used together with `--onlyChanged` or `--watch`, it runs tests related the
changes since the provided revision. If the current branch is not a child of the
given commit, then only changes made locally will be tested.

### `--changedFilesWithAncestor`

Runs tests related to the current changes and the changes made in the last
Expand Down
64 changes: 64 additions & 0 deletions integration-tests/__tests__/jest_changed_files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,37 @@ test('gets changed files for git', async () => {
.map(filePath => path.basename(filePath))
.sort(),
).toEqual(['file1.txt', 'file4.txt']);

run(`${GIT} add file4.txt`, DIR);
run(`${GIT} commit -m "test3"`, DIR);

({changedFiles: files} = await getChangedFilesForRoots(roots, {
toContributeTo: 'HEAD^^',
}));
// Returns files from the last 2 commits
expect(
Array.from(files)
.map(filePath => path.basename(filePath))
.sort(),
).toEqual(['file1.txt', 'file4.txt']);

run(`${GIT} checkout HEAD^^ -b feature-branch`, DIR);

writeFiles(DIR, {
'file5.txt': 'file5',
});
run(`${GIT} add file5.txt`, DIR);
run(`${GIT} commit -m "test5"`, DIR);

({changedFiles: files} = await getChangedFilesForRoots(roots, {
toContributeTo: 'master',
}));
// Returns files from this branch but not ones that only exist on master
expect(
Array.from(files)
.map(filePath => path.basename(filePath))
.sort(),
).toEqual(['file5.txt']);
});

test('gets changed files for hg', async () => {
Expand Down Expand Up @@ -261,4 +292,37 @@ test('gets changed files for hg', async () => {
.map(filePath => path.basename(filePath))
.sort(),
).toEqual(['file1.txt', 'file4.txt']);

run(`${HG} add file4.txt`, DIR);
run(`${HG} commit -m "test3"`, DIR);

({changedFiles: files} = await getChangedFilesForRoots(roots, {
toContributeTo: '-3',
}));
// Returns files from the last 2 commits
expect(
Array.from(files)
.map(filePath => path.basename(filePath))
.sort(),
).toEqual(['file1.txt', 'file4.txt']);

run(`${HG} bookmark master`, DIR);
// Back up and develop on a different branch
run(`${HG} checkout --rev=-2`, DIR);

writeFiles(DIR, {
'file5.txt': 'file5',
});
run(`${HG} add file5.txt`, DIR);
run(`${HG} commit -m "test4"`, DIR);

({changedFiles: files} = await getChangedFilesForRoots(roots, {
toContributeTo: 'master',
}));
// Returns files from this branch but not ones that only exist on master
expect(
Array.from(files)
.map(filePath => path.basename(filePath))
.sort(),
).toEqual(['file5.txt']);
});
25 changes: 18 additions & 7 deletions packages/jest-changed-files/src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import type {Options, SCMAdapter} from 'types/ChangedFiles';
import path from 'path';
import childProcess from 'child_process';

const findChangedFilesUsingCommand = async (args, cwd) => {
const findChangedFilesUsingCommand = async (
args: Array<string>,
cwd: Path,
): Promise<Array<Path>> => {
return new Promise((resolve, reject) => {
const child = childProcess.spawn('git', args, {cwd});
let stdout = '';
Expand All @@ -30,6 +33,7 @@ const findChangedFilesUsingCommand = async (args, cwd) => {
resolve(
stdout
.split('\n')
.filter(s => s !== '')
.map(changedPath => path.resolve(cwd, changedPath)),
);
}
Expand All @@ -45,21 +49,28 @@ const adapter: SCMAdapter = {
cwd: string,
options?: Options,
): Promise<Array<Path>> => {
const toContributeTo: ?string =
options && (options.withAncestor ? 'HEAD^' : options.toContributeTo);

if (options && options.lastCommit) {
return await findChangedFilesUsingCommand(
['show', '--name-only', '--pretty=%b', 'HEAD'],
cwd,
);
} else if (options && options.withAncestor) {
const changed = await findChangedFilesUsingCommand(
['diff', '--name-only', 'HEAD^'],
} else if (toContributeTo) {
const committed = await findChangedFilesUsingCommand(
['log', '--name-only', '--pretty=%b', 'HEAD', `^${toContributeTo}`],
cwd,
);
const staged = await findChangedFilesUsingCommand(
['diff', '--cached', '--name-only'],
cwd,
);
const untracked = await findChangedFilesUsingCommand(
['ls-files', '--other', '--exclude-standard'],
const unstaged = await findChangedFilesUsingCommand(
['ls-files', '--other', '--modified', '--exclude-standard'],
cwd,
);
return changed.concat(untracked);
return [...committed, ...staged, ...unstaged];
} else {
return await findChangedFilesUsingCommand(
['ls-files', '--other', '--modified', '--exclude-standard'],
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-changed-files/src/hg.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const adapter: SCMAdapter = {
let args = ['status', '-amnu'];
if (options && options.withAncestor) {
args.push('--rev', 'ancestor(.^)');
} else if (options && options.toContributeTo) {
args.push('--rev', `ancestor(., ${options.toContributeTo})`);
} else if (options && options.lastCommit === true) {
args = ['tip', '--template', '{files%"{file}\n"}'];
}
Expand Down
15 changes: 14 additions & 1 deletion packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export const check = (argv: Argv) => {
);
}

for (const key of ['onlyChanged', 'lastCommit', 'changedFilesWithAncestor']) {
for (const key of [
'onlyChanged',
'lastCommit',
'changedFilesWithAncestor',
'changedFilesToContributeTo',
]) {
if (argv[key]) {
argv.onlyChanged = true;
}
Expand Down Expand Up @@ -107,6 +112,14 @@ export const options = {
' dependency information.',
type: 'string',
},
changedFilesToContributeTo: {
description:
'Runs tests related the changes since the provided branch. If the ' +
'current branch has diverged from the given branch, then only changes ' +
'made locally will be tested. Behaves similarly to `--onlyChanged`.',
nargs: 1,
type: 'string',
},
changedFilesWithAncestor: {
description:
'Runs tests related to the current changes and the changes made in the ' +
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/src/get_changed_files_promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default (
);
return getChangedFilesForRoots(allRootsForAllProjects, {
lastCommit: globalConfig.lastCommit,
toContributeTo: globalConfig.changedFilesToContributeTo,
withAncestor: globalConfig.changedFilesWithAncestor,
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const getConfigs = (
return {
globalConfig: Object.freeze({
bail: options.bail,
changedFilesToContributeTo: options.changedFilesToContributeTo,
changedFilesWithAncestor: options.changedFilesWithAncestor,
collectCoverage: options.collectCoverage,
collectCoverageFrom: options.collectCoverageFrom,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
case 'bail':
case 'browser':
case 'cache':
case 'changedFilesToContributeTo':
case 'changedFilesWithAncestor':
case 'clearMocks':
case 'collectCoverage':
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/valid_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default ({
browser: false,
cache: true,
cacheDirectory: '/tmp/user/jest',
changedFilesToContributeTo: '',
changedFilesWithAncestor: false,
clearMocks: false,
collectCoverage: true,
Expand Down
1 change: 1 addition & 0 deletions test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {GlobalConfig, ProjectConfig} from 'types/Config';

const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
bail: false,
changedFilesToContributeTo: '',
changedFilesWithAncestor: false,
collectCoverage: false,
collectCoverageFrom: [],
Expand Down
1 change: 1 addition & 0 deletions types/Argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Argv = {|
browser: boolean,
cache: boolean,
cacheDirectory: string,
changedFilesToContributeTo: string,
changedFilesWithAncestor: boolean,
clearMocks: boolean,
ci: boolean,
Expand Down
1 change: 1 addition & 0 deletions types/ChangedFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {Path} from 'types/Config';
export type Options = {|
lastCommit?: boolean,
withAncestor?: boolean,
toContributeTo?: string,
|};

export type ChangedFiles = Set<Path>;
Expand Down
2 changes: 2 additions & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type InitialOptions = {
cacheDirectory?: Path,
clearMocks?: boolean,
changedFilesWithAncestor?: boolean,
changedFilesToContributeTo?: string,
collectCoverage?: boolean,
collectCoverageFrom?: Array<Glob>,
collectCoverageOnlyFrom?: {[key: string]: boolean},
Expand Down Expand Up @@ -157,6 +158,7 @@ export type SnapshotUpdateState = 'all' | 'new' | 'none';

export type GlobalConfig = {|
bail: boolean,
changedFilesToContributeTo: string,
changedFilesWithAncestor: boolean,
collectCoverage: boolean,
collectCoverageFrom: Array<Glob>,
Expand Down

0 comments on commit 0ba2431

Please sign in to comment.