Debian Codegraph @

tests/integrations/subprocess/test_Subprocess__Popen.py

raw
1 import os
2 from pathlib import Path
3 from unittest.mock import ANY
4
5 import pytest
6
7 from briefcase.console import LogLevel
8
9 from .conftest import CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW
10
11
12 @pytest.mark.parametrize("platform", ["Linux", "Darwin", "Windows"])
13 def test_call(mock_sub, capsys, platform, sub_kw):
14 """A simple call will be invoked."""
15
16 mock_sub.tools.host_os = platform
17 mock_sub.Popen(["hello", "world"])
18
19 mock_sub._subprocess.Popen.assert_called_with(["hello", "world"], **sub_kw)
20 assert capsys.readouterr().out == ""
21
22
23 def test_call_with_arg(mock_sub, capsys, sub_kw):
24 """Any extra keyword arguments are passed through as-is."""
25
26 mock_sub.Popen(["hello", "world"], extra_kw="extra")
27
28 mock_sub._subprocess.Popen.assert_called_with(
29 ["hello", "world"],
30 extra_kw="extra",
31 **sub_kw,
32 )
33 assert capsys.readouterr().out == ""
34
35
36 def test_call_with_path_arg(mock_sub, capsys, tmp_path, sub_kw):
37 """Path-based arguments are converted to strings and passed in as-is."""
38
39 mock_sub.Popen(["hello", tmp_path / "location"], cwd=tmp_path / "cwd")
40
41 mock_sub._subprocess.Popen.assert_called_with(
42 ["hello", os.fsdecode(tmp_path / "location")],
43 cwd=os.fsdecode(tmp_path / "cwd"),
44 **sub_kw,
45 )
46 assert capsys.readouterr().out == ""
47
48
49 @pytest.mark.parametrize(
50 ("platform", "start_new_session", "popen_kwargs"),
51 [
52 ("Linux", None, {}),
53 ("Linux", True, {}),
54 ("Linux", False, {}),
55 ("Darwin", None, {}),
56 ("Darwin", True, {}),
57 ("Darwin", False, {}),
58 ("Windows", None, {}),
59 (
60 "Windows",
61 True,
62 {"creationflags": CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW},
63 ),
64 ("Windows", False, {}),
65 ],
66 )
67 def test_call_with_start_new_session(
68 mock_sub,
69 capsys,
70 platform,
71 start_new_session,
72 popen_kwargs,
73 sub_kw,
74 ):
75 """start_new_session is passed thru on Linux and macOS but converted for Windows."""
76
77 mock_sub.tools.host_os = platform
78 mock_sub.Popen(["hello", "world"], start_new_session=start_new_session)
79
80 final_kwargs = {**sub_kw, **popen_kwargs}
81 if platform == "Windows":
82 mock_sub._subprocess.Popen.assert_called_with(
83 ["hello", "world"], **final_kwargs
84 )
85 assert capsys.readouterr().out == ""
86 else:
87 mock_sub._subprocess.Popen.assert_called_with(
88 ["hello", "world"],
89 start_new_session=start_new_session,
90 **final_kwargs,
91 )
92 assert capsys.readouterr().out == ""
93
94
95 @pytest.mark.parametrize(
96 ("creationflags", "final_creationflags"),
97 [
98 (0x1, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW | 1),
99 (CREATE_NEW_PROCESS_GROUP, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW),
100 (0, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW),
101 ],
102 )
103 def test_call_windows_with_start_new_session_and_creationflags(
104 mock_sub,
105 capsys,
106 creationflags,
107 final_creationflags,
108 ):
109 """Creationflags used to simulate start_new_session=True should be merged with any
110 existing flags."""
111
112 mock_sub.tools.host_os = "Windows"
113
114 # use commented test below when merging creationflags is allowed
115 with pytest.raises(
116 AssertionError, match="Subprocess called with creationflags set"
117 ):
118 mock_sub.Popen(
119 ["hello", "world"],
120 start_new_session=True,
121 creationflags=creationflags,
122 )
123
124
125 def test_debug_call(mock_sub, capsys, sub_kw):
126 """If verbosity is turned up, there is output."""
127 mock_sub.tools.console.verbosity = LogLevel.DEBUG
128 mock_sub.Popen(["hello", "world"])
129
130 mock_sub._subprocess.Popen.assert_called_with(["hello", "world"], **sub_kw)
131
132 assert capsys.readouterr().out == (
133 "\n"
134 ">>> Running Command:\n"
135 ">>> hello world\n"
136 ">>> Working Directory:\n"
137 f">>> {Path.cwd()}\n"
138 )
139
140
141 def test_debug_call_with_env(mock_sub, capsys, tmp_path, sub_kw):
142 """If verbosity is turned up, and injected env vars are included output."""
143 mock_sub.tools.console.verbosity = LogLevel.DEBUG
144
145 env = {"NewVar": "NewVarValue"}
146 mock_sub.Popen(["hello", "world"], env=env, cwd=tmp_path / "cwd")
147
148 merged_env = mock_sub.tools.os.environ.copy()
149 merged_env.update(env)
150
151 mock_sub._subprocess.Popen.assert_called_with(
152 ["hello", "world"],
153 env=merged_env,
154 cwd=os.fsdecode(tmp_path / "cwd"),
155 **sub_kw,
156 )
157
158 expected_output = (
159 "\n"
160 ">>> Running Command:\n"
161 ">>> hello world\n"
162 ">>> Working Directory:\n"
163 f">>> {tmp_path / 'cwd'}\n"
164 ">>> Environment Overrides:\n"
165 ">>> NewVar=NewVarValue\n"
166 )
167 assert capsys.readouterr().out == expected_output
168
169
170 @pytest.mark.parametrize(
171 ("in_kwargs", "kwargs"),
172 [
173 ({}, {"text": True, "encoding": ANY, "errors": "backslashreplace"}),
174 ({"text": True}, {"text": True, "encoding": ANY, "errors": "backslashreplace"}),
175 ({"text": False}, {"text": False}),
176 ({"universal_newlines": False}, {"text": False}),
177 (
178 {"universal_newlines": True},
179 {"text": True, "encoding": ANY, "errors": "backslashreplace"},
180 ),
181 ],
182 )
183 def test_text_eq_true_default_overriding(mock_sub, in_kwargs, kwargs):
184 """If text or universal_newlines is explicitly provided, those should override
185 text=true default and universal_newlines should be converted to text."""
186
187 mock_sub.Popen(["hello", "world"], **in_kwargs)
188 mock_sub._subprocess.Popen.assert_called_with(["hello", "world"], **kwargs)