Skip to content

Commit 23d377d

Browse files
committed
fix: format of tmgen files.
Signed-off-by: i4k <[email protected]>
1 parent f670dd3 commit 23d377d

File tree

7 files changed

+131
-74
lines changed

7 files changed

+131
-74
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
3030

3131
- Nested `map` blocks not rendered if the `value` block contain no attributes.
3232
- Fix `terramate fmt` not respecting the `.tmskip` file.
33+
- Fix the `terramate fmt` not recursively formatting `.tmgen` files.
3334

3435
## v0.11.1
3536

config/config.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type Tree struct {
6060

6161
TerramateFiles []string
6262
OtherFiles []string
63+
TmGenFiles []string
6364

6465
// Children is a map of configuration dir names to tree nodes.
6566
Children map[string]*Tree
@@ -442,12 +443,12 @@ func loadTree(parentTree *Tree, cfgdir string, rootcfg *hcl.Config) (_ *Tree, er
442443
Str("dir", cfgdir).
443444
Logger()
444445

445-
tmFiles, otherFiles, dirs, err := fs.ListTerramateFiles(cfgdir)
446+
filesResult, err := fs.ListTerramateFiles(cfgdir)
446447
if err != nil {
447448
return nil, err
448449
}
449450

450-
for _, fname := range otherFiles {
451+
for _, fname := range filesResult.OtherFiles {
451452
if fname == terramate.SkipFilename {
452453
logger.Debug().Msg("skip file found: skipping whole subtree")
453454
return newSkippedTree(cfgdir), nil
@@ -466,7 +467,7 @@ func loadTree(parentTree *Tree, cfgdir string, rootcfg *hcl.Config) (_ *Tree, er
466467
if err != nil {
467468
return nil, err
468469
}
469-
for _, filename := range tmFiles {
470+
for _, filename := range filesResult.TmFiles {
470471
path := filepath.Join(cfgdir, filename)
471472

472473
data, err := os.ReadFile(path)
@@ -488,20 +489,21 @@ func loadTree(parentTree *Tree, cfgdir string, rootcfg *hcl.Config) (_ *Tree, er
488489
}
489490

490491
tree.Node = cfg
491-
tree.TerramateFiles = tmFiles
492-
tree.OtherFiles = otherFiles
492+
tree.TerramateFiles = filesResult.TmFiles
493+
tree.OtherFiles = filesResult.OtherFiles
494+
tree.TmGenFiles = filesResult.TmGenFiles
493495
tree.Parent = parentTree
494496
parentTree.Children[filepath.Base(cfgdir)] = tree
495497

496498
parentTree = tree
497499
}
498500

499-
err = processTmGenFiles(parentTree.RootTree(), &parentTree.Node, cfgdir, otherFiles)
501+
err = processTmGenFiles(parentTree.RootTree(), &parentTree.Node, cfgdir, filesResult.TmGenFiles)
500502
if err != nil {
501503
return nil, err
502504
}
503505

504-
for _, fname := range dirs {
506+
for _, fname := range filesResult.Dirs {
505507
if Skip(fname) {
506508
continue
507509
}
@@ -525,10 +527,6 @@ func processTmGenFiles(rootTree *Tree, cfg *hcl.Config, cfgdir string, files []s
525527

526528
// process all .tmgen files.
527529
for _, fname := range files {
528-
if !strings.HasSuffix(fname, tmgenSuffix) || fname[0] == '.' {
529-
continue
530-
}
531-
532530
absFname := filepath.Join(cfgdir, fname)
533531

534532
if !tmgenEnabled {

e2etests/core/fmt_test.go

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,49 @@ package core_test
66
import (
77
stdfmt "fmt"
88
"path/filepath"
9+
"sort"
910
"strings"
1011
"testing"
1112

1213
"github.com/madlambda/spells/assert"
1314
. "github.com/terramate-io/terramate/e2etests/internal/runner"
1415
"github.com/terramate-io/terramate/hcl/fmt"
16+
. "github.com/terramate-io/terramate/test/hclwrite/hclutils"
1517
"github.com/terramate-io/terramate/test/sandbox"
1618
)
1719

1820
func TestFormatRecursively(t *testing.T) {
1921
t.Parallel()
2022

21-
const unformattedHCL = `
23+
const unformattedTmFile = `
2224
globals {
2325
name = "name"
2426
description = "desc"
2527
test = true
2628
}
2729
`
28-
formattedHCL, err := fmt.Format(unformattedHCL, "")
30+
const unformattedTmGenFile = `
31+
block {
32+
a = 1
33+
name = "test"
34+
}
35+
`
36+
formattedTmContent, err := fmt.Format(unformattedTmFile, "")
2937
assert.NoError(t, err)
30-
38+
formattedTmGenContent := `
39+
block {
40+
a = 1
41+
name = "test"
42+
}
43+
`
3144
s := sandbox.New(t)
45+
s.BuildTree([]string{
46+
"f:enable.tmgen.tm:" + Terramate(
47+
Config(
48+
Expr("experiments", `["tmgen"]`),
49+
),
50+
).String(),
51+
})
3252
cli := NewCLI(t, s.RootDir())
3353

3454
t.Run("checking succeeds when there is no Terramate files", func(t *testing.T) {
@@ -42,23 +62,25 @@ name = "name"
4262
sprintf := stdfmt.Sprintf
4363
writeUnformattedFiles := func() {
4464
s.BuildTree([]string{
45-
sprintf("f:globals.tm:%s", unformattedHCL),
46-
sprintf("f:another-stacks/globals.tm.hcl:%s", unformattedHCL),
47-
sprintf("f:another-stacks/stack-1/globals.tm.hcl:%s", unformattedHCL),
48-
sprintf("f:another-stacks/stack-2/globals.tm.hcl:%s", unformattedHCL),
49-
sprintf("f:stacks/globals.tm:%s", unformattedHCL),
50-
sprintf("f:stacks/stack-1/globals.tm:%s", unformattedHCL),
51-
sprintf("f:stacks/stack-2/globals.tm:%s", unformattedHCL),
65+
sprintf("f:globals.tm:%s", unformattedTmFile),
66+
sprintf("f:test.hcl.tmgen:%s", unformattedTmGenFile),
67+
sprintf("f:another-stacks/globals.tm.hcl:%s", unformattedTmFile),
68+
sprintf("f:another-stacks/stack-1/globals.tm.hcl:%s", unformattedTmFile),
69+
sprintf("f:another-stacks/stack-2/globals.tm.hcl:%s", unformattedTmFile),
70+
sprintf("f:stacks/globals.tm:%s", unformattedTmFile),
71+
sprintf("f:stacks/test.hcl.tmgen:%s", unformattedTmGenFile),
72+
sprintf("f:stacks/stack-1/globals.tm:%s", unformattedTmFile),
73+
sprintf("f:stacks/stack-2/globals.tm:%s", unformattedTmFile),
5274

5375
// dir below must always be ignored
5476
`f:skipped-dir/.tmskip:`,
55-
sprintf("f:skipped-dir/globals.tm:%s", unformattedHCL),
77+
sprintf("f:skipped-dir/globals.tm:%s", unformattedTmFile),
5678
})
5779
}
5880

5981
writeUnformattedFiles()
6082

61-
wantedFiles := []string{
83+
wantedTmFiles := []string{
6284
"globals.tm",
6385
"another-stacks/globals.tm.hcl",
6486
"another-stacks/stack-1/globals.tm.hcl",
@@ -67,26 +89,35 @@ name = "name"
6789
"stacks/stack-1/globals.tm",
6890
"stacks/stack-2/globals.tm",
6991
}
70-
filesListOutput := func(files []string) string {
71-
portablewantedFiles := make([]string, len(files))
72-
for i, f := range files {
73-
portablewantedFiles[i] = filepath.FromSlash(f)
92+
wantedTmGenFiles := []string{
93+
"test.hcl.tmgen",
94+
"stacks/test.hcl.tmgen",
95+
}
96+
filesListOutput := func(tmFiles []string, tmGenFiles []string) string {
97+
portableFiles := []string{}
98+
files := append(append([]string{}, tmFiles...), tmGenFiles...)
99+
sort.Strings(files)
100+
for _, f := range files {
101+
portableFiles = append(portableFiles, filepath.FromSlash(f))
74102
}
75-
return strings.Join(portablewantedFiles, "\n") + "\n"
103+
return strings.Join(portableFiles, "\n") + "\n"
76104
}
77-
wantedFilesStr := filesListOutput(wantedFiles)
105+
wantedFilesStr := filesListOutput(wantedTmFiles, wantedTmGenFiles)
78106

79107
assertFileContents := func(t *testing.T, path string, want string) {
80108
t.Helper()
81109
got := s.RootEntry().ReadFile(path)
82110
assert.EqualStrings(t, want, string(got))
83111
}
84112

85-
assertWantedFilesContents := func(t *testing.T, want string) {
113+
assertWantedFilesContents := func(t *testing.T, wantTm, wantTmGen string) {
86114
t.Helper()
87115

88-
for _, file := range wantedFiles {
89-
assertFileContents(t, file, want)
116+
for _, file := range wantedTmFiles {
117+
assertFileContents(t, file, wantTm)
118+
}
119+
for _, file := range wantedTmGenFiles {
120+
assertFileContents(t, file, wantTmGen)
90121
}
91122
}
92123

@@ -95,7 +126,7 @@ name = "name"
95126
Status: 1,
96127
Stdout: wantedFilesStr,
97128
})
98-
assertWantedFilesContents(t, unformattedHCL)
129+
assertWantedFilesContents(t, unformattedTmFile, unformattedTmGenFile)
99130
})
100131

101132
t.Run("--detailed-exit-code returns status=2 when used on unformatted files - do modify the files", func(t *testing.T) {
@@ -105,7 +136,7 @@ name = "name"
105136
Status: 2,
106137
Stdout: wantedFilesStr,
107138
})
108-
assertWantedFilesContents(t, formattedHCL)
139+
assertWantedFilesContents(t, formattedTmContent, formattedTmGenContent)
109140
})
110141

111142
t.Run("checking fails with unformatted files on subdirs", func(t *testing.T) {
@@ -118,29 +149,29 @@ name = "name"
118149
"globals.tm.hcl",
119150
"stack-1/globals.tm.hcl",
120151
"stack-2/globals.tm.hcl",
121-
}),
152+
}, nil),
122153
})
123-
assertWantedFilesContents(t, unformattedHCL)
154+
assertWantedFilesContents(t, unformattedTmFile, unformattedTmGenFile)
124155
})
125156

126157
t.Run("update unformatted files in place", func(t *testing.T) {
127158
writeUnformattedFiles()
128159
AssertRunResult(t, cli.Run("fmt"), RunExpected{
129160
Stdout: wantedFilesStr,
130161
})
131-
assertWantedFilesContents(t, formattedHCL)
162+
assertWantedFilesContents(t, formattedTmContent, formattedTmGenContent)
132163
})
133164

134165
t.Run("checking succeeds when all files are formatted", func(t *testing.T) {
135166
writeUnformattedFiles()
136167
AssertRunResult(t, cli.Run("fmt"), RunExpected{IgnoreStdout: true})
137168
AssertRunResult(t, cli.Run("fmt", "--check"), RunExpected{})
138-
assertWantedFilesContents(t, formattedHCL)
169+
assertWantedFilesContents(t, formattedTmContent, formattedTmGenContent)
139170
})
140171

141172
t.Run("--detailed-exit-code returns status=0 when all files are formatted", func(t *testing.T) {
142173
AssertRunResult(t, cli.Run("fmt", "--detailed-exit-code"), RunExpected{})
143-
assertWantedFilesContents(t, formattedHCL)
174+
assertWantedFilesContents(t, formattedTmContent, formattedTmGenContent)
144175
})
145176

146177
t.Run("--check and --detailed-exit-code conflict", func(t *testing.T) {
@@ -152,7 +183,7 @@ name = "name"
152183

153184
t.Run("formatting succeeds when all files are formatted", func(t *testing.T) {
154185
AssertRunResult(t, cli.Run("fmt"), RunExpected{})
155-
assertWantedFilesContents(t, formattedHCL)
186+
assertWantedFilesContents(t, formattedTmContent, formattedTmGenContent)
156187
})
157188

158189
t.Run("update unformatted files in subdirs", func(t *testing.T) {
@@ -165,17 +196,17 @@ name = "name"
165196
"globals.tm.hcl",
166197
"stack-1/globals.tm.hcl",
167198
"stack-2/globals.tm.hcl",
168-
}),
199+
}, nil),
169200
})
170201

171-
assertFileContents(t, "another-stacks/globals.tm.hcl", formattedHCL)
172-
assertFileContents(t, "another-stacks/stack-1/globals.tm.hcl", formattedHCL)
173-
assertFileContents(t, "another-stacks/stack-2/globals.tm.hcl", formattedHCL)
202+
assertFileContents(t, "another-stacks/globals.tm.hcl", formattedTmContent)
203+
assertFileContents(t, "another-stacks/stack-1/globals.tm.hcl", formattedTmContent)
204+
assertFileContents(t, "another-stacks/stack-2/globals.tm.hcl", formattedTmContent)
174205

175-
assertFileContents(t, "globals.tm", unformattedHCL)
176-
assertFileContents(t, "stacks/globals.tm", unformattedHCL)
177-
assertFileContents(t, "stacks/stack-1/globals.tm", unformattedHCL)
178-
assertFileContents(t, "stacks/stack-2/globals.tm", unformattedHCL)
206+
assertFileContents(t, "globals.tm", unformattedTmFile)
207+
assertFileContents(t, "stacks/globals.tm", unformattedTmFile)
208+
assertFileContents(t, "stacks/stack-1/globals.tm", unformattedTmFile)
209+
assertFileContents(t, "stacks/stack-2/globals.tm", unformattedTmFile)
179210

180211
stacks := filepath.Join(s.RootDir(), "stacks")
181212
cli = NewCLI(t, stacks)
@@ -184,24 +215,25 @@ name = "name"
184215
"globals.tm",
185216
"stack-1/globals.tm",
186217
"stack-2/globals.tm",
187-
}),
218+
"test.hcl.tmgen",
219+
}, nil),
188220
})
189221

190-
assertFileContents(t, "another-stacks/globals.tm.hcl", formattedHCL)
191-
assertFileContents(t, "another-stacks/stack-1/globals.tm.hcl", formattedHCL)
192-
assertFileContents(t, "another-stacks/stack-2/globals.tm.hcl", formattedHCL)
193-
assertFileContents(t, "stacks/globals.tm", formattedHCL)
194-
assertFileContents(t, "stacks/stack-1/globals.tm", formattedHCL)
195-
assertFileContents(t, "stacks/stack-2/globals.tm", formattedHCL)
222+
assertFileContents(t, "another-stacks/globals.tm.hcl", formattedTmContent)
223+
assertFileContents(t, "another-stacks/stack-1/globals.tm.hcl", formattedTmContent)
224+
assertFileContents(t, "another-stacks/stack-2/globals.tm.hcl", formattedTmContent)
225+
assertFileContents(t, "stacks/globals.tm", formattedTmContent)
226+
assertFileContents(t, "stacks/stack-1/globals.tm", formattedTmContent)
227+
assertFileContents(t, "stacks/stack-2/globals.tm", formattedTmContent)
196228

197-
assertFileContents(t, "globals.tm", unformattedHCL)
229+
assertFileContents(t, "globals.tm", unformattedTmFile)
198230

199231
cli = NewCLI(t, s.RootDir())
200232
AssertRunResult(t, cli.Run("fmt"), RunExpected{
201-
Stdout: filesListOutput([]string{"globals.tm"}),
233+
Stdout: filesListOutput([]string{"globals.tm"}, []string{"test.hcl.tmgen"}),
202234
})
203235

204-
assertWantedFilesContents(t, formattedHCL)
236+
assertWantedFilesContents(t, formattedTmContent, formattedTmGenContent)
205237
})
206238
}
207239

0 commit comments

Comments
 (0)