Skip to content

Commit 1ea33d4

Browse files
committed
chore: Fixing TestDiscoveryIncludeExcludeFilters
1 parent 77fd2fc commit 1ea33d4

File tree

3 files changed

+12
-68
lines changed

3 files changed

+12
-68
lines changed

internal/discovery/constructor.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"path/filepath"
55
"runtime"
66

7-
"github.com/gruntwork-io/terragrunt/config"
87
"github.com/gruntwork-io/terragrunt/internal/component"
98
"github.com/gruntwork-io/terragrunt/internal/experiment"
109
"github.com/gruntwork-io/terragrunt/internal/filter"
@@ -138,10 +137,6 @@ func NewDiscovery(dir string, opts ...DiscoveryOption) *Discovery {
138137
numWorkers := max(min(runtime.NumCPU(), maxDiscoveryWorkers), defaultDiscoveryWorkers)
139138

140139
discovery := &Discovery{
141-
includeDirs: []string{
142-
config.StackDir,
143-
filepath.Join(config.StackDir, "**"),
144-
},
145140
numWorkers: numWorkers,
146141
maxDependencyDepth: defaultMaxDependencyDepth,
147142
discoveryContext: &component.DiscoveryContext{

internal/discovery/discovery.go

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/gruntwork-io/terragrunt/options"
2929
"github.com/gruntwork-io/terragrunt/pkg/log"
3030
"github.com/hashicorp/hcl/v2"
31-
"github.com/mattn/go-zglob"
3231
"github.com/zclconf/go-cty/cty"
3332
"golang.org/x/sync/errgroup"
3433
)
@@ -77,9 +76,6 @@ type Discovery struct {
7776

7877
graphTarget string
7978

80-
// includeDirs is a list of directory patterns to include in discovery (for strict include mode).
81-
includeDirs []string
82-
8379
// configFilenames is the list of config filenames to discover. If nil, defaults are used.
8480
configFilenames []string
8581

@@ -236,12 +232,6 @@ func (d *Discovery) WithConfigFilenames(filenames []string) *Discovery {
236232
return d
237233
}
238234

239-
// WithIncludeDirs sets include directory glob patterns used for filtering during discovery.
240-
func (d *Discovery) WithIncludeDirs(dirs []string) *Discovery {
241-
d.includeDirs = dirs
242-
return d
243-
}
244-
245235
// WithParserOptions sets custom HCL parser options to use when parsing during discovery.
246236
func (d *Discovery) WithParserOptions(options []hclparse.Option) *Discovery {
247237
d.parserOptions = options
@@ -333,32 +323,6 @@ func (d *Discovery) withExcludeByDefault() *Discovery {
333323
return d
334324
}
335325

336-
// compileIncludePatterns compiles the include directory patterns for faster matching.
337-
func (d *Discovery) compileIncludePatterns(l log.Logger) {
338-
d.compiledIncludePatterns = make([]CompiledPattern, 0, len(d.includeDirs))
339-
for _, pattern := range d.includeDirs {
340-
if compiled, err := zglob.New(pattern); err == nil {
341-
d.compiledIncludePatterns = append(d.compiledIncludePatterns, CompiledPattern{
342-
Original: pattern,
343-
Compiled: compiled,
344-
})
345-
} else {
346-
l.Warnf("Failed to compile include pattern '%s': %v. Pattern will be ignored.", pattern, err)
347-
}
348-
}
349-
}
350-
351-
// matchesIncludePath reports whether the provided directory matches any compiled include pattern.
352-
func (d *Discovery) matchesIncludePath(dir string) bool {
353-
for _, pattern := range d.compiledIncludePatterns {
354-
if pattern.Compiled.Match(dir) {
355-
return true
356-
}
357-
}
358-
359-
return false
360-
}
361-
362326
// String returns a string representation of a Component.
363327
// String returns the path of the Component.
364328
func String(c component.Component) string {
@@ -731,11 +695,6 @@ func (d *Discovery) processFile(
731695
// Always allow .terragrunt-stack contents
732696
cleanDir := util.CleanPath(canonicalDir)
733697
allowHidden = isInStackDirectory(cleanDir)
734-
735-
if !allowHidden {
736-
// Use a common helper for include matching
737-
allowHidden = d.matchesIncludePath(canonicalDir)
738-
}
739698
}
740699

741700
if !allowHidden {
@@ -896,25 +855,6 @@ func (d *Discovery) Discover(
896855
filenames = DefaultConfigFilenames
897856
}
898857

899-
// Prepare include/exclude glob patterns (canonicalized) for matching
900-
var includePatterns []string
901-
902-
if len(d.includeDirs) > 0 {
903-
for _, p := range d.includeDirs {
904-
if !filepath.IsAbs(p) {
905-
p = filepath.Join(d.discoveryContext.WorkingDir, p)
906-
}
907-
908-
includePatterns = append(includePatterns, util.CleanPath(p))
909-
}
910-
}
911-
912-
// Compile patterns if not already compiled
913-
if len(d.compiledIncludePatterns) == 0 && len(includePatterns) > 0 {
914-
d.includeDirs = includePatterns
915-
d.compileIncludePatterns(l)
916-
}
917-
918858
// Use concurrent discovery for better performance
919859
components, err := d.discoverConcurrently(ctx, l, opts, &hiddenDirMemo{}, filenames)
920860
if err != nil {

internal/discovery/discovery_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,20 @@ func TestDiscoveryIncludeExcludeFilters(t *testing.T) {
483483
require.NoError(t, err)
484484
assert.ElementsMatch(t, []string{unit1Dir, unit3Dir}, cfgs.Filter(component.UnitKind).Paths())
485485

486+
filters, err = filter.ParseFilterQueries([]string{"./unit1"})
487+
require.NoError(t, err)
488+
486489
// Exclude-by-default and include only unit1
487-
d = discovery.NewDiscovery(tmpDir).WithIncludeDirs([]string{unit1Dir})
490+
d = discovery.NewDiscovery(tmpDir).WithFilters(filters)
488491
cfgs, err = d.Discover(t.Context(), l, opts)
489492
require.NoError(t, err)
490493
assert.ElementsMatch(t, []string{unit1Dir}, cfgs.Filter(component.UnitKind).Paths())
491494

495+
filters, err = filter.ParseFilterQueries([]string{"./unit3"})
496+
require.NoError(t, err)
497+
492498
// Strict include behaves the same
493-
d = discovery.NewDiscovery(tmpDir).WithIncludeDirs([]string{unit3Dir})
499+
d = discovery.NewDiscovery(tmpDir).WithFilters(filters)
494500
cfgs, err = d.Discover(t.Context(), l, opts)
495501
require.NoError(t, err)
496502
assert.ElementsMatch(t, []string{unit3Dir}, cfgs.Filter(component.UnitKind).Paths())
@@ -508,7 +514,10 @@ func TestDiscoveryHiddenIncludedByIncludeDirs(t *testing.T) {
508514
opts, err := options.NewTerragruntOptionsForTest(tmpDir)
509515
require.NoError(t, err)
510516

511-
d := discovery.NewDiscovery(tmpDir).WithIncludeDirs([]string{filepath.Join(tmpDir, ".hidden", "**")})
517+
filters, err := filter.ParseFilterQueries([]string{"./.hidden/**"})
518+
require.NoError(t, err)
519+
520+
d := discovery.NewDiscovery(tmpDir).WithFilters(filters)
512521
cfgs, err := d.Discover(t.Context(), l, opts)
513522
require.NoError(t, err)
514523
assert.ElementsMatch(t, []string{hiddenUnitDir}, cfgs.Filter(component.UnitKind).Paths())

0 commit comments

Comments
 (0)