diff --git a/__snapshots__/php-composer-update-packages.js b/__snapshots__/php-composer-update-packages.js index aaa6053e1..d72b3eb13 100644 --- a/__snapshots__/php-composer-update-packages.js +++ b/__snapshots__/php-composer-update-packages.js @@ -1,17 +1,17 @@ exports['PHPComposer updateContent does not update a version when version is the same 1'] = ` -{"version":"1.0.0","replace":{"version":"1.0.0"}} +{"version":"1.0.0","replace":{"my/package":"1.0.0"}} ` exports['PHPComposer updateContent update all versions in composer.json 1'] = ` -{"version":"1.0.0","replace":{"version":"1.0.0"}} +{"version":"1.0.0","replace":{"my/package":"1.0.0"}} ` -exports['PHPComposer updateContent update replace version in composer.json when version is missing 1'] = ` -{"replace":{"version":"1.0.0"}} +exports['PHPComposer updateContent update replace package in composer.json when package is missing 1'] = ` +{"replace":{"my/package":"1.0.0"}} ` -exports['PHPComposer updateContent update replace version in composer.json when version is present 1'] = ` -{"replace":{"version":"1.0.0"}} +exports['PHPComposer updateContent update replace package in composer.json when package is set in version map 1'] = ` +{"replace":{"my/package":"1.0.0"}} ` exports['PHPComposer updateContent update root version in composer.json 1'] = ` diff --git a/__snapshots__/root-composer-update-packages.js b/__snapshots__/root-composer-update-packages.js index 6f933de3f..c8a210006 100644 --- a/__snapshots__/root-composer-update-packages.js +++ b/__snapshots__/root-composer-update-packages.js @@ -1,3 +1,7 @@ +exports['composer-update-package.json updateContent does not update version in composer if it does not exist 1'] = ` +{} +` + exports['composer-update-package.json updateContent updates all versions in root composer file 1'] = ` { "name": "google/cloud", @@ -256,3 +260,7 @@ exports['composer-update-package.json updateContent updates all versions in root } ` + +exports['composer-update-package.json updateContent updates version in composer if it exists 1'] = ` +{"version":"1.0.0"} +` diff --git a/src/strategies/php-yoshi.ts b/src/strategies/php-yoshi.ts index 998d5c978..67a675c92 100644 --- a/src/strategies/php-yoshi.ts +++ b/src/strategies/php-yoshi.ts @@ -172,14 +172,11 @@ export class PHPYoshi extends BaseStrategy { version, }), }); - const directoryVersion: VersionsMap = new Map(); - directoryVersion.set('version', version); updates.push({ path: this.addPath(`${directory}/composer.json`), createIfMissing: false, updater: new RootComposerUpdatePackages({ version, - versionsMap: directoryVersion, }), }); if (componentInfo.composer.extra?.component?.entry) { diff --git a/src/strategies/php.ts b/src/strategies/php.ts index cba6468d9..ccf46047f 100644 --- a/src/strategies/php.ts +++ b/src/strategies/php.ts @@ -47,7 +47,6 @@ export class PHP extends BaseStrategy { const updates: Update[] = []; const version = options.newVersion; const versionsMap: VersionsMap = new Map(); - versionsMap.set('version', version); updates.push({ path: this.addPath(this.changelogPath), diff --git a/src/updaters/php/root-composer-update-packages.ts b/src/updaters/php/root-composer-update-packages.ts index c30de2153..b39592dfc 100644 --- a/src/updaters/php/root-composer-update-packages.ts +++ b/src/updaters/php/root-composer-update-packages.ts @@ -26,11 +26,17 @@ export class RootComposerUpdatePackages extends DefaultUpdater { * @returns {string} The updated content */ updateContent(content: string, logger: Logger = defaultLogger): string { - if (!this.versionsMap || this.versionsMap.size === 0) { + if (!this.version && (!this.versionsMap || this.versionsMap.size === 0)) { logger.info('no updates necessary'); return content; } const parsed = JSON.parse(content); + if (parsed['version']) { + const fromVersion: string = parsed['version']; + const toVersion = this.version.toString() || '1.0.0'; + parsed['version'] = toVersion; + logger.info(`updating "version" from ${fromVersion} to ${toVersion}`); + } if (this.versionsMap) { for (const [key, version] of this.versionsMap.entries()) { const toVersion = version.toString() || '1.0.0'; diff --git a/test/updaters/php-composer-update-packages.ts b/test/updaters/php-composer-update-packages.ts index 08059c1ae..7fc9c6369 100644 --- a/test/updaters/php-composer-update-packages.ts +++ b/test/updaters/php-composer-update-packages.ts @@ -21,32 +21,34 @@ import {Version, VersionsMap} from '../../src/version'; describe('PHPComposer', () => { describe('updateContent', () => { it('does not update a version when version is the same', async () => { - const oldContent = '{"version":"1.0.0","replace":{"version":"1.0.0"}}'; + const oldContent = '{"version":"1.0.0","replace":{"my/package":"1.0.0"}}'; const version = Version.parse('1.0.0'); const versionsMap: VersionsMap = new Map(); + versionsMap.set('my/package', version); + const newContent = new RootComposerUpdatePackages({ version, versionsMap, }).updateContent(oldContent); expect(newContent).to.eq( - '{"version":"1.0.0","replace":{"version":"1.0.0"}}' + '{"version":"1.0.0","replace":{"my/package":"1.0.0"}}' ); snapshot(newContent); }); it('update all versions in composer.json', async () => { - const oldContent = '{"version":"0.0.0","replace":{"version":"0.0.0"}}'; + const oldContent = '{"version":"0.0.0","replace":{"my/package":"0.0.0"}}'; const version = Version.parse('1.0.0'); const versionsMap: VersionsMap = new Map(); - versionsMap.set('version', version); + versionsMap.set('my/package', version); const newContent = new RootComposerUpdatePackages({ version, @@ -54,7 +56,7 @@ describe('PHPComposer', () => { }).updateContent(oldContent); expect(newContent).to.eq( - '{"version":"1.0.0","replace":{"version":"1.0.0"}}' + '{"version":"1.0.0","replace":{"my/package":"1.0.0"}}' ); snapshot(newContent); @@ -79,40 +81,40 @@ describe('PHPComposer', () => { snapshot(newContent); }); - it('update replace version in composer.json when version is present', async () => { - const oldContent = '{"replace":{"version":"0.0.0"}}'; + it('update replace package in composer.json when package is set in version map', async () => { + const oldContent = '{"replace":{"my/package":"0.0.0"}}'; const version = Version.parse('1.0.0'); const versionsMap: VersionsMap = new Map(); - versionsMap.set('version', version); + versionsMap.set('my/package', version); const newContent = new RootComposerUpdatePackages({ version, versionsMap, }).updateContent(oldContent); - expect(newContent).to.eq('{"replace":{"version":"1.0.0"}}'); + expect(newContent).to.eq('{"replace":{"my/package":"1.0.0"}}'); snapshot(newContent); }); - it('update replace version in composer.json when version is missing', async () => { + it('update replace package in composer.json when package is missing', async () => { const oldContent = '{"replace":{}}'; const version = Version.parse('1.0.0'); const versionsMap: VersionsMap = new Map(); - versionsMap.set('version', version); + versionsMap.set('my/package', version); const newContent = new RootComposerUpdatePackages({ version, versionsMap, }).updateContent(oldContent); - expect(newContent).to.eq('{"replace":{"version":"1.0.0"}}'); + expect(newContent).to.eq('{"replace":{"my/package":"1.0.0"}}'); snapshot(newContent); }); diff --git a/test/updaters/root-composer-update-packages.ts b/test/updaters/root-composer-update-packages.ts index dbf59f8ae..b87b1931c 100644 --- a/test/updaters/root-composer-update-packages.ts +++ b/test/updaters/root-composer-update-packages.ts @@ -16,6 +16,7 @@ import {readFileSync} from 'fs'; import {resolve} from 'path'; import * as snapshot from 'snap-shot-it'; import {describe, it} from 'mocha'; +import {expect} from 'chai'; import {RootComposerUpdatePackages} from '../../src/updaters/php/root-composer-update-packages'; import {Version} from '../../src/version'; @@ -38,5 +39,21 @@ describe('composer-update-package.json', () => { const newContent = composer.updateContent(oldContent); snapshot(newContent); }); + it('updates version in composer if it exists', async () => { + const composer = new RootComposerUpdatePackages({ + version: Version.parse('1.0.0'), + }); + const newContent = composer.updateContent('{"version": "0.8.0"}'); + expect(newContent).to.eql('{"version":"1.0.0"}'); + snapshot(newContent); + }); + it('does not update version in composer if it does not exist', async () => { + const composer = new RootComposerUpdatePackages({ + version: Version.parse('1.0.0'), + }); + const newContent = composer.updateContent('{}'); + expect(newContent).to.eql('{}'); + snapshot(newContent); + }); }); });