Skip to content

Commit

Permalink
fix #2072 make sure the generated code is formatted before writing …
Browse files Browse the repository at this point in the history
…it to a file.
  • Loading branch information
wakeful committed Feb 24, 2025
1 parent 237f07a commit db053bd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
19 changes: 16 additions & 3 deletions codegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsimple"

"github.com/hashicorp/hcl/v2/hclwrite"
ctyjson "github.com/zclconf/go-cty/cty/json"

Expand Down Expand Up @@ -87,6 +86,7 @@ type GenerateConfig struct {
Contents string `cty:"contents"`
DisableSignature bool `cty:"disable_signature"`
Disable bool `cty:"disable"`
HclFmt bool `cty:"hcl_fmt"`
}

// WriteToFile will generate a new file at the given target path with the given contents. If a file already exists at
Expand Down Expand Up @@ -135,10 +135,23 @@ func WriteToFile(terragruntOptions *options.TerragruntOptions, basePath string,
prefix = fmt.Sprintf("%s%s\n", config.CommentPrefix, TerragruntGeneratedSignature)
}

contentsToWrite := fmt.Sprintf("%s%s", prefix, config.Contents)
contentsToWrite := []byte(fmt.Sprintf("%s%s", prefix, config.Contents))

if config.HclFmt {
var fmtExt = map[string]struct{}{
".hcl": {},
".tf": {},
".tofu": {},
}

ext := filepath.Ext(config.Path)
if _, ok := fmtExt[ext]; ok {
contentsToWrite = hclwrite.Format(contentsToWrite)
}
}

const ownerWriteGlobalReadPerms = 0644
if err := os.WriteFile(targetPath, []byte(contentsToWrite), ownerWriteGlobalReadPerms); err != nil {
if err := os.WriteFile(targetPath, contentsToWrite, ownerWriteGlobalReadPerms); err != nil {
return errors.New(err)
}

Expand Down
68 changes: 68 additions & 0 deletions codegen/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codegen_test
import (
"bytes"
"fmt"
"os"
"testing"

"github.com/gruntwork-io/terragrunt/codegen"
Expand Down Expand Up @@ -144,6 +145,73 @@ func TestRemoteStateConfigToTerraformCode(t *testing.T) {
}
}

func TestFmtGeneratedFile(t *testing.T) {
t.Parallel()

testDir := t.TempDir()

tc := []struct {
name string
disabled bool
fmt bool
path string
contents string
expected string
ifExists codegen.GenerateConfigExists
}{
{
"fmt-simple-hcl-file",
false,
true,
fmt.Sprintf("%s/%s", testDir, "fmt_simple.hcl"),
"variable \"msg\"{\ntype=string\n default=\"hello\"\n}\n",
"variable \"msg\" {\n type = string\n default = \"hello\"\n}\n",
codegen.ExistsError,
},
{
"ignore-hcl-fmt",
false,
false,
fmt.Sprintf("%s/%s", testDir, "ignore_fmt.hcl"),
"variable \"msg\"{\ntype=string\n default=\"hello\"\n}\n",
"variable \"msg\"{\ntype=string\n default=\"hello\"\n}\n",
codegen.ExistsError,
},
}

for _, tt := range tc {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

config := codegen.GenerateConfig{
Path: tt.path,
IfExists: tt.ifExists,
CommentPrefix: "",
DisableSignature: true,
Contents: tt.contents,
Disable: tt.disabled,
HclFmt: tt.fmt,
}

opts, err := options.NewTerragruntOptionsForTest("mock-path-for-test.hcl")
require.NoError(t, err)
assert.NotNil(t, opts)

err = codegen.WriteToFile(opts, "", config)
require.NoError(t, err)

assert.True(t, util.FileExists(tt.path))

fileContent, err := os.ReadFile(tt.path)
require.NoError(t, err)

assert.Equal(t, tt.expected, string(fileContent))
})
}
}

func TestGenerateDisabling(t *testing.T) {
t.Parallel()

Expand Down
1 change: 1 addition & 0 deletions docs/_docs/04_reference/04-config-blocks-and-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,7 @@ The `generate` block supports the following arguments:
`false`. Optional.
- `contents` (attribute): The contents of the generated file.
- `disable` (attribute): Disables this generate block.
- `hcl_fmt` (attribute): When enabled, files with `.hcl`, `.tf`, and `.tofu` extensions will be formatted before being written to disk.

Example:

Expand Down

0 comments on commit db053bd

Please # to comment.