Skip to content

Commit bac0ac4

Browse files
committed
test(terraform): context setting also exists with module outputs
1 parent 31084ec commit bac0ac4

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

pkg/iac/scanners/terraform/parser/parser_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,7 @@ func TestPopulateContextWithBlockInstances(t *testing.T) {
17091709
tests := []struct {
17101710
name string
17111711
files map[string]string
1712+
skip func(b *terraform.Block) bool
17121713
}{
17131714
{
17141715
name: "data blocks with count",
@@ -1786,15 +1787,48 @@ resource "c" "foo" {
17861787
}`,
17871788
},
17881789
},
1790+
{
1791+
name: "module blocks with for_each",
1792+
skip: func(b *terraform.Block) bool {
1793+
return b.Type() == "module" || b.Type() == "variable"
1794+
},
1795+
files: map[string]string{
1796+
"main.tf": `
1797+
module "outputs" {
1798+
for_each = toset(["Index 0"])
1799+
source = "./modules/outputs"
1800+
input = each.key
1801+
}
1802+
1803+
resource "test" "foo" {
1804+
value = module.outputs["Index 0"].value
1805+
}
1806+
`,
1807+
"modules/outputs/main.tf": `
1808+
variable "input" {
1809+
type = string
1810+
}
1811+
1812+
output "value" {
1813+
value = "${var.value}"
1814+
}
1815+
`,
1816+
},
1817+
},
17891818
}
17901819

17911820
for _, tt := range tests {
17921821
t.Run(tt.name, func(t *testing.T) {
17931822
modules := parse(t, tt.files)
1794-
require.Len(t, modules, 1)
1823+
//require.Len(t, modules, 1)
17951824
for _, b := range modules.GetBlocks() {
1825+
if tt.skip != nil && tt.skip(b) {
1826+
continue
1827+
}
17961828
attr := b.GetAttribute("value")
1797-
assert.Equal(t, "Index 0", attr.Value().AsString())
1829+
if assert.True(t, !attr.Value().IsNull(), "attribute 'value' not found or is null in block %s", b.FullName()) {
1830+
assert.Equal(t, "Index 0", attr.Value().AsString())
1831+
}
17981832
}
17991833
})
18001834
}

0 commit comments

Comments
 (0)