aboutsummaryrefslogtreecommitdiffstats
path: root/runtests.py
diff options
context:
space:
mode:
authorJames Rutherford2015-03-11 10:38:03 +0000
committerJames Rutherford2015-03-11 10:38:03 +0000
commit4a2d27975ab5249269aebafd803be87a2107092b (patch)
tree55b524c93b02eef404304f734be98871bbb1324f /runtests.py
parent856dc855c952746f566a6a8de263afe951362dfb (diff)
parentdc56e5a0f41fdd6350e91a5749023d086bd1640f (diff)
downloaddjango-rest-framework-4a2d27975ab5249269aebafd803be87a2107092b.tar.bz2
Merge pull request #1 from tomchristie/master
Merge in from upstream
Diffstat (limited to 'runtests.py')
-rwxr-xr-xruntests.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/runtests.py b/runtests.py
new file mode 100755
index 00000000..0008bfae
--- /dev/null
+++ b/runtests.py
@@ -0,0 +1,91 @@
+#! /usr/bin/env python
+from __future__ import print_function
+
+import pytest
+import sys
+import os
+import subprocess
+
+
+PYTEST_ARGS = {
+ 'default': ['tests', '--tb=short'],
+ 'fast': ['tests', '--tb=short', '-q'],
+}
+
+FLAKE8_ARGS = ['rest_framework', 'tests', '--ignore=E501']
+
+
+sys.path.append(os.path.dirname(__file__))
+
+
+def exit_on_failure(ret, message=None):
+ if ret:
+ sys.exit(ret)
+
+
+def flake8_main(args):
+ print('Running flake8 code linting')
+ ret = subprocess.call(['flake8'] + args)
+ print('flake8 failed' if ret else 'flake8 passed')
+ return ret
+
+
+def split_class_and_function(string):
+ class_string, function_string = string.split('.', 1)
+ return "%s and %s" % (class_string, function_string)
+
+
+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
+
+
+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
+ else:
+ run_flake8 = False
+
+ 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
+
+ if len(sys.argv) > 1:
+ pytest_args = sys.argv[1:]
+ first_arg = pytest_args[0]
+ 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):
+ # `runtests.py TestCase [flags]`
+ # `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))
+ if run_flake8:
+ exit_on_failure(flake8_main(FLAKE8_ARGS))