Debian Codegraph @

tests/platforms/windows/app/test_run.py

raw
1 import json
2 import platform
3 import subprocess
4 from unittest import mock
5
6 import pytest
7
8 from briefcase.console import LogLevel
9 from briefcase.integrations.subprocess import Subprocess
10 from briefcase.platforms.windows.app import WindowsAppRunCommand
11
12
13 @pytest.fixture
14 def run_command(dummy_console, tmp_path):
15 command = WindowsAppRunCommand(
16 console=dummy_console,
17 base_path=tmp_path / "base_path",
18 data_path=tmp_path / "briefcase",
19 )
20 command.tools.home_path = tmp_path / "home"
21 command.tools.subprocess = mock.MagicMock(spec_set=Subprocess)
22
23 command._stream_app_logs = mock.MagicMock()
24
25 return command
26
27
28 def test_run_gui_app(run_command, first_app_config, tmp_path):
29 """A Windows GUI app can be started."""
30 # Set up the log streamer to return a known stream
31 log_popen = mock.MagicMock()
32 run_command.tools.subprocess.Popen.return_value = log_popen
33
34 # Run the app
35 run_command.run_app(first_app_config, passthrough=[])
36
37 # The process was started
38 run_command.tools.subprocess.Popen.assert_called_with(
39 [tmp_path / "base_path/build/first-app/windows/app/src/First App.exe"],
40 cwd=tmp_path / "home",
41 encoding="UTF-8",
42 stdout=subprocess.PIPE,
43 stderr=subprocess.STDOUT,
44 bufsize=1,
45 )
46
47 # The streamer was started
48 run_command._stream_app_logs.assert_called_once_with(
49 first_app_config,
50 popen=log_popen,
51 clean_output=False,
52 )
53
54
55 def test_run_gui_app_with_passthrough(run_command, first_app_config, tmp_path):
56 """A Windows GUI app can be started in debug mode with args."""
57 run_command.console.verbosity = LogLevel.DEBUG
58
59 # Set up the log streamer to return a known stream
60 log_popen = mock.MagicMock()
61 run_command.tools.subprocess.Popen.return_value = log_popen
62
63 # Run the app with args
64 run_command.run_app(
65 first_app_config,
66 passthrough=["foo", "--bar"],
67 )
68
69 # The process was started
70 run_command.tools.subprocess.Popen.assert_called_with(
71 [
72 tmp_path / "base_path/build/first-app/windows/app/src/First App.exe",
73 "foo",
74 "--bar",
75 ],
76 cwd=tmp_path / "home",
77 encoding="UTF-8",
78 stdout=subprocess.PIPE,
79 stderr=subprocess.STDOUT,
80 bufsize=1,
81 env={"BRIEFCASE_DEBUG": "1"},
82 )
83
84 # The streamer was started
85 run_command._stream_app_logs.assert_called_once_with(
86 first_app_config,
87 popen=log_popen,
88 clean_output=False,
89 )
90
91
92 def test_run_gui_app_failed(run_command, first_app_config, tmp_path):
93 """If there's a problem starting the GUI app, an exception is raised."""
94
95 run_command.tools.subprocess.Popen.side_effect = OSError("Some error")
96
97 with pytest.raises(OSError, match="Some error"):
98 run_command.run_app(first_app_config, passthrough=[])
99
100 # Popen was still invoked, though
101 run_command.tools.subprocess.Popen.assert_called_with(
102 [tmp_path / "base_path/build/first-app/windows/app/src/First App.exe"],
103 cwd=tmp_path / "home",
104 encoding="UTF-8",
105 stdout=subprocess.PIPE,
106 stderr=subprocess.STDOUT,
107 bufsize=1,
108 )
109
110 # No attempt to stream was made
111 run_command._stream_app_logs.assert_not_called()
112
113
114 def test_run_console_app(run_command, first_app_config, tmp_path):
115 """A Windows GUI app can be started."""
116 first_app_config.console_app = True
117
118 # Set up the log streamer to return a known stream
119 log_popen = mock.MagicMock()
120 run_command.tools.subprocess.Popen.return_value = log_popen
121
122 # Run the app
123 run_command.run_app(first_app_config, passthrough=[])
124
125 # The process was started
126 run_command.tools.subprocess.run.assert_called_with(
127 [tmp_path / "base_path/build/first-app/windows/app/src/first-app.exe"],
128 cwd=tmp_path / "home",
129 encoding="UTF-8",
130 bufsize=1,
131 stream_output=False,
132 )
133
134 # There is no streamer
135 run_command._stream_app_logs.assert_not_called()
136
137
138 def test_run_console_app_with_passthrough(run_command, first_app_config, tmp_path):
139 """A Windows console app can be started in debug mode with args."""
140 run_command.console.verbosity = LogLevel.DEBUG
141
142 first_app_config.console_app = True
143
144 # Run the app with args
145 run_command.run_app(
146 first_app_config,
147 passthrough=["foo", "--bar"],
148 )
149
150 # The process was started
151 run_command.tools.subprocess.run.assert_called_with(
152 [
153 tmp_path / "base_path/build/first-app/windows/app/src/first-app.exe",
154 "foo",
155 "--bar",
156 ],
157 cwd=tmp_path / "home",
158 encoding="UTF-8",
159 bufsize=1,
160 stream_output=False,
161 env={"BRIEFCASE_DEBUG": "1"},
162 )
163
164 # There is no streamer
165 run_command._stream_app_logs.assert_not_called()
166
167
168 def test_run_console_app_failed(run_command, first_app_config, tmp_path):
169 """If there's a problem starting the console app, an exception is raised."""
170 first_app_config.console_app = True
171
172 run_command.tools.subprocess.run.side_effect = OSError("Some error")
173
174 with pytest.raises(OSError, match="Some error"):
175 run_command.run_app(first_app_config, passthrough=[])
176
177 # Popen was still invoked, though
178 run_command.tools.subprocess.run.assert_called_with(
179 [tmp_path / "base_path/build/first-app/windows/app/src/first-app.exe"],
180 cwd=tmp_path / "home",
181 encoding="UTF-8",
182 bufsize=1,
183 stream_output=False,
184 )
185
186 # No attempt to stream was made
187 run_command._stream_app_logs.assert_not_called()
188
189
190 @pytest.mark.parametrize("is_console_app", [True, False])
191 def test_run_app_test_mode(run_command, first_app_config, is_console_app, tmp_path):
192 """A Windows app can be started in test mode."""
193 # Test mode apps are always streamed
194 first_app_config.console_app = is_console_app
195 first_app_config.test_mode = True
196
197 # Set up the log streamer to return a known stream
198 log_popen = mock.MagicMock()
199 run_command.tools.subprocess.Popen.return_value = log_popen
200
201 # Run the app
202 run_command.run_app(first_app_config, passthrough=[])
203
204 # The process was started
205 exe_name = "first-app" if is_console_app else "First App"
206 run_command.tools.subprocess.Popen.assert_called_with(
207 [tmp_path / f"base_path/build/first-app/windows/app/src/{exe_name}.exe"],
208 cwd=tmp_path / "home",
209 encoding="UTF-8",
210 stdout=subprocess.PIPE,
211 stderr=subprocess.STDOUT,
212 bufsize=1,
213 env={"BRIEFCASE_MAIN_MODULE": "tests.first_app"},
214 )
215
216 # The streamer was started
217 run_command._stream_app_logs.assert_called_once_with(
218 first_app_config,
219 popen=log_popen,
220 clean_output=False,
221 )
222
223
224 @pytest.mark.parametrize("is_console_app", [True, False])
225 def test_run_app_test_mode_with_passthrough(
226 run_command,
227 first_app_config,
228 is_console_app,
229 tmp_path,
230 ):
231 """A Windows app can be started in test mode with args."""
232 # Test mode apps are always streamed
233 first_app_config.console_app = is_console_app
234 first_app_config.test_mode = True
235
236 # Set up the log streamer to return a known stream
237 log_popen = mock.MagicMock()
238 run_command.tools.subprocess.Popen.return_value = log_popen
239
240 # Run the app with args
241 run_command.run_app(
242 first_app_config,
243 passthrough=["foo", "--bar"],
244 )
245
246 # The process was started
247 exe_name = "first-app" if is_console_app else "First App"
248 run_command.tools.subprocess.Popen.assert_called_with(
249 [
250 tmp_path / f"base_path/build/first-app/windows/app/src/{exe_name}.exe",
251 "foo",
252 "--bar",
253 ],
254 cwd=tmp_path / "home",
255 encoding="UTF-8",
256 stdout=subprocess.PIPE,
257 stderr=subprocess.STDOUT,
258 bufsize=1,
259 env={"BRIEFCASE_MAIN_MODULE": "tests.first_app"},
260 )
261
262 # The streamer was started
263 run_command._stream_app_logs.assert_called_once_with(
264 first_app_config,
265 popen=log_popen,
266 clean_output=False,
267 )
268
269
270 def test_run_gui_app_debugger(run_command, first_app_config, tmp_path, dummy_debugger):
271 """A Windows app can be started in debug mode."""
272 # Set up the log streamer to return a known stream
273 log_popen = mock.MagicMock()
274 run_command.tools.subprocess.Popen.return_value = log_popen
275
276 first_app_config.debugger = dummy_debugger
277 first_app_config.debugger_host = "somehost"
278 first_app_config.debugger_port = 9999
279
280 # Run the app
281 run_command.run_app(first_app_config, passthrough=[])
282
283 # The process was started
284 run_command.tools.subprocess.Popen.assert_called_with(
285 [tmp_path / "base_path/build/first-app/windows/app/src/First App.exe"],
286 cwd=tmp_path / "home",
287 encoding="UTF-8",
288 stdout=subprocess.PIPE,
289 stderr=subprocess.STDOUT,
290 bufsize=1,
291 env={
292 "BRIEFCASE_DEBUGGER": json.dumps(
293 {
294 "debugger": "dummy",
295 "host": "somehost",
296 "port": 9999,
297 "host_os": platform.system(),
298 "app_path_mappings": {
299 "device_sys_path_regex": "app$",
300 "device_subfolders": ["first_app"],
301 "host_folders": [str(tmp_path / "base_path/src/first_app")],
302 },
303 "app_packages_path_mappings": None,
304 }
305 )
306 },
307 )
308
309 # The streamer was started
310 run_command._stream_app_logs.assert_called_once_with(
311 first_app_config,
312 popen=log_popen,
313 clean_output=False,
314 )