Debian Codegraph @

test/units/ansible_test/_internal/test_util.py

raw
1 from __future__ import annotations
2
3 import pytest
4
5
6 def test_failed_non_interactive_captured_command() -> None:
7 """Verify failed non-interactive captured commands raise a `SubprocessError` with `stdout` and `stderr` set."""
8 from ansible_test._internal.util import raw_command, SubprocessError
9
10 with pytest.raises(SubprocessError, match='Command "ls /dev/null /does/not/exist" returned exit status [0-9]+.\n>>> Standard Error\n') as error:
11 raw_command(['ls', '/dev/null', '/does/not/exist'], True)
12
13 assert '/dev/null' in error.value.stdout
14 assert '/does/not/exist' in error.value.stderr
15
16
17 def test_failed_non_interactive_command() -> None:
18 """Verify failed non-interactive non-captured commands raise a `SubprocessError` with `stdout` and `stderr` set to an empty string."""
19 from ansible_test._internal.util import raw_command, SubprocessError
20
21 with pytest.raises(SubprocessError, match='Command "ls /dev/null /does/not/exist" returned exit status [0-9]+.') as error:
22 raw_command(['ls', '/dev/null', '/does/not/exist'], False)
23
24 assert error.value.stdout == ''
25 assert error.value.stderr == ''
26
27
28 def test_failed_interactive_command() -> None:
29 """Verify failed interactive commands raise a `SubprocessError` with `stdout` and `stderr` set to `None`."""
30 from ansible_test._internal.util import raw_command, SubprocessError
31
32 with pytest.raises(SubprocessError, match='Command "ls /dev/null /does/not/exist" returned exit status [0-9]+.') as error:
33 raw_command(['ls', '/dev/null', '/does/not/exist'], False, interactive=True)
34
35 assert error.value.stdout is None
36 assert error.value.stderr is None
37
38
39 @pytest.mark.parametrize("args,filters,expected", (
40 (
41 # args after a known option must be separated from existing args
42 ['--color', '--docker', 'default', 'ping', 'split'],
43 {'--docker': 1},
44 ['--color', '--', 'ping', 'split'],
45 ),
46 (
47 # args after '--' are not options
48 ['--', '--color'],
49 {'--color': 1},
50 ['--', '--color'],
51 ),
52 (
53 # args after a known option must be separated from existing args without conflicting with an explicit '--'
54 ['--color', '--docker', 'default', 'ping', '--', 'split'],
55 {'--docker': 1},
56 ['--color', '--', 'ping', 'split'],
57 ),
58 (
59 # args before options are properly handled
60 ['ping', '--docker', 'default', '-v'],
61 {'--docker': 1},
62 ['ping', '-v'],
63 ),
64 (
65 # no options
66 ['ping', 'split'],
67 {'--docker': 1},
68 ['ping', 'split'],
69 ),
70 (
71 # only options
72 ['--color', '--docker', 'default', '-v'],
73 {'--docker': 1},
74 ['--color', '-v'],
75 )
76 ))
77 def test_filter_args(args: list[str], filters: dict[str, int], expected: list[str]) -> None:
78 """Verify arg filtering properly handles various scenarios."""
79 from ansible_test._internal.util import filter_args
80
81 assert filter_args(args, filters) == expected