Skip to content

Commit 1baa6a0

Browse files
committed
chore: tests simplification
1 parent c9d8438 commit 1baa6a0

3 files changed

Lines changed: 63 additions & 34 deletions

File tree

internal/hclparse/autoinclude.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ func readAutoIncludeBody(fs vfs.FS, path string) (*hclsyntax.Body, error) {
236236
// extractDependencyConfigPath returns the resolved absolute config_path for a
237237
// dependency block, or ("", false) if the block is not a valid dependency or
238238
// config_path cannot be evaluated to a string.
239+
//
240+
// The nil eval context passed to Expr.Value is intentional: GenerateAutoIncludeFile
241+
// always writes config_path as a literal quoted string via writeDependencyBlock
242+
// (see generate.go), so no variable resolution is required. If that contract is
243+
// ever relaxed to emit interpolations, callers must pass a real eval context
244+
// here or the dependency will be silently dropped from the DAG.
239245
func extractDependencyConfigPath(block *hclsyntax.Block, unitDir string) (string, bool) {
240246
if block.Type != blockDependency || len(block.Labels) == 0 {
241247
return "", false

test/helpers/test_helpers.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package helpers
33
import (
44
"bytes"
55
"io"
6+
"io/fs"
67
"os"
78
"os/exec"
89
"path/filepath"
@@ -381,3 +382,35 @@ func ValidateAuthProviderScript(t *testing.T, dir string, script string) {
381382
err = externalcmd.ValidateResponse(scriptStdout.Bytes())
382383
require.NoError(t, err)
383384
}
385+
386+
// FindCachedFile searches unitDir recursively for the first file whose base
387+
// name equals filename, returning its absolute path. Centralizes the
388+
// .terragrunt-cache layout assumption so tests do not depend on the exact
389+
// nesting depth produced by Terragrunt's source caching.
390+
func FindCachedFile(t *testing.T, unitDir, filename string) string {
391+
t.Helper()
392+
393+
var found string
394+
395+
err := filepath.WalkDir(unitDir, func(path string, d fs.DirEntry, err error) error {
396+
if err != nil {
397+
return err
398+
}
399+
400+
if d.IsDir() {
401+
return nil
402+
}
403+
404+
if filepath.Base(path) != filename {
405+
return nil
406+
}
407+
408+
found = path
409+
410+
return filepath.SkipAll
411+
})
412+
require.NoError(t, err)
413+
require.NotEmpty(t, found, "%s not found under %s", filename, unitDir)
414+
415+
return found
416+
}

test/integration_stack_dependencies_test.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,9 @@ func TestStackDepsE2EBasic(t *testing.T) {
225225

226226
helpers.RunTerragrunt(t, "terragrunt run --all --non-interactive --experiment stack-dependencies --working-dir "+rootPath+" -- apply -auto-approve")
227227

228-
inputFiles, err := filepath.Glob(filepath.Join(rootPath, ".terragrunt-stack", "unit-w-inputs", ".terragrunt-cache", "*", "*", "input.txt"))
229-
require.NoError(t, err)
230-
require.Len(t, inputFiles, 1)
228+
inputPath := helpers.FindCachedFile(t, filepath.Join(rootPath, ".terragrunt-stack", "unit-w-inputs"), "input.txt")
231229

232-
inputContent, err := os.ReadFile(inputFiles[0])
230+
inputContent, err := os.ReadFile(inputPath)
233231
require.NoError(t, err)
234232
assert.Equal(t, "Received: Hello!", string(inputContent))
235233

@@ -258,28 +256,24 @@ func TestStackDepsE2EChain(t *testing.T) {
258256
helpers.RunTerragrunt(t, "terragrunt run --all --non-interactive --experiment stack-dependencies --working-dir "+rootPath+" -- apply -auto-approve")
259257

260258
// Verify unit-a received chained output: from-b(from-c)
261-
markerA, err := filepath.Glob(filepath.Join(rootPath, ".terragrunt-stack", "unit-a", ".terragrunt-cache", "*", "*", "marker.txt"))
262-
require.NoError(t, err)
263-
require.Len(t, markerA, 1)
259+
markerA := helpers.FindCachedFile(t, filepath.Join(rootPath, ".terragrunt-stack", "unit-a"), "marker.txt")
264260

265-
contentA, err := os.ReadFile(markerA[0])
261+
contentA, err := os.ReadFile(markerA)
266262
require.NoError(t, err)
267263
assert.Equal(t, "unit-a received: from-b(from-c)", string(contentA))
268264

269265
// Verify unit-b received: from-c
270-
markerB, err := filepath.Glob(filepath.Join(rootPath, ".terragrunt-stack", "unit-b", ".terragrunt-cache", "*", "*", "marker.txt"))
271-
require.NoError(t, err)
272-
require.Len(t, markerB, 1)
266+
markerB := helpers.FindCachedFile(t, filepath.Join(rootPath, ".terragrunt-stack", "unit-b"), "marker.txt")
273267

274-
contentB, err := os.ReadFile(markerB[0])
268+
contentB, err := os.ReadFile(markerB)
275269
require.NoError(t, err)
276270
assert.Equal(t, "unit-b received: from-c", string(contentB))
277271

278272
helpers.RunTerragrunt(t, "terragrunt run --all --non-interactive --experiment stack-dependencies --working-dir "+rootPath+" -- destroy -auto-approve")
279273

280274
// Destroy must remove the marker files produced by apply.
281-
assert.NoFileExists(t, markerA[0])
282-
assert.NoFileExists(t, markerB[0])
275+
assert.NoFileExists(t, markerA)
276+
assert.NoFileExists(t, markerB)
283277
}
284278

285279
// TestStackDepsE2ECrossStack tests stack generation with cross-stack dependencies:
@@ -523,14 +517,14 @@ func TestStackDepsFindJSON(t *testing.T) {
523517
continue
524518
}
525519

526-
if strings.HasSuffix(c.Path, "unit-w-inputs") {
520+
if filepath.Base(c.Path) == "unit-w-inputs" {
527521
foundInputs = true
528522

529523
require.Len(t, c.Dependencies, 1)
530524
assert.Contains(t, c.Dependencies[0], "unit-w-outputs")
531525
}
532526

533-
if strings.HasSuffix(c.Path, "unit-w-outputs") {
527+
if filepath.Base(c.Path) == "unit-w-outputs" {
534528
foundOutputs = true
535529

536530
assert.Empty(t, c.Dependencies)
@@ -652,11 +646,11 @@ func TestStackDepsFindChain(t *testing.T) {
652646

653647
for path := range depsByPath {
654648
switch {
655-
case strings.HasSuffix(path, "unit-a"):
649+
case filepath.Base(path) == "unit-a":
656650
unitAPath = path
657-
case strings.HasSuffix(path, "unit-b"):
651+
case filepath.Base(path) == "unit-b":
658652
unitBPath = path
659-
case strings.HasSuffix(path, "unit-c"):
653+
case filepath.Base(path) == "unit-c":
660654
unitCPath = path
661655
}
662656
}
@@ -713,15 +707,15 @@ func TestStackDepsFindTree(t *testing.T) {
713707

714708
for path := range depsByPath {
715709
switch {
716-
case strings.HasSuffix(path, "unit-a"):
710+
case filepath.Base(path) == "unit-a":
717711
aPath = path
718-
case strings.HasSuffix(path, "unit-b"):
712+
case filepath.Base(path) == "unit-b":
719713
bPath = path
720-
case strings.HasSuffix(path, "unit-c"):
714+
case filepath.Base(path) == "unit-c":
721715
cPath = path
722-
case strings.HasSuffix(path, "unit-d"):
716+
case filepath.Base(path) == "unit-d":
723717
dPath = path
724-
case strings.HasSuffix(path, "unit-e"):
718+
case filepath.Base(path) == "unit-e":
725719
ePath = path
726720
}
727721
}
@@ -802,26 +796,22 @@ func TestStackDepsE2ETree(t *testing.T) {
802796
helpers.RunTerragrunt(t, "terragrunt run --all --non-interactive --experiment stack-dependencies --working-dir "+rootPath+" -- apply -auto-approve")
803797

804798
// Verify unit-b received outputs from D and E
805-
markerB, err := filepath.Glob(filepath.Join(rootPath, ".terragrunt-stack", "unit-b", ".terragrunt-cache", "*", "*", "marker.txt"))
806-
require.NoError(t, err)
807-
require.Len(t, markerB, 1)
799+
markerB := helpers.FindCachedFile(t, filepath.Join(rootPath, ".terragrunt-stack", "unit-b"), "marker.txt")
808800

809-
contentB, err := os.ReadFile(markerB[0])
801+
contentB, err := os.ReadFile(markerB)
810802
require.NoError(t, err)
811803
assert.Equal(t, "unit-b(from-d,from-e)", string(contentB))
812804

813805
// Verify unit-a received outputs from B and C
814-
markerA, err := filepath.Glob(filepath.Join(rootPath, ".terragrunt-stack", "unit-a", ".terragrunt-cache", "*", "*", "marker.txt"))
815-
require.NoError(t, err)
816-
require.Len(t, markerA, 1)
806+
markerA := helpers.FindCachedFile(t, filepath.Join(rootPath, ".terragrunt-stack", "unit-a"), "marker.txt")
817807

818-
contentA, err := os.ReadFile(markerA[0])
808+
contentA, err := os.ReadFile(markerA)
819809
require.NoError(t, err)
820810
assert.Equal(t, "unit-a(from-b(from-d,from-e),from-c)", string(contentA))
821811

822812
helpers.RunTerragrunt(t, "terragrunt run --all --non-interactive --experiment stack-dependencies --working-dir "+rootPath+" -- destroy -auto-approve")
823813

824814
// Destroy must remove the marker files produced by apply.
825-
assert.NoFileExists(t, markerA[0])
826-
assert.NoFileExists(t, markerB[0])
815+
assert.NoFileExists(t, markerA)
816+
assert.NoFileExists(t, markerB)
827817
}

0 commit comments

Comments
 (0)