Skip to content

Commit

Permalink
fix(gomod): support updating semver go directive (#25479)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Oct 28, 2023
1 parent 074e516 commit afb2a3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/modules/versioning/go-mod-directive/index.spec.ts
Expand Up @@ -29,7 +29,7 @@ describe('modules/versioning/go-mod-directive/index', () => {
version | expected
${'1'} | ${false}
${'1.2'} | ${true}
${'1.2.3'} | ${false}
${'1.2.3'} | ${true}
`('isValid("$version") === $expected', ({ version, expected }) => {
expect(!!semver.isValid(version)).toBe(expected);
});
Expand Down Expand Up @@ -70,8 +70,10 @@ describe('modules/versioning/go-mod-directive/index', () => {
${'1.16'} | ${'bump'} | ${'1.16.4'} | ${'1.17.0'} | ${'1.17'}
${'1.16'} | ${'bump'} | ${'1.16.4'} | ${'1.16.4'} | ${'1.16'}
${'1.16'} | ${'replace'} | ${'1.16.4'} | ${'1.16.4'} | ${'1.16'}
${'1.16'} | ${'replace'} | ${'1.16.4'} | ${'2.0.0'} | ${'2.0'}
${'1.16'} | ${'replace'} | ${'1.21.2'} | ${'1.21.2'} | ${'1.21.2'}
${'1.16'} | ${'widen'} | ${'1.16.4'} | ${'1.16.4'} | ${'1.16'}
${'1.16'} | ${'bump'} | ${'1.16.4'} | ${'1.21.3'} | ${'1.21.3'}
${'1.21.2'} | ${'bump'} | ${'1.21.2'} | ${'1.21.3'} | ${'1.21.3'}
`(
'getNewValue("$currentValue", "$rangeStrategy", "$currentVersion", "$newVersion") === "$expected"',
({ currentValue, rangeStrategy, currentVersion, newVersion, expected }) => {
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/versioning/go-mod-directive/index.ts
Expand Up @@ -9,7 +9,7 @@ export const urls = ['https://go.dev/ref/mod'];
export const supportsRanges = true;
export const supportedRangeStrategies: RangeStrategy[] = ['bump', 'replace'];

const validRegex = regEx(/^\d+\.\d+$/);
const validRegex = regEx(/^\d+\.\d+(\.\d+)?$/);

function toNpmRange(range: string): string {
return `^${range}`;
Expand All @@ -25,9 +25,15 @@ function getNewValue({
newVersion,
}: NewValueConfig): string {
if (rangeStrategy === 'bump') {
if (npm.matches(newVersion, '>=1.20.0')) {
return newVersion;
}
return shorten(newVersion);
}
if (rangeStrategy === 'replace' && !matches(currentValue, newVersion)) {
if (npm.matches(newVersion, '>=1.20.0')) {
return newVersion;
}
return shorten(newVersion);
}
return currentValue;
Expand Down

0 comments on commit afb2a3d

Please sign in to comment.