From 12abfb06c21799cfc109db2eb520693071c82c1b Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 5 Nov 2024 16:52:53 -0800 Subject: [PATCH] [stable-2.16] ansible-test - Fix interactive cmd traceback (#84264) (#84267) (cherry picked from commit 68bfa378386f1f1b5ea9156324f2f5d7942d8a5c) --- .../ansible-test-fix-command-traceback.yml | 2 ++ test/lib/ansible_test/_internal/util.py | 6 ++-- test/units/ansible_test/_internal/__init__.py | 0 .../units/ansible_test/_internal/test_util.py | 36 +++++++++++++++++++ test/units/ansible_test/conftest.py | 4 +-- 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/ansible-test-fix-command-traceback.yml create mode 100644 test/units/ansible_test/_internal/__init__.py create mode 100644 test/units/ansible_test/_internal/test_util.py diff --git a/changelogs/fragments/ansible-test-fix-command-traceback.yml b/changelogs/fragments/ansible-test-fix-command-traceback.yml new file mode 100644 index 00000000000..d43294006f9 --- /dev/null +++ b/changelogs/fragments/ansible-test-fix-command-traceback.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test - Fix traceback that occurs after an interactive command fails. diff --git a/test/lib/ansible_test/_internal/util.py b/test/lib/ansible_test/_internal/util.py index 40db6636a4c..1ede3c41b74 100644 --- a/test/lib/ansible_test/_internal/util.py +++ b/test/lib/ansible_test/_internal/util.py @@ -974,15 +974,15 @@ class HostConnectionError(ApplicationError): self._callback() -def format_command_output(stdout: str, stderr: str) -> str: +def format_command_output(stdout: str | None, stderr: str | None) -> str: """Return a formatted string containing the given stdout and stderr (if any).""" message = '' - if stderr := stderr.strip(): + if stderr and (stderr := stderr.strip()): message += '>>> Standard Error\n' message += f'{stderr}{Display.clear}\n' - if stdout := stdout.strip(): + if stdout and (stdout := stdout.strip()): message += '>>> Standard Output\n' message += f'{stdout}{Display.clear}\n' diff --git a/test/units/ansible_test/_internal/__init__.py b/test/units/ansible_test/_internal/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/ansible_test/_internal/test_util.py b/test/units/ansible_test/_internal/test_util.py new file mode 100644 index 00000000000..97e2f05dd74 --- /dev/null +++ b/test/units/ansible_test/_internal/test_util.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +import pytest + + +def test_failed_non_interactive_captured_command() -> None: + """Verify failed non-interactive captured commands raise a `SubprocessError` with `stdout` and `stderr` set.""" + from ansible_test._internal.util import raw_command, SubprocessError + + with pytest.raises(SubprocessError, match='Command "ls /dev/null /does/not/exist" returned exit status [0-9]+.\n>>> Standard Error\n') as error: + raw_command(['ls', '/dev/null', '/does/not/exist'], True) + + assert '/dev/null' in error.value.stdout + assert '/does/not/exist' in error.value.stderr + + +def test_failed_non_interactive_command() -> None: + """Verify failed non-interactive non-captured commands raise a `SubprocessError` with `stdout` and `stderr` set to an empty string.""" + from ansible_test._internal.util import raw_command, SubprocessError + + with pytest.raises(SubprocessError, match='Command "ls /dev/null /does/not/exist" returned exit status [0-9]+.') as error: + raw_command(['ls', '/dev/null', '/does/not/exist'], False) + + assert error.value.stdout == '' + assert error.value.stderr == '' + + +def test_failed_interactive_command() -> None: + """Verify failed interactive commands raise a `SubprocessError` with `stdout` and `stderr` set to `None`.""" + from ansible_test._internal.util import raw_command, SubprocessError + + with pytest.raises(SubprocessError, match='Command "ls /dev/null /does/not/exist" returned exit status [0-9]+.') as error: + raw_command(['ls', '/dev/null', '/does/not/exist'], False, interactive=True) + + assert error.value.stdout is None + assert error.value.stderr is None diff --git a/test/units/ansible_test/conftest.py b/test/units/ansible_test/conftest.py index 9ec9a02f786..a6923a44e33 100644 --- a/test/units/ansible_test/conftest.py +++ b/test/units/ansible_test/conftest.py @@ -8,7 +8,7 @@ import sys @pytest.fixture(autouse=True, scope='session') -def ansible_test(): - """Make ansible_test available on sys.path for unit testing ansible-test.""" +def inject_ansible_test(): + """Make ansible_test available on `sys.path` for unit testing ansible-test.""" test_lib = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'lib') sys.path.insert(0, test_lib)