Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/psutil/tests/runner.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
| author | shellac |
|---|---|
| date | Thu, 14 May 2020 14:56:58 -0400 |
| parents | 26e78fe6e8c4 |
| children |
comparison
equal
deleted
inserted
replaced
| 1:75ca89e9b81c | 2:6af9afd405e9 |
|---|---|
| 1 #!/usr/bin/env python3 | |
| 2 | |
| 3 # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 """ | |
| 8 Unit test runner, providing new features on top of unittest module: | |
| 9 - colourized output (error, skip) | |
| 10 - print failures/tracebacks on CTRL+C | |
| 11 - re-run failed tests only (make test-failed) | |
| 12 """ | |
| 13 | |
| 14 from __future__ import print_function | |
| 15 import optparse | |
| 16 import os | |
| 17 import sys | |
| 18 import unittest | |
| 19 from unittest import TestResult | |
| 20 from unittest import TextTestResult | |
| 21 from unittest import TextTestRunner | |
| 22 try: | |
| 23 import ctypes | |
| 24 except ImportError: | |
| 25 ctypes = None | |
| 26 | |
| 27 import psutil | |
| 28 from psutil._common import hilite | |
| 29 from psutil._common import print_color | |
| 30 from psutil._common import term_supports_colors | |
| 31 from psutil.tests import safe_rmpath | |
| 32 from psutil.tests import TOX | |
| 33 | |
| 34 | |
| 35 HERE = os.path.abspath(os.path.dirname(__file__)) | |
| 36 VERBOSITY = 1 if TOX else 2 | |
| 37 FAILED_TESTS_FNAME = '.failed-tests.txt' | |
| 38 | |
| 39 | |
| 40 # ===================================================================== | |
| 41 # --- unittest subclasses | |
| 42 # ===================================================================== | |
| 43 | |
| 44 | |
| 45 class ColouredResult(TextTestResult): | |
| 46 | |
| 47 def _print_color(self, s, color, bold=False): | |
| 48 file = sys.stderr if color == "red" else sys.stdout | |
| 49 print_color(s, color, bold=bold, file=file) | |
| 50 | |
| 51 def addSuccess(self, test): | |
| 52 TestResult.addSuccess(self, test) | |
| 53 self._print_color("OK", "green") | |
| 54 | |
| 55 def addError(self, test, err): | |
| 56 TestResult.addError(self, test, err) | |
| 57 self._print_color("ERROR", "red", bold=True) | |
| 58 | |
| 59 def addFailure(self, test, err): | |
| 60 TestResult.addFailure(self, test, err) | |
| 61 self._print_color("FAIL", "red") | |
| 62 | |
| 63 def addSkip(self, test, reason): | |
| 64 TestResult.addSkip(self, test, reason) | |
| 65 self._print_color("skipped: %s" % reason, "brown") | |
| 66 | |
| 67 def printErrorList(self, flavour, errors): | |
| 68 flavour = hilite(flavour, "red", bold=flavour == 'ERROR') | |
| 69 TextTestResult.printErrorList(self, flavour, errors) | |
| 70 | |
| 71 | |
| 72 class ColouredRunner(TextTestRunner): | |
| 73 resultclass = ColouredResult if term_supports_colors() else TextTestResult | |
| 74 | |
| 75 def _makeResult(self): | |
| 76 # Store result instance so that it can be accessed on | |
| 77 # KeyboardInterrupt. | |
| 78 self.result = TextTestRunner._makeResult(self) | |
| 79 return self.result | |
| 80 | |
| 81 | |
| 82 # ===================================================================== | |
| 83 # --- public API | |
| 84 # ===================================================================== | |
| 85 | |
| 86 | |
| 87 def setup_tests(): | |
| 88 if 'PSUTIL_TESTING' not in os.environ: | |
| 89 # This won't work on Windows but set_testing() below will do it. | |
| 90 os.environ['PSUTIL_TESTING'] = '1' | |
| 91 psutil._psplatform.cext.set_testing() | |
| 92 | |
| 93 | |
| 94 def get_suite(name=None): | |
| 95 suite = unittest.TestSuite() | |
| 96 if name is None: | |
| 97 testmods = [os.path.splitext(x)[0] for x in os.listdir(HERE) | |
| 98 if x.endswith('.py') and x.startswith('test_') and not | |
| 99 x.startswith('test_memory_leaks')] | |
| 100 if "WHEELHOUSE_UPLOADER_USERNAME" in os.environ: | |
| 101 testmods = [x for x in testmods if not x.endswith(( | |
| 102 "osx", "posix", "linux"))] | |
| 103 for tm in testmods: | |
| 104 # ...so that the full test paths are printed on screen | |
| 105 tm = "psutil.tests.%s" % tm | |
| 106 suite.addTest(unittest.defaultTestLoader.loadTestsFromName(tm)) | |
| 107 else: | |
| 108 name = os.path.splitext(os.path.basename(name))[0] | |
| 109 suite.addTest(unittest.defaultTestLoader.loadTestsFromName(name)) | |
| 110 return suite | |
| 111 | |
| 112 | |
| 113 def get_suite_from_failed(): | |
| 114 # ...from previously failed test run | |
| 115 suite = unittest.TestSuite() | |
| 116 if not os.path.isfile(FAILED_TESTS_FNAME): | |
| 117 return suite | |
| 118 with open(FAILED_TESTS_FNAME, 'rt') as f: | |
| 119 names = f.read().split() | |
| 120 for n in names: | |
| 121 suite.addTest(unittest.defaultTestLoader.loadTestsFromName(n)) | |
| 122 return suite | |
| 123 | |
| 124 | |
| 125 def save_failed_tests(result): | |
| 126 if result.wasSuccessful(): | |
| 127 return safe_rmpath(FAILED_TESTS_FNAME) | |
| 128 with open(FAILED_TESTS_FNAME, 'wt') as f: | |
| 129 for t in result.errors + result.failures: | |
| 130 tname = str(t[0]) | |
| 131 unittest.defaultTestLoader.loadTestsFromName(tname) | |
| 132 f.write(tname + '\n') | |
| 133 | |
| 134 | |
| 135 def run(name=None, last_failed=False): | |
| 136 setup_tests() | |
| 137 runner = ColouredRunner(verbosity=VERBOSITY) | |
| 138 suite = get_suite_from_failed() if last_failed else get_suite(name) | |
| 139 try: | |
| 140 result = runner.run(suite) | |
| 141 except (KeyboardInterrupt, SystemExit) as err: | |
| 142 print("received %s" % err.__class__.__name__, file=sys.stderr) | |
| 143 runner.result.printErrors() | |
| 144 sys.exit(1) | |
| 145 else: | |
| 146 save_failed_tests(result) | |
| 147 success = result.wasSuccessful() | |
| 148 sys.exit(0 if success else 1) | |
| 149 | |
| 150 | |
| 151 def main(): | |
| 152 usage = "python3 -m psutil.tests [opts]" | |
| 153 parser = optparse.OptionParser(usage=usage, description="run unit tests") | |
| 154 parser.add_option("--last-failed", | |
| 155 action="store_true", default=False, | |
| 156 help="only run last failed tests") | |
| 157 opts, args = parser.parse_args() | |
| 158 run(last_failed=opts.last_failed) | |
| 159 | |
| 160 | |
| 161 if __name__ == '__main__': | |
| 162 main() |
