github.com/opentofu/opentofu@v1.7.1/internal/tofu/graph_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  	"testing"
    10  
    11  	"github.com/opentofu/opentofu/internal/dag"
    12  )
    13  
    14  // testGraphnotContains is an assertion helper that tests that a node is
    15  // NOT contained in the graph.
    16  func testGraphNotContains(t *testing.T, g *Graph, name string) {
    17  	for _, v := range g.Vertices() {
    18  		if dag.VertexName(v) == name {
    19  			t.Fatalf(
    20  				"Expected %q to NOT be in:\n\n%s",
    21  				name, g.String())
    22  		}
    23  	}
    24  }
    25  
    26  // testGraphHappensBefore is an assertion helper that tests that node
    27  // A (dag.VertexName value) happens before node B.
    28  func testGraphHappensBefore(t *testing.T, g *Graph, A, B string) {
    29  	t.Helper()
    30  	// Find the B vertex
    31  	var vertexB dag.Vertex
    32  	for _, v := range g.Vertices() {
    33  		if dag.VertexName(v) == B {
    34  			vertexB = v
    35  			break
    36  		}
    37  	}
    38  	if vertexB == nil {
    39  		t.Fatalf(
    40  			"Expected %q before %q. Couldn't find %q in:\n\n%s",
    41  			A, B, B, g.String())
    42  	}
    43  
    44  	// Look at ancestors
    45  	deps, err := g.Ancestors(vertexB)
    46  	if err != nil {
    47  		t.Fatalf("Error: %s in graph:\n\n%s", err, g.String())
    48  	}
    49  
    50  	// Make sure B is in there
    51  	for _, v := range deps.List() {
    52  		if dag.VertexName(v) == A {
    53  			// Success
    54  			return
    55  		}
    56  	}
    57  
    58  	t.Fatalf(
    59  		"Expected %q before %q in:\n\n%s",
    60  		A, B, g.String())
    61  }