Skip to content

Commit

Permalink
fix(manager/terraform): remove surplus .0s in lockfile constraints (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
secustor committed Nov 23, 2023
1 parent c1ec965 commit 8eefcbc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/modules/manager/terraform/lockfile/index.spec.ts
Expand Up @@ -7,6 +7,7 @@ import { getPkgReleases } from '../../../datasource';
import type { UpdateArtifactsConfig } from '../../types';
import { updateArtifacts } from '../index';
import { TerraformProviderHash } from './hash';
import { getNewConstraint } from './index';

// auto-mock fs
jest.mock('../../../../util/fs');
Expand Down Expand Up @@ -1089,4 +1090,19 @@ describe('modules/manager/terraform/lockfile/index', () => {
['https://registry.example.com', 'hashicorp/aws', '3.37.0'],
]);
});

describe('getNewConstraint', () => {
it('correctly calculate new constraint on pinning', () => {
expect(
getNewConstraint(
{
currentValue: '>= 4.3',
newValue: '5.26.0',
newVersion: '5.26.0',
},
'>= 4.3.0',
),
).toBe('5.26.0');
});
});
});
9 changes: 7 additions & 2 deletions lib/modules/manager/terraform/lockfile/index.ts
Expand Up @@ -65,7 +65,7 @@ async function updateAllLocks(
return updates.filter(is.truthy);
}

function getNewConstraint(
export function getNewConstraint(
dep: Upgrade<Record<string, unknown>>,
oldConstraint: string | undefined,
): string | undefined {
Expand All @@ -88,7 +88,12 @@ function getNewConstraint(
logger.debug(
`Updating constraint "${oldConstraint}" to replace "${currentValue}" with "${newValue}" for "${packageName}"`,
);
return oldConstraint.replace(currentValue, newValue);
let newConstraint = oldConstraint.replace(currentValue, newValue);
//remove surplus .0 version
while (newConstraint.split('.').length - 1 > 2) {
newConstraint = newConstraint.replace(RegExp('\\.0$'), '');
}
return newConstraint;
}

if (
Expand Down

0 comments on commit 8eefcbc

Please sign in to comment.