Jeremy Johnson | be1a940 | 2021-12-15 17:14:56 +0000 | [diff] [blame] | 1 | """Tests for tosa_verif_run_tests.py.""" |
| 2 | # Copyright (c) 2021-2022, ARM Limited. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | from runner.run_command import run_sh_command |
| 5 | from runner.run_command import RunShCommandError |
| 6 | |
| 7 | |
| 8 | def test_run_command_success(): |
| 9 | """Run successful command.""" |
| 10 | cmd = ["echo", "Hello Space Cadets"] |
| 11 | try: |
| 12 | run_sh_command(cmd) |
| 13 | ok = True |
| 14 | except RunShCommandError: |
| 15 | ok = False |
| 16 | assert ok |
| 17 | |
| 18 | |
| 19 | def test_run_command_fail(): |
| 20 | """Run unsuccessful command.""" |
| 21 | cmd = ["cat", "non-existant-file-432342.txt"] |
| 22 | try: |
| 23 | run_sh_command(cmd) |
| 24 | ok = True |
| 25 | except RunShCommandError as e: |
| 26 | assert e.return_code == 1 |
| 27 | ok = False |
| 28 | assert not ok |
| 29 | |
| 30 | |
| 31 | def test_run_command_fail_with_stderr(): |
| 32 | """Run unsuccessful command capturing output.""" |
| 33 | cmd = ["ls", "--unknown-option"] |
| 34 | try: |
| 35 | stdout, stderr = run_sh_command(cmd, capture_output=True) |
| 36 | ok = True |
| 37 | except RunShCommandError as e: |
| 38 | assert e.return_code == 2 |
| 39 | assert e.stderr |
| 40 | ok = False |
| 41 | assert not ok |
| 42 | |
| 43 | |
| 44 | def test_run_command_success_verbose_with_stdout(): |
| 45 | """Run successful command capturing output.""" |
| 46 | output = "There is no Planet B" |
| 47 | cmd = ["echo", output] |
| 48 | try: |
| 49 | stdout, stderr = run_sh_command(cmd, verbose=True, capture_output=True) |
| 50 | assert stdout == f"{output}\n" |
| 51 | ok = True |
| 52 | except RunShCommandError: |
| 53 | ok = False |
| 54 | assert ok |