Skip to content

Commit

Permalink
fix(@schematics/update): respect semver rules for migration from & to
Browse files Browse the repository at this point in the history
This implements the logic provided in the following link with the exception that the lower range is exclusive instead of inclusive.
https://github.com/npm/node-semver#hyphen-ranges-xyz---abc

Fixes #14559
  • Loading branch information
clydin authored and alexeagle committed May 30, 2019
1 parent a57f053 commit 0048bf2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/schematics/update/migrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ export default function(options: PostUpdateSchema): Rule {
return (tree: Tree, context: SchematicContext) => {
const schematicsToRun: { name: string; version: string; }[] = [];

const from = _coerceVersionNumber(options.from);
if (!from) {
throw new SchematicsException(
`Invalid from option: ${JSON.stringify(options.from)}`,
);
}

const to = semver.validRange('<=' + options.to);
if (!to) {
throw new SchematicsException(
`Invalid to option: ${JSON.stringify(options.to)}`,
);
}

// Create the collection for the package.
const collection = context.engine.createCollection(options.collection);
for (const name of collection.listSchematicNames()) {
Expand All @@ -68,7 +82,8 @@ export default function(options: PostUpdateSchema): Rule {
);
}

if (semver.gt(version, options.from) && semver.lte(version, options.to)) {
if (semver.gt(version, from) &&
semver.satisfies(version, to, { includePrerelease: true })) {
schematicsToRun.push({ name, version });
}
}
Expand Down
69 changes: 69 additions & 0 deletions packages/schematics/update/migrate/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,75 @@ describe('@schematics/update:migrate', () => {
}),
).toPromise().then(done, done.fail);
});

it('supports partial version ranges with only major', done => {
// Since we cannot run tasks in unit tests, we need to validate that the default
// update schematic updates the package.json appropriately, AND validate that the
// migrate schematic actually do work appropriately, in a separate test.
schematicRunner.runSchematicAsync('migrate', {
package: 'test',
collection: require.resolve('./test/migration.json'),
from: '1',
to: '2',
}, appTree).pipe(
map(tree => {
const resultJson = JSON.parse(tree.readContent('/migrations'));

expect(resultJson).toEqual([
'migration-03', // "1.0.5"
'migration-05', // "1.1.0-beta.0"
'migration-04', // "1.1.0-beta.1"
'migration-02', // "1.1.0"
'migration-13', // "1.1.0"
'migration-19', // "1.1"
'migration-06', // "1.4.0"
'migration-17', // "2.0.0-alpha"
'migration-16', // "2.0.0-alpha.5"
'migration-08', // "2.0.0-beta.0"
'migration-07', // "2.0.0-rc.0"
'migration-12', // "2.0.0-rc.4"
'migration-14', // "2.0.0"
'migration-20', // "2"
'migration-15', // "2.0.1"
'migration-11', // "2.1.0"
]);
}),
).toPromise().then(done, done.fail);
});

it('supports partial version ranges with major and minor', done => {
// Since we cannot run tasks in unit tests, we need to validate that the default
// update schematic updates the package.json appropriately, AND validate that the
// migrate schematic actually do work appropriately, in a separate test.
schematicRunner.runSchematicAsync('migrate', {
package: 'test',
collection: require.resolve('./test/migration.json'),
from: '1.0',
to: '2.0',
}, appTree).pipe(
map(tree => {
const resultJson = JSON.parse(tree.readContent('/migrations'));

expect(resultJson).toEqual([
'migration-03', // "1.0.5"
'migration-05', // "1.1.0-beta.0"
'migration-04', // "1.1.0-beta.1"
'migration-02', // "1.1.0"
'migration-13', // "1.1.0"
'migration-19', // "1.1"
'migration-06', // "1.4.0"
'migration-17', // "2.0.0-alpha"
'migration-16', // "2.0.0-alpha.5"
'migration-08', // "2.0.0-beta.0"
'migration-07', // "2.0.0-rc.0"
'migration-12', // "2.0.0-rc.4"
'migration-14', // "2.0.0"
'migration-20', // "2"
'migration-15', // "2.0.1"
]);
}),
).toPromise().then(done, done.fail);
});
});


Expand Down

0 comments on commit 0048bf2

Please sign in to comment.