2019-04-30 18:53:44 +03:00
|
|
|
#! /usr/bin/env python3
|
2014-08-19 16:28:07 +04:00
|
|
|
import subprocess
|
2015-06-25 23:58:24 +03:00
|
|
|
import sys
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2015-06-25 23:58:24 +03:00
|
|
|
import pytest
|
2014-08-19 16:28:07 +04:00
|
|
|
|
|
|
|
PYTEST_ARGS = {
|
2018-06-21 16:28:25 +03:00
|
|
|
'default': [],
|
|
|
|
'fast': ['-q'],
|
2014-08-19 16:28:07 +04:00
|
|
|
}
|
|
|
|
|
2017-05-16 13:18:33 +03:00
|
|
|
FLAKE8_ARGS = ['rest_framework', 'tests']
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2018-02-14 23:12:14 +03:00
|
|
|
ISORT_ARGS = ['--recursive', '--check-only', '--diff', '-o' 'uritemplate', '-p', 'tests', 'rest_framework', 'tests']
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2014-12-05 02:29:28 +03:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
def exit_on_failure(ret, message=None):
|
|
|
|
if ret:
|
|
|
|
sys.exit(ret)
|
|
|
|
|
2014-12-05 02:29:28 +03:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
def flake8_main(args):
|
|
|
|
print('Running flake8 code linting')
|
|
|
|
ret = subprocess.call(['flake8'] + args)
|
|
|
|
print('flake8 failed' if ret else 'flake8 passed')
|
|
|
|
return ret
|
|
|
|
|
2014-12-05 02:29:28 +03:00
|
|
|
|
2015-06-25 23:58:24 +03:00
|
|
|
def isort_main(args):
|
|
|
|
print('Running isort code checking')
|
|
|
|
ret = subprocess.call(['isort'] + args)
|
2015-06-27 23:06:53 +03:00
|
|
|
|
|
|
|
if ret:
|
2015-06-29 11:39:27 +03:00
|
|
|
print('isort failed: Some modules have incorrectly ordered imports. Fix by running `isort --recursive .`')
|
2015-06-27 23:06:53 +03:00
|
|
|
else:
|
|
|
|
print('isort passed')
|
|
|
|
|
2015-06-25 23:58:24 +03:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
def split_class_and_function(string):
|
|
|
|
class_string, function_string = string.split('.', 1)
|
|
|
|
return "%s and %s" % (class_string, function_string)
|
|
|
|
|
2014-12-05 02:29:28 +03:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
def is_function(string):
|
|
|
|
# `True` if it looks like a test function is included in the string.
|
|
|
|
return string.startswith('test_') or '.test_' in string
|
|
|
|
|
2014-12-05 02:29:28 +03:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
def is_class(string):
|
|
|
|
# `True` if first character is uppercase - assume it's a class name.
|
|
|
|
return string[0] == string[0].upper()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
|
|
|
sys.argv.remove('--nolint')
|
|
|
|
except ValueError:
|
|
|
|
run_flake8 = True
|
2015-06-25 23:58:24 +03:00
|
|
|
run_isort = True
|
2014-08-19 16:28:07 +04:00
|
|
|
else:
|
|
|
|
run_flake8 = False
|
2015-06-25 23:58:24 +03:00
|
|
|
run_isort = False
|
2014-08-19 16:28:07 +04:00
|
|
|
|
|
|
|
try:
|
|
|
|
sys.argv.remove('--lintonly')
|
|
|
|
except ValueError:
|
|
|
|
run_tests = True
|
|
|
|
else:
|
|
|
|
run_tests = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
sys.argv.remove('--fast')
|
|
|
|
except ValueError:
|
|
|
|
style = 'default'
|
|
|
|
else:
|
|
|
|
style = 'fast'
|
|
|
|
run_flake8 = False
|
2015-06-25 23:58:24 +03:00
|
|
|
run_isort = False
|
2014-08-19 16:28:07 +04:00
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
pytest_args = sys.argv[1:]
|
|
|
|
first_arg = pytest_args[0]
|
2015-07-16 17:17:18 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
pytest_args.remove('--coverage')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
2015-10-08 08:20:46 +03:00
|
|
|
pytest_args = [
|
2018-05-08 16:02:45 +03:00
|
|
|
'--cov', '.',
|
|
|
|
'--cov-report', 'xml',
|
|
|
|
] + pytest_args
|
2015-07-16 17:17:18 +03:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
if first_arg.startswith('-'):
|
|
|
|
# `runtests.py [flags]`
|
|
|
|
pytest_args = ['tests'] + pytest_args
|
|
|
|
elif is_class(first_arg) and is_function(first_arg):
|
|
|
|
# `runtests.py TestCase.test_function [flags]`
|
|
|
|
expression = split_class_and_function(first_arg)
|
|
|
|
pytest_args = ['tests', '-k', expression] + pytest_args[1:]
|
|
|
|
elif is_class(first_arg) or is_function(first_arg):
|
2015-06-25 23:58:24 +03:00
|
|
|
# `runtests.py TestCase [flags]`
|
2014-08-19 16:28:07 +04:00
|
|
|
# `runtests.py test_function [flags]`
|
|
|
|
pytest_args = ['tests', '-k', pytest_args[0]] + pytest_args[1:]
|
|
|
|
else:
|
|
|
|
pytest_args = PYTEST_ARGS[style]
|
|
|
|
|
|
|
|
if run_tests:
|
|
|
|
exit_on_failure(pytest.main(pytest_args))
|
2015-06-25 23:58:24 +03:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
if run_flake8:
|
|
|
|
exit_on_failure(flake8_main(FLAKE8_ARGS))
|
2015-06-25 23:58:24 +03:00
|
|
|
|
|
|
|
if run_isort:
|
|
|
|
exit_on_failure(isort_main(ISORT_ARGS))
|