Skip to content

Commit

Permalink
feat(go): add support for bumping a Go version file (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 committed Nov 29, 2021
1 parent 26442f1 commit 8f6e52b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __snapshots__/version-go.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exports['version.go updateContent updates version in version.go 1'] = `
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
const Version = "0.59.0"
`
8 changes: 8 additions & 0 deletions src/strategies/go-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {ConventionalCommit} from '../commit';
import {Version} from '../version';
import {TagName} from '../util/tag-name';
import {Release} from '../release';
import {VersionGo} from '../updaters/go/version-go';

// Commits containing a scope prefixed with an item in this array will be
// ignored when generating a release PR for the parent module.
Expand Down Expand Up @@ -55,6 +56,13 @@ export class GoYoshi extends Strategy {
changelogEntry: options.changelogEntry,
}),
});
updates.push({
path: this.addPath('internal/version.go'),
createIfMissing: false,
updater: new VersionGo({
version,
}),
});

return updates;
}
Expand Down
24 changes: 24 additions & 0 deletions src/updaters/go/version-go.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {DefaultUpdater} from '../default';

export class VersionGo extends DefaultUpdater {
updateContent(content: string): string {
return content.replace(
/const Version = "[0-9]+\.[0-9]+\.[0-9](-\w+)?"/,
`const Version = "${this.version.toString()}"`
);
}
}
2 changes: 2 additions & 0 deletions test/strategies/go-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {TagName} from '../../src/util/tag-name';
import {Version} from '../../src/version';
import {Changelog} from '../../src/updaters/changelog';
import snapshot = require('snap-shot-it');
import {VersionGo} from '../../src/updaters/go/version-go';

const sandbox = sinon.createSandbox();

Expand Down Expand Up @@ -96,6 +97,7 @@ describe('GoYoshi', () => {
);
const updates = release!.updates;
assertHasUpdate(updates, 'CHANGES.md', Changelog);
assertHasUpdate(updates, 'internal/version.go', VersionGo);
});
});
describe('buildReleasePullRequest', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/updaters/fixtures/go/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

const Version = "0.58.0"
38 changes: 38 additions & 0 deletions test/updaters/version-go.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {readFileSync} from 'fs';
import {resolve} from 'path';
import * as snapshot from 'snap-shot-it';
import {describe, it} from 'mocha';
import {VersionGo} from '../../src/updaters/go/version-go';
import {Version} from '../../src/version';

const fixturesPath = './test/updaters/fixtures/go';

describe('version.go', () => {
describe('updateContent', () => {
it('updates version in version.go', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './version.go'),
'utf8'
).replace(/\r\n/g, '\n');
const version = new VersionGo({
version: Version.parse('0.59.0'),
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
});
});

0 comments on commit 8f6e52b

Please sign in to comment.