Files
opentf/internal/command/jsonconfig/expression_test.go
Martin Atkins 6a27c82bb4 tofu show: -module=DIR mode, for showing just a single module
We previously added the -config mode for showing the entire assembled
configuration tree, including the content of any descendent modules, but
that mode requires first running "tofu init" to install all of the
provider and module dependencies of the configuration.

This new -module=DIR mode returns a subset of the same JSON representation
for only a single module that can be generated without first installing
any dependencies, making this mode more appropriate for situations like
generating documentation for a single module when importing it into the
OpenTofu Registry. The registry generation process does not want to endure
the overhead of installing other providers and modules when all it actually
needs is metadata about the top-level declarations in the module.

To minimize the risk to the already-working full-config JSON representation
while still reusing most of its code, the implementation details of package
jsonconfig are a little awkward here. Since this code changes relatively
infrequently and is implementing an external interface subject to
compatibility constraints, and since this new behavior is relatively
marginal and intended primarily for our own OpenTofu Registry purposes,
this is a pragmatic tradeoff that is hopefully compensated for well enough
by the code comments that aim to explain what's going on for the benefit
of future maintainers. If we _do_ find ourselves making substantial changes
to this code at a later date then we can consider a more significant
restructure of the code at that point; the weird stuff is intentionally
encapsulated inside package jsonconfig so it can change later without
changing any callers.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-10 13:18:26 -07:00

171 lines
3.6 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package jsonconfig
import (
"encoding/json"
"reflect"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hcltest"
"github.com/zclconf/go-cty/cty"
"github.com/opentofu/opentofu/internal/configs/configschema"
)
func TestMarshalExpressions(t *testing.T) {
tests := []struct {
Input hcl.Body
Want expressions
}{
{
&hclsyntax.Body{
Attributes: hclsyntax.Attributes{
"foo": &hclsyntax.Attribute{
Expr: &hclsyntax.LiteralValueExpr{
Val: cty.StringVal("bar"),
},
},
},
},
expressions{
"foo": expression{
ConstantValue: json.RawMessage([]byte(`"bar"`)),
References: []string(nil),
},
},
},
{
hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"foo": {
Name: "foo",
Expr: hcltest.MockExprTraversalSrc(`var.list[1]`),
},
},
}),
expressions{
"foo": expression{
References: []string{"var.list[1]", "var.list"},
},
},
},
{
hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"foo": {
Name: "foo",
Expr: hcltest.MockExprTraversalSrc(`data.template_file.foo[1].vars["baz"]`),
},
},
}),
expressions{
"foo": expression{
References: []string{"data.template_file.foo[1].vars[\"baz\"]", "data.template_file.foo[1].vars", "data.template_file.foo[1]", "data.template_file.foo"},
},
},
},
{
hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"foo": {
Name: "foo",
Expr: hcltest.MockExprTraversalSrc(`module.foo.bar`),
},
},
}),
expressions{
"foo": expression{
References: []string{"module.foo.bar", "module.foo"},
},
},
},
{
hcltest.MockBody(&hcl.BodyContent{
Blocks: hcl.Blocks{
{
Type: "block_to_attr",
Body: hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"foo": {
Name: "foo",
Expr: hcltest.MockExprTraversalSrc(`module.foo.bar`),
},
},
}),
},
},
}),
expressions{
"block_to_attr": expression{
References: []string{"module.foo.bar", "module.foo"},
},
},
},
}
for _, test := range tests {
schema := &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.String,
Optional: true,
},
"block_to_attr": {
Type: cty.List(cty.Object(map[string]cty.Type{
"foo": cty.String,
})),
},
},
}
got := marshalExpressions(test.Input, schema)
if !reflect.DeepEqual(got, test.Want) {
t.Errorf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
}
}
}
func TestMarshalExpressions_singleModuleMode(t *testing.T) {
// In single-module mode the given schema is nil, which should
// cause the result to always be nil. Refer to the docs on
// [inSingleExpressionMode] for more information.
input := hcltest.MockBody(&hcl.BodyContent{
Attributes: hcl.Attributes{
"foo": {
Name: "foo",
Expr: hcltest.MockExprTraversalSrc(`var.list[1]`),
},
},
})
got := marshalExpressions(input, nil)
if got != nil {
t.Errorf("wrong result:\nGot: %#v\nWant: <nil>", got)
}
}
func TestMarshalExpression(t *testing.T) {
tests := []struct {
Input hcl.Expression
Want expression
}{
{
nil,
expression{},
},
}
for _, test := range tests {
got := marshalExpression(test.Input)
if !reflect.DeepEqual(got, test.Want) {
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
}
}
}