| 1 |
import subprocess |
| 2 |
from unittest.mock import ANY |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from briefcase.integrations.subprocess import CommandOutputParseError, ParseError |
| 7 |
|
| 8 |
|
| 9 |
def splitlines_parser(data): |
| 10 |
"""A test parser that returns the input data, split by line.""" |
| 11 |
return data.splitlines() |
| 12 |
|
| 13 |
|
| 14 |
def second_line_parser(data): |
| 15 |
"""A test parser that returns the second line of input.""" |
| 16 |
try: |
| 17 |
return data.splitlines()[1] |
| 18 |
except IndexError as e: |
| 19 |
raise ParseError("Input does not contain 2 lines") from e |
| 20 |
|
| 21 |
|
| 22 |
def third_line_parser(data): |
| 23 |
"""A test parser that returns the third line of input.""" |
| 24 |
try: |
| 25 |
return data.splitlines()[2] |
| 26 |
except IndexError as e: |
| 27 |
raise ParseError("Input does not contain 3 lines") from e |
| 28 |
|
| 29 |
|
| 30 |
def test_call(mock_sub, capsys, sub_check_output_kw): |
| 31 |
"""A simple call to check_output will be invoked.""" |
| 32 |
|
| 33 |
output = mock_sub.parse_output(splitlines_parser, ["hello", "world"]) |
| 34 |
|
| 35 |
mock_sub._subprocess.check_output.assert_called_with( |
| 36 |
["hello", "world"], |
| 37 |
**sub_check_output_kw, |
| 38 |
) |
| 39 |
assert capsys.readouterr().out == "" |
| 40 |
assert output == ["some output line 1", "more output line 2"] |
| 41 |
|
| 42 |
|
| 43 |
def test_call_with_arg(mock_sub, capsys, sub_check_output_kw): |
| 44 |
"""Any extra keyword arguments are passed through as-is to check_output.""" |
| 45 |
|
| 46 |
output = mock_sub.parse_output( |
| 47 |
splitlines_parser, |
| 48 |
["hello", "world"], |
| 49 |
extra_kw="extra", |
| 50 |
) |
| 51 |
|
| 52 |
mock_sub._subprocess.check_output.assert_called_with( |
| 53 |
["hello", "world"], |
| 54 |
extra_kw="extra", |
| 55 |
**sub_check_output_kw, |
| 56 |
) |
| 57 |
assert capsys.readouterr().out == "" |
| 58 |
assert output == ["some output line 1", "more output line 2"] |
| 59 |
|
| 60 |
|
| 61 |
def test_call_with_parser_success(mock_sub, capsys, sub_check_output_kw): |
| 62 |
"""Parser returns expected portion of check_output's output.""" |
| 63 |
|
| 64 |
output = mock_sub.parse_output(second_line_parser, ["hello", "world"]) |
| 65 |
|
| 66 |
mock_sub._subprocess.check_output.assert_called_with( |
| 67 |
["hello", "world"], |
| 68 |
**sub_check_output_kw, |
| 69 |
) |
| 70 |
|
| 71 |
assert output == "more output line 2" |
| 72 |
|
| 73 |
|
| 74 |
def test_call_with_parser_error(mock_sub, capsys, sub_check_output_kw): |
| 75 |
"""Parser errors on output from check_output.""" |
| 76 |
|
| 77 |
with pytest.raises( |
| 78 |
CommandOutputParseError, |
| 79 |
match="Unable to parse command output: Input does not contain 3 lines", |
| 80 |
): |
| 81 |
mock_sub.parse_output(third_line_parser, ["hello", "world"]) |
| 82 |
|
| 83 |
mock_sub._subprocess.check_output.assert_called_with( |
| 84 |
["hello", "world"], |
| 85 |
**sub_check_output_kw, |
| 86 |
) |
| 87 |
|
| 88 |
expected_output = ( |
| 89 |
"\n" |
| 90 |
"Command Output Parsing Error:\n" |
| 91 |
" Input does not contain 3 lines\n" |
| 92 |
"Command:\n" |
| 93 |
" hello world\n" |
| 94 |
"Command Output:\n" |
| 95 |
" some output line 1\n" |
| 96 |
" more output line 2\n" |
| 97 |
) |
| 98 |
assert capsys.readouterr().out == expected_output |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize( |
| 102 |
("in_kwargs", "kwargs"), |
| 103 |
[ |
| 104 |
({}, {"text": True, "encoding": ANY, "errors": "backslashreplace"}), |
| 105 |
({"text": True}, {"text": True, "encoding": ANY, "errors": "backslashreplace"}), |
| 106 |
({"text": False}, {"text": False}), |
| 107 |
({"universal_newlines": False}, {"text": False}), |
| 108 |
( |
| 109 |
{"universal_newlines": True}, |
| 110 |
{"text": True, "encoding": ANY, "errors": "backslashreplace"}, |
| 111 |
), |
| 112 |
], |
| 113 |
) |
| 114 |
def test_text_eq_true_default_overriding(mock_sub, in_kwargs, kwargs): |
| 115 |
"""If text or universal_newlines is explicitly provided, those should override |
| 116 |
text=true default and universal_newlines should be converted to text.""" |
| 117 |
|
| 118 |
mock_sub.parse_output(splitlines_parser, ["hello", "world"], **in_kwargs) |
| 119 |
|
| 120 |
mock_sub._subprocess.check_output.assert_called_with( |
| 121 |
["hello", "world"], |
| 122 |
stderr=subprocess.STDOUT, |
| 123 |
**kwargs, |
| 124 |
) |