Debian Codegraph @

tests/platforms/windows/visualstudio/test_run.py

raw
1 # The run command inherits most of its behavior from the common base
2 # implementation. Do a surface-level verification here, but the app
3 # tests provide the actual test coverage.
4 import json
5 import platform
6 import subprocess
7 from unittest import mock
8
9 import pytest
10
11 from briefcase.integrations.subprocess import Subprocess
12 from briefcase.platforms.windows.visualstudio import WindowsVisualStudioRunCommand
13
14
15 @pytest.fixture
16 def run_command(dummy_console, tmp_path):
17 command = WindowsVisualStudioRunCommand(
18 console=dummy_console,
19 base_path=tmp_path / "base_path",
20 data_path=tmp_path / "briefcase",
21 )
22 command.tools.home_path = tmp_path / "home"
23 command.tools.subprocess = mock.MagicMock(spec_set=Subprocess)
24
25 command._stream_app_logs = mock.MagicMock()
26
27 return command
28
29
30 def test_run_app(run_command, first_app_config, tmp_path):
31 """A windows Visual Studio project app can be started."""
32
33 # Set up the log streamer to return a known stream with a good returncode
34 log_popen = mock.MagicMock()
35 run_command.tools.subprocess.Popen.return_value = log_popen
36
37 # Run the app
38 run_command.run_app(first_app_config, passthrough=[])
39
40 # Popen was called
41 run_command.tools.subprocess.Popen.assert_called_with(
42 [
43 tmp_path
44 / "base_path/build/first-app/windows/visualstudio/x64/Release/First App.exe"
45 ],
46 cwd=tmp_path / "home",
47 encoding="UTF-8",
48 stdout=subprocess.PIPE,
49 stderr=subprocess.STDOUT,
50 bufsize=1,
51 )
52
53 # The streamer was started
54 run_command._stream_app_logs.assert_called_once_with(
55 first_app_config,
56 popen=log_popen,
57 clean_output=False,
58 )
59
60
61 def test_run_app_with_args(run_command, first_app_config, tmp_path):
62 """A windows Visual Studio project app can be started with args."""
63
64 # Set up the log streamer to return a known stream with a good returncode
65 log_popen = mock.MagicMock()
66 run_command.tools.subprocess.Popen.return_value = log_popen
67
68 # Run the app with args
69 run_command.run_app(
70 first_app_config,
71 passthrough=["foo", "--bar"],
72 )
73
74 # Popen was called
75 run_command.tools.subprocess.Popen.assert_called_with(
76 [
77 tmp_path
78 / "base_path/build/first-app/windows/visualstudio/x64/Release/First App.exe",
79 "foo",
80 "--bar",
81 ],
82 cwd=tmp_path / "home",
83 encoding="UTF-8",
84 stdout=subprocess.PIPE,
85 stderr=subprocess.STDOUT,
86 bufsize=1,
87 )
88
89 # The streamer was started
90 run_command._stream_app_logs.assert_called_once_with(
91 first_app_config,
92 popen=log_popen,
93 clean_output=False,
94 )
95
96
97 def test_run_app_test_mode(run_command, first_app_config, tmp_path):
98 """A windows Visual Studio project app can be started in test mode."""
99 first_app_config.test_mode = True
100
101 # Set up the log streamer to return a known stream with a good returncode
102 log_popen = mock.MagicMock()
103 run_command.tools.subprocess.Popen.return_value = log_popen
104
105 # Run the app in test mode
106 run_command.run_app(first_app_config, passthrough=[])
107
108 # Popen was called
109 run_command.tools.subprocess.Popen.assert_called_with(
110 [
111 tmp_path
112 / "base_path/build/first-app/windows/visualstudio/x64/Release/First App.exe"
113 ],
114 cwd=tmp_path / "home",
115 encoding="UTF-8",
116 stdout=subprocess.PIPE,
117 stderr=subprocess.STDOUT,
118 bufsize=1,
119 env={"BRIEFCASE_MAIN_MODULE": "tests.first_app"},
120 )
121
122 # The streamer was started
123 run_command._stream_app_logs.assert_called_once_with(
124 first_app_config,
125 popen=log_popen,
126 clean_output=False,
127 )
128
129
130 def test_run_app_test_mode_with_args(run_command, first_app_config, tmp_path):
131 """A windows Visual Studio project app can be started in test mode with args."""
132 first_app_config.test_mode = True
133
134 # Set up the log streamer to return a known stream with a good returncode
135 log_popen = mock.MagicMock()
136 run_command.tools.subprocess.Popen.return_value = log_popen
137
138 # Run the app with args
139 run_command.run_app(
140 first_app_config,
141 passthrough=["foo", "--bar"],
142 )
143
144 # Popen was called
145 run_command.tools.subprocess.Popen.assert_called_with(
146 [
147 tmp_path
148 / "base_path/build/first-app/windows/visualstudio/x64/Release/First App.exe",
149 "foo",
150 "--bar",
151 ],
152 cwd=tmp_path / "home",
153 encoding="UTF-8",
154 stdout=subprocess.PIPE,
155 stderr=subprocess.STDOUT,
156 bufsize=1,
157 env={"BRIEFCASE_MAIN_MODULE": "tests.first_app"},
158 )
159
160 # The streamer was started
161 run_command._stream_app_logs.assert_called_once_with(
162 first_app_config,
163 popen=log_popen,
164 clean_output=False,
165 )
166
167
168 def test_run_app_debugger(run_command, first_app_config, tmp_path, dummy_debugger):
169 """A windows Visual Studio project app can be started in debug mode."""
170 first_app_config.debugger = dummy_debugger
171 first_app_config.debugger_host = "somehost"
172 first_app_config.debugger_port = 9999
173
174 # Set up the log streamer to return a known stream with a good returncode
175 log_popen = mock.MagicMock()
176 run_command.tools.subprocess.Popen.return_value = log_popen
177
178 # Run the app in test mode
179 run_command.run_app(first_app_config, passthrough=[])
180
181 # Popen was called
182 run_command.tools.subprocess.Popen.assert_called_with(
183 [
184 tmp_path
185 / "base_path/build/first-app/windows/visualstudio/x64/Release/First App.exe"
186 ],
187 cwd=tmp_path / "home",
188 encoding="UTF-8",
189 stdout=subprocess.PIPE,
190 stderr=subprocess.STDOUT,
191 bufsize=1,
192 env={
193 "BRIEFCASE_DEBUGGER": json.dumps(
194 {
195 "debugger": "dummy",
196 "host": "somehost",
197 "port": 9999,
198 "host_os": platform.system(),
199 "app_path_mappings": {
200 "device_sys_path_regex": "app$",
201 "device_subfolders": ["first_app"],
202 "host_folders": [str(tmp_path / "base_path/src/first_app")],
203 },
204 "app_packages_path_mappings": None,
205 }
206 )
207 },
208 )
209
210 # The streamer was started
211 run_command._stream_app_logs.assert_called_once_with(
212 first_app_config,
213 popen=log_popen,
214 clean_output=False,
215 )