github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/graph_builder_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     8  
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/dag"
    10  )
    11  
    12  func TestBasicGraphBuilder_impl(t *testing.T) {
    13  	var _ GraphBuilder = new(BasicGraphBuilder)
    14  }
    15  
    16  func TestBasicGraphBuilder(t *testing.T) {
    17  	b := &BasicGraphBuilder{
    18  		Steps: []GraphTransformer{
    19  			&testBasicGraphBuilderTransform{1},
    20  		},
    21  	}
    22  
    23  	g, err := b.Build(addrs.RootModuleInstance)
    24  	if err != nil {
    25  		t.Fatalf("err: %s", err)
    26  	}
    27  
    28  	if g.Path.String() != addrs.RootModuleInstance.String() {
    29  		t.Fatalf("wrong module path %q", g.Path)
    30  	}
    31  
    32  	actual := strings.TrimSpace(g.String())
    33  	expected := strings.TrimSpace(testBasicGraphBuilderStr)
    34  	if actual != expected {
    35  		t.Fatalf("bad: %s", actual)
    36  	}
    37  }
    38  
    39  func TestBasicGraphBuilder_validate(t *testing.T) {
    40  	b := &BasicGraphBuilder{
    41  		Steps: []GraphTransformer{
    42  			&testBasicGraphBuilderTransform{1},
    43  			&testBasicGraphBuilderTransform{2},
    44  		},
    45  		Validate: true,
    46  	}
    47  
    48  	_, err := b.Build(addrs.RootModuleInstance)
    49  	if err == nil {
    50  		t.Fatal("should error")
    51  	}
    52  }
    53  
    54  func TestBasicGraphBuilder_validateOff(t *testing.T) {
    55  	b := &BasicGraphBuilder{
    56  		Steps: []GraphTransformer{
    57  			&testBasicGraphBuilderTransform{1},
    58  			&testBasicGraphBuilderTransform{2},
    59  		},
    60  		Validate: false,
    61  	}
    62  
    63  	_, err := b.Build(addrs.RootModuleInstance)
    64  	if err != nil {
    65  		t.Fatalf("expected no error, got: %s", err)
    66  	}
    67  }
    68  
    69  type testBasicGraphBuilderTransform struct {
    70  	V dag.Vertex
    71  }
    72  
    73  func (t *testBasicGraphBuilderTransform) Transform(g *Graph) error {
    74  	g.Add(t.V)
    75  	return nil
    76  }
    77  
    78  const testBasicGraphBuilderStr = `
    79  1
    80  `