github.com/opentofu/opentofu@v1.7.1/internal/tofu/transform_transitive_reduction_test.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package tofu
     7  
     8  import (
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/opentofu/opentofu/internal/addrs"
    13  	"github.com/opentofu/opentofu/internal/configs/configschema"
    14  	"github.com/opentofu/opentofu/internal/providers"
    15  	"github.com/zclconf/go-cty/cty"
    16  )
    17  
    18  func TestTransitiveReductionTransformer(t *testing.T) {
    19  	mod := testModule(t, "transform-trans-reduce-basic")
    20  
    21  	g := Graph{Path: addrs.RootModuleInstance}
    22  	{
    23  		tf := &ConfigTransformer{Config: mod}
    24  		if err := tf.Transform(&g); err != nil {
    25  			t.Fatalf("err: %s", err)
    26  		}
    27  		t.Logf("graph after ConfigTransformer:\n%s", g.String())
    28  	}
    29  
    30  	{
    31  		transform := &AttachResourceConfigTransformer{Config: mod}
    32  		if err := transform.Transform(&g); err != nil {
    33  			t.Fatalf("err: %s", err)
    34  		}
    35  	}
    36  
    37  	{
    38  		transform := &AttachSchemaTransformer{
    39  			Plugins: schemaOnlyProvidersForTesting(map[addrs.Provider]providers.ProviderSchema{
    40  				addrs.NewDefaultProvider("aws"): {
    41  					ResourceTypes: map[string]providers.Schema{
    42  						"aws_instance": {
    43  							Block: &configschema.Block{
    44  								Attributes: map[string]*configschema.Attribute{
    45  									"A": {
    46  										Type:     cty.String,
    47  										Optional: true,
    48  									},
    49  									"B": {
    50  										Type:     cty.String,
    51  										Optional: true,
    52  									},
    53  								},
    54  							},
    55  						},
    56  					},
    57  				},
    58  			}, t),
    59  		}
    60  		if err := transform.Transform(&g); err != nil {
    61  			t.Fatalf("err: %s", err)
    62  		}
    63  	}
    64  
    65  	{
    66  		transform := &ReferenceTransformer{}
    67  		if err := transform.Transform(&g); err != nil {
    68  			t.Fatalf("err: %s", err)
    69  		}
    70  		t.Logf("graph after ReferenceTransformer:\n%s", g.String())
    71  	}
    72  
    73  	{
    74  		transform := &TransitiveReductionTransformer{}
    75  		if err := transform.Transform(&g); err != nil {
    76  			t.Fatalf("err: %s", err)
    77  		}
    78  		t.Logf("graph after TransitiveReductionTransformer:\n%s", g.String())
    79  	}
    80  
    81  	actual := strings.TrimSpace(g.String())
    82  	expected := strings.TrimSpace(testTransformTransReduceBasicStr)
    83  	if actual != expected {
    84  		t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s", actual, expected)
    85  	}
    86  }
    87  
    88  const testTransformTransReduceBasicStr = `
    89  aws_instance.A
    90  aws_instance.B
    91    aws_instance.A
    92  aws_instance.C
    93    aws_instance.B
    94  `