|
2 | 2 | # under the Apache License Version 2.0, see <https://www.apache.org/licenses/>
|
3 | 3 | import pickle
|
4 | 4 | import shutil
|
| 5 | +import re |
5 | 6 | from logging import warning
|
6 | 7 | from pathlib import Path
|
7 | 8 | from shutil import rmtree
|
@@ -85,22 +86,26 @@ def finalize_fixture_store(request, fixture_store):
|
85 | 86 | global harvested_fixture_data
|
86 | 87 | harvested_fixture_data = dict(fixture_store)
|
87 | 88 |
|
| 89 | +def strip_ansi_escape_codes(text): |
| 90 | + ansi_escape = re.compile(r'\x1b\[.*?m') |
| 91 | + return ansi_escape.sub('', text) |
| 92 | + |
| 93 | + |
| 94 | +# Function to center text with ANSI colors, adjusting for escape codes |
| 95 | +def center_colored_text(text, width): |
| 96 | + visible_length = len(strip_ansi_escape_codes(text)) |
| 97 | + padding = max(0, (width - visible_length) // 2) |
| 98 | + return " " * padding + text + " " * (width - visible_length - padding) |
| 99 | + |
88 | 100 |
|
89 | 101 | def pytest_terminal_summary(terminalreporter, exitstatus, config):
|
90 | 102 | """
|
91 |
| - Custom pytest terminal summary to display mini SBIBM results with relative coloring |
92 |
| - per task. |
| 103 | + Custom pytest terminal summary to display mini SBIBM results with relative coloring per task. |
93 | 104 |
|
94 | 105 | This function is called after the test session ends and generates a summary
|
95 | 106 | of the results if the `--bm` option is specified. It displays the results
|
96 | 107 | in a formatted table with methods as rows and tasks as columns, applying
|
97 | 108 | relative coloring to metrics based on their performance within each task.
|
98 |
| -
|
99 |
| - Args: |
100 |
| - terminalreporter (TerminalReporter): The terminal reporter object for writing |
101 |
| - output. |
102 |
| - exitstatus (int): The exit status of the test session. |
103 |
| - config (Config): The pytest config object. |
104 | 109 | """
|
105 | 110 | if config.getoption("--bm"):
|
106 | 111 | terminal_width = shutil.get_terminal_size().columns
|
@@ -174,15 +179,26 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
|
174 | 179 | )
|
175 | 180 |
|
176 | 181 | # Determine color based on normalized value
|
177 |
| - if normalized_val == 1.0: |
| 182 | + if normalized_val == 0.0: |
178 | 183 | color = "\033[92m" # Green for best
|
179 |
| - elif normalized_val == 0.0: |
| 184 | + elif normalized_val == 1.0: |
180 | 185 | color = "\033[91m" # Red for worst
|
181 | 186 | else:
|
182 | 187 | color = f"\033[9{int(2 + normalized_val * 3)}m"
|
183 | 188 |
|
184 | 189 | val_str = format(val, ".3f")
|
185 |
| - row += f"{color}{val_str}\033[0m".center(task_col_widths[t] + 2) |
| 190 | + colored_val_str = f"{color}{val_str}\033[0m" |
| 191 | + |
| 192 | + # Correct spacing by adjusting for visible length |
| 193 | + # padding = ( |
| 194 | + # task_col_widths[t] |
| 195 | + # + 2 |
| 196 | + # - len(strip_ansi_escape_codes(colored_val_str)) |
| 197 | + # ) |
| 198 | + row += center_colored_text( |
| 199 | + colored_val_str, task_col_widths[t] + 2 |
| 200 | + ) |
| 201 | + |
186 | 202 | terminalreporter.write_line(row)
|
187 | 203 | else:
|
188 | 204 | terminalreporter.write_line("No harvested fixture data found yet.")
|
|
0 commit comments