Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set 'version' of composer.json in RootComposerUpdatePackages #2201

Merged
merged 8 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
}
Comment on lines +34 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests for setting this root version entry (with the value set and unset)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is the kind of test you're looking for? 5093438

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);
});
});
});
Loading