test_module_names.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_blackout",
  117. "integration.minion.test_executor",
  118. "integration.minion.test_minion_cache",
  119. "integration.minion.test_pillar",
  120. "integration.minion.test_timeout",
  121. "integration.modules.test_decorators",
  122. "integration.modules.test_pkg",
  123. "integration.modules.test_service",
  124. "integration.modules.test_state_jinja_filters",
  125. "integration.modules.test_sysctl",
  126. "integration.netapi.rest_cherrypy.test_app_pam",
  127. "integration.netapi.rest_tornado.test_app",
  128. "integration.netapi.test_client",
  129. "integration.output.test_output",
  130. "integration.pillar.test_pillar_include",
  131. "integration.proxy.test_shell",
  132. "integration.proxy.test_simple",
  133. "integration.reactor.test_reactor",
  134. "integration.returners.test_noop_return",
  135. "integration.runners.test_runner_returns",
  136. "integration.shell.test_arguments",
  137. "integration.shell.test_auth",
  138. "integration.shell.test_call",
  139. "integration.shell.test_cloud",
  140. "integration.shell.test_cp",
  141. "integration.shell.test_enabled",
  142. "integration.shell.test_key",
  143. "integration.shell.test_master",
  144. "integration.shell.test_master_tops",
  145. "integration.shell.test_matcher",
  146. "integration.shell.test_minion",
  147. "integration.shell.test_proxy",
  148. "integration.shell.test_runner",
  149. "integration.shell.test_saltcli",
  150. "integration.shell.test_spm",
  151. "integration.shell.test_syndic",
  152. "integration.spm.test_build",
  153. "integration.spm.test_files",
  154. "integration.spm.test_info",
  155. "integration.spm.test_install",
  156. "integration.spm.test_remove",
  157. "integration.spm.test_repo",
  158. "integration.ssh.test_deploy",
  159. "integration.ssh.test_grains",
  160. "integration.ssh.test_jinja_filters",
  161. "integration.ssh.test_master",
  162. "integration.ssh.test_mine",
  163. "integration.ssh.test_pillar",
  164. "integration.ssh.test_pre_flight",
  165. "integration.ssh.test_raw",
  166. "integration.ssh.test_saltcheck",
  167. "integration.ssh.test_state",
  168. "integration.states.test_compiler",
  169. "integration.states.test_handle_error",
  170. "integration.states.test_handle_iorder",
  171. "integration.states.test_match",
  172. "integration.states.test_renderers",
  173. "integration.wheel.test_client",
  174. "multimaster.minion.test_event",
  175. "unit.cache.test_cache",
  176. "unit.serializers.test_serializers",
  177. "unit.setup.test_install",
  178. "unit.setup.test_man",
  179. "unit.states.test_postgres",
  180. "unit.test_doc",
  181. "unit.test_mock",
  182. "unit.test_module_names",
  183. "unit.test_proxy_minion",
  184. "unit.test_pytest_pass_fail",
  185. "unit.test_simple",
  186. "unit.test_virtualname",
  187. "unit.test_zypp_plugins",
  188. "unit.utils.scheduler.test_error",
  189. "unit.utils.scheduler.test_eval",
  190. "unit.utils.scheduler.test_helpers",
  191. "unit.utils.scheduler.test_maxrunning",
  192. "unit.utils.scheduler.test_postpone",
  193. "unit.utils.scheduler.test_run_job",
  194. "unit.utils.scheduler.test_schedule",
  195. "unit.utils.scheduler.test_skip",
  196. )
  197. errors = []
  198. def _format_errors(errors):
  199. msg = (
  200. "The following {} test module(s) could not be matched to a "
  201. "source code file:\n\n".format(len(errors))
  202. )
  203. msg += "".join(errors)
  204. return msg
  205. for mod_name in list_test_mods():
  206. if mod_name in ignore:
  207. # Test module is being ignored, skip it
  208. continue
  209. # Separate the test_foo away from the rest of the mod name, because
  210. # we'll need to remove the "test_" from the beginning and add .py
  211. stem, flower = mod_name.rsplit(".", 1)
  212. # Lop off the integration/unit from the beginning of the mod name
  213. try:
  214. stem = stem.split(".", 1)[1]
  215. except IndexError:
  216. # This test mod was in the root of the unit/integration dir
  217. stem = ""
  218. # The path from the root of the repo
  219. relpath = salt.utils.path.join(
  220. stem.replace(".", os.sep), ".".join((flower[5:], "py"))
  221. )
  222. # The full path to the file we expect to find
  223. abspath = salt.utils.path.join(RUNTIME_VARS.SALT_CODE_DIR, relpath)
  224. if not os.path.isfile(abspath):
  225. # Maybe this is in a dunder init?
  226. alt_relpath = salt.utils.path.join(relpath[:-3], "__init__.py")
  227. alt_abspath = salt.utils.path.join(abspath[:-3], "__init__.py")
  228. if os.path.isfile(alt_abspath):
  229. # Yep, it is. Carry on!
  230. continue
  231. errors.append("{} (expected: {})\n".format(mod_name, relpath))
  232. assert not errors, _format_errors(errors)