github.com/sercand/please@v13.4.0+incompatible/src/output/shell_output_test.go (about)

     1  package output
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/thought-machine/please/src/core"
    10  )
    11  
    12  func TestFindGraphCycle(t *testing.T) {
    13  	graph := core.NewGraph()
    14  	graph.AddTarget(makeTarget("//src/output:target1", "//src/output:target2", "//src/output:target3", "//src/core:target2"))
    15  	graph.AddTarget(makeTarget("//src/output:target2", "//src/output:target3", "//src/core:target1"))
    16  	graph.AddTarget(makeTarget("//src/output:target3"))
    17  	graph.AddTarget(makeTarget("//src/core:target1", "//third_party/go:target2", "//third_party/go:target3", "//src/core:target3"))
    18  	graph.AddTarget(makeTarget("//src/core:target2", "//third_party/go:target3", "//src/output:target2"))
    19  	graph.AddTarget(makeTarget("//src/core:target3", "//third_party/go:target2", "//src/output:target2"))
    20  	graph.AddTarget(makeTarget("//third_party/go:target2", "//third_party/go:target1"))
    21  	graph.AddTarget(makeTarget("//third_party/go:target3", "//third_party/go:target1"))
    22  	graph.AddTarget(makeTarget("//third_party/go:target1"))
    23  	updateDependencies(graph)
    24  
    25  	cycle := findGraphCycle(graph, graph.TargetOrDie(core.ParseBuildLabel("//src/output:target1", "")))
    26  	if len(cycle) == 0 {
    27  		t.Fatalf("Failed to find cycle")
    28  	} else if len(cycle) != 3 {
    29  		t.Errorf("Found unexpected cycle of length %d", len(cycle))
    30  	}
    31  	assertTarget(t, cycle[0], "//src/output:target2")
    32  	assertTarget(t, cycle[1], "//src/core:target1")
    33  	assertTarget(t, cycle[2], "//src/core:target3")
    34  }
    35  
    36  func TestColouriseError(t *testing.T) {
    37  	err := fmt.Errorf("/opt/tm/toolchains/1.8.2/usr/include/fst/label-reachable.h:176:39: error: non-const lvalue reference to type 'unordered_map<int, int>' cannot bind to a value of unrelated type 'unordered_map<unsigned char, unsigned char>'")
    38  	expected := fmt.Errorf("\x1b[37;1m/opt/tm/toolchains/1.8.2/usr/include/fst/label-reachable.h, line 176, column 39:\x1b[0m \x1b[31;1merror: \x1b[0m\x1b[37;1mnon-const lvalue reference to type 'unordered_map<int, int>' cannot bind to a value of unrelated type 'unordered_map<unsigned char, unsigned char>'\x1b[0m")
    39  	assert.EqualValues(t, expected, colouriseError(err))
    40  }
    41  
    42  // Factory function for build targets
    43  func makeTarget(label string, deps ...string) *core.BuildTarget {
    44  	target := core.NewBuildTarget(core.ParseBuildLabel(label, ""))
    45  	for _, dep := range deps {
    46  		target.AddDependency(core.ParseBuildLabel(dep, ""))
    47  	}
    48  	return target
    49  }
    50  
    51  // Set dependency pointers on all contents of the graph.
    52  // Has to be done after to test cycles etc.
    53  func updateDependencies(graph *core.BuildGraph) {
    54  	for _, target := range graph.AllTargets() {
    55  		for _, dep := range target.DeclaredDependencies() {
    56  			graph.AddDependency(target.Label, dep)
    57  		}
    58  	}
    59  }
    60  
    61  func assertTarget(t *testing.T, target *core.BuildTarget, label string) {
    62  	if target.Label != core.ParseBuildLabel(label, "") {
    63  		t.Errorf("Unexpected target in detected cycle; expected %s, was %s", label, target.Label)
    64  	}
    65  }