Skip to content

Commit 686eb1e

Browse files
committed
fix: Fixing TestTerragruntWorksWithNonDefaultConfigNamesAndRunAllCommand
1 parent ef19257 commit 686eb1e

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

internal/runner/runnerpool/helpers_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestBuildCanonicalConfigPath_DirectoryPath(t *testing.T) {
2121
tmpDir := t.TempDir()
2222
unit := component.NewUnit(tmpDir)
2323

24-
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir)
24+
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir, config.DefaultTerragruntConfigPath)
2525

2626
require.NoError(t, err)
2727
assert.Equal(t, filepath.Join(tmpDir, config.DefaultTerragruntConfigPath), canonicalPath)
@@ -36,7 +36,7 @@ func TestBuildCanonicalConfigPath_HCLSuffix(t *testing.T) {
3636
configPath := filepath.Join(tmpDir, "terragrunt.hcl")
3737
unit := component.NewUnit(configPath)
3838

39-
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir)
39+
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir, config.DefaultTerragruntConfigPath)
4040

4141
require.NoError(t, err)
4242
assert.Equal(t, configPath, canonicalPath)
@@ -50,7 +50,7 @@ func TestBuildCanonicalConfigPath_JSONSuffix(t *testing.T) {
5050
configPath := filepath.Join(tmpDir, "terragrunt.hcl.json")
5151
unit := component.NewUnit(configPath)
5252

53-
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir)
53+
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir, config.DefaultTerragruntConfigPath)
5454

5555
require.NoError(t, err)
5656
assert.Equal(t, configPath, canonicalPath)
@@ -66,7 +66,7 @@ func TestBuildCanonicalConfigPath_RelativePath(t *testing.T) {
6666

6767
unit := component.NewUnit("subdir")
6868

69-
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir)
69+
canonicalPath, canonicalDir, err := buildCanonicalConfigPath(unit, tmpDir, config.DefaultTerragruntConfigPath)
7070

7171
require.NoError(t, err)
7272
assert.Equal(t, filepath.Join(subDir, config.DefaultTerragruntConfigPath), canonicalPath)

internal/runner/runnerpool/runner.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ type Runner struct {
4040
}
4141

4242
// buildCanonicalConfigPath computes the canonical config path for a unit.
43-
// It handles .hcl/.json suffixes, joins DefaultTerragruntConfigPath when needed,
43+
// It handles .hcl/.json suffixes, joins an appropriate config filename when needed,
4444
// converts to canonical absolute path, and updates the unit's path.
4545
// Returns the canonical config path and the canonical unit directory.
46-
func buildCanonicalConfigPath(unit *component.Unit, basePath string) (canonicalConfigPath string, canonicalDir string, err error) {
47-
// Discovery paths are directories (e.g., "./other") not files
48-
// We need to append the config filename to get the full config path
46+
func buildCanonicalConfigPath(
47+
unit *component.Unit,
48+
basePath string,
49+
configFileName string,
50+
) (canonicalConfigPath string, canonicalDir string, err error) {
4951
terragruntConfigPath := unit.Path()
5052
if !strings.HasSuffix(terragruntConfigPath, ".hcl") && !strings.HasSuffix(terragruntConfigPath, ".json") {
51-
terragruntConfigPath = filepath.Join(unit.Path(), config.DefaultTerragruntConfigPath)
53+
fileName := config.DefaultTerragruntConfigPath
54+
if configFileName != "" && configFileName != "." {
55+
fileName = configFileName
56+
}
57+
58+
terragruntConfigPath = filepath.Join(unit.Path(), fileName)
5259
}
5360

5461
// Convert to canonical absolute path - this is critical for dependency resolution
@@ -152,6 +159,10 @@ func resolveUnitsFromDiscovery(
152159
}
153160

154161
basePath := stack.Path()
162+
configFileName := config.DefaultTerragruntConfigPath
163+
if stack.Execution != nil && stack.Execution.TerragruntOptions != nil {
164+
configFileName = filepath.Base(stack.Execution.TerragruntOptions.TerragruntConfigPath)
165+
}
155166

156167
for _, c := range discovered {
157168
unit, ok := c.(*component.Unit)
@@ -160,7 +171,7 @@ func resolveUnitsFromDiscovery(
160171
}
161172

162173
// Build canonical config path and update unit path
163-
canonicalConfigPath, canonicalDir, err := buildCanonicalConfigPath(unit, basePath)
174+
canonicalConfigPath, canonicalDir, err := buildCanonicalConfigPath(unit, basePath, configFileName)
164175
if err != nil {
165176
return nil, err
166177
}
@@ -861,22 +872,22 @@ func FilterDiscoveredUnits(discovered component.Components, units []*component.U
861872

862873
// Add any missing allowed dependencies from the resolved unit graph
863874
for _, dep := range u.Dependencies() {
864-
depUnit, ok := dep.(*component.Unit)
865-
if !ok || depUnit == nil {
875+
depUnit, okDep := dep.(*component.Unit)
876+
if !okDep || depUnit == nil {
866877
continue
867878
}
868879

869-
if _, ok := allowed[depUnit.Path()]; !ok {
880+
if _, allowedOK := allowed[depUnit.Path()]; !allowedOK {
870881
continue
871882
}
872883

873-
if _, ok := existing[depUnit.Path()]; ok {
884+
if _, existsOK := existing[depUnit.Path()]; existsOK {
874885
continue
875886
}
876887

877888
// Ensure the dependency config exists in the filtered set
878-
depCfg, ok := present[depUnit.Path()]
879-
if !ok {
889+
depCfg, presentOK := present[depUnit.Path()]
890+
if !presentOK {
880891
depCfg = component.NewUnit(depUnit.Path())
881892
filtered = append(filtered, depCfg)
882893
present[depUnit.Path()] = depCfg

0 commit comments

Comments
 (0)