@@ -628,8 +628,8 @@ func TestDiscoveryMatchesIncludePatterns(t *testing.T) {
628628 tests := []struct {
629629 name string
630630 includeDirs []string
631- strictInclude bool
632631 wantUnits []string
632+ strictInclude bool
633633 }{
634634 {
635635 name : "no include patterns - should match everything" ,
@@ -737,8 +737,8 @@ dependency "vpc" {
737737 tests := []struct {
738738 name string
739739 includeDirs []string
740- strictInclude bool
741740 wantUnits []string
741+ strictInclude bool
742742 }{
743743 {
744744 name : "no include patterns - should match everything" ,
@@ -824,8 +824,8 @@ func TestDiscoveryWithStrictIncludeMode(t *testing.T) {
824824 tests := []struct {
825825 name string
826826 includeDirs []string
827- strictInclude bool
828827 wantUnits []string
828+ strictInclude bool
829829 }{
830830 {
831831 name : "strict include mode - only app directories" ,
@@ -873,3 +873,191 @@ func TestDiscoveryWithStrictIncludeMode(t *testing.T) {
873873 })
874874 }
875875}
876+
877+ func TestDiscoveryWithExcludeByDefault (t * testing.T ) {
878+ t .Parallel ()
879+
880+ // Create a temporary directory for testing
881+ tmpDir := t .TempDir ()
882+
883+ // Create test directory structure
884+ testDirs := []string {
885+ "app" ,
886+ "app/frontend" ,
887+ "infra" ,
888+ "infra/vpc" ,
889+ "shared" ,
890+ }
891+
892+ for _ , dir := range testDirs {
893+ err := os .MkdirAll (filepath .Join (tmpDir , dir ), 0755 )
894+ require .NoError (t , err )
895+ }
896+
897+ // Create test files
898+ testFiles := map [string ]string {
899+ "app/terragrunt.hcl" : "" ,
900+ "app/frontend/terragrunt.hcl" : "" ,
901+ "infra/terragrunt.hcl" : "" ,
902+ "infra/vpc/terragrunt.hcl" : "" ,
903+ "shared/terragrunt.hcl" : "" ,
904+ }
905+
906+ for path , content := range testFiles {
907+ err := os .WriteFile (filepath .Join (tmpDir , path ), []byte (content ), 0644 )
908+ require .NoError (t , err )
909+ }
910+
911+ tests := []struct {
912+ name string
913+ includeDirs []string
914+ wantUnits []string
915+ excludeByDefault bool
916+ }{
917+ {
918+ name : "exclude by default with app pattern" ,
919+ includeDirs : []string {"app" },
920+ excludeByDefault : true ,
921+ wantUnits : []string {filepath .Join (tmpDir , "app" ), filepath .Join (tmpDir , "app" , "frontend" )},
922+ },
923+ {
924+ name : "exclude by default with infra pattern" ,
925+ includeDirs : []string {"infra" },
926+ excludeByDefault : true ,
927+ wantUnits : []string {filepath .Join (tmpDir , "infra" ), filepath .Join (tmpDir , "infra" , "vpc" )},
928+ },
929+ {
930+ name : "exclude by default with multiple patterns" ,
931+ includeDirs : []string {"app" , "infra" },
932+ excludeByDefault : true ,
933+ wantUnits : []string {filepath .Join (tmpDir , "app" ), filepath .Join (tmpDir , "app" , "frontend" ), filepath .Join (tmpDir , "infra" ), filepath .Join (tmpDir , "infra" , "vpc" )},
934+ },
935+ {
936+ name : "exclude by default with glob pattern" ,
937+ includeDirs : []string {"app*" },
938+ excludeByDefault : true ,
939+ wantUnits : []string {filepath .Join (tmpDir , "app" ), filepath .Join (tmpDir , "app" , "frontend" )},
940+ },
941+ {
942+ name : "exclude by default with no patterns - should include all" ,
943+ includeDirs : []string {},
944+ excludeByDefault : true ,
945+ wantUnits : []string {filepath .Join (tmpDir , "app" ), filepath .Join (tmpDir , "app" , "frontend" ), filepath .Join (tmpDir , "infra" ), filepath .Join (tmpDir , "infra" , "vpc" ), filepath .Join (tmpDir , "shared" )},
946+ },
947+ }
948+
949+ for _ , tt := range tests {
950+ t .Run (tt .name , func (t * testing.T ) {
951+ t .Parallel ()
952+
953+ d := discovery .NewDiscovery (tmpDir ).WithIncludeDirs (tt .includeDirs )
954+ if tt .excludeByDefault {
955+ d = d .WithExcludeByDefault ()
956+ }
957+
958+ opts , err := options .NewTerragruntOptionsForTest (tmpDir )
959+ require .NoError (t , err )
960+
961+ configs , err := d .Discover (t .Context (), logger .CreateLogger (), opts )
962+ require .NoError (t , err )
963+
964+ units := configs .Filter (discovery .ConfigTypeUnit ).Paths ()
965+ assert .ElementsMatch (t , tt .wantUnits , units )
966+ })
967+ }
968+ }
969+
970+ func TestDiscoveryWithStrictIncludeAndExcludeByDefault (t * testing.T ) {
971+ t .Parallel ()
972+
973+ // Create a temporary directory for testing
974+ tmpDir := t .TempDir ()
975+
976+ // Create test directory structure
977+ testDirs := []string {
978+ "app" ,
979+ "app/frontend" ,
980+ "infra" ,
981+ "infra/vpc" ,
982+ "shared" ,
983+ }
984+
985+ for _ , dir := range testDirs {
986+ err := os .MkdirAll (filepath .Join (tmpDir , dir ), 0755 )
987+ require .NoError (t , err )
988+ }
989+
990+ // Create test files
991+ testFiles := map [string ]string {
992+ "app/terragrunt.hcl" : "" ,
993+ "app/frontend/terragrunt.hcl" : "" ,
994+ "infra/terragrunt.hcl" : "" ,
995+ "infra/vpc/terragrunt.hcl" : "" ,
996+ "shared/terragrunt.hcl" : "" ,
997+ }
998+
999+ for path , content := range testFiles {
1000+ err := os .WriteFile (filepath .Join (tmpDir , path ), []byte (content ), 0644 )
1001+ require .NoError (t , err )
1002+ }
1003+
1004+ tests := []struct {
1005+ name string
1006+ includeDirs []string
1007+ wantUnits []string
1008+ strictInclude bool
1009+ excludeByDefault bool
1010+ }{
1011+ {
1012+ name : "strict include with exclude by default - app only" ,
1013+ includeDirs : []string {"app" },
1014+ strictInclude : true ,
1015+ excludeByDefault : true ,
1016+ wantUnits : []string {filepath .Join (tmpDir , "app" ), filepath .Join (tmpDir , "app" , "frontend" )},
1017+ },
1018+ {
1019+ name : "strict include with exclude by default - infra only" ,
1020+ includeDirs : []string {"infra" },
1021+ strictInclude : true ,
1022+ excludeByDefault : true ,
1023+ wantUnits : []string {filepath .Join (tmpDir , "infra" ), filepath .Join (tmpDir , "infra" , "vpc" )},
1024+ },
1025+ {
1026+ name : "strict include with exclude by default - multiple patterns" ,
1027+ includeDirs : []string {"app" , "infra" },
1028+ strictInclude : true ,
1029+ excludeByDefault : true ,
1030+ wantUnits : []string {filepath .Join (tmpDir , "app" ), filepath .Join (tmpDir , "app" , "frontend" ), filepath .Join (tmpDir , "infra" ), filepath .Join (tmpDir , "infra" , "vpc" )},
1031+ },
1032+ {
1033+ name : "strict include without exclude by default - should include all" ,
1034+ includeDirs : []string {"app" },
1035+ strictInclude : true ,
1036+ excludeByDefault : false ,
1037+ wantUnits : []string {filepath .Join (tmpDir , "app" ), filepath .Join (tmpDir , "app" , "frontend" ), filepath .Join (tmpDir , "infra" ), filepath .Join (tmpDir , "infra" , "vpc" ), filepath .Join (tmpDir , "shared" )},
1038+ },
1039+ }
1040+
1041+ for _ , tt := range tests {
1042+ t .Run (tt .name , func (t * testing.T ) {
1043+ t .Parallel ()
1044+
1045+ d := discovery .NewDiscovery (tmpDir ).WithIncludeDirs (tt .includeDirs )
1046+ if tt .strictInclude {
1047+ d = d .WithStrictInclude ()
1048+ }
1049+ if tt .excludeByDefault {
1050+ d = d .WithExcludeByDefault ()
1051+ }
1052+
1053+ opts , err := options .NewTerragruntOptionsForTest (tmpDir )
1054+ require .NoError (t , err )
1055+
1056+ configs , err := d .Discover (t .Context (), logger .CreateLogger (), opts )
1057+ require .NoError (t , err )
1058+
1059+ units := configs .Filter (discovery .ConfigTypeUnit ).Paths ()
1060+ assert .ElementsMatch (t , tt .wantUnits , units )
1061+ })
1062+ }
1063+ }
0 commit comments