Skip to content

Commit

Permalink
Add "latest" flag to "yarn upgrade" (yarnpkg#3510)
Browse files Browse the repository at this point in the history
* Added additional (passing) tests for existing upgrade behavior

* [yarnpkg#3384] add --latest/-L flag to

* added previously forgotten files to make tests actually pass
  • Loading branch information
rally25rs authored and cpojer committed Jun 7, 2017
1 parent 645d0d8 commit b9fe983
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
64 changes: 64 additions & 0 deletions __tests__/commands/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,70 @@ test.concurrent('upgrades from fixed version to latest', (): Promise<void> => {
});
});

test.concurrent('upgrades to latest matching package.json semver when no package name passed', (): Promise<void> => {
return runUpgrade([], {}, 'range-to-latest', async (config): ?Promise<void> => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const lockEntryIndex = lockfile.indexOf('left-pad@<=1.1.1:');

expect(lockEntryIndex).toEqual(0);
expect(lockfile[lockEntryIndex + 1]).toContain('1.1.1');
expect(pkg.dependencies).toEqual({'left-pad': '<=1.1.1'});
});
});

test.concurrent('--latest upgrades to latest ignoring package.json when no package name passed', (): Promise<void> => {
return runUpgrade([], {latest: true}, 'range-to-latest', async (config): ?Promise<void> => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const lockEntryIndex = lockfile.indexOf('left-pad@^1.1.3:');

expect(lockEntryIndex).toEqual(0);
expect(lockfile.indexOf('left-pad@<=1.1.1:')).toEqual(-1);
expect(lockfile[lockEntryIndex + 1]).toContain('1.1.3');
expect(pkg.dependencies).toEqual({'left-pad': '^1.1.3'});
});
});

test.concurrent('upgrades to latest matching semver when package name passed with version', (): Promise<void> => {
return runUpgrade(['left-pad@~1.1.2'], {}, 'range-to-latest', async (config): ?Promise<void> => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const lockEntryIndex = lockfile.indexOf('left-pad@~1.1.2:');

expect(lockEntryIndex).toEqual(0);
expect(lockfile[lockEntryIndex + 1]).toContain('1.1.3');
expect(pkg.dependencies).toEqual({'left-pad': '~1.1.2'});
});
});

test.concurrent('--latest upgrades to passed in version when package name passed with version', (): Promise<void> => {
return runUpgrade(['[email protected]'], {latest: true}, 'range-to-latest', async (config): ?Promise<void> => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const lockEntryIndex = lockfile.indexOf('[email protected]:');

expect(lockEntryIndex).toEqual(0);
expect(lockfile.indexOf('left-pad@<=1.1.1:')).toEqual(-1);
expect(lockfile.indexOf('left-pad@^1.1.3:')).toEqual(-1);
expect(lockfile[lockEntryIndex + 1]).toContain('1.1.2');
expect(pkg.dependencies).toEqual({'left-pad': '1.1.2'});
});
});

test.concurrent('upgrades to latest ignoring package.json semver when package name passed', (): Promise<void> => {
return runUpgrade(['left-pad'], {}, 'range-to-latest', async (config): ?Promise<void> => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const lockEntryIndex = lockfile.indexOf('left-pad@^1.1.3:');

expect(lockEntryIndex).toEqual(0);
expect(lockfile.indexOf('left-pad@<=1.1.1:')).toEqual(-1);
expect(lockfile[lockEntryIndex + 1]).toContain('1.1.3');
expect(pkg.dependencies).toEqual({'left-pad': '^1.1.3'});
});
});

test.concurrent('upgrades dependency packages not in registry', (): Promise<void> => {
const packages = ['yarn-test-git-repo', 'e2e-test-repo'];
return runUpgrade(packages, {}, 'package-not-in-registry', async (config): ?Promise<void> => {
Expand Down
5 changes: 5 additions & 0 deletions __tests__/fixtures/upgrade/range-to-latest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"left-pad": "<=1.1.1"
}
}
7 changes: 6 additions & 1 deletion src/cli/commands/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function setFlags(commander: Object) {
// TODO: support some flags that install command has
commander.usage('upgrade [flags]');
commander.option('-S, --scope <scope>', 'upgrade packages under the specified scope');
commander.option('--latest', 'upgrade packages to the latest version, ignoring version ranges in package.json');
}

export function hasWrapper(): boolean {
Expand All @@ -20,7 +21,8 @@ export function hasWrapper(): boolean {
export const requireLockfile = true;

export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
const lockfile = args.length ? await Lockfile.fromDirectory(config.lockfileFolder, reporter) : new Lockfile();
const useLockfile = args.length || flags.latest;
const lockfile = useLockfile ? await Lockfile.fromDirectory(config.lockfileFolder, reporter) : new Lockfile();
const {
dependencies,
devDependencies,
Expand Down Expand Up @@ -50,6 +52,9 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
} else {
throw new MessageError(reporter.lang('scopeNotValid'));
}
} else if (flags.latest && args.length === 0) {
addArgs = Object.keys(allDependencies)
.map(dependency => getDependency(allDependencies, dependency));
} else {
addArgs = args.map(dependency => {
return getDependency(allDependencies, dependency);
Expand Down

0 comments on commit b9fe983

Please sign in to comment.