Skip to content

Commit 74d9df0

Browse files
authored
Merge pull request #1376 from gruntwork-io/bug/validate-all
Fix root path lookup in validate-all
2 parents 28c0f43 + 11e2d26 commit 74d9df0

File tree

3 files changed

+78
-18
lines changed

3 files changed

+78
-18
lines changed

modules/git/git.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package git
33

44
import (
5+
"os"
56
"os/exec"
67
"strings"
78

@@ -106,7 +107,24 @@ func GetRepoRoot(t testing.TestingT) string {
106107

107108
// GetRepoRootE retrieves the path to the root directory of the repo.
108109
func GetRepoRootE(t testing.TestingT) (string, error) {
110+
dir, err := os.Getwd()
111+
if err != nil {
112+
return "", err
113+
}
114+
return GetRepoRootForDirE(t, dir)
115+
}
116+
117+
// GetRepoRootForDir retrieves the path to the root directory of the repo in which dir resides
118+
func GetRepoRootForDir(t testing.TestingT, dir string) string {
119+
out, err := GetRepoRootForDirE(t, dir)
120+
require.NoError(t, err)
121+
return out
122+
}
123+
124+
// GetRepoRootForDirE retrieves the path to the root directory of the repo in which dir resides
125+
func GetRepoRootForDirE(t testing.TestingT, dir string) (string, error) {
109126
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
127+
cmd.Dir = dir
110128
bytes, err := cmd.Output()
111129
if err != nil {
112130
return "", err

modules/test-structure/test_structure.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"path/filepath"
77
"strings"
88

9+
"github.com/gruntwork-io/terratest/modules/git"
10+
911
go_test "testing"
1012

1113
"github.com/gruntwork-io/terratest/modules/files"
@@ -192,29 +194,33 @@ func runValidateOnAllTerraformModules(
192194
opts *ValidationOptions,
193195
validationFunc func(t *go_test.T, fileType ValidateFileType, tfOps *terraform.Options),
194196
) {
195-
dirsToValidate, readErr := FindTerraformModulePathsInRootE(opts)
197+
// Find the Git root
198+
gitRoot, err := git.GetRepoRootForDirE(t, opts.RootDir)
199+
require.NoError(t, err)
200+
201+
// Find the relative path between the root dir and the git root
202+
relPath, err := filepath.Rel(gitRoot, opts.RootDir)
203+
require.NoError(t, err)
204+
205+
// Copy git root to tmp
206+
testFolder := CopyTerraformFolderToTemp(t, gitRoot, relPath)
207+
require.NotNil(t, testFolder)
208+
209+
// Clone opts and override the root dir to the temp folder
210+
clonedOpts, err := CloneWithNewRootDir(opts, testFolder)
211+
require.NoError(t, err)
212+
213+
// Find TF modules
214+
dirsToValidate, readErr := FindTerraformModulePathsInRootE(clonedOpts)
196215
require.NoError(t, readErr)
197216

198217
for _, dir := range dirsToValidate {
199218
dir := dir
200219
t.Run(strings.TrimLeft(dir, "/"), func(t *go_test.T) {
201-
// Determine the absolute path to the git repository root
202-
cwd, cwdErr := os.Getwd()
203-
require.NoError(t, cwdErr)
204-
gitRoot, gitRootErr := filepath.Abs(filepath.Join(cwd, "../../"))
205-
require.NoError(t, gitRootErr)
206-
207-
// Determine the relative path to the example, module, etc that is currently being considered
208-
relativePath, pathErr := filepath.Rel(gitRoot, dir)
209-
require.NoError(t, pathErr)
210-
// Copy git root to tmp and supply the path to the current module to run init and validate on
211-
testFolder := CopyTerraformFolderToTemp(t, gitRoot, relativePath)
212-
require.NotNil(t, testFolder)
213-
214-
// Run Terraform init and terraform validate on the test folder that was copied to /tmp
215-
// to avoid any potential conflicts with tests that may not use the same copy to /tmp behavior
216-
tfOpts := &terraform.Options{TerraformDir: testFolder}
217-
validationFunc(t, opts.FileType, tfOpts)
220+
// Run the validation function on the test folder that was copied to /tmp to avoid any potential conflicts
221+
// with tests that may not use the same copy to /tmp behavior
222+
tfOpts := &terraform.Options{TerraformDir: dir}
223+
validationFunc(t, clonedOpts.FileType, tfOpts)
218224
})
219225
}
220226
}

modules/test-structure/validate_struct.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ type ValidationOptions struct {
4242
ExcludeDirs []string
4343
}
4444

45+
// CloneWithNewRootDir clones the given opts with a new root dir. Updates all include and exclude dirs to be relative
46+
// to the new root dir.
47+
func CloneWithNewRootDir(opts *ValidationOptions, newRootDir string) (*ValidationOptions, error) {
48+
includeDirs, err := buildRelPathsFromFull(opts.RootDir, opts.IncludeDirs)
49+
if err != nil {
50+
return nil, err
51+
}
52+
excludeDirs, err := buildRelPathsFromFull(opts.RootDir, opts.ExcludeDirs)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
out, err := NewValidationOptions(newRootDir, includeDirs, excludeDirs)
58+
if err != nil {
59+
return nil, err
60+
}
61+
out.FileType = opts.FileType
62+
return out, nil
63+
}
64+
4565
// configureBaseValidationOptions returns a pointer to a ValidationOptions struct configured with sane, override-able defaults
4666
// Note that the ValidationOptions's fields IncludeDirs and ExcludeDirs must be absolute paths, but this method will accept relative paths
4767
// and build the absolute paths when instantiating the ValidationOptions struct, making it the preferred means of configuring
@@ -104,6 +124,22 @@ func NewTerragruntValidationOptions(rootDir string, includeDirs, excludeDirs []s
104124
return opts, nil
105125
}
106126

127+
func buildRelPathsFromFull(rootDir string, fullPaths []string) ([]string, error) {
128+
var relPaths []string
129+
for _, maybeFullPath := range fullPaths {
130+
if filepath.IsAbs(maybeFullPath) {
131+
relPath, err := filepath.Rel(rootDir, maybeFullPath)
132+
if err != nil {
133+
return nil, err
134+
}
135+
relPaths = append(relPaths, relPath)
136+
} else {
137+
relPaths = append(relPaths, maybeFullPath)
138+
}
139+
}
140+
return relPaths, nil
141+
}
142+
107143
func buildFullPathsFromRelative(rootDir string, relativePaths []string) []string {
108144
var fullPaths []string
109145
for _, maybeRelativePath := range relativePaths {

0 commit comments

Comments
 (0)