|
| 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 shutil |
| 7 | +import unittest |
| 8 | + |
| 9 | +from color_runner import BufferingMixin, ColorTextRunner |
| 10 | + |
| 11 | +from tools.colored_logger import CYAN, GREEN, RED, with_color |
| 12 | + |
| 13 | + |
| 14 | +def clearline(stream): |
| 15 | + stream.write('\r\033[K') |
| 16 | + stream.flush() |
| 17 | + |
| 18 | + |
| 19 | +def term_width(): |
| 20 | + return shutil.get_terminal_size()[0] |
| 21 | + |
| 22 | + |
| 23 | +class SingleLineTestResult(BufferingMixin, unittest.TextTestResult): |
| 24 | + """Similar to the standard TextTestResult but uses ANSI escape codes |
| 25 | + for color output and reusing a single line on the terminal. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, *args, **kwargs): |
| 29 | + super().__init__(*args, **kwargs) |
| 30 | + self.progress_counter = 0 |
| 31 | + |
| 32 | + def writeStatusLine(self, line): |
| 33 | + clearline(self._original_stderr) |
| 34 | + self._original_stderr.write(line) |
| 35 | + self._original_stderr.flush() |
| 36 | + |
| 37 | + def updateStatus(self, test, msg, color): |
| 38 | + progress = f'[{self.progress_counter}/{self.test_count}] ' |
| 39 | + # Format the line so that it fix within the terminal width, unless it's less then min_len |
| 40 | + # in which case there is not much we can do, and we just overflow the line. |
| 41 | + min_len = len(progress) + len(msg) + 5 |
| 42 | + test_name = str(test) |
| 43 | + if term_width() > min_len: |
| 44 | + max_name = term_width() - min_len |
| 45 | + test_name = test_name[:max_name] |
| 46 | + line = f'{with_color(CYAN, progress)}{test_name} ... {with_color(color, msg)}' |
| 47 | + self.writeStatusLine(line) |
| 48 | + |
| 49 | + def startTest(self, test): |
| 50 | + self.progress_counter += 1 |
| 51 | + assert self.test_count > 0 |
| 52 | + # Note: We explicitly do not use `super()` here but instead call `unittest.TestResult`. i.e. |
| 53 | + # we skip the superclass (since we don't want its specific behaviour) and instead call its |
| 54 | + # superclass. |
| 55 | + unittest.TestResult.startTest(self, test) |
| 56 | + if self.progress_counter == 1: |
| 57 | + self.updateStatus(test, '', GREEN) |
| 58 | + |
| 59 | + def addSuccess(self, test): |
| 60 | + unittest.TestResult.addSuccess(self, test) |
| 61 | + self.updateStatus(test, 'ok', GREEN) |
| 62 | + |
| 63 | + def addFailure(self, test, err): |
| 64 | + unittest.TestResult.addFailure(self, test, err) |
| 65 | + self.updateStatus(test, 'FAIL', RED) |
| 66 | + |
| 67 | + def addError(self, test, err): |
| 68 | + unittest.TestResult.addError(self, test, err) |
| 69 | + self.updateStatus(test, 'ERROR', RED) |
| 70 | + |
| 71 | + def addExpectedFailure(self, test, err): |
| 72 | + unittest.TestResult.addExpectedFailure(self, test, err) |
| 73 | + self.updateStatus(test, 'expected failure', RED) |
| 74 | + |
| 75 | + def addUnexpectedSuccess(self, test, err): |
| 76 | + unittest.TestResult.addUnexpectedSuccess(self, test, err) |
| 77 | + self.updateStatus(test, 'UNEXPECTED SUCCESS', RED) |
| 78 | + |
| 79 | + def addSkip(self, test, reason): |
| 80 | + unittest.TestResult.addSkip(self, test, reason) |
| 81 | + self.updateStatus(test, f"skipped '{reason}'", CYAN) |
| 82 | + |
| 83 | + def printErrors(self): |
| 84 | + # All tests have been run at this point so print a final newline |
| 85 | + # to end out status line |
| 86 | + self._original_stderr.write('\n') |
| 87 | + super().printErrors() |
| 88 | + |
| 89 | + |
| 90 | +class SingleLineTestRunner(ColorTextRunner): |
| 91 | + """Subclass of TextTestResult that uses SingleLineTestResult""" |
| 92 | + resultclass = SingleLineTestResult # type: ignore |
| 93 | + |
| 94 | + def __init__(self, *args, **kwargs): |
| 95 | + super().__init__(*args, buffer=True, **kwargs) |
0 commit comments