Skip to content

Commit f4f2459

Browse files
authored
Updated CopyTerraformFolderToTemp to include .terraform-version file (#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.
1 parent 05b96f7 commit f4f2459

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

modules/files/files.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ func IsExistingDir(path string) bool {
4242
// CopyTerraformFolderToTemp creates a copy of the given folder and all its contents in a temp folder with a unique name and the given prefix.
4343
// This is useful when running multiple tests in parallel against the same set of Terraform files to ensure the
4444
// tests don't overwrite each other's .terraform working directory and terraform.tfstate files. This method returns
45-
// the path to the temp folder with the copied contents. Hidden files and folders, Terraform state files, and
46-
// terraform.tfvars files are not copied to this temp folder, as you typically don't want them interfering with your
47-
// tests.
45+
// the path to the temp folder with the copied contents. Hidden files and folders (with the exception of the `.terraform-version` files used
46+
// by the [tfenv tool](https://github.com/tfutils/tfenv)), Terraform state files, and terraform.tfvars files are not copied to this temp folder,
47+
// as you typically don't want them interfering with your tests.
4848
func CopyTerraformFolderToTemp(folderPath string, tempFolderPrefix string) (string, error) {
4949
filter := func(path string) bool {
50-
return !PathContainsHiddenFileOrFolder(path) && !PathContainsTerraformStateOrVars(path)
50+
if PathIsTerraformVersionFile(path) {
51+
return true
52+
}
53+
if PathContainsHiddenFileOrFolder(path) || PathContainsTerraformStateOrVars(path) {
54+
return false
55+
}
56+
return true
5157
}
5258

5359
destFolder, err := CopyFolderToTemp(folderPath, tempFolderPrefix, filter)
@@ -176,6 +182,11 @@ func PathContainsHiddenFileOrFolder(path string) bool {
176182
return false
177183
}
178184

185+
// PathIsTerraformVersionFile returns true if the given path is the special '.terraform-version' file used by the [tfenv](https://github.com/tfutils/tfenv) tool.
186+
func PathIsTerraformVersionFile(path string) bool {
187+
return filepath.Base(path) == ".terraform-version"
188+
}
189+
179190
// CopyFile copies a file from source to destination.
180191
func CopyFile(source string, destination string) error {
181192
contents, err := ioutil.ReadFile(source)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.15.5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.15.5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.15.5

0 commit comments

Comments
 (0)