@@ -6,29 +6,49 @@ package core_test
66import (
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
1820func TestFormatRecursively (t * testing.T ) {
1921 t .Parallel ()
2022
21- const unformattedHCL = `
23+ const unformattedTmFile = `
2224globals {
2325name = "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