|
12 | 12 | import taskgraph |
13 | 13 | from taskgraph.actions import registry |
14 | 14 | from taskgraph.graph import Graph |
15 | | -from taskgraph.main import get_filtered_taskgraph |
| 15 | +from taskgraph.main import format_kind_graph_mermaid, get_filtered_taskgraph |
16 | 16 | from taskgraph.main import main as taskgraph_main |
17 | 17 | from taskgraph.task import Task |
18 | 18 | from taskgraph.taskgraph import TaskGraph |
@@ -521,3 +521,124 @@ def test_load_task_command_with_task_id(run_load_task): |
521 | 521 | user=None, |
522 | 522 | custom_image=None, |
523 | 523 | ) |
| 524 | + |
| 525 | + |
| 526 | +def test_format_kind_graph_mermaid(): |
| 527 | + """Test conversion of kind graph to Mermaid format""" |
| 528 | + # Test with simple graph |
| 529 | + kinds = frozenset(["docker-image"]) |
| 530 | + edges = frozenset() |
| 531 | + kind_graph = Graph(kinds, edges) |
| 532 | + |
| 533 | + output = format_kind_graph_mermaid(kind_graph) |
| 534 | + assert "flowchart TD" in output |
| 535 | + assert "docker_image[docker-image]" in output |
| 536 | + |
| 537 | + # Test with complex graph with dependencies |
| 538 | + kinds = frozenset(["docker-image", "build", "test", "lint"]) |
| 539 | + edges = frozenset( |
| 540 | + [ |
| 541 | + ("build", "docker-image", "kind-dependency"), |
| 542 | + ("test", "build", "kind-dependency"), |
| 543 | + ("lint", "docker-image", "kind-dependency"), |
| 544 | + ] |
| 545 | + ) |
| 546 | + kind_graph = Graph(kinds, edges) |
| 547 | + |
| 548 | + output = format_kind_graph_mermaid(kind_graph) |
| 549 | + lines = output.split("\n") |
| 550 | + |
| 551 | + assert lines[0] == "flowchart TD" |
| 552 | + # Check all nodes are present |
| 553 | + assert any("build[build]" in line for line in lines) |
| 554 | + assert any("docker_image[docker-image]" in line for line in lines) |
| 555 | + assert any("lint[lint]" in line for line in lines) |
| 556 | + assert any("test[test]" in line for line in lines) |
| 557 | + # Check edges are reversed (dependencies point to dependents) |
| 558 | + assert any("docker_image --> build" in line for line in lines) |
| 559 | + assert any("build --> test" in line for line in lines) |
| 560 | + assert any("docker_image --> lint" in line for line in lines) |
| 561 | + |
| 562 | + |
| 563 | +def test_show_kinds_command(run_taskgraph, capsys): |
| 564 | + """Test the kinds command outputs Mermaid format""" |
| 565 | + res = run_taskgraph( |
| 566 | + ["kind-graph"], |
| 567 | + kinds=[ |
| 568 | + ("_fake", {"kind-dependencies": []}), |
| 569 | + ], |
| 570 | + ) |
| 571 | + assert res == 0 |
| 572 | + |
| 573 | + out, _ = capsys.readouterr() |
| 574 | + assert "flowchart TD" in out |
| 575 | + assert "_fake[_fake]" in out |
| 576 | + |
| 577 | + |
| 578 | +def test_show_kinds_with_dependencies(run_taskgraph, capsys): |
| 579 | + """Test the kinds command with kind dependencies""" |
| 580 | + res = run_taskgraph( |
| 581 | + ["kind-graph"], |
| 582 | + kinds=[ |
| 583 | + ("_fake3", {"kind-dependencies": ["_fake2"]}), |
| 584 | + ("_fake2", {"kind-dependencies": ["_fake1"]}), |
| 585 | + ("_fake1", {"kind-dependencies": []}), |
| 586 | + ], |
| 587 | + ) |
| 588 | + assert res == 0 |
| 589 | + |
| 590 | + out, _ = capsys.readouterr() |
| 591 | + assert "flowchart TD" in out |
| 592 | + # Check all kinds are present |
| 593 | + assert "_fake1[_fake1]" in out |
| 594 | + assert "_fake2[_fake2]" in out |
| 595 | + assert "_fake3[_fake3]" in out |
| 596 | + # Check edges are present and reversed |
| 597 | + assert "_fake1 --> _fake2" in out |
| 598 | + assert "_fake2 --> _fake3" in out |
| 599 | + |
| 600 | + |
| 601 | +def test_show_kinds_output_file(run_taskgraph, tmpdir): |
| 602 | + """Test the kinds command writes to file""" |
| 603 | + output_file = tmpdir.join("kinds.mmd") |
| 604 | + assert not output_file.check() |
| 605 | + |
| 606 | + res = run_taskgraph( |
| 607 | + ["kind-graph", f"--output-file={output_file.strpath}"], |
| 608 | + kinds=[ |
| 609 | + ("_fake", {"kind-dependencies": []}), |
| 610 | + ], |
| 611 | + ) |
| 612 | + assert res == 0 |
| 613 | + assert output_file.check() |
| 614 | + |
| 615 | + content = output_file.read_text("utf-8") |
| 616 | + assert "flowchart TD" in content |
| 617 | + assert "_fake[_fake]" in content |
| 618 | + |
| 619 | + |
| 620 | +def test_show_kinds_with_target_kinds(run_taskgraph, capsys): |
| 621 | + """Test the kinds command with --target-kind filter""" |
| 622 | + res = run_taskgraph( |
| 623 | + ["kind-graph", "-k", "_fake2"], |
| 624 | + kinds=[ |
| 625 | + ("_fake3", {"kind-dependencies": ["_fake2"]}), |
| 626 | + ("_fake2", {"kind-dependencies": ["_fake1"]}), |
| 627 | + ("_fake1", {"kind-dependencies": []}), |
| 628 | + ("_other", {"kind-dependencies": []}), |
| 629 | + ("docker-image", {"kind-dependencies": []}), |
| 630 | + ], |
| 631 | + params={"target-kinds": ["_fake2"]}, |
| 632 | + ) |
| 633 | + assert res == 0 |
| 634 | + |
| 635 | + out, _ = capsys.readouterr() |
| 636 | + assert "flowchart TD" in out |
| 637 | + # Should include _fake2 and its dependencies |
| 638 | + assert "_fake2[_fake2]" in out |
| 639 | + assert "_fake1[_fake1]" in out |
| 640 | + # Should include docker-image (implicit dependency for target_kinds) |
| 641 | + assert "docker_image[docker-image]" in out |
| 642 | + # Should not include _fake3 or _other |
| 643 | + assert "_fake3" not in out |
| 644 | + assert "_other" not in out |
0 commit comments