Execute a positive test. Will trigger a test failure if the subprocess return code is any value other than zero. Args: cmd: The cmd to execute. pattern: A regex pattern to search for, must contain a single group which will be
(self, cmd: list[str | Path],
pattern: Optional[re.Pattern] = None)
| 397 | self.assertAlmostEqual(ref_channel, new_channel, delta=max_delta) |
| 398 | |
| 399 | def exec(self, cmd: list[str | Path], |
| 400 | pattern: Optional[re.Pattern] = None) -> str: |
| 401 | ''' |
| 402 | Execute a positive test. |
| 403 | |
| 404 | Will trigger a test failure if the subprocess return code is any value |
| 405 | other than zero. |
| 406 | |
| 407 | Args: |
| 408 | cmd: The cmd to execute. |
| 409 | pattern: A regex pattern to search for, must contain a single group |
| 410 | which will be returned to the caller. The test will fail if no |
| 411 | pattern match is found. |
| 412 | |
| 413 | Return: |
| 414 | The stdout output of the child process if no pattern, or the first |
| 415 | group match from the pattern when applied to stdout output of the |
| 416 | child process. |
| 417 | ''' |
| 418 | try: |
| 419 | cmd_str = [str(x) for x in cmd] |
| 420 | result = sp.run(cmd_str, stdout=sp.PIPE, stderr=sp.PIPE, |
| 421 | text=True, check=True) |
| 422 | rcode = result.returncode |
| 423 | rstdout = result.stdout |
| 424 | rstderr = result.stderr |
| 425 | |
| 426 | except sp.CalledProcessError as ex: |
| 427 | rcode = ex.returncode |
| 428 | rstdout = ex.stdout |
| 429 | rstderr = ex.stderr |
| 430 | |
| 431 | passed = rcode == 0 |
| 432 | |
| 433 | # Emit debug logging if needed |
| 434 | if ASTCENC_CLI_ALWAYS or (not passed and ASTCENC_CLI_ON_ERROR): |
| 435 | print('\n' + ' '.join(cmd_str)) |
| 436 | |
| 437 | if ASTCENC_LOG_ALWAYS or (not passed and ASTCENC_LOG_ON_ERROR): |
| 438 | print(rstdout) |
| 439 | print(rstderr) |
| 440 | |
| 441 | if rcode < 0: |
| 442 | msg = f'Exec died with signal {signal.Signals(-rcode).name}' |
| 443 | self.assertGreaterEqual(rcode, 0, msg) |
| 444 | |
| 445 | if rcode > 0: |
| 446 | msg = f'Exec died with application error {rcode}' |
| 447 | self.assertEqual(rcode, 0, msg) |
| 448 | |
| 449 | # If there is a regex pattern provided, then search for it |
| 450 | if pattern: |
| 451 | if match := pattern.search(rstdout): |
| 452 | return match.group(1) |
| 453 | self.assertIsNotNone(match) |
| 454 | |
| 455 | return rstdout |
| 456 |
no test coverage detected