Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(manager/terraform): search lockfile on parent dirs #22403

Merged
merged 3 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
32 changes: 23 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,7 @@ 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 +273,7 @@ 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 +329,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 +402,7 @@ 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 +532,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