|
| 1 | +# Copyright 2025 The Emscripten Authors. All rights reserved. |
| 2 | +# Emscripten is available under two separate licenses, the MIT license and the |
| 3 | +# University of Illinois/NCSA Open Source License. Both these licenses can be |
| 4 | +# found in the LICENSE file. |
| 5 | + |
| 6 | +import logging |
| 7 | +import shutil |
| 8 | +import unittest |
| 9 | + |
| 10 | +from tools.colored_logger import CYAN, GREEN, RED, with_color |
| 11 | + |
| 12 | + |
| 13 | +def clearline(stream): |
| 14 | + stream.write('\r\033[K') |
| 15 | + stream.flush() |
| 16 | + |
| 17 | + |
| 18 | +def term_width(): |
| 19 | + return shutil.get_terminal_size()[0] |
| 20 | + |
| 21 | + |
| 22 | +class SingleLineTestResult(unittest.TextTestResult): |
| 23 | + """Similar to the standard TextTestResult but uses ANSI escape codes |
| 24 | + for color output and reusing a single line on the terminal. |
| 25 | +
|
| 26 | + This class also takes care of redirectting `logging` output when |
| 27 | + in `buffer=True` mode. |
| 28 | + """ |
| 29 | + test_count = 0 |
| 30 | + |
| 31 | + def __init__(self, *args, **kwargs): |
| 32 | + super().__init__(*args, **kwargs) |
| 33 | + self.progress_counter = 0 |
| 34 | + |
| 35 | + def writeStatusLine(self, line): |
| 36 | + clearline(self._original_stderr) |
| 37 | + self._original_stderr.write(line) |
| 38 | + self._original_stderr.flush() |
| 39 | + |
| 40 | + def updateStatus(self, test, msg, color): |
| 41 | + self.progress_counter += 1 |
| 42 | + progress = f'[{self.progress_counter}/{self.test_count}] ' |
| 43 | + # Format the line so that it fix within the terminal width, unless its less then min_len |
| 44 | + # in which case there is not much we can do, and we just overflow the line. |
| 45 | + min_len = len(progress) + len(msg) + 5 |
| 46 | + test_name = str(test) |
| 47 | + if term_width() > min_len: |
| 48 | + max_name = term_width() - min_len |
| 49 | + test_name = test_name[:max_name] |
| 50 | + line = f'{with_color(CYAN, progress)}{test_name} ... {with_color(color, msg)}' |
| 51 | + self.writeStatusLine(line) |
| 52 | + |
| 53 | + def startTest(self, test): |
| 54 | + assert self.test_count > 0 |
| 55 | + # Note: We explicitly do not use `super()` here but instead call `unittest.TestResult`. i.e. |
| 56 | + # we skip the superclass (since we don't want its specific behaviour) and instead call its |
| 57 | + # superclass. |
| 58 | + unittest.TestResult.startTest(self, test) |
| 59 | + # When we start a test in buffering mode, also update the python |
| 60 | + # loggers to redirect to the _stderr_buffer |
| 61 | + if self.buffer: |
| 62 | + for handler in logging.root.handlers: |
| 63 | + if handler.stream == self._original_stderr: |
| 64 | + handler.stream = self._stderr_buffer |
| 65 | + if self.progress_counter == 0: |
| 66 | + self.writeStatusLine('.. awaiting first test result') |
| 67 | + |
| 68 | + def stopTest(self, test): |
| 69 | + unittest.TestResult.stopTest(self, test) |
| 70 | + if self.buffer: |
| 71 | + for handler in logging.root.handlers: |
| 72 | + if handler.stream == self._stderr_buffer: |
| 73 | + handler.stream = self._original_stderr |
| 74 | + |
| 75 | + def addSuccess(self, test): |
| 76 | + unittest.TestResult.addSuccess(self, test) |
| 77 | + self.updateStatus(test, 'ok', GREEN) |
| 78 | + |
| 79 | + def addFailure(self, test, err): |
| 80 | + unittest.TestResult.addFailure(self, test, err) |
| 81 | + self.updateStatus(test, 'FAIL', RED) |
| 82 | + |
| 83 | + def addError(self, test, err): |
| 84 | + unittest.TestResult.addError(self, test, err) |
| 85 | + self.updateStatus(test, 'ERROR', RED) |
| 86 | + |
| 87 | + def addExpectedFailure(self, test, err): |
| 88 | + unittest.TestResult.addExpectedFailure(self, test, err) |
| 89 | + self.updateStatus(test, 'expected failure', RED) |
| 90 | + |
| 91 | + def addUnexpectedSuccess(self, test, err): |
| 92 | + unittest.TestResult.addUnexpectedSuccess(self, test, err) |
| 93 | + self.updateStatus(test, 'UNEXPECTED SUCCESS', RED) |
| 94 | + |
| 95 | + def addSkip(self, test, reason): |
| 96 | + unittest.TestResult.addSkip(self, test, reason) |
| 97 | + self.updateStatus(test, f"skipped '{reason}'", CYAN) |
| 98 | + |
| 99 | + |
| 100 | +class SingleLineTestRunner(unittest.TextTestRunner): |
| 101 | + """Subclass of TextTestResult that uses SingleLineTestResult""" |
| 102 | + resultclass = SingleLineTestResult |
| 103 | + |
| 104 | + def __init__(self, *args, **kwargs): |
| 105 | + super().__init__(*args, buffer=True, **kwargs) |
| 106 | + |
| 107 | + def _makeResult(self): |
| 108 | + result = super()._makeResult() |
| 109 | + result.test_count = self.test_count |
| 110 | + return result |
| 111 | + |
| 112 | + def run(self, test): |
| 113 | + self.test_count = test.countTestCases() |
| 114 | + return super().run(test) |
0 commit comments