test_module_names.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. """
  2. tests.unit.test_test_module_name
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. """
  5. import fnmatch
  6. import os
  7. import salt.utils.path
  8. import salt.utils.stringutils
  9. from tests.support.paths import list_test_mods
  10. from tests.support.runtests import RUNTIME_VARS
  11. from tests.support.unit import TestCase
  12. EXCLUDED_DIRS = [
  13. os.path.join("tests", "integration", "cloud", "helpers"),
  14. os.path.join("tests", "integration", "files"),
  15. os.path.join("tests", "kitchen", "tests"),
  16. os.path.join("tests", "perf"),
  17. os.path.join("tests", "pkg"),
  18. os.path.join("tests", "support"),
  19. os.path.join("tests", "unit", "files"),
  20. os.path.join("tests", "unit", "modules", "inspectlib"),
  21. os.path.join("tests", "unit", "modules", "nxos"),
  22. os.path.join("tests", "unit", "modules", "zypp"),
  23. os.path.join("tests", "unit", "setup"),
  24. os.path.join("tests", "unit", "templates", "files"),
  25. os.path.join("tests", "unit", "utils", "cache_mods"),
  26. ]
  27. INCLUDED_DIRS = [
  28. os.path.join("tests", "kitchen", "tests", "*", "tests", "*"),
  29. ]
  30. EXCLUDED_FILES = [
  31. os.path.join("tests", "buildpackage.py"),
  32. os.path.join("tests", "committer_parser.py"),
  33. os.path.join("tests", "consist.py"),
  34. os.path.join("tests", "eventlisten.py"),
  35. os.path.join("tests", "jenkins.py"),
  36. os.path.join("tests", "minionswarm.py"),
  37. os.path.join("tests", "modparser.py"),
  38. os.path.join("tests", "packdump.py"),
  39. os.path.join("tests", "runtests.py"),
  40. os.path.join("tests", "salt-tcpdump.py"),
  41. os.path.join("tests", "saltsh.py"),
  42. os.path.join("tests", "unit", "test_pytest_pass_fail.py"),
  43. os.path.join("tests", "unit", "transport", "mixins.py"),
  44. os.path.join("tests", "unit", "utils", "scheduler", "base.py"),
  45. os.path.join("tests", "virtualname.py"),
  46. os.path.join("tests", "wheeltest.py"),
  47. os.path.join("tests", "zypp_plugin.py"),
  48. ]
  49. class BadTestModuleNamesTestCase(TestCase):
  50. """
  51. Unit test case for testing bad names for test modules
  52. """
  53. maxDiff = None
  54. def _match_dirs(self, reldir, matchdirs):
  55. return any(fnmatch.fnmatchcase(reldir, mdir) for mdir in matchdirs)
  56. def test_module_name(self):
  57. """
  58. Make sure all test modules conform to the test_*.py naming scheme
  59. """
  60. excluded_dirs, included_dirs = tuple(EXCLUDED_DIRS), tuple(INCLUDED_DIRS)
  61. tests_dir = os.path.join(RUNTIME_VARS.CODE_DIR, "tests")
  62. bad_names = []
  63. for root, _, files in salt.utils.path.os_walk(tests_dir):
  64. reldir = os.path.relpath(root, RUNTIME_VARS.CODE_DIR)
  65. if (
  66. reldir.startswith(excluded_dirs)
  67. and not self._match_dirs(reldir, included_dirs)
  68. ) or reldir.endswith("__pycache__"):
  69. continue
  70. for fname in files:
  71. if fname in ("__init__.py", "conftest.py") or not fname.endswith(".py"):
  72. continue
  73. relpath = os.path.join(reldir, fname)
  74. if relpath in EXCLUDED_FILES:
  75. continue
  76. if not fname.startswith("test_"):
  77. bad_names.append(relpath)
  78. error_msg = "\n\nPlease rename the following files:\n"
  79. for path in bad_names:
  80. directory, filename = path.rsplit(os.sep, 1)
  81. filename, _ = os.path.splitext(filename)
  82. error_msg += " {} -> {}/test_{}.py\n".format(
  83. path, directory, filename.split("_test")[0]
  84. )
  85. error_msg += "\nIf you believe one of the entries above should be ignored, please add it to either\n"
  86. error_msg += "'EXCLUDED_DIRS' or 'EXCLUDED_FILES' in 'tests/unit/test_module_names.py'.\n"
  87. error_msg += "If it is a tests module, then please rename as suggested."
  88. self.assertEqual([], bad_names, error_msg)
  89. def test_module_name_source_match(self):
  90. """
  91. Check all the test mods and check if they correspond to actual files in
  92. the codebase. If this test fails, then a test module is likely not
  93. named correctly, and should be adjusted.
  94. If a test module doesn't have a natural name match (as does this very
  95. file), then its should be included in the "ignore" tuple below.
  96. However, if there is no matching source code file, then you should
  97. consider mapping it to files manually via tests/filename_map.yml.
  98. """
  99. ignore = (
  100. "integration.cli.test_custom_module",
  101. "integration.cli.test_grains",
  102. "integration.client.test_kwarg",
  103. "integration.client.test_runner",
  104. "integration.client.test_standard",
  105. "integration.client.test_syndic",
  106. "integration.cloud.test_cloud",
  107. "integration.doc.test_man",
  108. "integration.externalapi.test_venafiapi",
  109. "integration.grains.test_custom",
  110. "integration.loader.test_ext_grains",
  111. "integration.loader.test_ext_modules",
  112. "integration.logging.handlers.test_logstash_mod",
  113. "integration.logging.test_jid_logging",
  114. "integration.master.test_clear_funcs",
  115. "integration.master.test_event_return",
  116. "integration.minion.test_executor",
  117. "integration.minion.test_minion_cache",
  118. "integration.minion.test_timeout",
  119. "integration.modules.test_decorators",
  120. "integration.modules.test_pkg",
  121. "integration.modules.test_service",
  122. "integration.modules.test_state_jinja_filters",
  123. "integration.modules.test_sysctl",
  124. "integration.netapi.rest_cherrypy.test_app_pam",
  125. "integration.netapi.rest_tornado.test_app",
  126. "integration.netapi.test_client",
  127. "integration.output.test_output",
  128. "integration.pillar.test_pillar_include",
  129. "integration.proxy.test_shell",
  130. "integration.proxy.test_simple",
  131. "integration.reactor.test_reactor",
  132. "integration.returners.test_noop_return",
  133. "integration.runners.test_runner_returns",
  134. "integration.shell.test_arguments",
  135. "integration.shell.test_auth",
  136. "integration.shell.test_call",
  137. "integration.shell.test_cloud",
  138. "integration.shell.test_cp",
  139. "integration.shell.test_enabled",
  140. "integration.shell.test_key",
  141. "integration.shell.test_master",
  142. "integration.shell.test_master_tops",
  143. "integration.shell.test_minion",
  144. "integration.shell.test_proxy",
  145. "integration.shell.test_runner",
  146. "integration.shell.test_saltcli",
  147. "integration.shell.test_spm",
  148. "integration.shell.test_syndic",
  149. "integration.spm.test_build",
  150. "integration.spm.test_files",
  151. "integration.spm.test_info",
  152. "integration.spm.test_install",
  153. "integration.spm.test_remove",
  154. "integration.spm.test_repo",
  155. "integration.ssh.test_deploy",
  156. "integration.ssh.test_grains",
  157. "integration.ssh.test_jinja_filters",
  158. "integration.ssh.test_master",
  159. "integration.ssh.test_mine",
  160. "integration.ssh.test_pillar",
  161. "integration.ssh.test_pre_flight",
  162. "integration.ssh.test_raw",
  163. "integration.ssh.test_saltcheck",
  164. "integration.ssh.test_state",
  165. "integration.states.test_compiler",
  166. "integration.states.test_handle_error",
  167. "integration.states.test_handle_iorder",
  168. "integration.states.test_match",
  169. "integration.states.test_renderers",
  170. "integration.wheel.test_client",
  171. "unit.cache.test_cache",
  172. "unit.serializers.test_serializers",
  173. "unit.setup.test_install",
  174. "unit.setup.test_man",
  175. "unit.states.test_postgres",
  176. "unit.test_doc",
  177. "unit.test_mock",
  178. "unit.test_module_names",
  179. "unit.test_proxy_minion",
  180. "unit.test_pytest_pass_fail",
  181. "unit.test_simple",
  182. "unit.test_virtualname",
  183. "unit.test_zypp_plugins",
  184. "unit.utils.scheduler.test_error",
  185. "unit.utils.scheduler.test_eval",
  186. "unit.utils.scheduler.test_helpers",
  187. "unit.utils.scheduler.test_maxrunning",
  188. "unit.utils.scheduler.test_postpone",
  189. "unit.utils.scheduler.test_run_job",
  190. "unit.utils.scheduler.test_schedule",
  191. "unit.utils.scheduler.test_skip",
  192. )
  193. errors = []
  194. def _format_errors(errors):
  195. msg = (
  196. "The following {} test module(s) could not be matched to a "
  197. "source code file:\n\n".format(len(errors))
  198. )
  199. msg += "".join(errors)
  200. return msg
  201. for mod_name in list_test_mods():
  202. if mod_name in ignore:
  203. # Test module is being ignored, skip it
  204. continue
  205. # Separate the test_foo away from the rest of the mod name, because
  206. # we'll need to remove the "test_" from the beginning and add .py
  207. stem, flower = mod_name.rsplit(".", 1)
  208. # Lop off the integration/unit from the beginning of the mod name
  209. try:
  210. stem = stem.split(".", 1)[1]
  211. except IndexError:
  212. # This test mod was in the root of the unit/integration dir
  213. stem = ""
  214. # The path from the root of the repo
  215. relpath = salt.utils.path.join(
  216. stem.replace(".", os.sep), ".".join((flower[5:], "py"))
  217. )
  218. # The full path to the file we expect to find
  219. abspath = salt.utils.path.join(RUNTIME_VARS.SALT_CODE_DIR, relpath)
  220. if not os.path.isfile(abspath):
  221. # Maybe this is in a dunder init?
  222. alt_relpath = salt.utils.path.join(relpath[:-3], "__init__.py")
  223. alt_abspath = salt.utils.path.join(abspath[:-3], "__init__.py")
  224. if os.path.isfile(alt_abspath):
  225. # Yep, it is. Carry on!
  226. continue
  227. errors.append("{} (expected: {})\n".format(mod_name, relpath))
  228. assert not errors, _format_errors(errors)