github.com/opentofu/opentofu@v1.7.1/internal/tofu/graph_builder_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  
    14  	"github.com/opentofu/opentofu/internal/dag"
    15  )
    16  
    17  func TestBasicGraphBuilder_impl(t *testing.T) {
    18  	var _ GraphBuilder = new(BasicGraphBuilder)
    19  }
    20  
    21  func TestBasicGraphBuilder(t *testing.T) {
    22  	b := &BasicGraphBuilder{
    23  		Steps: []GraphTransformer{
    24  			&testBasicGraphBuilderTransform{1},
    25  		},
    26  	}
    27  
    28  	g, err := b.Build(addrs.RootModuleInstance)
    29  	if err != nil {
    30  		t.Fatalf("err: %s", err)
    31  	}
    32  
    33  	if g.Path.String() != addrs.RootModuleInstance.String() {
    34  		t.Fatalf("wrong module path %q", g.Path)
    35  	}
    36  
    37  	actual := strings.TrimSpace(g.String())
    38  	expected := strings.TrimSpace(testBasicGraphBuilderStr)
    39  	if actual != expected {
    40  		t.Fatalf("bad: %s", actual)
    41  	}
    42  }
    43  
    44  func TestBasicGraphBuilder_validate(t *testing.T) {
    45  	b := &BasicGraphBuilder{
    46  		Steps: []GraphTransformer{
    47  			&testBasicGraphBuilderTransform{1},
    48  			&testBasicGraphBuilderTransform{2},
    49  		},
    50  	}
    51  
    52  	_, err := b.Build(addrs.RootModuleInstance)
    53  	if err == nil {
    54  		t.Fatal("should error")
    55  	}
    56  }
    57  
    58  type testBasicGraphBuilderTransform struct {
    59  	V dag.Vertex
    60  }
    61  
    62  func (t *testBasicGraphBuilderTransform) Transform(g *Graph) error {
    63  	g.Add(t.V)
    64  	return nil
    65  }
    66  
    67  const testBasicGraphBuilderStr = `
    68  1
    69  `