Skip to content

Commit 985de73

Browse files
authored
chore: runner-pool tests fixes (#4823)
* chore: Added detection of custom HCL file in discovery and runner-pool * Discovery fixes * Documentation update * Added handling of directories as config paths * chore: review notes
1 parent 1c09c91 commit 985de73

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

internal/discovery/discovery.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,16 @@ func (c *DiscoveredConfig) Parse(ctx context.Context, l log.Logger, opts *option
341341
parseOpts.ErrWriter = io.Discard
342342
parseOpts.SkipOutput = true
343343

344+
// If the user provided a specific terragrunt config path and it is not a directory,
345+
// use its base name as the file to parse. This allows users to run terragrunt with
346+
// a specific config file instead of the default terragrunt.hcl.
347+
// Otherwise, use the default terragrunt.hcl filename.
344348
filename := config.DefaultTerragruntConfigPath
349+
if opts.TerragruntConfigPath != "" && !util.IsDir(opts.TerragruntConfigPath) {
350+
filename = filepath.Base(opts.TerragruntConfigPath)
351+
}
345352

353+
// For stack configurations, always use the default stack config filename
346354
if c.Type == ConfigTypeStack {
347355
filename = config.DefaultStackFile
348356
}

internal/runner/runnerpool/builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package runnerpool
22

33
import (
44
"context"
5+
"path/filepath"
56

67
"github.com/gruntwork-io/terragrunt/config"
78
"github.com/gruntwork-io/terragrunt/internal/discovery"
@@ -21,7 +22,7 @@ func Build(ctx context.Context, l log.Logger, terragruntOptions *options.Terragr
2122
WithParseExclude().
2223
WithDiscoverDependencies().
2324
WithSuppressParseErrors().
24-
WithConfigFilenames([]string{config.DefaultTerragruntConfigPath}).
25+
WithConfigFilenames([]string{filepath.Base(terragruntOptions.TerragruntConfigPath)}).
2526
WithDiscoveryContext(&discovery.DiscoveryContext{Cmd: terragruntOptions.TerraformCommand})
2627

2728
// Pass parser options

internal/runner/runnerpool/runner.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,30 @@ func NewRunnerPoolStack(ctx context.Context, l log.Logger, terragruntOptions *op
5454

5555
// Collect all terragrunt.hcl paths for resolution.
5656
unitPaths := make([]string, 0, len(discovered))
57+
5758
for _, cfg := range discovered {
5859
if cfg.Parsed == nil {
5960
// Skip configurations that could not be parsed
6061
l.Warnf("Skipping unit at %s due to parse error", cfg.Path)
6162
continue
6263
}
6364

64-
terragruntConfigPath := config.GetDefaultConfigPath(cfg.Path)
65+
// Determine per-unit config filename
66+
var fname string
67+
if cfg.Type == discovery.ConfigTypeStack {
68+
fname = config.DefaultStackFile
69+
} else {
70+
fname = config.DefaultTerragruntConfigPath
71+
if terragruntOptions.TerragruntConfigPath != "" && !util.IsDir(terragruntOptions.TerragruntConfigPath) {
72+
fname = filepath.Base(terragruntOptions.TerragruntConfigPath)
73+
}
74+
}
75+
76+
terragruntConfigPath := filepath.Join(cfg.Path, fname)
6577
unitPaths = append(unitPaths, terragruntConfigPath)
6678
}
6779

68-
// Resolve units (this applies include/exclude logic and sets FlagExcluded accordingly).
80+
// Resolve units (this applies to include/exclude logic and sets FlagExcluded accordingly).
6981
unitResolver, err := common.NewUnitResolver(ctx, runner.Stack)
7082
if err != nil {
7183
return nil, err

test/integration_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -860,16 +860,12 @@ func TestTerragruntWorksWithNonDefaultConfigNamesAndRunAllCommand(t *testing.T)
860860
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureConfigWithNonDefaultNames)
861861
tmpEnvPath = path.Join(tmpEnvPath, testFixtureConfigWithNonDefaultNames)
862862

863-
stdout := bytes.Buffer{}
864-
stderr := bytes.Buffer{}
865-
866-
err := helpers.RunTerragruntCommand(t, "terragrunt run --all apply --config main.hcl --non-interactive --working-dir "+tmpEnvPath, &stdout, &stderr)
863+
_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt run --all apply --log-level debug --config main.hcl --non-interactive --working-dir "+tmpEnvPath)
867864
require.NoError(t, err)
868865

869-
out := stdout.String()
870-
assert.Equal(t, 1, strings.Count(out, "parent_hcl_file"))
871-
assert.Equal(t, 1, strings.Count(out, "dependency_hcl"))
872-
assert.Equal(t, 1, strings.Count(out, "common_hcl"))
866+
assert.Contains(t, stderr, "run_cmd output: [parent_hcl_file]")
867+
assert.Contains(t, stderr, "run_cmd output: [dependency_hcl]")
868+
assert.Contains(t, stderr, "run_cmd output: [common_hcl]")
873869
}
874870

875871
func TestTerragruntWorksWithNonDefaultConfigNames(t *testing.T) {

0 commit comments

Comments
 (0)