Debian Codegraph @

debugger/tests/test_debugpy.py

raw
1 import json
2 import os
3 import sys
4 from unittest.mock import MagicMock
5
6 import briefcase_debugger
7 import debugpy
8 import pytest
9 from briefcase_debugger.config import AppPathMappings
10
11
12 def test_no_env_vars(monkeypatch, capsys):
13 """Nothing happens, when no env vars are set."""
14 os_environ = {}
15 monkeypatch.setattr(os, "environ", os_environ)
16
17 # start test function
18 briefcase_debugger.start_remote_debugger()
19
20 captured = capsys.readouterr()
21 assert captured.out == ""
22 assert captured.err == ""
23
24
25 def test_no_debugger_verbose(monkeypatch, capsys):
26 """Nothing happens except a short message, when only verbose is requested."""
27 os_environ = {}
28 os_environ["BRIEFCASE_DEBUG"] = "1"
29 monkeypatch.setattr(os, "environ", os_environ)
30
31 # start test function
32 briefcase_debugger.start_remote_debugger()
33
34 captured = capsys.readouterr()
35 assert (
36 captured.out
37 == "No 'BRIEFCASE_DEBUGGER' environment variable found. Debugger not starting.\n"
38 )
39 assert captured.err == ""
40
41
42 @pytest.mark.parametrize(
43 ("os_name", "app_path_mappings", "sys_path", "expected_path_mappings"),
44 [
45 (
46 "nt",
47 AppPathMappings(
48 device_sys_path_regex="app$",
49 device_subfolders=["helloworld"],
50 host_folders=["C:\\PROJECT_ROOT\\src\\helloworld"],
51 ),
52 ["C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app"],
53 [
54 (
55 "C:\\PROJECT_ROOT\\src\\helloworld",
56 "C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app\\helloworld",
57 )
58 ],
59 ),
60 (
61 "posix",
62 AppPathMappings(
63 device_sys_path_regex="app$",
64 device_subfolders=["helloworld"],
65 host_folders=["/PROJECT_ROOT/src/helloworld"],
66 ),
67 [
68 "/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Resources/app"
69 ],
70 [
71 (
72 "/PROJECT_ROOT/src/helloworld",
73 "/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Resources/app/helloworld",
74 )
75 ],
76 ),
77 ],
78 )
79 @pytest.mark.parametrize(
80 ("verbose", "some_verbose_output", "pydevd_trace_level"),
81 [
82 (True, "Extracted path mappings:\n[0] host = ", 3),
83 (False, "", 0),
84 ],
85 )
86 def test_with_debugger(
87 os_name: str,
88 app_path_mappings: AppPathMappings,
89 sys_path: list[str],
90 expected_path_mappings: list[tuple[str, str]],
91 verbose: bool,
92 some_verbose_output: str,
93 pydevd_trace_level: int,
94 monkeypatch,
95 capsys,
96 ):
97 """Normal debug session."""
98 if os.name != os_name:
99 pytest.skip(f"Test only runs on {os_name} systems")
100
101 os_environ = {}
102 os_environ["BRIEFCASE_DEBUG"] = "1" if verbose else "0"
103 os_environ["BRIEFCASE_DEBUGGER"] = json.dumps(
104 {
105 "debugger": "debugpy",
106 "host": "somehost",
107 "port": 9999,
108 "host_os": "SomeOS",
109 "app_path_mappings": app_path_mappings,
110 "app_packages_path_mappings": None,
111 }
112 )
113 monkeypatch.setattr(os, "environ", os_environ)
114
115 monkeypatch.setattr(sys, "path", sys_path)
116
117 fake_debugpy_listen = MagicMock()
118 monkeypatch.setattr(debugpy, "listen", fake_debugpy_listen)
119
120 fake_debugpy_wait_for_client = MagicMock()
121 monkeypatch.setattr(debugpy, "wait_for_client", fake_debugpy_wait_for_client)
122
123 # pydevd is dynamically loaded and only available when a real debugger is attached. So
124 # we fake the whole module, as otherwise the import in start_remote_debugger would fail
125 fake_pydevd = MagicMock()
126 monkeypatch.setitem(sys.modules, "pydevd", fake_pydevd)
127 fake_pydevd.DebugInfoHolder.DEBUG_TRACE_LEVEL = 0
128 fake_pydevd_file_utils = MagicMock()
129 fake_pydevd_file_utils.setup_client_server_paths.return_value = None
130 monkeypatch.setitem(sys.modules, "pydevd_file_utils", fake_pydevd_file_utils)
131
132 # start test function
133 briefcase_debugger.start_remote_debugger()
134
135 fake_debugpy_listen.assert_called_once_with(
136 ("somehost", 9999),
137 in_process_debug_adapter=True,
138 )
139
140 fake_debugpy_wait_for_client.assert_called_once()
141 fake_pydevd_file_utils.setup_client_server_paths.assert_called_once_with(
142 expected_path_mappings
143 )
144
145 captured = capsys.readouterr()
146 assert "Waiting for debugger to attach..." in captured.out
147 assert captured.err == ""
148
149 assert some_verbose_output in captured.out
150 assert pydevd_trace_level == fake_pydevd.DebugInfoHolder.DEBUG_TRACE_LEVEL
151
152
153 def test_with_debugger_without_path_mappings(monkeypatch, capsys):
154 """Debug session without path mappings."""
155 os_environ = {}
156 os_environ["BRIEFCASE_DEBUG"] = "0"
157 os_environ["BRIEFCASE_DEBUGGER"] = json.dumps(
158 {
159 "debugger": "debugpy",
160 "host": "somehost",
161 "port": 9999,
162 "host_os": "SomeOS",
163 "app_path_mappings": None,
164 "app_packages_path_mappings": None,
165 }
166 )
167 monkeypatch.setattr(os, "environ", os_environ)
168
169 fake_debugpy_listen = MagicMock()
170 monkeypatch.setattr(debugpy, "listen", fake_debugpy_listen)
171
172 fake_debugpy_wait_for_client = MagicMock()
173 monkeypatch.setattr(debugpy, "wait_for_client", fake_debugpy_wait_for_client)
174
175 # pydevd is dynamically loaded and only available when a real debugger is attached. So
176 # we fake the whole module, as otherwise the import in start_remote_debugger would fail
177 fake_pydevd = MagicMock()
178 monkeypatch.setitem(sys.modules, "pydevd", fake_pydevd)
179 fake_pydevd.DebugInfoHolder.DEBUG_TRACE_LEVEL = 0
180 fake_pydevd_file_utils = MagicMock()
181 fake_pydevd_file_utils.setup_client_server_paths.return_value = None
182 monkeypatch.setitem(sys.modules, "pydevd_file_utils", fake_pydevd_file_utils)
183
184 # start test function
185 briefcase_debugger.start_remote_debugger()
186
187 fake_debugpy_listen.assert_called_once_with(
188 ("somehost", 9999),
189 in_process_debug_adapter=True,
190 )
191
192 fake_debugpy_wait_for_client.assert_called_once()
193 fake_pydevd_file_utils.setup_client_server_paths.assert_not_called()
194
195 captured = capsys.readouterr()
196 assert "Waiting for debugger to attach..." in captured.out
197 assert captured.err == ""