|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | import os |
| 6 | +import re |
6 | 7 | import shutil |
7 | 8 | import subprocess |
8 | | -import sys |
9 | 9 | from pathlib import Path |
10 | 10 |
|
11 | 11 | from Deeploy.Logging import DEFAULT_LOGGER as log |
@@ -148,6 +148,12 @@ def build_binary(config: DeeployTestConfig) -> None: |
148 | 148 | raise RuntimeError(f"Build failed for {config.test_name}") |
149 | 149 |
|
150 | 150 |
|
| 151 | +# Source: https://stackoverflow.com/a/38662876 |
| 152 | +def escapeAnsi(line): |
| 153 | + ansi_escape = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]') |
| 154 | + return ansi_escape.sub('', line) |
| 155 | + |
| 156 | + |
151 | 157 | def run_simulation(config: DeeployTestConfig, skip: bool = False) -> TestResult: |
152 | 158 | """ |
153 | 159 | Run simulation and parse output. |
@@ -191,15 +197,29 @@ def run_simulation(config: DeeployTestConfig, skip: bool = False) -> TestResult: |
191 | 197 |
|
192 | 198 | log.debug(f"[Execution] Simulation command: {' '.join(cmd)}") |
193 | 199 |
|
194 | | - result = subprocess.run(cmd, capture_output = True, text = True, env = env) |
195 | | - |
196 | | - if result.stdout: |
197 | | - print(result.stdout, end = '') |
198 | | - if result.stderr: |
199 | | - print(result.stderr, end = '', file = sys.stderr) |
| 200 | + cmd_str = " ".join(cmd) |
| 201 | + with subprocess.Popen(cmd_str, |
| 202 | + stdout = subprocess.PIPE, |
| 203 | + stderr = subprocess.STDOUT, |
| 204 | + shell = True, |
| 205 | + encoding = 'utf-8') as process: |
| 206 | + |
| 207 | + with open('out.txt', 'a', encoding = 'utf-8') as fileHandle: |
| 208 | + fileHandle.write( |
| 209 | + f"################## Testing {config.test_dir} on {config.platform} Platform ##################\n") |
| 210 | + |
| 211 | + result = "" |
| 212 | + while True: |
| 213 | + output = process.stdout.readline() |
| 214 | + if output == '' and process.poll() is not None: |
| 215 | + break |
| 216 | + if output: |
| 217 | + print(output.strip()) |
| 218 | + result += output |
| 219 | + fileHandle.write(f"{escapeAnsi(output)}") |
200 | 220 |
|
201 | 221 | # Parse output for error count and cycles |
202 | | - test_result = parse_test_output(result.stdout, result.stderr) |
| 222 | + test_result = parse_test_output(result, "") |
203 | 223 |
|
204 | 224 | if not test_result.success and test_result.error_count == -1: |
205 | 225 | log.warning(f"Could not parse error count from output") |
|
0 commit comments