| 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.fixture |
| 15 |
def caplog(mock_sub): |
| 16 |
# Capture the logged content independent of the console. |
| 17 |
actual_log = [] |
| 18 |
|
| 19 |
def test_log(*messages, **kwargs): |
| 20 |
for message in messages: |
| 21 |
actual_log.append(message + "\n") |
| 22 |
|
| 23 |
mock_sub.tools.console.to_log = test_log |
| 24 |
return actual_log |
| 25 |
|
| 26 |
|
| 27 |
EXPECTED_SUCCESS_OUTPUT = ( |
| 28 |
"\n" |
| 29 |
">>> Running Command:\n" |
| 30 |
">>> hello world\n" |
| 31 |
">>> Working Directory:\n" |
| 32 |
f">>> {Path.cwd()}\n" |
| 33 |
">>> Command Output:\n" |
| 34 |
">>> some output line 1\n" |
| 35 |
">>> more output line 2\n" |
| 36 |
">>> Return code: 0\n" |
| 37 |
"\n" |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize("platform", ["Linux", "Darwin", "Windows"]) |
| 42 |
@pytest.mark.parametrize( |
| 43 |
("quiet", "verbosity", "expected_output", "expected_log"), |
| 44 |
[ |
| 45 |
(0, LogLevel.INFO, "", EXPECTED_SUCCESS_OUTPUT), |
| 46 |
(0, LogLevel.DEBUG, EXPECTED_SUCCESS_OUTPUT, EXPECTED_SUCCESS_OUTPUT), |
| 47 |
(1, LogLevel.INFO, "", EXPECTED_SUCCESS_OUTPUT), |
| 48 |
(1, LogLevel.DEBUG, EXPECTED_SUCCESS_OUTPUT, EXPECTED_SUCCESS_OUTPUT), |
| 49 |
(2, LogLevel.INFO, "", ""), |
| 50 |
(2, LogLevel.DEBUG, "", ""), |
| 51 |
], |
| 52 |
) |
| 53 |
def test_call( |
| 54 |
mock_sub, |
| 55 |
capsys, |
| 56 |
caplog, |
| 57 |
platform, |
| 58 |
quiet, |
| 59 |
verbosity, |
| 60 |
expected_output, |
| 61 |
expected_log, |
| 62 |
sub_check_output_kw, |
| 63 |
): |
| 64 |
"""A simple call will be invoked.""" |
| 65 |
mock_sub.tools.console.verbosity = verbosity |
| 66 |
|
| 67 |
mock_sub.tools.host_os = platform |
| 68 |
mock_sub.check_output(["hello", "world"], quiet=quiet) |
| 69 |
|
| 70 |
mock_sub._subprocess.check_output.assert_called_with( |
| 71 |
["hello", "world"], **sub_check_output_kw |
| 72 |
) |
| 73 |
assert capsys.readouterr().out == expected_output |
| 74 |
assert "".join(caplog) == expected_log |
| 75 |
|
| 76 |
|
| 77 |
def test_call_with_arg(mock_sub, capsys, sub_check_output_kw): |
| 78 |
"""Any extra keyword arguments are passed through as-is.""" |
| 79 |
|
| 80 |
mock_sub.check_output(["hello", "world"], extra_kw="extra") |
| 81 |
|
| 82 |
mock_sub._subprocess.check_output.assert_called_with( |
| 83 |
["hello", "world"], |
| 84 |
extra_kw="extra", |
| 85 |
**sub_check_output_kw, |
| 86 |
) |
| 87 |
assert capsys.readouterr().out == "" |
| 88 |
|
| 89 |
|
| 90 |
def test_call_with_path_arg(mock_sub, capsys, tmp_path, sub_check_output_kw): |
| 91 |
"""Path-based arguments are converted to strings and passed in as-is.""" |
| 92 |
|
| 93 |
mock_sub.check_output(["hello", tmp_path / "location"], cwd=tmp_path / "cwd") |
| 94 |
|
| 95 |
mock_sub._subprocess.check_output.assert_called_with( |
| 96 |
["hello", os.fsdecode(tmp_path / "location")], |
| 97 |
cwd=os.fsdecode(tmp_path / "cwd"), |
| 98 |
**sub_check_output_kw, |
| 99 |
) |
| 100 |
assert capsys.readouterr().out == "" |
| 101 |
|
| 102 |
|
| 103 |
def test_call_with_escaped_arg(mock_sub, capsys, caplog): |
| 104 |
"""If the command contains special characters, they are shell escaped in output.""" |
| 105 |
mock_sub.tools.console.verbosity = LogLevel.INFO |
| 106 |
|
| 107 |
called_process_error = CalledProcessError( |
| 108 |
returncode=-1, |
| 109 |
cmd=["hello", "my world"], |
| 110 |
output="output line 1\noutput line 2\n", |
| 111 |
stderr="error line 1\nerror line 2\n", |
| 112 |
) |
| 113 |
mock_sub._subprocess.check_output.side_effect = called_process_error |
| 114 |
|
| 115 |
with pytest.raises(CalledProcessError): |
| 116 |
mock_sub.check_output(["hello", "my world"]) |
| 117 |
|
| 118 |
assert capsys.readouterr().out == ( |
| 119 |
"\n" |
| 120 |
"Running Command:\n" |
| 121 |
" hello 'my world'\n" |
| 122 |
"Command Output:\n" |
| 123 |
" output line 1\n" |
| 124 |
" output line 2\n" |
| 125 |
"Command Error Output (stderr):\n" |
| 126 |
" error line 1\n" |
| 127 |
" error line 2\n" |
| 128 |
"Return code: -1\n" |
| 129 |
"\n" |
| 130 |
) |
| 131 |
assert "".join(caplog) == ( |
| 132 |
"\n" |
| 133 |
">>> Running Command:\n" |
| 134 |
">>> hello 'my world'\n" |
| 135 |
">>> Working Directory:\n" |
| 136 |
f">>> {Path.cwd()}\n" |
| 137 |
">>> Command Output:\n" |
| 138 |
">>> output line 1\n" |
| 139 |
">>> output line 2\n" |
| 140 |
">>> Command Error Output (stderr):\n" |
| 141 |
">>> error line 1\n" |
| 142 |
">>> error line 2\n" |
| 143 |
">>> Return code: -1\n" |
| 144 |
"\n" |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
@pytest.mark.parametrize( |
| 149 |
("platform", "start_new_session", "check_output_kwargs"), |
| 150 |
[ |
| 151 |
("Linux", None, {}), |
| 152 |
("Linux", True, {}), |
| 153 |
("Linux", False, {}), |
| 154 |
("Darwin", None, {}), |
| 155 |
("Darwin", True, {}), |
| 156 |
("Darwin", False, {}), |
| 157 |
("Windows", None, {}), |
| 158 |
( |
| 159 |
"Windows", |
| 160 |
True, |
| 161 |
{"creationflags": CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW}, |
| 162 |
), |
| 163 |
("Windows", False, {}), |
| 164 |
], |
| 165 |
) |
| 166 |
def test_call_with_start_new_session( |
| 167 |
mock_sub, |
| 168 |
capsys, |
| 169 |
platform, |
| 170 |
start_new_session, |
| 171 |
check_output_kwargs, |
| 172 |
sub_check_output_kw, |
| 173 |
): |
| 174 |
"""start_new_session is passed thru on Linux and macOS but converted for Windows.""" |
| 175 |
|
| 176 |
mock_sub.tools.host_os = platform |
| 177 |
mock_sub.check_output(["hello", "world"], start_new_session=start_new_session) |
| 178 |
|
| 179 |
final_kwargs = {**check_output_kwargs, **sub_check_output_kw} |
| 180 |
|
| 181 |
if platform == "Windows": |
| 182 |
mock_sub._subprocess.check_output.assert_called_with( |
| 183 |
["hello", "world"], |
| 184 |
**final_kwargs, |
| 185 |
) |
| 186 |
assert capsys.readouterr().out == "" |
| 187 |
else: |
| 188 |
mock_sub._subprocess.check_output.assert_called_with( |
| 189 |
["hello", "world"], |
| 190 |
start_new_session=start_new_session, |
| 191 |
**final_kwargs, |
| 192 |
) |
| 193 |
assert capsys.readouterr().out == "" |
| 194 |
|
| 195 |
|
| 196 |
@pytest.mark.parametrize( |
| 197 |
("creationflags", "final_creationflags"), |
| 198 |
[ |
| 199 |
(0x1, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW | 1), |
| 200 |
(CREATE_NEW_PROCESS_GROUP, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW), |
| 201 |
(0, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW), |
| 202 |
], |
| 203 |
) |
| 204 |
def test_call_windows_with_start_new_session_and_creationflags( |
| 205 |
mock_sub, |
| 206 |
capsys, |
| 207 |
creationflags, |
| 208 |
final_creationflags, |
| 209 |
): |
| 210 |
"""Creationflags used to simulate start_new_session=True should be merged with any |
| 211 |
existing flags.""" |
| 212 |
|
| 213 |
mock_sub.tools.host_os = "Windows" |
| 214 |
|
| 215 |
# use commented test below when merging creationflags is allowed |
| 216 |
with pytest.raises( |
| 217 |
AssertionError, match="Subprocess called with creationflags set" |
| 218 |
): |
| 219 |
mock_sub.check_output( |
| 220 |
["hello", "world"], |
| 221 |
start_new_session=True, |
| 222 |
creationflags=creationflags, |
| 223 |
) |
| 224 |
|
| 225 |
|
| 226 |
def test_debug_call_with_env(mock_sub, capsys, tmp_path, sub_check_output_kw): |
| 227 |
"""If verbosity is turned up, injected env vars are included in output.""" |
| 228 |
mock_sub.tools.console.verbosity = LogLevel.DEBUG |
| 229 |
|
| 230 |
env = {"NewVar": "NewVarValue"} |
| 231 |
mock_sub.check_output(["hello", "world"], env=env, cwd=tmp_path / "cwd") |
| 232 |
|
| 233 |
merged_env = mock_sub.tools.os.environ.copy() |
| 234 |
merged_env.update(env) |
| 235 |
|
| 236 |
mock_sub._subprocess.check_output.assert_called_with( |
| 237 |
["hello", "world"], |
| 238 |
env=merged_env, |
| 239 |
cwd=os.fsdecode(tmp_path / "cwd"), |
| 240 |
**sub_check_output_kw, |
| 241 |
) |
| 242 |
|
| 243 |
expected_output = ( |
| 244 |
"\n" |
| 245 |
">>> Running Command:\n" |
| 246 |
">>> hello world\n" |
| 247 |
">>> Working Directory:\n" |
| 248 |
f">>> {tmp_path / 'cwd'}\n" |
| 249 |
">>> Environment Overrides:\n" |
| 250 |
">>> NewVar=NewVarValue\n" |
| 251 |
">>> Command Output:\n" |
| 252 |
">>> some output line 1\n" |
| 253 |
">>> more output line 2\n" |
| 254 |
">>> Return code: 0\n" |
| 255 |
"\n" |
| 256 |
) |
| 257 |
|
| 258 |
assert capsys.readouterr().out == expected_output |
| 259 |
|
| 260 |
|
| 261 |
def test_debug_call_with_stderr(mock_sub, capsys, tmp_path, sub_check_output_kw): |
| 262 |
"""If stderr is specified, it is not defaulted to stdout.""" |
| 263 |
mock_sub.tools.console.verbosity = LogLevel.DEBUG |
| 264 |
|
| 265 |
mock_sub.check_output( |
| 266 |
["hello", "world"], |
| 267 |
cwd=tmp_path / "cwd", |
| 268 |
stderr=subprocess.DEVNULL, |
| 269 |
) |
| 270 |
|
| 271 |
sub_check_output_kw.pop("stderr") |
| 272 |
mock_sub._subprocess.check_output.assert_called_with( |
| 273 |
["hello", "world"], |
| 274 |
cwd=os.fsdecode(tmp_path / "cwd"), |
| 275 |
stderr=subprocess.DEVNULL, |
| 276 |
**sub_check_output_kw, |
| 277 |
) |
| 278 |
|
| 279 |
expected_output = ( |
| 280 |
"\n" |
| 281 |
">>> Running Command:\n" |
| 282 |
">>> hello world\n" |
| 283 |
">>> Working Directory:\n" |
| 284 |
f">>> {tmp_path / 'cwd'}\n" |
| 285 |
">>> Command Output:\n" |
| 286 |
">>> some output line 1\n" |
| 287 |
">>> more output line 2\n" |
| 288 |
">>> Return code: 0\n" |
| 289 |
"\n" |
| 290 |
) |
| 291 |
|
| 292 |
assert capsys.readouterr().out == expected_output |
| 293 |
|
| 294 |
|
| 295 |
EXPECTED_ERROR_OUTPUT = ( |
| 296 |
"\n" |
| 297 |
"Running Command:\n" |
| 298 |
" hello world\n" |
| 299 |
"Command Output:\n" |
| 300 |
" output line 1\n" |
| 301 |
" output line 2\n" |
| 302 |
"Command Error Output (stderr):\n" |
| 303 |
" error line 1\n" |
| 304 |
" error line 2\n" |
| 305 |
"Return code: -1\n" |
| 306 |
"\n" |
| 307 |
) |
| 308 |
EXPECTED_ERROR_LOG_OUTPUT = ( |
| 309 |
"\n" |
| 310 |
">>> Running Command:\n" |
| 311 |
">>> hello world\n" |
| 312 |
">>> Working Directory:\n" |
| 313 |
f">>> {Path.cwd()}\n" |
| 314 |
">>> Command Output:\n" |
| 315 |
">>> output line 1\n" |
| 316 |
">>> output line 2\n" |
| 317 |
">>> Command Error Output (stderr):\n" |
| 318 |
">>> error line 1\n" |
| 319 |
">>> error line 2\n" |
| 320 |
">>> Return code: -1\n" |
| 321 |
"\n" |
| 322 |
) |
| 323 |
|
| 324 |
|
| 325 |
@pytest.mark.parametrize( |
| 326 |
("quiet", "verbosity", "expected_output", "expected_log"), |
| 327 |
[ |
| 328 |
(0, LogLevel.INFO, EXPECTED_ERROR_OUTPUT, EXPECTED_ERROR_LOG_OUTPUT), |
| 329 |
(0, LogLevel.DEBUG, EXPECTED_ERROR_LOG_OUTPUT, EXPECTED_ERROR_LOG_OUTPUT), |
| 330 |
(1, LogLevel.INFO, "", EXPECTED_ERROR_LOG_OUTPUT), |
| 331 |
(1, LogLevel.DEBUG, EXPECTED_ERROR_LOG_OUTPUT, EXPECTED_ERROR_LOG_OUTPUT), |
| 332 |
(2, LogLevel.INFO, "", ""), |
| 333 |
(2, LogLevel.DEBUG, "", ""), |
| 334 |
], |
| 335 |
) |
| 336 |
def test_calledprocesserror_exception_logging( |
| 337 |
mock_sub, |
| 338 |
capsys, |
| 339 |
caplog, |
| 340 |
quiet, |
| 341 |
verbosity, |
| 342 |
expected_output, |
| 343 |
expected_log, |
| 344 |
): |
| 345 |
"""If command errors, command output is handled appropriately.""" |
| 346 |
mock_sub.tools.console.verbosity = verbosity |
| 347 |
|
| 348 |
called_process_error = CalledProcessError( |
| 349 |
returncode=-1, |
| 350 |
cmd=["hello", "world"], |
| 351 |
output="output line 1\noutput line 2\n", |
| 352 |
stderr="error line 1\nerror line 2\n", |
| 353 |
) |
| 354 |
mock_sub._subprocess.check_output.side_effect = called_process_error |
| 355 |
|
| 356 |
with pytest.raises(CalledProcessError): |
| 357 |
mock_sub.check_output(["hello", "world"], quiet=quiet) |
| 358 |
|
| 359 |
assert capsys.readouterr().out == expected_output |
| 360 |
assert "".join(caplog) == expected_log |
| 361 |
|
| 362 |
|
| 363 |
def test_calledprocesserror_exception_logging_no_output(mock_sub, capsys, caplog): |
| 364 |
"""If command errors, and there is no command output, return code is still |
| 365 |
printed.""" |
| 366 |
called_process_error = CalledProcessError( |
| 367 |
returncode=-1, |
| 368 |
cmd=["hello", "world"], |
| 369 |
output=None, |
| 370 |
stderr=None, |
| 371 |
) |
| 372 |
mock_sub._subprocess.check_output.side_effect = called_process_error |
| 373 |
|
| 374 |
with pytest.raises(CalledProcessError): |
| 375 |
mock_sub.check_output(["hello", "world"]) |
| 376 |
|
| 377 |
assert capsys.readouterr().out == ( |
| 378 |
"\nRunning Command:\n hello world\nReturn code: -1\n\n" |
| 379 |
) |
| 380 |
assert "".join(caplog) == ( |
| 381 |
"\n" |
| 382 |
">>> Running Command:\n" |
| 383 |
">>> hello world\n" |
| 384 |
">>> Working Directory:\n" |
| 385 |
f">>> {Path.cwd()}\n" |
| 386 |
">>> Return code: -1\n" |
| 387 |
"\n" |
| 388 |
) |
| 389 |
|
| 390 |
|
| 391 |
@pytest.mark.parametrize( |
| 392 |
("in_kwargs", "kwargs"), |
| 393 |
[ |
| 394 |
( |
| 395 |
{}, |
| 396 |
{ |
| 397 |
"text": True, |
| 398 |
"encoding": ANY, |
| 399 |
"stderr": subprocess.STDOUT, |
| 400 |
"errors": "backslashreplace", |
| 401 |
}, |
| 402 |
), |
| 403 |
( |
| 404 |
{"text": True}, |
| 405 |
{ |
| 406 |
"text": True, |
| 407 |
"encoding": ANY, |
| 408 |
"stderr": subprocess.STDOUT, |
| 409 |
"errors": "backslashreplace", |
| 410 |
}, |
| 411 |
), |
| 412 |
({"text": False}, {"text": False, "stderr": subprocess.STDOUT}), |
| 413 |
( |
| 414 |
{"universal_newlines": False}, |
| 415 |
{"text": False, "stderr": subprocess.STDOUT}, |
| 416 |
), |
| 417 |
( |
| 418 |
{"universal_newlines": True}, |
| 419 |
{ |
| 420 |
"text": True, |
| 421 |
"encoding": ANY, |
| 422 |
"stderr": subprocess.STDOUT, |
| 423 |
"errors": "backslashreplace", |
| 424 |
}, |
| 425 |
), |
| 426 |
], |
| 427 |
) |
| 428 |
def test_text_eq_true_default_overriding(mock_sub, in_kwargs, kwargs): |
| 429 |
"""If text or universal_newlines is explicitly provided, those should override |
| 430 |
text=true default and universal_newlines should be converted to text.""" |
| 431 |
mock_sub.check_output(["hello", "world"], stderr=subprocess.STDOUT, **in_kwargs) |
| 432 |
|
| 433 |
mock_sub._subprocess.check_output.assert_called_with(["hello", "world"], **kwargs) |