Skip to content
Snippets Groups Projects
Unverified Commit f206b9e6 authored by RahulGautamSingh's avatar RahulGautamSingh Committed by GitHub
Browse files

feat(terraform): updateLockedDependency() (#18405)

parent a0008d37
No related branches found
No related tags found
No related merge requests found
......@@ -206,7 +206,7 @@ export async function updateLockedDependency(
}
} else if (depType) {
// TODO: `newValue` can probably null
// The constaint comes from the package.json file, so we need to update it
// The constraint comes from the package.json file, so we need to update it
const newValue = semver.getNewValue({
currentValue: constraint,
rangeStrategy: 'replace',
......
import { Fixtures } from '../../../../../test/fixtures';
import type { UpdateLockedConfig } from '../../types';
import { updateLockedDependency } from './update-locked';
import * as utilFns from './util';
const lockFile = 'terraform.hcl';
const lockFileContent = Fixtures.get('validLockfile.hcl');
describe('modules/manager/terraform/lockfile/update-locked', () => {
it('detects already updated', () => {
const config: UpdateLockedConfig = {
packageFile: 'main.tf',
lockFile,
lockFileContent,
depName: 'hashicorp/aws',
newVersion: '3.0.0',
currentVersion: '3.0.0',
};
expect(updateLockedDependency(config).status).toBe('already-updated');
});
it('returns unsupported if dependency is undefined', () => {
const config: UpdateLockedConfig = {
packageFile: 'main.tf',
lockFile,
lockFileContent,
depName: undefined as never,
newVersion: '3.1.0',
currentVersion: '3.0.0',
};
expect(updateLockedDependency(config).status).toBe('unsupported');
});
it('returns unsupported if lockfileContent is undefined', () => {
const config: UpdateLockedConfig = {
packageFile: 'main.tf',
lockFile,
depName: 'hashicorp/not-there',
newVersion: '3.1.0',
currentVersion: '3.0.0',
};
expect(updateLockedDependency(config).status).toBe('unsupported');
});
it('returns unsupported', () => {
const config: UpdateLockedConfig = {
packageFile: 'main.tf',
lockFile,
lockFileContent,
depName: 'hashicorp/aws',
newVersion: '3.1.0',
currentVersion: '3.0.0',
};
expect(updateLockedDependency(config).status).toBe('unsupported');
});
it('returns update-failed for errors', () => {
const config: UpdateLockedConfig = {
packageFile: 'main.tf',
lockFile,
lockFileContent,
depName: 'hashicorp/aws',
newVersion: '3.1.0',
currentVersion: '3.0.0',
};
jest
.spyOn(utilFns, 'extractLocks')
.mockReturnValueOnce(new Error() as never);
expect(updateLockedDependency(config).status).toBe('update-failed');
});
});
import { logger } from '../../../../logger';
import type { UpdateLockedConfig, UpdateLockedResult } from '../../types';
import { extractLocks } from './util';
export function updateLockedDependency(
config: UpdateLockedConfig
): UpdateLockedResult {
const { depName, currentVersion, newVersion, lockFile, lockFileContent } =
config;
// TODO: fix types (#7154)
logger.debug(
`terraform.updateLockedDependency: ${depName}@${currentVersion!} -> ${newVersion} [${lockFile}]`
);
try {
const locked = extractLocks(lockFileContent ?? '');
const lockedDep = locked?.find((dep) => dep.packageName === depName ?? '');
if (lockedDep?.version === newVersion) {
return { status: 'already-updated' };
}
return { status: 'unsupported' };
} catch (err) {
logger.debug({ err }, 'bundler.updateLockedDependency() error');
return { status: 'update-failed' };
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment