Skip to content

Commit

Permalink
Include .terraform.lock.hcl in CopyTerraformFolderToDest (#1117)
Browse files Browse the repository at this point in the history
This commit adds in the filter from the function
CopyTerragruntFolderToDest an exclusion exception for the lock file.

The `.terraform.lock.hcl` used by Terraform to lock the versions of
the providers is not included during the copy. It can bring weird
behaviors when the copy is skipped (check the function
test_structure.CopyTerraformFolderToDest) where version 1.1 of a
provider would be used in the example folder but might be updated during
the `terraform init` due to the copy without the lock file. The lock
file should be included within the copy to keep the same providers'
versions.

Fix: #1116
  • Loading branch information
Erouan50 committed May 18, 2022
1 parent 4694196 commit 4859f7c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions modules/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func IsExistingDir(path string) bool {
// This is useful when running multiple tests in parallel against the same set of Terraform files to ensure the
// tests don't overwrite each other's .terraform working directory and terraform.tfstate files. This method returns
// the path to the dest folder with the copied contents. Hidden files and folders (with the exception of the `.terraform-version` files used
// by the [tfenv tool](https://github.com/tfutils/tfenv)), Terraform state files, and terraform.tfvars files are not copied to this temp folder,
// as you typically don't want them interfering with your tests.
// by the [tfenv tool](https://github.com/tfutils/tfenv) and `.terraform.lock.hcl` used by Terraform to lock providers versions), Terraform state
// files, and terraform.tfvars files are not copied to this temp folder, as you typically don't want them interfering with your tests.
// This method is useful when running through a build tool so the files are copied to a destination that is cleaned on each run of the pipeline.
func CopyTerraformFolderToDest(folderPath string, destRootFolder string, tempFolderPrefix string) (string, error) {
filter := func(path string) bool {
if PathIsTerraformVersionFile(path) {
if PathIsTerraformVersionFile(path) || PathIsTerraformLockFile(path) {
return true
}
if PathContainsHiddenFileOrFolder(path) || PathContainsTerraformStateOrVars(path) {
Expand Down Expand Up @@ -211,6 +211,11 @@ func PathIsTerraformVersionFile(path string) bool {
return filepath.Base(path) == ".terraform-version"
}

// PathIsTerraformLockFile return true if the given path is the special '.terraform.lock.hcl' file used by Terraform to lock providers versions
func PathIsTerraformLockFile(path string) bool {
return filepath.Base(path) == ".terraform.lock.hcl"
}

// CopyFile copies a file from source to destination.
func CopyFile(source string, destination string) error {
contents, err := ioutil.ReadFile(source)
Expand Down

0 comments on commit 4859f7c

Please sign in to comment.