Skip to content

Commit

Permalink
feat: set 'version' of composer.json in RootComposerUpdatePackages (#…
Browse files Browse the repository at this point in the history
…2201)

* feat: set 'version' of composer.json in RootComposerUpdatePackages

* remove adding 'version' to versionsMap for php strategy

* merge main

* fix test

* fix lint

* add tests for updating version in composer updater

* fixlint
  • Loading branch information
bshaffer authored Jan 26, 2024
1 parent 32a972a commit 00ae8d1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
12 changes: 6 additions & 6 deletions __snapshots__/php-composer-update-packages.js
Original file line number Diff line number Diff line change
@@ -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'] = `
Expand Down
8 changes: 8 additions & 0 deletions __snapshots__/root-composer-update-packages.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"}
`
3 changes: 0 additions & 3 deletions src/strategies/php-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/strategies/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 7 additions & 1 deletion src/updaters/php/root-composer-update-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
26 changes: 14 additions & 12 deletions test/updaters/php-composer-update-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,42 @@ 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,
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);
Expand All @@ -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);
});
Expand Down
17 changes: 17 additions & 0 deletions test/updaters/root-composer-update-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
});
});
});

0 comments on commit 00ae8d1

Please sign in to comment.