ansible-test - Fix interactive cmd traceback (#84264)

pull/84208/head
Matt Clay 3 weeks ago committed by GitHub
parent 9cdd6e58b3
commit 68bfa37838
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- ansible-test - Fix traceback that occurs after an interactive command fails.

@ -1014,15 +1014,15 @@ class HostConnectionError(ApplicationError):
self._callback() 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).""" """Return a formatted string containing the given stdout and stderr (if any)."""
message = '' message = ''
if stderr := stderr.strip(): if stderr and (stderr := stderr.strip()):
message += '>>> Standard Error\n' message += '>>> Standard Error\n'
message += f'{stderr}{Display.clear}\n' message += f'{stderr}{Display.clear}\n'
if stdout := stdout.strip(): if stdout and (stdout := stdout.strip()):
message += '>>> Standard Output\n' message += '>>> Standard Output\n'
message += f'{stdout}{Display.clear}\n' message += f'{stdout}{Display.clear}\n'

@ -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

@ -7,7 +7,7 @@ import sys
@pytest.fixture(autouse=True, scope='session') @pytest.fixture(autouse=True, scope='session')
def ansible_test(): def inject_ansible_test():
"""Make ansible_test available on sys.path for unit testing 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') test_lib = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'lib')
sys.path.insert(0, test_lib) sys.path.insert(0, test_lib)

Loading…
Cancel
Save