Skip to content

Commit

Permalink
Updated CopyTerraformFolderToTemp to include .terraform-version file (#…
Browse files Browse the repository at this point in the history
…963)

The CopyTerraformFolderToTemp method will now copy the '.terraform-version' file if it exists in the source folder. This adds support for users who use the 'tfenv' tool to enforce that a certain version of Terraform is used in deployments and testing.
  • Loading branch information
yardbirdsax authored Dec 10, 2021
1 parent 05b96f7 commit f4f2459
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
19 changes: 15 additions & 4 deletions modules/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ func IsExistingDir(path string) bool {
// CopyTerraformFolderToTemp creates a copy of the given folder and all its contents in a temp folder with a unique name and the given prefix.
// 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 temp folder with the copied contents. Hidden files and folders, 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.
// the path to the temp 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.
func CopyTerraformFolderToTemp(folderPath string, tempFolderPrefix string) (string, error) {
filter := func(path string) bool {
return !PathContainsHiddenFileOrFolder(path) && !PathContainsTerraformStateOrVars(path)
if PathIsTerraformVersionFile(path) {
return true
}
if PathContainsHiddenFileOrFolder(path) || PathContainsTerraformStateOrVars(path) {
return false
}
return true
}

destFolder, err := CopyFolderToTemp(folderPath, tempFolderPrefix, filter)
Expand Down Expand Up @@ -176,6 +182,11 @@ func PathContainsHiddenFileOrFolder(path string) bool {
return false
}

// PathIsTerraformVersionFile returns true if the given path is the special '.terraform-version' file used by the [tfenv](https://github.com/tfutils/tfenv) tool.
func PathIsTerraformVersionFile(path string) bool {
return filepath.Base(path) == ".terraform-version"
}

// CopyFile copies a file from source to destination.
func CopyFile(source string, destination string) error {
contents, err := ioutil.ReadFile(source)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.15.5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.15.5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.15.5

0 comments on commit f4f2459

Please sign in to comment.