|
6 | 6 | "path/filepath" |
7 | 7 | "strings" |
8 | 8 |
|
| 9 | + "github.com/gruntwork-io/terratest/modules/git" |
| 10 | + |
9 | 11 | go_test "testing" |
10 | 12 |
|
11 | 13 | "github.com/gruntwork-io/terratest/modules/files" |
@@ -192,29 +194,33 @@ func runValidateOnAllTerraformModules( |
192 | 194 | opts *ValidationOptions, |
193 | 195 | validationFunc func(t *go_test.T, fileType ValidateFileType, tfOps *terraform.Options), |
194 | 196 | ) { |
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) |
196 | 215 | require.NoError(t, readErr) |
197 | 216 |
|
198 | 217 | for _, dir := range dirsToValidate { |
199 | 218 | dir := dir |
200 | 219 | 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) |
218 | 224 | }) |
219 | 225 | } |
220 | 226 | } |
0 commit comments