Skip to content

Commit ebaed4c

Browse files
committed
file matchers2
1 parent 917c4a2 commit ebaed4c

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

internal/configs/parser_config_dir.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (p *Parser) LoadConfigDir(path string, opts ...Option) (*Module, hcl.Diagno
9292
// LoadConfigDirWithTests matches LoadConfigDir, but the return Module also
9393
// contains any relevant .tftest.hcl files.
9494
func (p *Parser) LoadConfigDirWithTests(path string, testDirectory string) (*Module, hcl.Diagnostics) {
95-
return p.LoadConfigDir(path, WithTestFiles(testDirectory))
95+
return p.LoadConfigDir(path, MatchTestFiles(testDirectory))
9696
}
9797

9898
func (p *Parser) LoadMockDataDir(dir string, useForPlanDefault bool, source hcl.Range) (*MockData, hcl.Diagnostics) {

internal/configs/parser_config_dir_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestParserLoadConfigDirWithTests(t *testing.T) {
134134
}
135135

136136
parser := NewParser(nil)
137-
mod, diags := parser.LoadConfigDir(directory, WithTestFiles(testDirectory))
137+
mod, diags := parser.LoadConfigDir(directory, MatchTestFiles(testDirectory))
138138
if len(diags) > 0 { // We don't want any warnings or errors.
139139
t.Errorf("unexpected diagnostics")
140140
for _, diag := range diags {
@@ -284,7 +284,7 @@ func TestParserLoadConfigDirFailure(t *testing.T) {
284284
parser := NewParser(nil)
285285
path := filepath.Join("testdata/invalid-modules", name)
286286

287-
_, diags := parser.LoadConfigDir(path, WithTestFiles("tests"))
287+
_, diags := parser.LoadConfigDir(path, MatchTestFiles("tests"))
288288
if !diags.HasErrors() {
289289
t.Errorf("no errors; want at least one")
290290
for _, diag := range diags {

internal/configs/parser_file_processor.go renamed to internal/configs/parser_file_matcher.go

+18-20
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (p *Parser) dirFileSet(dir string, opts ...Option) (ConfigFileSet, hcl.Diag
5959

6060
// Set up options
6161
options := &Options{
62-
// We always process module and override files
62+
// We always match module and override files
6363
matchers: []FileMatcher{&moduleFiles{}},
6464
testDirectory: DefaultTestDirectory,
6565
fs: p.fs,
@@ -123,37 +123,31 @@ func (p *Parser) rootFiles(dir string, matchers []FileMatcher, fileSet *ConfigFi
123123
case *queryFiles:
124124
fileSet.Queries = append(fileSet.Queries, fullPath)
125125
}
126+
break // Stop checking other matchers once a match is found
126127
}
127128
}
128129
}
129130

130131
return diags
131132
}
132133

133-
// WithFileHandler adds a file matcher to the parser
134-
func WithFileHandler(matcher FileMatcher) Option {
134+
// MatchTestFiles adds a matcher for Terraform test files (.tftest.hcl and .tftest.json)
135+
func MatchTestFiles(dir string) Option {
135136
return func(o *Options) {
136-
o.matchers = append(o.matchers, matcher)
137+
o.testDirectory = dir
138+
o.matchers = append(o.matchers, &testFiles{})
137139
}
138140
}
139141

140-
// WithTestFiles adds a matcher for Terraform test files (.tftest.hcl and .tftest.json)
141-
func WithTestFiles(dir string) Option {
142+
// MatchQueryFiles adds a matcher for Terraform query files (.tfquery.hcl)
143+
func MatchQueryFiles() Option {
142144
return func(o *Options) {
143-
o.testDirectory = dir
144-
WithFileHandler(&testFiles{})(o)
145+
o.matchers = append(o.matchers, &queryFiles{})
145146
}
146147
}
147148

148-
// WithQueryFiles adds a matcher for Terraform query files (.tfquery.hcl)
149-
func WithQueryFiles() Option {
150-
return WithFileHandler(&queryFiles{})
151-
}
152-
153-
// moduleFiles processes regular Terraform configuration files (.tf and .tf.json)
154-
type moduleFiles struct {
155-
overrideOnly bool
156-
}
149+
// moduleFiles matches regular Terraform configuration files (.tf and .tf.json)
150+
type moduleFiles struct{}
157151

158152
func (m *moduleFiles) Matches(name string) bool {
159153
ext := fileExt(name)
@@ -179,7 +173,7 @@ func (m *moduleFiles) DirFiles(dir string, options *Options, fileSet *ConfigFile
179173
return nil
180174
}
181175

182-
// testFiles processes Terraform test files (.tftest.hcl and .tftest.json)
176+
// testFiles matches Terraform test files (.tftest.hcl and .tftest.json)
183177
type testFiles struct{}
184178

185179
func (t *testFiles) Matches(name string) bool {
@@ -189,7 +183,6 @@ func (t *testFiles) Matches(name string) bool {
189183
func (t *testFiles) DirFiles(dir string, opts *Options, fileSet *ConfigFileSet) hcl.Diagnostics {
190184
var diags hcl.Diagnostics
191185

192-
// Process test directory
193186
testPath := path.Join(dir, opts.testDirectory)
194187
testInfos, err := opts.fs.ReadDir(testPath)
195188

@@ -228,14 +221,19 @@ func (t *testFiles) DirFiles(dir string, opts *Options, fileSet *ConfigFileSet)
228221

229222
// Process test directory files
230223
for _, info := range testInfos {
224+
if info.IsDir() || IsIgnoredFile(info.Name()) {
225+
// We only care about files.
226+
continue
227+
}
228+
231229
name := info.Name()
232230
fileSet.Tests = append(fileSet.Tests, filepath.Join(testPath, name))
233231
}
234232

235233
return diags
236234
}
237235

238-
// queryFiles processes Terraform query files (.tfquery.hcl)
236+
// queryFiles matches Terraform query files (.tfquery.hcl)
239237
type queryFiles struct{}
240238

241239
func (q *queryFiles) Matches(name string) bool {

internal/configs/parser_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func testNestedModuleConfigFromDirWithTests(t *testing.T, path string) (*Config,
7474
t.Helper()
7575

7676
parser := NewParser(nil)
77-
mod, diags := parser.LoadConfigDir(path, WithTestFiles("tests"))
77+
mod, diags := parser.LoadConfigDir(path, MatchTestFiles("tests"))
7878
if mod == nil {
7979
t.Fatal("got nil root module; want non-nil")
8080
}

internal/configs/query_file.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
14
package configs
25

36
import (

0 commit comments

Comments
 (0)