@@ -6,7 +6,6 @@ package configs
6
6
import (
7
7
"fmt"
8
8
"os"
9
- "path"
10
9
"path/filepath"
11
10
"strings"
12
11
@@ -26,10 +25,10 @@ const (
26
25
// overrides.
27
26
// Optionally, test files (.tftest.hcl and .tftest.json) can be loaded from
28
27
// a subdirectory of the given directory, which is specified by the
29
- // WithTestFiles option, or from the default test directory.
28
+ // MatchTestFiles option, or from the default test directory.
30
29
// If this option is not specified, test files will not be loaded.
31
30
// Query files (.tfquery.hcl) are also loaded from the given directory if
32
- // specified by the WithQueryFiles option.
31
+ // specified by the MatchQueryFiles option.
33
32
//
34
33
// If this method returns nil, that indicates that the given directory does not
35
34
// exist at all or could not be opened for some reason. Callers may wish to
@@ -52,7 +51,7 @@ func (p *Parser) LoadConfigDir(path string, opts ...Option) (*Module, hcl.Diagno
52
51
return nil , diags
53
52
}
54
53
55
- // Load the actual files
54
+ // Load the .tf configuration files
56
55
primary , fDiags := p .loadFiles (fileSet .Primary , false )
57
56
diags = diags .Extend (fDiags )
58
57
@@ -154,10 +153,10 @@ func (p Parser) ConfigDirFiles(dir string, opts ...Option) (primary, override []
154
153
// IsConfigDir determines whether the given path refers to a directory that
155
154
// exists and contains at least one Terraform config file (with a .tf or
156
155
// .tf.json extension.). Note, we explicitely exclude checking for tests here
157
- // as tests must live alongside actual .tf config files.
156
+ // as tests must live alongside actual .tf config files. Same goes for query files.
158
157
func (p * Parser ) IsConfigDir (path string ) bool {
159
- primaryPaths , overridePaths , _ , _ := p .dirFiles (path , "" )
160
- return (len (primaryPaths ) + len (overridePaths )) > 0
158
+ pathSet , _ := p .dirFileSet (path )
159
+ return (len (pathSet . Primary ) + len (pathSet . Override )) > 0
161
160
}
162
161
163
162
func (p * Parser ) loadFiles (paths []string , override bool ) ([]* File , hcl.Diagnostics ) {
@@ -181,109 +180,6 @@ func (p *Parser) loadFiles(paths []string, override bool) ([]*File, hcl.Diagnost
181
180
return files , diags
182
181
}
183
182
184
- // dirFiles finds Terraform configuration files within dir, splitting them into
185
- // primary and override files based on the filename.
186
- //
187
- // If testsDir is not empty, dirFiles will also retrieve Terraform testing files
188
- // both directly within dir and within testsDir as a subdirectory of dir. In
189
- // this way, testsDir acts both as a direction to retrieve test files within the
190
- // main direction and as the location for additional test files.
191
- func (p * Parser ) dirFiles (dir string , testsDir string ) (primary , override , tests []string , diags hcl.Diagnostics ) {
192
- includeTests := len (testsDir ) > 0
193
-
194
- if includeTests {
195
- testPath := path .Join (dir , testsDir )
196
-
197
- infos , err := p .fs .ReadDir (testPath )
198
- if err != nil {
199
- // Then we couldn't read from the testing directory for some reason.
200
-
201
- if os .IsNotExist (err ) {
202
- // Then this means the testing directory did not exist.
203
- // We won't actually stop loading the rest of the configuration
204
- // for this, we will add a warning to explain to the user why
205
- // test files weren't processed but leave it at that.
206
- if testsDir != DefaultTestDirectory {
207
- // We'll only add the warning if a directory other than the
208
- // default has been requested. If the user is just loading
209
- // the default directory then we have no expectation that
210
- // it should actually exist.
211
- diags = append (diags , & hcl.Diagnostic {
212
- Severity : hcl .DiagWarning ,
213
- Summary : "Test directory does not exist" ,
214
- Detail : fmt .Sprintf ("Requested test directory %s does not exist." , testPath ),
215
- })
216
- }
217
- } else {
218
- // Then there is some other reason we couldn't load. We will
219
- // treat this as a full error.
220
- diags = append (diags , & hcl.Diagnostic {
221
- Severity : hcl .DiagError ,
222
- Summary : "Failed to read test directory" ,
223
- Detail : fmt .Sprintf ("Test directory %s could not be read: %v." , testPath , err ),
224
- })
225
-
226
- // We'll also stop loading the rest of the config for this.
227
- return
228
- }
229
-
230
- } else {
231
- for _ , testInfo := range infos {
232
- if testInfo .IsDir () || IsIgnoredFile (testInfo .Name ()) {
233
- continue
234
- }
235
-
236
- if strings .HasSuffix (testInfo .Name (), ".tftest.hcl" ) || strings .HasSuffix (testInfo .Name (), ".tftest.json" ) {
237
- tests = append (tests , filepath .Join (testPath , testInfo .Name ()))
238
- }
239
- }
240
- }
241
-
242
- }
243
-
244
- infos , err := p .fs .ReadDir (dir )
245
- if err != nil {
246
- diags = append (diags , & hcl.Diagnostic {
247
- Severity : hcl .DiagError ,
248
- Summary : "Failed to read module directory" ,
249
- Detail : fmt .Sprintf ("Module directory %s does not exist or cannot be read." , dir ),
250
- })
251
- return
252
- }
253
-
254
- for _ , info := range infos {
255
- if info .IsDir () {
256
- // We only care about terraform configuration files.
257
- continue
258
- }
259
-
260
- name := info .Name ()
261
- ext := fileExt (name )
262
- if ext == "" || IsIgnoredFile (name ) {
263
- continue
264
- }
265
-
266
- if ext == ".tftest.hcl" || ext == ".tftest.json" {
267
- if includeTests {
268
- tests = append (tests , filepath .Join (dir , name ))
269
- }
270
- continue
271
- }
272
-
273
- baseName := name [:len (name )- len (ext )] // strip extension
274
- isOverride := baseName == "override" || strings .HasSuffix (baseName , "_override" )
275
-
276
- fullPath := filepath .Join (dir , name )
277
- if isOverride {
278
- override = append (override , fullPath )
279
- } else {
280
- primary = append (primary , fullPath )
281
- }
282
- }
283
-
284
- return
285
- }
286
-
287
183
func (p * Parser ) loadTestFiles (basePath string , paths []string ) (map [string ]* TestFile , hcl.Diagnostics ) {
288
184
var diags hcl.Diagnostics
289
185
@@ -349,6 +245,8 @@ func fileExt(path string) string {
349
245
return ".tftest.json"
350
246
} else if strings .HasSuffix (path , ".tfquery.hcl" ) {
351
247
return ".tfquery.hcl"
248
+ } else if strings .HasSuffix (path , ".tfquery.json" ) {
249
+ return ".tfquery.json"
352
250
} else {
353
251
return ""
354
252
}
@@ -374,10 +272,10 @@ func IsEmptyDir(path, testDir string) (bool, error) {
374
272
}
375
273
376
274
p := NewParser (nil )
377
- fs , os , tests , diags := p .dirFiles (path , testDir )
275
+ fSet , diags := p .dirFileSet (path , MatchTestFiles ( testDir ) )
378
276
if diags .HasErrors () {
379
277
return false , diags
380
278
}
381
279
382
- return len (fs ) == 0 && len (os ) == 0 && len (tests ) == 0 , nil
280
+ return len (fSet . Primary ) == 0 && len (fSet . Override ) == 0 && len (fSet . Tests ) == 0 , nil
383
281
}
0 commit comments