From ecfa26682a4348dab0ad57d0cafffbc024b8954e Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 29 May 2019 14:22:17 -0400 Subject: [PATCH] fix(@schematics/update): respect semver rules for migration from & to 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 --- packages/schematics/update/migrate/index.ts | 17 ++++- .../schematics/update/migrate/index_spec.ts | 69 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/packages/schematics/update/migrate/index.ts b/packages/schematics/update/migrate/index.ts index 4d9a371f9027..3f3529575d2c 100644 --- a/packages/schematics/update/migrate/index.ts +++ b/packages/schematics/update/migrate/index.ts @@ -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()) { @@ -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 }); } } diff --git a/packages/schematics/update/migrate/index_spec.ts b/packages/schematics/update/migrate/index_spec.ts index ea5856f88e76..54daeb2558e1 100644 --- a/packages/schematics/update/migrate/index_spec.ts +++ b/packages/schematics/update/migrate/index_spec.ts @@ -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); + }); });