forked from ycm-core/ycmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.py
executable file
·340 lines (283 loc) · 11.6 KB
/
run_tests.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
# Passing an environment variable containing unicode literals to a subprocess
# on Windows and Python2 raises a TypeError. Since there is no unicode
# string in this script, we don't import unicode_literals to avoid the issue.
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
# Not installing aliases from python-future; it's unreliable and slow.
from builtins import * # noqa
import argparse
import platform
import os
import glob
import subprocess
import os.path as p
import sys
DIR_OF_THIS_SCRIPT = p.dirname( p.abspath( __file__ ) )
DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
LIBCLANG_DIR = p.join( DIR_OF_THIRD_PARTY, 'clang', 'lib' )
# We skip python-future because it needs to be inserted in sys.path AFTER the
# standard library imports but we can't do that with PYTHONPATH because the std
# lib paths are always appended to PYTHONPATH. We do it correctly in ycmd
# because we have access to the right sys.path. So for dev, we rely on
# python-future being installed correctly with
# pip install -r test_requirements.txt
#
# Pip knows how to install this correctly so that it doesn't matter where in
# sys.path the path is.
python_path = [
p.join( DIR_OF_THIRD_PARTY, 'bottle' ),
p.join( DIR_OF_THIRD_PARTY,
'cregex',
'regex_{}'.format( sys.version_info[ 0 ] ) ),
p.join( DIR_OF_THIRD_PARTY, 'frozendict' ),
p.join( DIR_OF_THIRD_PARTY, 'jedi_deps', 'jedi' ),
p.join( DIR_OF_THIRD_PARTY, 'jedi_deps', 'numpydoc' ),
p.join( DIR_OF_THIRD_PARTY, 'jedi_deps', 'parso' ),
p.join( DIR_OF_THIRD_PARTY, 'requests_deps', 'certifi' ),
p.join( DIR_OF_THIRD_PARTY, 'requests_deps', 'chardet' ),
p.join( DIR_OF_THIRD_PARTY, 'requests_deps', 'idna' ),
p.join( DIR_OF_THIRD_PARTY, 'requests_deps', 'requests' ),
p.join( DIR_OF_THIRD_PARTY, 'requests_deps', 'urllib3', 'src' ),
p.join( DIR_OF_THIRD_PARTY, 'waitress' ),
]
if os.environ.get( 'PYTHONPATH' ) is not None:
python_path.append( os.environ[ 'PYTHONPATH' ] )
os.environ[ 'PYTHONPATH' ] = os.pathsep.join( python_path )
def OnWindows():
return platform.system() == 'Windows'
def RunFlake8():
print( 'Running flake8' )
args = [ sys.executable,
'-m',
'flake8',
p.join( DIR_OF_THIS_SCRIPT, 'ycmd' ) ]
root_dir_scripts = glob.glob( p.join( DIR_OF_THIS_SCRIPT, '*.py' ) )
args.extend( root_dir_scripts )
subprocess.check_call( args )
# Newer completers follow a standard convention of:
# - build: --<completer>-completer
# - test directory: ycmd/tests/<completer>
# - no aliases.
SIMPLE_COMPLETERS = [
'clangd',
'rust',
'go',
]
# More complex or legacy cases can specify all of:
# - build: flags to add to build.py to include this completer
# - test: flags to add to run_tests.py when _not_ testing this completer
# - aliases?: list of completer aliases for the --completers option
COMPLETERS = {
'cfamily': {
'build': [ '--clang-completer' ],
'test': [ '--exclude-dir=ycmd/tests/clang' ],
'aliases': [ 'c', 'cpp', 'c++', 'objc', 'clang', ]
},
'cs': {
'build': [ '--cs-completer' ],
'test': [ '--exclude-dir=ycmd/tests/cs' ],
'aliases': [ 'omnisharp', 'csharp', 'c#' ]
},
'javascript': {
'build': [ '--js-completer' ],
'test': [ '--exclude-dir=ycmd/tests/tern' ],
'aliases': [ 'js', 'tern' ]
},
'typescript': {
'build': [ '--ts-completer' ],
'test': [ '--exclude-dir=ycmd/tests/javascript',
'--exclude-dir=ycmd/tests/typescript' ],
'aliases': [ 'ts' ]
},
'python': {
'build': [],
'test': [ '--exclude-dir=ycmd/tests/python' ],
'aliases': [ 'jedi', 'jedihttp', ]
},
'java': {
'build': [ '--java-completer' ],
'test': [ '--exclude-dir=ycmd/tests/java' ],
'aliases': [ 'jdt' ],
},
}
# Add in the simple completers
for completer in SIMPLE_COMPLETERS:
COMPLETERS[ completer ] = {
'build': [ '--{}-completer'.format( completer ) ],
'test': [ '--exclude-dir=ycmd/tests/{}'.format( completer ) ],
}
def CompleterType( value ):
value = value.lower()
if value in COMPLETERS:
return value
else:
aliases_to_completer = { i: k for k, v in COMPLETERS.items()
for i in v[ 'aliases' ] }
if value in aliases_to_completer:
return aliases_to_completer[ value ]
else:
raise argparse.ArgumentTypeError(
'{0} is not a valid completer - should be one of {1}'.format(
value, COMPLETERS.keys() ) )
def ParseArguments():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument( '--no-clang-completer', action = 'store_true',
help = argparse.SUPPRESS ) # deprecated
group.add_argument( '--no-completers', nargs ='*', type = CompleterType,
help = 'Do not build or test with listed semantic '
'completion engine(s). Valid values: {0}'.format(
COMPLETERS.keys() ) )
group.add_argument( '--completers', nargs ='*', type = CompleterType,
help = 'Only build and test with listed semantic '
'completion engine(s). Valid values: {0}'.format(
COMPLETERS.keys() ) )
parser.add_argument( '--skip-build', action = 'store_true',
help = 'Do not build ycmd before testing.' )
parser.add_argument( '--msvc', type = int, choices = [ 14, 15, 16 ],
default = 16, help = 'Choose the Microsoft Visual '
'Studio version (default: %(default)s).' )
parser.add_argument( '--coverage', action = 'store_true',
help = 'Enable coverage report (requires coverage pkg)' )
parser.add_argument( '--no-flake8', action = 'store_true',
help = 'Disable flake8 run.' )
parser.add_argument( '--dump-path', action = 'store_true',
help = 'Dump the PYTHONPATH required to run tests '
'manually, then exit.' )
parser.add_argument( '--no-retry', action = 'store_true',
help = 'Disable retry of flaky tests' )
parser.add_argument( '--quiet', action = 'store_true',
help = 'Quiet installation mode. Just print overall '
'progress and errors' )
parsed_args, nosetests_args = parser.parse_known_args()
parsed_args.completers = FixupCompleters( parsed_args )
if 'COVERAGE' in os.environ:
parsed_args.coverage = ( os.environ[ 'COVERAGE' ] == 'true' )
return parsed_args, nosetests_args
def FixupCompleters( parsed_args ):
completers = set( COMPLETERS.keys() )
if parsed_args.completers is not None:
completers = set( parsed_args.completers )
elif parsed_args.no_completers is not None:
completers = completers.difference( parsed_args.no_completers )
elif parsed_args.no_clang_completer:
print( 'WARNING: The "--no-clang-completer" flag is deprecated. '
'Please use "--no-completers cfamily" instead.' )
completers.discard( 'cfamily' )
if 'USE_CLANG_COMPLETER' in os.environ:
if os.environ[ 'USE_CLANG_COMPLETER' ] == 'false':
completers.discard( 'cfamily' )
else:
completers.add( 'cfamily' )
return list( completers )
def BuildYcmdLibs( args ):
if not args.skip_build:
if 'EXTRA_CMAKE_ARGS' in os.environ:
os.environ[ 'EXTRA_CMAKE_ARGS' ] += ' -DUSE_DEV_FLAGS=ON'
else:
os.environ[ 'EXTRA_CMAKE_ARGS' ] = '-DUSE_DEV_FLAGS=ON'
build_cmd = [
sys.executable,
p.join( DIR_OF_THIS_SCRIPT, 'build.py' ),
'--core-tests'
]
for key in COMPLETERS:
if key in args.completers:
build_cmd.extend( COMPLETERS[ key ][ 'build' ] )
if args.msvc:
build_cmd.extend( [ '--msvc', str( args.msvc ) ] )
if args.coverage:
# In order to generate coverage data for C++, we use gcov. This requires
# some files generated when building (*.gcno), so we store the build
# output in a known directory, which is then used by the CI infrastructure
# to generate the c++ coverage information.
build_cmd.extend( [ '--enable-coverage', '--build-dir', '.build' ] )
if args.quiet:
build_cmd.append( '--quiet' )
subprocess.check_call( build_cmd )
def NoseTests( parsed_args, extra_nosetests_args ):
# Always passing --with-id to nosetests enables non-surprising usage of
# its --failed flag.
# By default, nose does not include files starting with a underscore in its
# report but we want __main__.py to be included. Only ignore files starting
# with a dot and setup.py.
nosetests_args = [ '-v', '--with-id', r'--ignore-files=(^\.|^setup\.py$)' ]
for key in COMPLETERS:
if key not in parsed_args.completers:
nosetests_args.extend( COMPLETERS[ key ][ 'test' ] )
if parsed_args.coverage:
# We need to exclude the ycmd/tests/python/testdata directory since it
# contains Python files and its base name starts with "test".
nosetests_args += [ '--exclude-dir=ycmd/tests/python/testdata',
'--with-coverage',
'--cover-erase',
'--cover-package=ycmd',
'--cover-html',
'--cover-inclusive' ]
if extra_nosetests_args:
nosetests_args.extend( extra_nosetests_args )
else:
nosetests_args.append( p.join( DIR_OF_THIS_SCRIPT, 'ycmd' ) )
env = os.environ.copy()
if parsed_args.no_retry:
# Useful for _writing_ tests
env[ 'YCM_TEST_NO_RETRY' ] = '1'
if OnWindows():
# We prepend the Clang third-party directory to the PATH instead of
# overwriting it so that the executable is able to find the Python library.
env[ 'PATH' ] = LIBCLANG_DIR + ';' + env[ 'PATH' ]
else:
env[ 'LD_LIBRARY_PATH' ] = LIBCLANG_DIR
subprocess.check_call( [ sys.executable, '-m', 'nose' ] + nosetests_args,
env=env )
# On Windows, distutils.spawn.find_executable only works for .exe files
# but .bat and .cmd files are also executables, so we use our own
# implementation.
def FindExecutable( executable ):
# Executable extensions used on Windows
WIN_EXECUTABLE_EXTS = [ '.exe', '.bat', '.cmd' ]
paths = os.environ[ 'PATH' ].split( os.pathsep )
base, extension = os.path.splitext( executable )
if OnWindows() and extension.lower() not in WIN_EXECUTABLE_EXTS:
extensions = WIN_EXECUTABLE_EXTS
else:
extensions = [ '' ]
for extension in extensions:
executable_name = executable + extension
if not os.path.isfile( executable_name ):
for path in paths:
executable_path = os.path.join( path, executable_name )
if os.path.isfile( executable_path ):
return executable_path
else:
return executable_name
return None
def FindExecutableOrDie( executable, message ):
path = FindExecutable( executable )
if not path:
sys.exit( "ERROR: Unable to find executable '{0}'. {1}".format(
executable,
message ) )
return path
def SetUpGenericLSPCompleter():
old_cwd = os.getcwd()
os.chdir( os.path.join( DIR_OF_THIRD_PARTY, 'generic_server' ) )
npm = FindExecutableOrDie( 'npm', 'npm is required to'
'run GenericLSPCompleter tests.' )
subprocess.check_call( [ npm, 'install' ] )
os.chdir( old_cwd )
def Main():
parsed_args, nosetests_args = ParseArguments()
if parsed_args.dump_path:
print( os.environ[ 'PYTHONPATH' ] )
sys.exit()
print( 'Running tests on Python', platform.python_version() )
SetUpGenericLSPCompleter()
if not parsed_args.no_flake8:
RunFlake8()
BuildYcmdLibs( parsed_args )
NoseTests( parsed_args, nosetests_args )
if __name__ == "__main__":
Main()