Skip to content

Commit

Permalink
adapt tests to cache not being used if lockfile is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Jan 4, 2025
1 parent e9dd577 commit eca01cc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
24 changes: 11 additions & 13 deletions src/python/pants/backend/terraform/dependencies_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import annotations

import dataclasses
import json
import textwrap
from pathlib import Path
Expand Down Expand Up @@ -75,9 +74,9 @@ def test_init_terraform(rule_runner: RuleRunner, standard_deployment: StandardDe
assert stub_tfstate["backend"]["config"]["path"] == str(standard_deployment.state_file)

# Assert dependencies are initialised by checking for the dependency itself
assert find_link(
initialised_links,
".terraform/providers/registry.terraform.io/hashicorp/null/*/*",
assert find_file(
initialised_files,
".terraform/providers/registry.terraform.io/hashicorp/null/*/*/terraform-provider-null*",
), "Did not find expected provider"

# Assert lockfile is included
Expand All @@ -90,9 +89,8 @@ def test_init_terraform_uses_lockfiles(
"""Test that we can use generated lockfiles."""
requested_version = "3.2.0"

deployment_with_lockfile = dataclasses.replace(
standard_deployment,
files={**standard_deployment.files, **{"src/tf/.terraform.lock.hcl": terraform_lockfile}},
deployment_with_lockfile = standard_deployment.with_files(
{"src/tf/.terraform.lock.hcl": terraform_lockfile}
)

initialised_files, initialised_entries = _do_init_terraform(
Expand Down Expand Up @@ -130,9 +128,9 @@ def test_init_terraform_without_backends(
), "Terraform state file should not be present if the request was to not initialise the backend"

# The dependencies should still be present
assert find_link(
initialised_entries,
".terraform/providers/registry.terraform.io/hashicorp/null/*/*",
assert find_file(
initialised_files,
".terraform/providers/registry.terraform.io/hashicorp/null/*/*/terraform-provider-null*",
), "Did not find expected provider"


Expand Down Expand Up @@ -192,7 +190,7 @@ def test_init_terraform_with_transitive_module(rule_runner: RuleRunner, tmpdir)
)

# Assert that the provider dependency was initialised
assert find_link(
initialised_entries,
".terraform/providers/registry.terraform.io/hashicorp/null/*/*",
assert find_file(
initialised_files,
".terraform/providers/registry.terraform.io/hashicorp/null/*/*/terraform-provider-null*",
), "Did not find expected provider contained in module, did we successfully include it in the files passed to `init`?"
7 changes: 7 additions & 0 deletions src/python/pants/backend/terraform/testutil.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import annotations

import dataclasses
from dataclasses import dataclass
from pathlib import Path
from textwrap import dedent # noqa: PNT20
Expand Down Expand Up @@ -66,6 +69,10 @@ class StandardDeployment:
state_file: Path
target: Address = Address("src/tf", target_name="stg")

def with_files(self, files: dict[str, str]) -> StandardDeployment:
"""Get a new StandardDeployment with additional files."""
return dataclasses.replace(self, files={**self.files, **files})


@pytest.fixture
def standard_deployment(tmpdir) -> StandardDeployment:
Expand Down
4 changes: 3 additions & 1 deletion src/python/pants/backend/terraform/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ class TerraformProcess:
output_files: tuple[str, ...] = ()
output_directories: tuple[str, ...] = ()
chdir: str = "." # directory for terraform's `-chdir` argument
use_provider_cache: bool = True # The Terraform provider cache is not concurrency-safe for writes (ex generating lockfiles or initialising without a lockfile)
use_provider_cache: bool = (
True # The Terraform provider cache is not concurrency-safe for writes (ex generating lockfiles or initialising without a lockfile)
)


@rule
Expand Down

0 comments on commit eca01cc

Please sign in to comment.