func makeTarget(name string, command string, dep *core.BuildTarget) *core.BuildTarget { target := core.NewBuildTarget(core.ParseBuildLabel(name, "")) target.Command = command target.AddOutput(target.Label.Name + ".py") if dep != nil { target.AddDependency(dep.Label) // This is a bit awkward but I don't want to add a public interface just for a test. graph := core.NewGraph() graph.AddTarget(target) graph.AddTarget(dep) graph.AddDependency(target.Label, dep.Label) } return target }
func TestConstructsMapFromGraph(t *testing.T) { core.State = &core.BuildState{} graph := core.NewGraph() m := filesToLabelMap(graph) assert.Equal(t, 0, len(m)) label := core.ParseBuildLabel("//package1:target1", "") makeTarget(graph, "package1", "target1", []string{"out1", "out2"}) m = filesToLabelMap(graph) assert.Equal(t, 2, len(m)) for _, l := range m { assert.Equal(t, label.String(), l.String()) } }
func makeGraph() *core.BuildGraph { core.State = &core.BuildState{} graph := core.NewGraph() pkg1 := core.NewPackage("package1") pkg1.Targets["target1"] = makeTarget("//package1:target1") pkg1.Targets["target2"] = makeTarget("//package1:target2", "//package1:target1") graph.AddPackage(pkg1) graph.AddTarget(pkg1.Targets["target1"]) graph.AddTarget(pkg1.Targets["target2"]) pkg2 := core.NewPackage("package2") pkg2.Targets["target3"] = makeTarget("//package2:target3", "//package1:target2") graph.AddPackage(pkg2) graph.AddTarget(pkg2.Targets["target3"]) graph.AddDependency(core.ParseBuildLabel("//package1:target2", ""), core.ParseBuildLabel("//package1:target1", "")) graph.AddDependency(core.ParseBuildLabel("//package2:target3", ""), core.ParseBuildLabel("//package1:target2", "")) return graph }
func TestMapKeysContainFullPathFromProjectRoot(t *testing.T) { core.State = &core.BuildState{} graph := core.NewGraph() makeTarget(graph, "package1", "target1", []string{"out1", "out2"}) makeTarget(graph, "package1", "target2", []string{"out3"}) makeTarget(graph, "package2", "target1", []string{"out4"}) m := filesToLabelMap(graph) label1 := core.ParseBuildLabel("//package1:target1", "") label2 := core.ParseBuildLabel("//package1:target2", "") label3 := core.ParseBuildLabel("//package2:target1", "") p1 := graph.PackageOrDie("package1") p2 := graph.PackageOrDie("package2") assert.Equal(t, m[path.Join(p1.Targets["target1"].OutDir(), "out1")].String(), label1.String()) assert.Equal(t, m[path.Join(p1.Targets["target1"].OutDir(), "out2")].String(), label1.String()) assert.Equal(t, m[path.Join(p1.Targets["target2"].OutDir(), "out3")].String(), label2.String()) assert.Equal(t, m[path.Join(p2.Targets["target1"].OutDir(), "out4")].String(), label3.String()) }
func TestFindGraphCycle(t *testing.T) { graph := core.NewGraph() graph.AddTarget(makeTarget("//src/output:target1", "//src/output:target2", "//src/output:target3", "//src/core:target2")) graph.AddTarget(makeTarget("//src/output:target2", "//src/output:target3", "//src/core:target1")) graph.AddTarget(makeTarget("//src/output:target3")) graph.AddTarget(makeTarget("//src/core:target1", "//third_party/go:target2", "//third_party/go:target3", "//src/core:target3")) graph.AddTarget(makeTarget("//src/core:target2", "//third_party/go:target3", "//src/output:target2")) graph.AddTarget(makeTarget("//src/core:target3", "//third_party/go:target2", "//src/output:target2")) graph.AddTarget(makeTarget("//third_party/go:target2", "//third_party/go:target1")) graph.AddTarget(makeTarget("//third_party/go:target3", "//third_party/go:target1")) graph.AddTarget(makeTarget("//third_party/go:target1")) updateDependencies(graph) cycle := findGraphCycle(graph, graph.TargetOrDie(core.ParseBuildLabel("//src/output:target1", ""))) if len(cycle) == 0 { t.Fatalf("Failed to find cycle") } else if len(cycle) != 3 { t.Errorf("Found unexpected cycle of length %d", len(cycle)) } assertTarget(t, cycle[0], "//src/output:target2") assertTarget(t, cycle[1], "//src/core:target1") assertTarget(t, cycle[2], "//src/core:target3") }