|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from platform import python_implementation |
| 6 | +import inspect |
| 7 | + |
| 8 | +import nose |
| 9 | +from nose.config import Config |
| 10 | +from nose.plugins import Plugin, DefaultPluginManager |
| 11 | +from nose.plugins.cover import Coverage |
| 12 | + |
| 13 | +import tests |
| 14 | + |
| 15 | + |
| 16 | +class CustomCoverage(Coverage): |
| 17 | + enabled = True |
| 18 | + name = 'coverage' |
| 19 | + score = 201 # One higher than original package |
| 20 | + |
| 21 | + def report(self, stream): |
| 22 | + fold('coverage', 'Coverage report', stream=stream) |
| 23 | + super(CustomCoverage, self).report(stream) |
| 24 | + fold('coverage', stream=stream) |
| 25 | + |
| 26 | + |
| 27 | +class FoldPlugin(Plugin): |
| 28 | + enabled = True |
| 29 | + name = 'travis-fold' |
| 30 | + score = 100 |
| 31 | + |
| 32 | + def setOutputStream(self, stream): |
| 33 | + self.stream = stream |
| 34 | + |
| 35 | + def startContext(self, context): |
| 36 | + if inspect.ismodule(context) and context != tests: |
| 37 | + fold(context.__name__, context.__name__, stream=self.stream) |
| 38 | + |
| 39 | + def stopContext(self, context): |
| 40 | + if inspect.ismodule(context) and context != tests: |
| 41 | + fold(context.__name__, stream=self.stream) |
| 42 | + |
| 43 | +folds = set() |
| 44 | + |
| 45 | + |
| 46 | +def fold(foldname, comment=None, stream=sys.stdout): |
| 47 | + if foldname in folds: |
| 48 | + folds.remove(foldname) |
| 49 | + print('\ntravis_fold:end:{}'.format(foldname), file=stream, end='') |
| 50 | + else: |
| 51 | + folds.add(foldname) |
| 52 | + print('travis_fold:start:{}'.format(foldname), file=stream, end='') |
| 53 | + |
| 54 | + if comment: |
| 55 | + print('\n{}'.format(comment), file=stream) |
| 56 | + else: |
| 57 | + print('', file=stream) |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + print('Starting...') |
| 62 | + fold('tests', 'Running tests...') |
| 63 | + config = Config(verbosity=2, plugins=DefaultPluginManager(), env={'NOSE_REDNOSE': '1'}) |
| 64 | + tests = nose.run(argv=['--with-flaky', '--no-flaky-report', |
| 65 | + '--with-coverage', '--cover-package=telegram/', |
| 66 | + '--with-travis-fold', |
| 67 | + 'tests'], |
| 68 | + addplugins=[FoldPlugin(), CustomCoverage()], |
| 69 | + config=config) |
| 70 | + print('\n' * 2) |
| 71 | + if tests: |
| 72 | + fold('tests') |
| 73 | + |
| 74 | + # Only run pre-commit hooks once |
| 75 | + pre_commit = True |
| 76 | + if sys.version_info[:2] == (3, 6) and python_implementation() == 'CPython': |
| 77 | + fold('pre-commit', 'Running pre-commits') |
| 78 | + # TODO: Only run pre-commit hooks on changed files |
| 79 | + # Using something like git diff-tree and $TRAVIS_COMMIT_RANGE |
| 80 | + pre_commit = subprocess.call(['pre-commit', 'run', '--all-files']) == 0 |
| 81 | + if pre_commit: |
| 82 | + fold('pre-commit') |
| 83 | + |
| 84 | + fold('bdist_dumb', 'Testing build...') |
| 85 | + # run_setup('setup.py', ['bdist_dumb']) # Makes us unable to fetch exit code |
| 86 | + bdist_dumb = subprocess.call(['python', 'setup.py', 'bdist_dumb']) == 0 |
| 87 | + if bdist_dumb: |
| 88 | + fold('bdist_dumb') |
| 89 | + |
| 90 | + sys.exit(0 if all((tests, pre_commit, bdist_dumb)) else 1) |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + main() |
0 commit comments