Skip to content

Commit

Permalink
feat(manager/terraform): search lockfile on parent dirs (#22403)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed May 25, 2023
1 parent 0fc01f5 commit a8c8c00
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/modules/manager/terraform/extract.spec.ts
Expand Up @@ -655,7 +655,7 @@ describe('modules/manager/terraform/extract', () => {

it('update lockfile constraints with range strategy update-lockfile', async () => {
fs.readLocalFile.mockResolvedValueOnce(lockedVersionLockfile);
fs.getSiblingFileName.mockReturnValueOnce('aLockFile.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('aLockFile.hcl');

const res = await extractPackageFile(
lockedVersion,
Expand Down
38 changes: 29 additions & 9 deletions lib/modules/manager/terraform/lockfile/index.spec.ts
Expand Up @@ -39,7 +39,19 @@ describe('modules/manager/terraform/lockfile/index', () => {
});

it('returns null if no .terraform.lock.hcl found', async () => {
expect(
await updateArtifacts({
packageFileName: 'main.tf',
updatedDeps: [{ depName: 'aws' }],
newPackageFileContent: '',
config,
})
).toBeNull();
});

it('returns null if .terraform.lock.hcl is empty', async () => {
fs.readLocalFile.mockResolvedValueOnce('');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

expect(
await updateArtifacts({
Expand All @@ -51,8 +63,9 @@ describe('modules/manager/terraform/lockfile/index', () => {
).toBeNull();
});

it('returns null if .terraform.lock.hcl is empty', async () => {
it('returns null if .terraform.lock.hcl is invalid', async () => {
fs.readLocalFile.mockResolvedValueOnce('empty');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

expect(
await updateArtifacts({
Expand All @@ -66,7 +79,7 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('update single dependency with exact constraint and depType provider', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

mockHash.mockResolvedValueOnce([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
Expand Down Expand Up @@ -101,7 +114,7 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('update single dependency with exact constraint and and depType required_provider', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

mockHash.mockResolvedValueOnce([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
Expand Down Expand Up @@ -154,7 +167,7 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('update single dependency with range constraint and minor update from private registry', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

mockHash.mockResolvedValueOnce([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
Expand Down Expand Up @@ -190,7 +203,7 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('update single dependency with range constraint and major update', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

mockHash.mockResolvedValueOnce([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
Expand Down Expand Up @@ -225,7 +238,9 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('update single dependency in subfolder', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.getSiblingFileName.mockReturnValueOnce('test/.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce(
'test/.terraform.lock.hcl'
);

mockHash.mockResolvedValueOnce([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
Expand Down Expand Up @@ -260,7 +275,9 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('update multiple dependencies which are not ordered', async () => {
fs.readLocalFile.mockResolvedValue(validLockfile2);
fs.getSiblingFileName.mockReturnValue('test/.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce(
'test/.terraform.lock.hcl'
);

mockHash.mockResolvedValue([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
Expand Down Expand Up @@ -316,7 +333,7 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('do full lock file maintenance', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.getSiblingFileName.mockReturnValueOnce('.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

mockGetPkgReleases
.mockResolvedValueOnce({
Expand Down Expand Up @@ -389,7 +406,9 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('do full lock file maintenance with lockfile in subfolder', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.getSiblingFileName.mockReturnValueOnce('subfolder/.terraform.lock.hcl');
fs.findLocalSiblingOrParent.mockResolvedValueOnce(
'subfolder/.terraform.lock.hcl'
);

mockGetPkgReleases
.mockResolvedValueOnce({
Expand Down Expand Up @@ -519,6 +538,7 @@ describe('modules/manager/terraform/lockfile/index', () => {

it('return null if hashing fails', async () => {
fs.readLocalFile.mockResolvedValueOnce(validLockfile);
fs.findLocalSiblingOrParent.mockResolvedValueOnce('.terraform.lock.hcl');

mockGetPkgReleases
.mockResolvedValueOnce({
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/manager/terraform/lockfile/index.ts
Expand Up @@ -69,7 +69,13 @@ export async function updateArtifacts({
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`terraform.updateArtifacts(${packageFileName})`);

const lockFilePath = findLockFile(packageFileName);
const lockFilePath = await findLockFile(packageFileName);

if (!lockFilePath) {
logger.debug('No .terraform.lock.hcl found');
return null;
}

try {
const lockFileContent = await readLockFile(lockFilePath);
if (!lockFileContent) {
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/manager/terraform/lockfile/util.ts
@@ -1,4 +1,4 @@
import { getSiblingFileName, readLocalFile } from '../../../../util/fs';
import { findLocalSiblingOrParent, readLocalFile } from '../../../../util/fs';
import { newlineRegex, regEx } from '../../../../util/regex';
import { get as getVersioning } from '../../../versioning';
import type { UpdateArtifactsResult } from '../../types';
Expand All @@ -22,8 +22,8 @@ const hashLineRegex = regEx(`^(?<prefix>\\s*")(?<hash>[^"]+)(?<suffix>",.*)$`);

const lockFile = '.terraform.lock.hcl';

export function findLockFile(packageFilePath: string): string {
return getSiblingFileName(packageFilePath, lockFile);
export function findLockFile(packageFilePath: string): Promise<string | null> {
return findLocalSiblingOrParent(packageFilePath, lockFile);
}

export function readLockFile(lockFilePath: string): Promise<string | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/terraform/util.ts
Expand Up @@ -55,7 +55,7 @@ export async function extractLocksForPackageFile(
fileName: string
): Promise<ProviderLock[]> {
const locks: ProviderLock[] = [];
const lockFilePath = findLockFile(fileName);
const lockFilePath = await findLockFile(fileName);
if (lockFilePath) {
const lockFileContent = await readLockFile(lockFilePath);
if (lockFileContent) {
Expand Down

0 comments on commit a8c8c00

Please sign in to comment.