noxfile.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. # -*- coding: utf-8 -*-
  2. '''
  3. noxfile
  4. ~~~~~~~
  5. Nox configuration script
  6. '''
  7. # pylint: disable=resource-leakage
  8. # Import Python libs
  9. from __future__ import absolute_import, unicode_literals, print_function
  10. import os
  11. import sys
  12. import glob
  13. import json
  14. import pprint
  15. import shutil
  16. import tempfile
  17. import datetime
  18. if __name__ == '__main__':
  19. sys.stderr.write('Do not execute this file directly. Use nox instead, it will know how to handle this file\n')
  20. sys.stderr.flush()
  21. exit(1)
  22. # Import 3rd-party libs
  23. import nox
  24. from nox.command import CommandFailed
  25. IS_PY3 = sys.version_info > (2,)
  26. # Be verbose when runing under a CI context
  27. PIP_INSTALL_SILENT = (os.environ.get('JENKINS_URL') or os.environ.get('CI') or os.environ.get('DRONE')) is None
  28. # Global Path Definitions
  29. REPO_ROOT = os.path.abspath(os.path.dirname(__file__))
  30. SITECUSTOMIZE_DIR = os.path.join(REPO_ROOT, 'tests', 'support', 'coverage')
  31. IS_DARWIN = sys.platform.lower().startswith('darwin')
  32. IS_WINDOWS = sys.platform.lower().startswith('win')
  33. # Python versions to run against
  34. _PYTHON_VERSIONS = ('2', '2.7', '3', '3.4', '3.5', '3.6', '3.7')
  35. # Nox options
  36. # Reuse existing virtualenvs
  37. nox.options.reuse_existing_virtualenvs = True
  38. # Don't fail on missing interpreters
  39. nox.options.error_on_missing_interpreters = False
  40. # Change current directory to REPO_ROOT
  41. os.chdir(REPO_ROOT)
  42. RUNTESTS_LOGFILE = os.path.join(
  43. 'artifacts', 'logs',
  44. 'runtests-{}.log'.format(datetime.datetime.now().strftime('%Y%m%d%H%M%S.%f'))
  45. )
  46. # Prevent Python from writing bytecode
  47. os.environ[str('PYTHONDONTWRITEBYTECODE')] = str('1')
  48. def _create_ci_directories():
  49. for dirname in ('logs', 'coverage', 'xml-unittests-output'):
  50. path = os.path.join('artifacts', dirname)
  51. if not os.path.exists(path):
  52. os.makedirs(path)
  53. def _get_session_python_version_info(session):
  54. try:
  55. version_info = session._runner._real_python_version_info
  56. except AttributeError:
  57. old_install_only_value = session._runner.global_config.install_only
  58. try:
  59. # Force install only to be false for the following chunk of code
  60. # For additional information as to why see:
  61. # https://github.com/theacodes/nox/pull/181
  62. session._runner.global_config.install_only = False
  63. session_py_version = session.run(
  64. 'python', '-c'
  65. 'import sys; sys.stdout.write("{}.{}.{}".format(*sys.version_info))',
  66. silent=True,
  67. log=False,
  68. )
  69. version_info = tuple(int(part) for part in session_py_version.split('.') if part.isdigit())
  70. session._runner._real_python_version_info = version_info
  71. finally:
  72. session._runner.global_config.install_only = old_install_only_value
  73. return version_info
  74. def _get_session_python_site_packages_dir(session):
  75. try:
  76. site_packages_dir = session._runner._site_packages_dir
  77. except AttributeError:
  78. old_install_only_value = session._runner.global_config.install_only
  79. try:
  80. # Force install only to be false for the following chunk of code
  81. # For additional information as to why see:
  82. # https://github.com/theacodes/nox/pull/181
  83. session._runner.global_config.install_only = False
  84. site_packages_dir = session.run(
  85. 'python', '-c'
  86. 'import sys; from distutils.sysconfig import get_python_lib; sys.stdout.write(get_python_lib())',
  87. silent=True,
  88. log=False,
  89. )
  90. session._runner._site_packages_dir = site_packages_dir
  91. finally:
  92. session._runner.global_config.install_only = old_install_only_value
  93. return site_packages_dir
  94. def _get_pydir(session):
  95. version_info = _get_session_python_version_info(session)
  96. if version_info < (2, 7):
  97. session.error('Only Python >= 2.7 is supported')
  98. return 'py{}.{}'.format(*version_info)
  99. def _get_distro_info(session):
  100. try:
  101. distro = session._runner._distro
  102. except AttributeError:
  103. # The distro package doesn't output anything for Windows
  104. old_install_only_value = session._runner.global_config.install_only
  105. try:
  106. # Force install only to be false for the following chunk of code
  107. # For additional information as to why see:
  108. # https://github.com/theacodes/nox/pull/181
  109. session._runner.global_config.install_only = False
  110. session.install('--progress-bar=off', 'distro', silent=PIP_INSTALL_SILENT)
  111. output = session.run('distro', '-j', silent=True)
  112. distro = json.loads(output.strip())
  113. session.log('Distro information:\n%s', pprint.pformat(distro))
  114. session._runner._distro = distro
  115. finally:
  116. session._runner.global_config.install_only = old_install_only_value
  117. return distro
  118. def _install_system_packages(session):
  119. '''
  120. Because some python packages are provided by the distribution and cannot
  121. be pip installed, and because we don't want the whole system python packages
  122. on our virtualenvs, we copy the required system python packages into
  123. the virtualenv
  124. '''
  125. system_python_packages = {
  126. '__debian_based_distros__': [
  127. '/usr/lib/python{py_version}/dist-packages/*apt*'
  128. ]
  129. }
  130. distro = _get_distro_info(session)
  131. if not distro['id'].startswith(('debian', 'ubuntu')):
  132. # This only applies to debian based distributions
  133. return
  134. system_python_packages['{id}-{version}'.format(**distro)] = \
  135. system_python_packages['{id}-{version_parts[major]}'.format(**distro)] = \
  136. system_python_packages['__debian_based_distros__'][:]
  137. distro_keys = [
  138. '{id}'.format(**distro),
  139. '{id}-{version}'.format(**distro),
  140. '{id}-{version_parts[major]}'.format(**distro)
  141. ]
  142. version_info = _get_session_python_version_info(session)
  143. py_version_keys = [
  144. '{}'.format(*version_info),
  145. '{}.{}'.format(*version_info)
  146. ]
  147. session_site_packages_dir = _get_session_python_site_packages_dir(session)
  148. for distro_key in distro_keys:
  149. if distro_key not in system_python_packages:
  150. continue
  151. patterns = system_python_packages[distro_key]
  152. for pattern in patterns:
  153. for py_version in py_version_keys:
  154. matches = set(glob.glob(pattern.format(py_version=py_version)))
  155. if not matches:
  156. continue
  157. for match in matches:
  158. src = os.path.realpath(match)
  159. dst = os.path.join(session_site_packages_dir, os.path.basename(match))
  160. if os.path.exists(dst):
  161. session.log('Not overwritting already existing %s with %s', dst, src)
  162. continue
  163. session.log('Copying %s into %s', src, dst)
  164. if os.path.isdir(src):
  165. shutil.copytree(src, dst)
  166. else:
  167. shutil.copyfile(src, dst)
  168. def _get_distro_pip_constraints(session, transport):
  169. # Install requirements
  170. distro_constraints = []
  171. if transport == 'tcp':
  172. # The TCP requirements are the exact same requirements as the ZeroMQ ones
  173. transport = 'zeromq'
  174. pydir = _get_pydir(session)
  175. if IS_WINDOWS:
  176. _distro_constraints = os.path.join('requirements',
  177. 'static',
  178. pydir,
  179. '{}-windows.txt'.format(transport))
  180. if os.path.exists(_distro_constraints):
  181. distro_constraints.append(_distro_constraints)
  182. _distro_constraints = os.path.join('requirements',
  183. 'static',
  184. pydir,
  185. 'windows.txt')
  186. if os.path.exists(_distro_constraints):
  187. distro_constraints.append(_distro_constraints)
  188. _distro_constraints = os.path.join('requirements',
  189. 'static',
  190. pydir,
  191. 'windows-crypto.txt')
  192. if os.path.exists(_distro_constraints):
  193. distro_constraints.append(_distro_constraints)
  194. elif IS_DARWIN:
  195. _distro_constraints = os.path.join('requirements',
  196. 'static',
  197. pydir,
  198. '{}-darwin.txt'.format(transport))
  199. if os.path.exists(_distro_constraints):
  200. distro_constraints.append(_distro_constraints)
  201. _distro_constraints = os.path.join('requirements',
  202. 'static',
  203. pydir,
  204. 'darwin.txt')
  205. if os.path.exists(_distro_constraints):
  206. distro_constraints.append(_distro_constraints)
  207. _distro_constraints = os.path.join('requirements',
  208. 'static',
  209. pydir,
  210. 'darwin-crypto.txt')
  211. if os.path.exists(_distro_constraints):
  212. distro_constraints.append(_distro_constraints)
  213. else:
  214. _install_system_packages(session)
  215. distro = _get_distro_info(session)
  216. distro_keys = [
  217. 'linux',
  218. '{id}'.format(**distro),
  219. '{id}-{version}'.format(**distro),
  220. '{id}-{version_parts[major]}'.format(**distro)
  221. ]
  222. for distro_key in distro_keys:
  223. _distro_constraints = os.path.join('requirements',
  224. 'static',
  225. pydir,
  226. '{}.txt'.format(distro_key))
  227. if os.path.exists(_distro_constraints):
  228. distro_constraints.append(_distro_constraints)
  229. _distro_constraints = os.path.join('requirements',
  230. 'static',
  231. pydir,
  232. '{}-crypto.txt'.format(distro_key))
  233. if os.path.exists(_distro_constraints):
  234. distro_constraints.append(_distro_constraints)
  235. _distro_constraints = os.path.join('requirements',
  236. 'static',
  237. pydir,
  238. '{}-{}.txt'.format(transport, distro_key))
  239. if os.path.exists(_distro_constraints):
  240. distro_constraints.append(_distro_constraints)
  241. distro_constraints.append(_distro_constraints)
  242. _distro_constraints = os.path.join('requirements',
  243. 'static',
  244. pydir,
  245. '{}-{}-crypto.txt'.format(transport, distro_key))
  246. if os.path.exists(_distro_constraints):
  247. distro_constraints.append(_distro_constraints)
  248. return distro_constraints
  249. def _install_requirements(session, transport, *extra_requirements):
  250. # Install requirements
  251. distro_constraints = _get_distro_pip_constraints(session, transport)
  252. _requirements_files = [
  253. os.path.join('requirements', 'base.txt'),
  254. os.path.join('requirements', 'zeromq.txt'),
  255. os.path.join('requirements', 'pytest.txt')
  256. ]
  257. if sys.platform.startswith('linux'):
  258. requirements_files = [
  259. os.path.join('requirements', 'static', 'linux.in')
  260. ]
  261. elif sys.platform.startswith('win'):
  262. requirements_files = [
  263. os.path.join('pkg', 'windows', 'req.txt'),
  264. os.path.join('requirements', 'static', 'windows.in')
  265. ]
  266. elif sys.platform.startswith('darwin'):
  267. requirements_files = [
  268. os.path.join('pkg', 'osx', 'req.txt'),
  269. os.path.join('pkg', 'osx', 'req_ext.txt'),
  270. os.path.join('requirements', 'static', 'darwin.in')
  271. ]
  272. while True:
  273. if not requirements_files:
  274. break
  275. requirements_file = requirements_files.pop(0)
  276. if requirements_file not in _requirements_files:
  277. _requirements_files.append(requirements_file)
  278. session.log('Processing {}'.format(requirements_file))
  279. with open(requirements_file) as rfh: # pylint: disable=resource-leakage
  280. for line in rfh:
  281. line = line.strip()
  282. if not line:
  283. continue
  284. if line.startswith('-r'):
  285. reqfile = os.path.join(os.path.dirname(requirements_file), line.strip().split()[-1])
  286. if reqfile in _requirements_files:
  287. continue
  288. _requirements_files.append(reqfile)
  289. continue
  290. for requirements_file in _requirements_files:
  291. install_command = [
  292. '--progress-bar=off', '-r', requirements_file
  293. ]
  294. for distro_constraint in distro_constraints:
  295. install_command.extend([
  296. '--constraint', distro_constraint
  297. ])
  298. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  299. if extra_requirements:
  300. install_command = [
  301. '--progress-bar=off',
  302. ]
  303. for distro_constraint in distro_constraints:
  304. install_command.extend([
  305. '--constraint', distro_constraint
  306. ])
  307. install_command += list(extra_requirements)
  308. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  309. def _run_with_coverage(session, *test_cmd):
  310. session.install('--progress-bar=off', 'coverage==4.5.3', silent=PIP_INSTALL_SILENT)
  311. session.run('coverage', 'erase')
  312. python_path_env_var = os.environ.get('PYTHONPATH') or None
  313. if python_path_env_var is None:
  314. python_path_env_var = SITECUSTOMIZE_DIR
  315. else:
  316. python_path_entries = python_path_env_var.split(os.pathsep)
  317. if SITECUSTOMIZE_DIR in python_path_entries:
  318. python_path_entries.remove(SITECUSTOMIZE_DIR)
  319. python_path_entries.insert(0, SITECUSTOMIZE_DIR)
  320. python_path_env_var = os.pathsep.join(python_path_entries)
  321. env = {
  322. # The updated python path so that sitecustomize is importable
  323. 'PYTHONPATH': python_path_env_var,
  324. # The full path to the .coverage data file. Makes sure we always write
  325. # them to the same directory
  326. 'COVERAGE_FILE': os.path.abspath(os.path.join(REPO_ROOT, '.coverage')),
  327. # Instruct sub processes to also run under coverage
  328. 'COVERAGE_PROCESS_START': os.path.join(REPO_ROOT, '.coveragerc')
  329. }
  330. if IS_DARWIN:
  331. # Don't nuke our multiprocessing efforts objc!
  332. # https://stackoverflow.com/questions/50168647/multiprocessing-causes-python-to-crash-and-gives-an-error-may-have-been-in-progr
  333. env['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'
  334. try:
  335. session.run(*test_cmd, env=env)
  336. finally:
  337. # Always combine and generate the XML coverage report
  338. try:
  339. session.run('coverage', 'combine')
  340. except CommandFailed:
  341. # Sometimes some of the coverage files are corrupt which would trigger a CommandFailed
  342. # exception
  343. pass
  344. # Generate report for salt code coverage
  345. session.run(
  346. 'coverage', 'xml',
  347. '-o', os.path.join('artifacts', 'coverage', 'salt.xml'),
  348. '--omit=tests/*',
  349. '--include=salt/*'
  350. )
  351. # Generate report for tests code coverage
  352. session.run(
  353. 'coverage', 'xml',
  354. '-o', os.path.join('artifacts', 'coverage', 'tests.xml'),
  355. '--omit=salt/*',
  356. '--include=tests/*'
  357. )
  358. @nox.session(python=_PYTHON_VERSIONS, name='pytest-parametrized')
  359. @nox.parametrize('coverage', [False, True])
  360. @nox.parametrize('transport', ['zeromq', 'tcp'])
  361. @nox.parametrize('crypto', [None, 'm2crypto', 'pycryptodomex'])
  362. def pytest_parametrized(session, coverage, transport, crypto):
  363. # Install requirements
  364. _install_requirements(session, transport)
  365. if crypto:
  366. if crypto == 'm2crypto':
  367. session.run('pip', 'uninstall', '-y', 'pycrypto', 'pycryptodome', 'pycryptodomex', silent=True)
  368. else:
  369. session.run('pip', 'uninstall', '-y', 'm2crypto', silent=True)
  370. distro_constraints = _get_distro_pip_constraints(session, transport)
  371. install_command = [
  372. '--progress-bar=off',
  373. ]
  374. for distro_constraint in distro_constraints:
  375. install_command.extend([
  376. '--constraint', distro_constraint
  377. ])
  378. install_command.append(crypto)
  379. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  380. cmd_args = [
  381. '--rootdir', REPO_ROOT,
  382. '--log-file={}'.format(RUNTESTS_LOGFILE),
  383. '--log-file-level=debug',
  384. '--no-print-logs',
  385. '-ra',
  386. '-s',
  387. '--transport={}'.format(transport)
  388. ] + session.posargs
  389. _pytest(session, coverage, cmd_args)
  390. @nox.session(python=_PYTHON_VERSIONS)
  391. @nox.parametrize('coverage', [False, True])
  392. def pytest(session, coverage):
  393. '''
  394. pytest session with zeromq transport and default crypto
  395. '''
  396. session.notify(
  397. 'pytest-parametrized-{}(coverage={}, crypto=None, transport=\'zeromq\')'.format(
  398. session.python,
  399. coverage
  400. )
  401. )
  402. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tcp')
  403. @nox.parametrize('coverage', [False, True])
  404. def pytest_tcp(session, coverage):
  405. '''
  406. pytest session with TCP transport and default crypto
  407. '''
  408. session.notify(
  409. 'pytest-parametrized-{}(coverage={}, crypto=None, transport=\'tcp\')'.format(
  410. session.python,
  411. coverage
  412. )
  413. )
  414. @nox.session(python=_PYTHON_VERSIONS, name='pytest-zeromq')
  415. @nox.parametrize('coverage', [False, True])
  416. def pytest_zeromq(session, coverage):
  417. '''
  418. pytest session with zeromq transport and default crypto
  419. '''
  420. session.notify(
  421. 'pytest-parametrized-{}(coverage={}, crypto=None, transport=\'zeromq\')'.format(
  422. session.python,
  423. coverage
  424. )
  425. )
  426. @nox.session(python=_PYTHON_VERSIONS, name='pytest-m2crypto')
  427. @nox.parametrize('coverage', [False, True])
  428. def pytest_m2crypto(session, coverage):
  429. '''
  430. pytest session with zeromq transport and m2crypto
  431. '''
  432. session.notify(
  433. 'pytest-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'zeromq\')'.format(
  434. session.python,
  435. coverage
  436. )
  437. )
  438. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tcp-m2crypto')
  439. @nox.parametrize('coverage', [False, True])
  440. def pytest_tcp_m2crypto(session, coverage):
  441. '''
  442. pytest session with TCP transport and m2crypto
  443. '''
  444. session.notify(
  445. 'pytest-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'tcp\')'.format(
  446. session.python,
  447. coverage
  448. )
  449. )
  450. @nox.session(python=_PYTHON_VERSIONS, name='pytest-zeromq-m2crypto')
  451. @nox.parametrize('coverage', [False, True])
  452. def pytest_zeromq_m2crypto(session, coverage):
  453. '''
  454. pytest session with zeromq transport and m2crypto
  455. '''
  456. session.notify(
  457. 'pytest-parametrized-{}(coverage={}, crypto=\'m2crypto\', transport=\'zeromq\')'.format(
  458. session.python,
  459. coverage
  460. )
  461. )
  462. @nox.session(python=_PYTHON_VERSIONS, name='pytest-pycryptodomex')
  463. @nox.parametrize('coverage', [False, True])
  464. def pytest_pycryptodomex(session, coverage):
  465. '''
  466. pytest session with zeromq transport and pycryptodomex
  467. '''
  468. session.notify(
  469. 'pytest-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'zeromq\')'.format(
  470. session.python,
  471. coverage
  472. )
  473. )
  474. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tcp-pycryptodomex')
  475. @nox.parametrize('coverage', [False, True])
  476. def pytest_tcp_pycryptodomex(session, coverage):
  477. '''
  478. pytest session with TCP transport and pycryptodomex
  479. '''
  480. session.notify(
  481. 'pytest-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'tcp\')'.format(
  482. session.python,
  483. coverage
  484. )
  485. )
  486. @nox.session(python=_PYTHON_VERSIONS, name='pytest-zeromq-pycryptodomex')
  487. @nox.parametrize('coverage', [False, True])
  488. def pytest_zeromq_pycryptodomex(session, coverage):
  489. '''
  490. pytest session with zeromq transport and pycryptodomex
  491. '''
  492. session.notify(
  493. 'pytest-parametrized-{}(coverage={}, crypto=\'pycryptodomex\', transport=\'zeromq\')'.format(
  494. session.python,
  495. coverage
  496. )
  497. )
  498. @nox.session(python=_PYTHON_VERSIONS, name='pytest-cloud')
  499. @nox.parametrize('coverage', [False, True])
  500. def pytest_cloud(session, coverage):
  501. # Install requirements
  502. _install_requirements(session, 'zeromq')
  503. pydir = _get_pydir(session)
  504. cloud_requirements = os.path.join('requirements', 'static', pydir, 'cloud.txt')
  505. session.install('--progress-bar=off', '-r', cloud_requirements, silent=PIP_INSTALL_SILENT)
  506. cmd_args = [
  507. '--rootdir', REPO_ROOT,
  508. '--log-file={}'.format(RUNTESTS_LOGFILE),
  509. '--log-file-level=debug',
  510. '--no-print-logs',
  511. '-ra',
  512. '-s',
  513. os.path.join('tests', 'integration', 'cloud', 'providers')
  514. ] + session.posargs
  515. _pytest(session, coverage, cmd_args)
  516. @nox.session(python=_PYTHON_VERSIONS, name='pytest-tornado')
  517. @nox.parametrize('coverage', [False, True])
  518. def pytest_tornado(session, coverage):
  519. # Install requirements
  520. _install_requirements(session, 'zeromq')
  521. session.install('--progress-bar=off', 'tornado==5.0.2', silent=PIP_INSTALL_SILENT)
  522. session.install('--progress-bar=off', 'pyzmq==17.0.0', silent=PIP_INSTALL_SILENT)
  523. cmd_args = [
  524. '--rootdir', REPO_ROOT,
  525. '--log-file={}'.format(RUNTESTS_LOGFILE),
  526. '--log-file-level=debug',
  527. '--no-print-logs',
  528. '-ra',
  529. '-s',
  530. ] + session.posargs
  531. _pytest(session, coverage, cmd_args)
  532. def _pytest(session, coverage, cmd_args):
  533. # Create required artifacts directories
  534. _create_ci_directories()
  535. env = None
  536. if IS_DARWIN:
  537. # Don't nuke our multiprocessing efforts objc!
  538. # https://stackoverflow.com/questions/50168647/multiprocessing-causes-python-to-crash-and-gives-an-error-may-have-been-in-progr
  539. env = {'OBJC_DISABLE_INITIALIZE_FORK_SAFETY': 'YES'}
  540. try:
  541. if coverage is True:
  542. _run_with_coverage(session, 'coverage', 'run', '-m', 'py.test', *cmd_args)
  543. else:
  544. session.run('py.test', *cmd_args, env=env)
  545. except CommandFailed:
  546. # Not rerunning failed tests for now
  547. raise
  548. # pylint: disable=unreachable
  549. # Re-run failed tests
  550. session.log('Re-running failed tests')
  551. for idx, parg in enumerate(cmd_args):
  552. if parg.startswith('--junitxml='):
  553. cmd_args[idx] = parg.replace('.xml', '-rerun-failed.xml')
  554. cmd_args.append('--lf')
  555. if coverage is True:
  556. _run_with_coverage(session, 'coverage', 'run', '-m', 'py.test', *cmd_args)
  557. else:
  558. session.run('py.test', *cmd_args, env=env)
  559. # pylint: enable=unreachable
  560. def _lint(session, rcfile, flags, paths):
  561. _install_requirements(session, 'zeromq')
  562. requirements_file = 'requirements/static/lint.in'
  563. distro_constraints = [
  564. 'requirements/static/{}/lint.txt'.format(_get_pydir(session))
  565. ]
  566. install_command = [
  567. '--progress-bar=off', '-r', requirements_file
  568. ]
  569. for distro_constraint in distro_constraints:
  570. install_command.extend([
  571. '--constraint', distro_constraint
  572. ])
  573. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  574. session.run('pylint', '--version')
  575. pylint_report_path = os.environ.get('PYLINT_REPORT')
  576. cmd_args = [
  577. 'pylint',
  578. '--rcfile={}'.format(rcfile)
  579. ] + list(flags) + list(paths)
  580. stdout = tempfile.TemporaryFile(mode='w+b')
  581. lint_failed = False
  582. try:
  583. session.run(*cmd_args, stdout=stdout)
  584. except CommandFailed:
  585. lint_failed = True
  586. raise
  587. finally:
  588. stdout.seek(0)
  589. contents = stdout.read()
  590. if contents:
  591. if IS_PY3:
  592. contents = contents.decode('utf-8')
  593. else:
  594. contents = contents.encode('utf-8')
  595. sys.stdout.write(contents)
  596. sys.stdout.flush()
  597. if pylint_report_path:
  598. # Write report
  599. with open(pylint_report_path, 'w') as wfh:
  600. wfh.write(contents)
  601. session.log('Report file written to %r', pylint_report_path)
  602. stdout.close()
  603. @nox.session(python='2.7')
  604. def lint(session):
  605. '''
  606. Run PyLint against Salt and it's test suite. Set PYLINT_REPORT to a path to capture output.
  607. '''
  608. session.notify('lint-salt-{}'.format(session.python))
  609. session.notify('lint-tests-{}'.format(session.python))
  610. @nox.session(python='2.7', name='lint-salt')
  611. def lint_salt(session):
  612. '''
  613. Run PyLint against Salt. Set PYLINT_REPORT to a path to capture output.
  614. '''
  615. flags = [
  616. '--disable=I,W1307,C0411,C0413,W8410,str-format-in-logging'
  617. ]
  618. if session.posargs:
  619. paths = session.posargs
  620. else:
  621. paths = ['setup.py', 'noxfile.py', 'salt/']
  622. _lint(session, '.testing.pylintrc', flags, paths)
  623. @nox.session(python='2.7', name='lint-tests')
  624. def lint_tests(session):
  625. '''
  626. Run PyLint against Salt and it's test suite. Set PYLINT_REPORT to a path to capture output.
  627. '''
  628. flags = [
  629. '--disable=I,W0232,E1002,W1307,C0411,C0413,W8410,str-format-in-logging'
  630. ]
  631. if session.posargs:
  632. paths = session.posargs
  633. else:
  634. paths = ['tests/']
  635. _lint(session, '.testing.pylintrc', flags, paths)
  636. @nox.session(python='3')
  637. @nox.parametrize('update', [False, True])
  638. @nox.parametrize('compress', [False, True])
  639. def docs(session, compress, update):
  640. '''
  641. Build Salt's Documentation
  642. '''
  643. session.notify('docs-html(compress={})'.format(compress))
  644. session.notify('docs-man(compress={}, update={})'.format(compress, update))
  645. @nox.session(name='docs-html', python='3')
  646. @nox.parametrize('compress', [False, True])
  647. def docs_html(session, compress):
  648. '''
  649. Build Salt's HTML Documentation
  650. '''
  651. pydir = _get_pydir(session)
  652. if pydir == 'py3.4':
  653. session.error('Sphinx only runs on Python >= 3.5')
  654. requirements_file = 'requirements/static/docs.in'
  655. distro_constraints = [
  656. 'requirements/static/{}/docs.txt'.format(_get_pydir(session))
  657. ]
  658. install_command = [
  659. '--progress-bar=off', '-r', requirements_file
  660. ]
  661. for distro_constraint in distro_constraints:
  662. install_command.extend([
  663. '--constraint', distro_constraint
  664. ])
  665. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  666. os.chdir('doc/')
  667. session.run('make', 'clean', external=True)
  668. session.run('make', 'html', 'SPHINXOPTS=-W', external=True)
  669. if compress:
  670. session.run('tar', '-cJvf', 'html-archive.tar.xz', '_build/html', external=True)
  671. os.chdir('..')
  672. @nox.session(name='docs-man', python='3')
  673. @nox.parametrize('update', [False, True])
  674. @nox.parametrize('compress', [False, True])
  675. def docs_man(session, compress, update):
  676. '''
  677. Build Salt's Manpages Documentation
  678. '''
  679. pydir = _get_pydir(session)
  680. if pydir == 'py3.4':
  681. session.error('Sphinx only runs on Python >= 3.5')
  682. requirements_file = 'requirements/static/docs.in'
  683. distro_constraints = [
  684. 'requirements/static/{}/docs.txt'.format(_get_pydir(session))
  685. ]
  686. install_command = [
  687. '--progress-bar=off', '-r', requirements_file
  688. ]
  689. for distro_constraint in distro_constraints:
  690. install_command.extend([
  691. '--constraint', distro_constraint
  692. ])
  693. session.install(*install_command, silent=PIP_INSTALL_SILENT)
  694. os.chdir('doc/')
  695. session.run('make', 'clean', external=True)
  696. session.run('make', 'man', 'SPHINXOPTS=-W', external=True)
  697. if update:
  698. session.run('rm', '-rf', 'man/', external=True)
  699. session.run('cp', '-Rp', '_build/man', 'man/', external=True)
  700. if compress:
  701. session.run('tar', '-cJvf', 'man-archive.tar.xz', '_build/man', external=True)
  702. os.chdir('..')