Debian Codegraph @

tests/integrations/subprocess/test_Subprocess__run__stream_output__False.py

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