Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/iac/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,51 @@ check "cats_mittens_is_special" {
require.NotNil(t, checkBlocks[0].GetBlock("assert"))
}

// Test_OpenTofuLanguageBlock reproduces aquasecurity/trivy#10906.
//
// Given a .tofu file using the OpenTofu v1.12 top-level `language` block
// (https://opentofu.org/docs/language/settings/#language-compatibility)
// When the parser reads the file
// Then it must not fail with "Unsupported block type", and the nested
// `compatible_with.opentofu` constraint must be readable.
func Test_OpenTofuLanguageBlock(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Given/When/Then doc comment is out of style for this file — compare with TestConfigWithEphemeralBlock nearby. A link to #10906 is enough. The require messages, the resource block check, and the compatible_with assertions can go — the point of the test is just that the language block no longer triggers "Unsupported block type".

fs := testutil.CreateFS(map[string]string{
"main.tofu": `
language {
compatible_with {
opentofu = ">= 1.12"
}
}

resource "cats_cat" "mittens" {
name = "mittens"
}
`,
})

parser := New(fs, "", OptionStopOnHCLError(true))
err := parser.ParseFS(t.Context(), ".")
require.NoError(t, err, "parser must accept the OpenTofu `language` block")

modules, err := parser.EvaluateAll(t.Context())
require.NoError(t, err)
require.NotEmpty(t, modules)

blocks := modules[0].GetBlocks()

languageBlocks := blocks.OfType("language")
require.Len(t, languageBlocks, 1)

compatibleWith := languageBlocks[0].GetBlock("compatible_with")
require.NotNil(t, compatibleWith)
require.NotNil(t, compatibleWith.GetAttribute("opentofu"))
assert.Equal(t, ">= 1.12", compatibleWith.GetAttribute("opentofu").Value().AsString())

// sanity: the rest of the file still parses normally
resourceBlocks := blocks.OfType("resource")
require.Len(t, resourceBlocks, 1)
}

func Test_Modules(t *testing.T) {

fs := testutil.CreateFS(map[string]string{
Expand Down
5 changes: 5 additions & 0 deletions pkg/iac/terraform/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ var Schema = &hcl.BodySchema{
{
Type: "terraform",
},
{
// language declares OpenTofu/language compatibility constraints.
// Introduced in OpenTofu v1.12: https://opentofu.org/docs/language/settings/#language-compatibility
Type: "language",
},
{
Type: "required_providers",
},
Expand Down