|
| 1 | +#!/usr/bin/env python |
| 2 | +import coverage |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +PYTEST_ARGS = ['tests', '--tb=short'] |
| 10 | +FLAKE8_ARGS = ['openapi_codec', 'tests', '--ignore=E501'] |
| 11 | +COVERAGE_OPTIONS = { |
| 12 | + 'include': ['openapi_codec/*', 'tests/*'] |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +sys.path.append(os.path.dirname(__file__)) |
| 17 | + |
| 18 | + |
| 19 | +class NullFile(object): |
| 20 | + def write(self, data): |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +def exit_on_failure(ret, message=None): |
| 25 | + if ret: |
| 26 | + sys.exit(ret) |
| 27 | + |
| 28 | + |
| 29 | +def flake8_main(args): |
| 30 | + print('Running flake8 code linting') |
| 31 | + ret = subprocess.call(['flake8'] + args) |
| 32 | + print('flake8 failed' if ret else 'flake8 passed') |
| 33 | + return ret |
| 34 | + |
| 35 | + |
| 36 | +def report_coverage(cov, fail_if_not_100=False): |
| 37 | + precent_covered = cov.report( |
| 38 | + file=NullFile(), **COVERAGE_OPTIONS |
| 39 | + ) |
| 40 | + if precent_covered == 100: |
| 41 | + print('100% coverage') |
| 42 | + return |
| 43 | + if fail_if_not_100: |
| 44 | + print('Tests passed, but not 100% coverage.') |
| 45 | + cov.report(**COVERAGE_OPTIONS) |
| 46 | + cov.html_report(**COVERAGE_OPTIONS) |
| 47 | + if fail_if_not_100: |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + |
| 51 | +def split_class_and_function(string): |
| 52 | + class_string, function_string = string.split('.', 1) |
| 53 | + return "%s and %s" % (class_string, function_string) |
| 54 | + |
| 55 | + |
| 56 | +def is_function(string): |
| 57 | + # `True` if it looks like a test function is included in the string. |
| 58 | + return string.startswith('test_') or '.test_' in string |
| 59 | + |
| 60 | + |
| 61 | +def is_class(string): |
| 62 | + # `True` if first character is uppercase - assume it's a class name. |
| 63 | + return string[0] == string[0].upper() |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + if len(sys.argv) > 1: |
| 68 | + pytest_args = sys.argv[1:] |
| 69 | + first_arg = pytest_args[0] |
| 70 | + if first_arg.startswith('-'): |
| 71 | + # `runtests.py [flags]` |
| 72 | + pytest_args = PYTEST_ARGS + pytest_args |
| 73 | + elif is_class(first_arg) and is_function(first_arg): |
| 74 | + # `runtests.py TestCase.test_function [flags]` |
| 75 | + expression = split_class_and_function(first_arg) |
| 76 | + pytest_args = PYTEST_ARGS + ['-k', expression] + pytest_args[1:] |
| 77 | + elif is_class(first_arg) or is_function(first_arg): |
| 78 | + # `runtests.py TestCase [flags]` |
| 79 | + # `runtests.py test_function [flags]` |
| 80 | + pytest_args = PYTEST_ARGS + ['-k', pytest_args[0]] + pytest_args[1:] |
| 81 | + else: |
| 82 | + pytest_args = PYTEST_ARGS |
| 83 | + |
| 84 | + cov = coverage.coverage() |
| 85 | + cov.start() |
| 86 | + exit_on_failure(pytest.main(pytest_args)) |
| 87 | + cov.stop() |
| 88 | + exit_on_failure(flake8_main(FLAKE8_ARGS)) |
| 89 | + report_coverage(cov) |
0 commit comments