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

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform-plugin-sdk/internal/dag"
     7  )
     8  
     9  // testGraphContains is an assertion helper that tests that a node is
    10  // contained in the graph.
    11  func testGraphContains(t *testing.T, g *Graph, name string) {
    12  	for _, v := range g.Vertices() {
    13  		if dag.VertexName(v) == name {
    14  			return
    15  		}
    16  	}
    17  
    18  	t.Fatalf(
    19  		"Expected %q in:\n\n%s",
    20  		name, g.String())
    21  }
    22  
    23  // testGraphnotContains is an assertion helper that tests that a node is
    24  // NOT contained in the graph.
    25  func testGraphNotContains(t *testing.T, g *Graph, name string) {
    26  	for _, v := range g.Vertices() {
    27  		if dag.VertexName(v) == name {
    28  			t.Fatalf(
    29  				"Expected %q to NOT be in:\n\n%s",
    30  				name, g.String())
    31  		}
    32  	}
    33  }
    34  
    35  // testGraphHappensBefore is an assertion helper that tests that node
    36  // A (dag.VertexName value) happens before node B.
    37  func testGraphHappensBefore(t *testing.T, g *Graph, A, B string) {
    38  	t.Helper()
    39  	// Find the B vertex
    40  	var vertexB dag.Vertex
    41  	for _, v := range g.Vertices() {
    42  		if dag.VertexName(v) == B {
    43  			vertexB = v
    44  			break
    45  		}
    46  	}
    47  	if vertexB == nil {
    48  		t.Fatalf(
    49  			"Expected %q before %q. Couldn't find %q in:\n\n%s",
    50  			A, B, B, g.String())
    51  	}
    52  
    53  	// Look at ancestors
    54  	deps, err := g.Ancestors(vertexB)
    55  	if err != nil {
    56  		t.Fatalf("Error: %s in graph:\n\n%s", err, g.String())
    57  	}
    58  
    59  	// Make sure B is in there
    60  	for _, v := range deps.List() {
    61  		if dag.VertexName(v) == A {
    62  			// Success
    63  			return
    64  		}
    65  	}
    66  
    67  	t.Fatalf(
    68  		"Expected %q before %q in:\n\n%s",
    69  		A, B, g.String())
    70  }