testgraph.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """Test cases for graph processing code in build.py."""
  2. from __future__ import annotations
  3. import sys
  4. from typing import AbstractSet
  5. from mypy.build import (
  6. BuildManager,
  7. BuildSourceSet,
  8. State,
  9. order_ascc,
  10. sorted_components,
  11. strongly_connected_components,
  12. topsort,
  13. )
  14. from mypy.errors import Errors
  15. from mypy.fscache import FileSystemCache
  16. from mypy.modulefinder import SearchPaths
  17. from mypy.options import Options
  18. from mypy.plugin import Plugin
  19. from mypy.report import Reports
  20. from mypy.test.helpers import Suite, assert_equal
  21. from mypy.version import __version__
  22. class GraphSuite(Suite):
  23. def test_topsort(self) -> None:
  24. a = frozenset({"A"})
  25. b = frozenset({"B"})
  26. c = frozenset({"C"})
  27. d = frozenset({"D"})
  28. data: dict[AbstractSet[str], set[AbstractSet[str]]] = {a: {b, c}, b: {d}, c: {d}}
  29. res = list(topsort(data))
  30. assert_equal(res, [{d}, {b, c}, {a}])
  31. def test_scc(self) -> None:
  32. vertices = {"A", "B", "C", "D"}
  33. edges: dict[str, list[str]] = {"A": ["B", "C"], "B": ["C"], "C": ["B", "D"], "D": []}
  34. sccs = {frozenset(x) for x in strongly_connected_components(vertices, edges)}
  35. assert_equal(sccs, {frozenset({"A"}), frozenset({"B", "C"}), frozenset({"D"})})
  36. def _make_manager(self) -> BuildManager:
  37. options = Options()
  38. options.use_builtins_fixtures = True
  39. errors = Errors(options)
  40. fscache = FileSystemCache()
  41. search_paths = SearchPaths((), (), (), ())
  42. manager = BuildManager(
  43. data_dir="",
  44. search_paths=search_paths,
  45. ignore_prefix="",
  46. source_set=BuildSourceSet([]),
  47. reports=Reports("", {}),
  48. options=options,
  49. version_id=__version__,
  50. plugin=Plugin(options),
  51. plugins_snapshot={},
  52. errors=errors,
  53. flush_errors=lambda msgs, serious: None,
  54. fscache=fscache,
  55. stdout=sys.stdout,
  56. stderr=sys.stderr,
  57. )
  58. return manager
  59. def test_sorted_components(self) -> None:
  60. manager = self._make_manager()
  61. graph = {
  62. "a": State("a", None, "import b, c", manager),
  63. "d": State("d", None, "pass", manager),
  64. "b": State("b", None, "import c", manager),
  65. "c": State("c", None, "import b, d", manager),
  66. }
  67. res = sorted_components(graph)
  68. assert_equal(res, [frozenset({"d"}), frozenset({"c", "b"}), frozenset({"a"})])
  69. def test_order_ascc(self) -> None:
  70. manager = self._make_manager()
  71. graph = {
  72. "a": State("a", None, "import b, c", manager),
  73. "d": State("d", None, "def f(): import a", manager),
  74. "b": State("b", None, "import c", manager),
  75. "c": State("c", None, "import b, d", manager),
  76. }
  77. res = sorted_components(graph)
  78. assert_equal(res, [frozenset({"a", "d", "c", "b"})])
  79. ascc = res[0]
  80. scc = order_ascc(graph, ascc)
  81. assert_equal(scc, ["d", "c", "b", "a"])