py_dep_analysis_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import os
  2. import tempfile
  3. import unittest
  4. import py_dep_analysis as pda
  5. class TestPyDepAnalysis(unittest.TestCase):
  6. def create_tmp_file(self, path: str, content: str):
  7. with open(path, "w") as f:
  8. f.write(content)
  9. def test_full_module_path(self):
  10. self.assertEqual(pda._full_module_path("aa.bb.cc", "__init__.py"), "aa.bb.cc")
  11. self.assertEqual(pda._full_module_path("aa.bb.cc", "dd.py"), "aa.bb.cc.dd")
  12. self.assertEqual(pda._full_module_path("", "dd.py"), "dd")
  13. def test_bazel_path_to_module_path(self):
  14. self.assertEqual(
  15. pda._bazel_path_to_module_path("//python/ray/rllib:xxx/yyy/dd"),
  16. "ray.rllib.xxx.yyy.dd",
  17. )
  18. self.assertEqual(
  19. pda._bazel_path_to_module_path("python:ray/rllib/xxx/yyy/dd"),
  20. "ray.rllib.xxx.yyy.dd",
  21. )
  22. self.assertEqual(
  23. pda._bazel_path_to_module_path("python/ray/rllib:xxx/yyy/dd"),
  24. "ray.rllib.xxx.yyy.dd",
  25. )
  26. def test_file_path_to_module_path(self):
  27. self.assertEqual(
  28. pda._file_path_to_module_path("python/ray/rllib/env/env.py"),
  29. "ray.rllib.env.env",
  30. )
  31. self.assertEqual(
  32. pda._file_path_to_module_path("python/ray/rllib/env/__init__.py"),
  33. "ray.rllib.env",
  34. )
  35. def test_import_line_continuation(self):
  36. graph = pda.DepGraph()
  37. graph.ids["ray"] = 0
  38. with tempfile.TemporaryDirectory() as tmpdir:
  39. src_path = os.path.join(tmpdir, "continuation1.py")
  40. self.create_tmp_file(
  41. src_path,
  42. """
  43. import ray.rllib.env.\\
  44. mock_env
  45. b = 2
  46. """,
  47. )
  48. pda._process_file(graph, src_path, "ray")
  49. self.assertEqual(len(graph.ids), 2)
  50. print(graph.ids)
  51. # Shoud pick up the full module name.
  52. self.assertEqual(graph.ids["ray.rllib.env.mock_env"], 1)
  53. self.assertEqual(graph.edges[0], {1: True})
  54. def test_import_line_continuation_parenthesis(self):
  55. graph = pda.DepGraph()
  56. graph.ids["ray"] = 0
  57. with tempfile.TemporaryDirectory() as tmpdir:
  58. src_path = os.path.join(tmpdir, "continuation1.py")
  59. self.create_tmp_file(
  60. src_path,
  61. """
  62. from ray.rllib.env import (ClassName,
  63. module1, module2)
  64. b = 2
  65. """,
  66. )
  67. pda._process_file(graph, src_path, "ray")
  68. self.assertEqual(len(graph.ids), 2)
  69. print(graph.ids)
  70. # Shoud pick up the full module name without trailing (.
  71. self.assertEqual(graph.ids["ray.rllib.env"], 1)
  72. self.assertEqual(graph.edges[0], {1: True})
  73. def test_from_import_file_module(self):
  74. graph = pda.DepGraph()
  75. graph.ids["ray"] = 0
  76. with tempfile.TemporaryDirectory() as tmpdir:
  77. src_path = "multi_line_comment_3.py"
  78. self.create_tmp_file(
  79. os.path.join(tmpdir, src_path),
  80. """
  81. from ray.rllib.env import mock_env
  82. a = 1
  83. b = 2
  84. """,
  85. )
  86. # Touch ray/rllib/env/mock_env.py in tmpdir,
  87. # so that it looks like a module.
  88. module_dir = os.path.join(tmpdir, "python", "ray", "rllib", "env")
  89. os.makedirs(module_dir, exist_ok=True)
  90. f = open(os.path.join(module_dir, "mock_env.py"), "w")
  91. f.write("print('hello world!')")
  92. f.close
  93. pda._process_file(graph, src_path, "ray", _base_dir=tmpdir)
  94. self.assertEqual(len(graph.ids), 2)
  95. self.assertEqual(graph.ids["ray.rllib.env.mock_env"], 1)
  96. # Only 1 edge from ray to ray.rllib.env.mock_env
  97. # ray.tune.tune is ignored.
  98. self.assertEqual(graph.edges[0], {1: True})
  99. def test_from_import_class_object(self):
  100. graph = pda.DepGraph()
  101. graph.ids["ray"] = 0
  102. with tempfile.TemporaryDirectory() as tmpdir:
  103. src_path = "multi_line_comment_3.py"
  104. self.create_tmp_file(
  105. os.path.join(tmpdir, src_path),
  106. """
  107. from ray.rllib.env import MockEnv
  108. a = 1
  109. b = 2
  110. """,
  111. )
  112. # Touch ray/rllib/env.py in tmpdir,
  113. # MockEnv is a class on env module.
  114. module_dir = os.path.join(tmpdir, "python", "ray", "rllib")
  115. os.makedirs(module_dir, exist_ok=True)
  116. f = open(os.path.join(module_dir, "env.py"), "w")
  117. f.write("print('hello world!')")
  118. f.close
  119. pda._process_file(graph, src_path, "ray", _base_dir=tmpdir)
  120. self.assertEqual(len(graph.ids), 2)
  121. # Should depend on env.py instead.
  122. self.assertEqual(graph.ids["ray.rllib.env"], 1)
  123. # Only 1 edge from ray to ray.rllib.env.mock_env
  124. # ray.tune.tune is ignored.
  125. self.assertEqual(graph.edges[0], {1: True})
  126. if __name__ == "__main__":
  127. import pytest
  128. import sys
  129. sys.exit(pytest.main(["-v", __file__]))