Skip to content

Commit

Permalink
make add command working with latest tag if offline or preferOffline …
Browse files Browse the repository at this point in the history
…is setted (#2521)
  • Loading branch information
voxsim authored and bestander committed Jan 26, 2017
1 parent b0eb13a commit dfb6a2b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
29 changes: 29 additions & 0 deletions __tests__/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,38 @@ test.skip('add asks for correct package version if user passes an incorrect one'

test.concurrent('install with latest tag', (): Promise<void> => {
return runAdd(['left-pad@latest'], {}, 'latest-version-in-package', async (config) => {
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const version = await getPackageVersion(config, 'left-pad');

assert.deepEqual(pkg.dependencies, {'left-pad': `^${version}`});
assert(lockfile.indexOf(`left-pad@^${version}:`) === 0);
});
});

test.concurrent('install with latest tag and --offline flag', (): Promise<void> => {
return runAdd(['left-pad@latest'], {}, 'latest-version-in-package', async (config, reporter, previousAdd) => {
config.offline = true;
const add = new Add(['left-pad@latest'], {}, config, reporter, previousAdd.lockfile);
await add.init();

const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const version = await getPackageVersion(config, 'left-pad');

assert.deepEqual(pkg.dependencies, {'left-pad': `^${version}`});
});
});

test.concurrent('install with latest tag and --prefer-offline flag', (): Promise<void> => {
return runAdd(['[email protected]'], {}, 'latest-version-in-package', async (config, reporter, previousAdd) => {
config.preferOffline = true;
const add = new Add(['left-pad@latest'], {}, config, reporter, previousAdd.lockfile);
await add.init();

const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
const version = await getPackageVersion(config, 'left-pad');

assert.deepEqual(pkg.dependencies, {'left-pad': `^${version}`});
assert.notEqual(version, '1.1.0');
});
});
7 changes: 6 additions & 1 deletion src/package-constraint-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default class PackageConstraintResolver {
config: Config;

reduce(versions: Array<string>, range: string): Promise<?string> {
return Promise.resolve(semver.maxSatisfying(versions, range, this.config.looseSemver));
if (range === 'latest') {
// Usually versions are already ordered and the last one is the latest
return Promise.resolve(versions[versions.length - 1]);
} else {
return Promise.resolve(semver.maxSatisfying(versions, range, this.config.looseSemver));
}
}
}

0 comments on commit dfb6a2b

Please sign in to comment.