1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
import subprocess
from unittest.mock import ANY
import pytest
from briefcase.console import LogLevel
# These tests ignore the elsewhere-tested complexities of Dockerizing
# the arguments and just focuses on the semantics of a run() call.
@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_simple_call(mock_tools, my_app, tmp_path, sub_stream_kw, capsys):
"""A simple call will be invoked."""
mock_tools[my_app].app_context.run(["hello", "world"])
mock_tools[my_app].app_context._dockerize_args.assert_called_once_with(
["hello", "world"]
)
# calls to run() default to using Popen() due to output streaming
mock_tools.subprocess._subprocess.Popen.assert_called_once_with(
ANY,
env={"DOCKER_CLI_HINTS": "false", "PROCESS_ENV_VAR": "VALUE"},
**sub_stream_kw,
)
assert capsys.readouterr().out == (
"\n"
"Entering Docker context...\n"
"Docker| ------------------------------------------------------------------\n"
"Docker| ------------------------------------------------------------------\n"
"Leaving Docker context.\n"
"\n"
)
@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_interactive(mock_tools, my_app, tmp_path, sub_kw, capsys):
"""Docker can be invoked in interactive mode."""
mock_tools[my_app].app_context.run(["hello", "world"], interactive=True)
# Interactive Docker runs must disable output streaming
mock_tools[my_app].app_context._dockerize_args.assert_called_once_with(
["hello", "world"],
interactive=True,
stream_output=False,
)
# Interactive means the call to run is direct, rather than going through Popen
mock_tools.subprocess._subprocess.run.assert_called_once_with(
ANY,
env={"DOCKER_CLI_HINTS": "false", "PROCESS_ENV_VAR": "VALUE"},
**sub_kw,
)
assert capsys.readouterr().out == (
"\n"
"Entering Docker context...\n"
"Docker| ------------------------------------------------------------------\n"
"Docker| ------------------------------------------------------------------\n"
"Leaving Docker context.\n"
"\n"
)
@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_call_with_extra_kwargs(
mock_tools,
my_app,
tmp_path,
sub_stream_kw,
capsys,
):
"""Extra keyword arguments are passed through as-is; env modifications are
converted."""
mock_tools[my_app].app_context.run(
["hello", "world"],
encoding="ISO-42",
extra_kw="extra",
)
mock_tools[my_app].app_context._dockerize_args.assert_called_once_with(
["hello", "world"],
encoding="ISO-42",
extra_kw="extra",
)
mock_tools.subprocess._subprocess.Popen.assert_called_once_with(
ANY,
extra_kw="extra",
encoding="ISO-42",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
text=True,
errors="backslashreplace",
env={"DOCKER_CLI_HINTS": "false", "PROCESS_ENV_VAR": "VALUE"},
)
assert capsys.readouterr().out == (
"\n"
"Entering Docker context...\n"
"Docker| ------------------------------------------------------------------\n"
"Docker| ------------------------------------------------------------------\n"
"Leaving Docker context.\n"
"\n"
)
@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_simple_verbose_call(mock_tools, my_app, tmp_path, sub_stream_kw, capsys):
"""If verbosity is turned out, there is output."""
mock_tools[my_app].app_context.tools.console.verbosity = LogLevel.DEBUG
mock_tools[my_app].app_context.run(["hello", "world"])
mock_tools[my_app].app_context._dockerize_args.assert_called_once_with(
["hello", "world"],
)
mock_tools.subprocess._subprocess.Popen.assert_called_once_with(
ANY,
env={"DOCKER_CLI_HINTS": "false", "PROCESS_ENV_VAR": "VALUE"},
**sub_stream_kw,
)
console_output = capsys.readouterr().out
assert "Entering Docker context...\n" in console_output
assert "Docker| >>> Running Command:\n" in console_output
|