testgraph.py 3.0 KB

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