| 1 |
import os |
| 2 |
import subprocess |
| 3 |
from pathlib import Path |
| 4 |
from subprocess import CalledProcessError |
| 5 |
from unittest.mock import ANY |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from briefcase.console import LogLevel |
| 10 |
|
| 11 |
from .conftest import CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.parametrize("platform", ["Linux", "Darwin", "Windows"]) |
| 15 |
def test_call(mock_sub, capsys, platform, sub_stream_kw, sleep_zero): |
| 16 |
"""A simple call will be invoked.""" |
| 17 |
|
| 18 |
mock_sub.tools.sys.platform = platform |
| 19 |
mock_sub.run(["hello", "world"]) |
| 20 |
|
| 21 |
mock_sub._subprocess.Popen.assert_called_with(["hello", "world"], **sub_stream_kw) |
| 22 |
# fmt: off |
| 23 |
expected_output = ( |
| 24 |
"output line 1\n" |
| 25 |
"\n" |
| 26 |
"output line 3\n" |
| 27 |
) |
| 28 |
# fmt: on |
| 29 |
assert capsys.readouterr().out == expected_output |
| 30 |
|
| 31 |
|
| 32 |
def test_call_with_arg(mock_sub, capsys, sub_stream_kw, sleep_zero): |
| 33 |
"""Any extra keyword arguments are passed through as-is.""" |
| 34 |
|
| 35 |
mock_sub.run(["hello", "world"], extra_kw="extra") |
| 36 |
|
| 37 |
mock_sub._subprocess.Popen.assert_called_with( |
| 38 |
["hello", "world"], |
| 39 |
extra_kw="extra", |
| 40 |
**sub_stream_kw, |
| 41 |
) |
| 42 |
# fmt: off |
| 43 |
expected_output = ( |
| 44 |
"output line 1\n" |
| 45 |
"\n" |
| 46 |
"output line 3\n" |
| 47 |
) |
| 48 |
# fmt: on |
| 49 |
assert capsys.readouterr().out == expected_output |
| 50 |
|
| 51 |
|
| 52 |
def test_call_with_path_arg(mock_sub, capsys, tmp_path, sub_stream_kw, sleep_zero): |
| 53 |
"""Path-based arguments are converted to strings and passed in as-is.""" |
| 54 |
|
| 55 |
mock_sub.run(["hello", tmp_path / "location"], cwd=tmp_path / "cwd") |
| 56 |
|
| 57 |
mock_sub._subprocess.Popen.assert_called_with( |
| 58 |
["hello", os.fsdecode(tmp_path / "location")], |
| 59 |
cwd=os.fsdecode(tmp_path / "cwd"), |
| 60 |
**sub_stream_kw, |
| 61 |
) |
| 62 |
# fmt: off |
| 63 |
expected_output = ( |
| 64 |
"output line 1\n" |
| 65 |
"\n" |
| 66 |
"output line 3\n" |
| 67 |
) |
| 68 |
# fmt: on |
| 69 |
assert capsys.readouterr().out == expected_output |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.parametrize( |
| 73 |
("platform", "start_new_session", "run_kwargs"), |
| 74 |
[ |
| 75 |
("Linux", None, {}), |
| 76 |
("Linux", True, {}), |
| 77 |
("Linux", False, {}), |
| 78 |
("Darwin", None, {}), |
| 79 |
("Darwin", True, {}), |
| 80 |
("Darwin", False, {}), |
| 81 |
("Windows", None, {}), |
| 82 |
( |
| 83 |
"Windows", |
| 84 |
True, |
| 85 |
{"creationflags": CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW}, |
| 86 |
), |
| 87 |
("Windows", False, {}), |
| 88 |
], |
| 89 |
) |
| 90 |
def test_call_with_start_new_session( |
| 91 |
mock_sub, |
| 92 |
capsys, |
| 93 |
platform, |
| 94 |
start_new_session, |
| 95 |
run_kwargs, |
| 96 |
sub_stream_kw, |
| 97 |
sleep_zero, |
| 98 |
): |
| 99 |
"""start_new_session is passed thru on Linux and macOS but converted for Windows.""" |
| 100 |
|
| 101 |
mock_sub.tools.host_os = platform |
| 102 |
mock_sub.run(["hello", "world"], start_new_session=start_new_session) |
| 103 |
|
| 104 |
final_kwargs = {**sub_stream_kw, **run_kwargs} |
| 105 |
if platform == "Windows": |
| 106 |
mock_sub._subprocess.Popen.assert_called_with( |
| 107 |
["hello", "world"], |
| 108 |
**final_kwargs, |
| 109 |
) |
| 110 |
else: |
| 111 |
mock_sub._subprocess.Popen.assert_called_with( |
| 112 |
["hello", "world"], |
| 113 |
start_new_session=start_new_session, |
| 114 |
**final_kwargs, |
| 115 |
) |
| 116 |
|
| 117 |
# fmt: off |
| 118 |
expected_output = ( |
| 119 |
"output line 1\n" |
| 120 |
"\n" |
| 121 |
"output line 3\n" |
| 122 |
) |
| 123 |
# fmt: on |
| 124 |
assert capsys.readouterr().out == expected_output |
| 125 |
|
| 126 |
|
| 127 |
@pytest.mark.parametrize( |
| 128 |
("creationflags", "final_creationflags"), |
| 129 |
[ |
| 130 |
(0x1, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW | 1), |
| 131 |
(CREATE_NEW_PROCESS_GROUP, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW), |
| 132 |
(0, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW), |
| 133 |
], |
| 134 |
) |
| 135 |
def test_call_windows_with_start_new_session_and_creationflags( |
| 136 |
mock_sub, |
| 137 |
capsys, |
| 138 |
creationflags, |
| 139 |
final_creationflags, |
| 140 |
sleep_zero, |
| 141 |
): |
| 142 |
"""Creationflags used to simulate start_new_session=True should be merged with any |
| 143 |
existing flags.""" |
| 144 |
|
| 145 |
mock_sub.tools.host_os = "Windows" |
| 146 |
|
| 147 |
# use commented test below when merging creationflags is allowed |
| 148 |
with pytest.raises( |
| 149 |
AssertionError, |
| 150 |
match="Subprocess called with creationflags set", |
| 151 |
): |
| 152 |
mock_sub.run( |
| 153 |
["hello", "world"], |
| 154 |
start_new_session=True, |
| 155 |
creationflags=creationflags, |
| 156 |
) |
| 157 |
|
| 158 |
|
| 159 |
def test_debug_call(mock_sub, capsys, sub_stream_kw, sleep_zero): |
| 160 |
"""If verbosity is turned up, there is output.""" |
| 161 |
mock_sub.tools.console.verbosity = LogLevel.DEBUG |
| 162 |
|
| 163 |
mock_sub.run(["hello", "world"]) |
| 164 |
|
| 165 |
mock_sub._subprocess.Popen.assert_called_with(["hello", "world"], **sub_stream_kw) |
| 166 |
# fmt: off |
| 167 |
expected_output = ( |
| 168 |
"\n" |
| 169 |
">>> Running Command:\n" |
| 170 |
">>> hello world\n" |
| 171 |
">>> Working Directory:\n" |
| 172 |
f">>> {Path.cwd()}\n" |
| 173 |
"output line 1\n" |
| 174 |
"\n" |
| 175 |
"output line 3\n" |
| 176 |
">>> Return code: -3\n" |
| 177 |
"\n" |
| 178 |
) |
| 179 |
# fmt: on |
| 180 |
|
| 181 |
assert capsys.readouterr().out == expected_output |
| 182 |
|
| 183 |
|
| 184 |
def test_debug_call_with_env(mock_sub, capsys, tmp_path, sub_stream_kw, sleep_zero): |
| 185 |
"""If verbosity is turned up, injected env vars are included output.""" |
| 186 |
mock_sub.tools.console.verbosity = LogLevel.DEBUG |
| 187 |
|
| 188 |
env = {"NewVar": "NewVarValue"} |
| 189 |
mock_sub.run(["hello", "world"], env=env, cwd=tmp_path / "cwd") |
| 190 |
|
| 191 |
merged_env = mock_sub.tools.os.environ.copy() |
| 192 |
merged_env.update(env) |
| 193 |
|
| 194 |
mock_sub._subprocess.Popen.assert_called_with( |
| 195 |
["hello", "world"], |
| 196 |
env=merged_env, |
| 197 |
cwd=os.fsdecode(tmp_path / "cwd"), |
| 198 |
**sub_stream_kw, |
| 199 |
) |
| 200 |
expected_output = ( |
| 201 |
"\n" |
| 202 |
">>> Running Command:\n" |
| 203 |
">>> hello world\n" |
| 204 |
">>> Working Directory:\n" |
| 205 |
f">>> {tmp_path / 'cwd'}\n" |
| 206 |
">>> Environment Overrides:\n" |
| 207 |
">>> NewVar=NewVarValue\n" |
| 208 |
"output line 1\n" |
| 209 |
"\n" |
| 210 |
"output line 3\n" |
| 211 |
">>> Return code: -3\n" |
| 212 |
"\n" |
| 213 |
) |
| 214 |
assert capsys.readouterr().out == expected_output |
| 215 |
|
| 216 |
|
| 217 |
def test_calledprocesserror_exception_logging(mock_sub, sleep_zero, capsys): |
| 218 |
mock_sub.tools.console.verbosity = LogLevel.DEBUG |
| 219 |
|
| 220 |
with pytest.raises(CalledProcessError): |
| 221 |
mock_sub.run(["hello", "world"], check=True) |
| 222 |
|
| 223 |
# fmt: off |
| 224 |
expected_output = ( |
| 225 |
"\n" |
| 226 |
">>> Running Command:\n" |
| 227 |
">>> hello world\n" |
| 228 |
">>> Working Directory:\n" |
| 229 |
f">>> {Path.cwd()}\n" |
| 230 |
"output line 1\n" |
| 231 |
"\n" |
| 232 |
"output line 3\n" |
| 233 |
">>> Return code: -3\n" |
| 234 |
"\n" |
| 235 |
) |
| 236 |
# fmt: on |
| 237 |
assert capsys.readouterr().out == expected_output |
| 238 |
|
| 239 |
|
| 240 |
@pytest.mark.parametrize( |
| 241 |
("in_kwargs", "kwargs"), |
| 242 |
[ |
| 243 |
({}, {"text": True, "encoding": ANY, "errors": "backslashreplace"}), |
| 244 |
({"text": True}, {"text": True, "encoding": ANY, "errors": "backslashreplace"}), |
| 245 |
({"text": False}, {"text": False}), |
| 246 |
({"universal_newlines": False}, {"text": False}), |
| 247 |
( |
| 248 |
{"universal_newlines": True}, |
| 249 |
{"text": True, "encoding": ANY, "errors": "backslashreplace"}, |
| 250 |
), |
| 251 |
], |
| 252 |
) |
| 253 |
def test_text_eq_true_default_overriding(mock_sub, in_kwargs, kwargs, sleep_zero): |
| 254 |
"""If text or universal_newlines is explicitly provided, those should override |
| 255 |
text=true default and universal_newlines should be converted to text.""" |
| 256 |
mock_sub.run(["hello", "world"], **in_kwargs) |
| 257 |
|
| 258 |
mock_sub._subprocess.Popen.assert_called_with( |
| 259 |
["hello", "world"], |
| 260 |
stdout=subprocess.PIPE, |
| 261 |
stderr=subprocess.STDOUT, |
| 262 |
bufsize=1, |
| 263 |
**kwargs, |
| 264 |
) |
| 265 |
|
| 266 |
|
| 267 |
@pytest.mark.parametrize("platform", ["Linux", "Darwin", "Windows"]) |
| 268 |
def test_filtered_output(mock_sub, capsys, platform, sub_stream_kw, sleep_zero): |
| 269 |
"""Streamed output can be filtered.""" |
| 270 |
|
| 271 |
# Add an output filter that replaces all spaces with newlines. |
| 272 |
def space_filter(line): |
| 273 |
yield from line.split(" ") |
| 274 |
|
| 275 |
mock_sub.tools.sys.platform = platform |
| 276 |
mock_sub.run( |
| 277 |
["hello", "world"], |
| 278 |
filter_func=space_filter, |
| 279 |
) |
| 280 |
|
| 281 |
mock_sub._subprocess.Popen.assert_called_with( |
| 282 |
["hello", "world"], |
| 283 |
**sub_stream_kw, |
| 284 |
) |
| 285 |
# fmt: off |
| 286 |
expected_output = ( |
| 287 |
"output\n" |
| 288 |
"line\n" |
| 289 |
"1\n" |
| 290 |
"\n" |
| 291 |
"output\n" |
| 292 |
"line\n" |
| 293 |
"3\n" |
| 294 |
) |
| 295 |
# fmt: on |
| 296 |
assert capsys.readouterr().out == expected_output |