Skip to content

Commit a103528

Browse files
committed
feat: add generate_*.inherit attribute.
Signed-off-by: Tiago Natel <t.nateldemoura@gmail.com>
1 parent 7bb34e3 commit a103528

10 files changed

Lines changed: 1156 additions & 149 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
2424

2525
### Added
2626

27+
- Add `generate_*.inherit` attribute for controlling if generate blocks must be inherited
28+
into child stacks.
2729
- Add `terramate cloud login --github` for authenticating with the Github account.
2830
- Add experimental support for `tmgen` file extension for easy code generation/templating
2931
of existing infrastructure. You can enable it with `terramate.config.experimental = ["tmgen"]`.

cmd/terramate/e2etests/core/generate_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,60 @@ func TestGenerateWarnsForTmGenNonInheritable(t *testing.T) {
475475
})
476476
}
477477

478+
func TestGenerateFileNonInheritableOutsideStackGeneratesWarning(t *testing.T) {
479+
t.Parallel()
480+
481+
s := sandbox.NoGit(t, true)
482+
s.BuildTree([]string{
483+
"d:non-stack",
484+
})
485+
486+
s.RootEntry().
487+
CreateDir("non-stack").
488+
CreateFile(
489+
config.DefaultFilename,
490+
GenerateFile(
491+
Labels("test.txt"),
492+
Bool("inherit", false),
493+
Str("content", "test"),
494+
).String(),
495+
)
496+
497+
tmcli := NewCLI(t, s.RootDir())
498+
AssertRunResult(t, tmcli.Run("generate"), RunExpected{
499+
Stdout: "Nothing to do, generated code is up to date\n",
500+
StderrRegex: "Warning: non-inheritable generate found outside a stack",
501+
})
502+
}
503+
504+
func TestGenerateHCLNonInheritableOutsideStackGeneratesWarning(t *testing.T) {
505+
t.Parallel()
506+
507+
s := sandbox.NoGit(t, true)
508+
s.BuildTree([]string{
509+
"d:non-stack",
510+
})
511+
512+
s.RootEntry().
513+
CreateDir("non-stack").
514+
CreateFile(
515+
config.DefaultFilename,
516+
GenerateHCL(
517+
Labels("test.txt"),
518+
Bool("inherit", false),
519+
Content(
520+
Str("hello", "world"),
521+
),
522+
).String(),
523+
)
524+
525+
tmcli := NewCLI(t, s.RootDir())
526+
AssertRunResult(t, tmcli.Run("generate"), RunExpected{
527+
Stdout: "Nothing to do, generated code is up to date\n",
528+
StderrRegex: "Warning: non-inheritable generate found outside a stack",
529+
})
530+
}
531+
478532
type str string
479533

480534
func (s str) String() string {

config/config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,22 @@ func processTmGenFiles(root *Root, cfg *hcl.Config, cfgdir string, dirEntries []
508508
Type: "content",
509509
Body: body,
510510
}
511+
512+
// should never fail
513+
inheritFalse, diags := hclsyntax.ParseExpression([]byte("false"), "<implicit block>", hhcl.InitialPos)
514+
if diags.HasErrors() {
515+
panic(errors.E(errors.ErrInternal, diags))
516+
}
517+
518+
inheritAttr := &hclsyntax.Attribute{
519+
Name: "inherit",
520+
Expr: inheritFalse,
521+
}
522+
511523
implicitGenBlock := hcl.GenHCLBlock{
512524
IsImplicitBlock: true,
513525
Dir: project.PrjAbsPath(root.HostDir(), cfgdir),
514-
NonInheritable: true,
526+
Inherit: inheritAttr,
515527
Range: info.NewRange(root.HostDir(), hhcl.Range{
516528
Filename: absFname,
517529
Start: hhcl.InitialPos,

0 commit comments

Comments
 (0)