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