| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
import briefcase_debugger.debugpy |
| 5 |
import pytest |
| 6 |
from briefcase_debugger.config import ( |
| 7 |
AppPackagesPathMappings, |
| 8 |
AppPathMappings, |
| 9 |
DebuggerConfig, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
def test_mappings_not_existing(): |
| 14 |
"""Complete empty config.""" |
| 15 |
path_mappings = briefcase_debugger.debugpy.load_path_mappings({}, False) |
| 16 |
assert path_mappings == [] |
| 17 |
|
| 18 |
|
| 19 |
def test_mappings_none(monkeypatch): |
| 20 |
"""Config with no mappings set.""" |
| 21 |
config = DebuggerConfig( |
| 22 |
debugger="debugpy", |
| 23 |
host="", |
| 24 |
port=0, |
| 25 |
app_path_mappings=None, |
| 26 |
app_packages_path_mappings=None, |
| 27 |
) |
| 28 |
path_mappings = briefcase_debugger.debugpy.load_path_mappings(config, False) |
| 29 |
assert path_mappings == [] |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize( |
| 33 |
( |
| 34 |
"os_name", |
| 35 |
"app_path_mappings", |
| 36 |
"app_packages_path_mappings", |
| 37 |
"sys_path", |
| 38 |
"expected_path_mappings", |
| 39 |
), |
| 40 |
[ |
| 41 |
# Windows |
| 42 |
pytest.param( |
| 43 |
"nt", |
| 44 |
AppPathMappings( |
| 45 |
device_sys_path_regex="app$", |
| 46 |
device_subfolders=["helloworld"], |
| 47 |
host_folders=["C:\\PROJECT_ROOT\\src\\helloworld"], |
| 48 |
), |
| 49 |
None, |
| 50 |
[ |
| 51 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\python313.zip", |
| 52 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src", |
| 53 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app", |
| 54 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app_packages", |
| 55 |
], |
| 56 |
[ |
| 57 |
( |
| 58 |
"C:\\PROJECT_ROOT\\src\\helloworld", |
| 59 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app\\helloworld", |
| 60 |
), |
| 61 |
], |
| 62 |
id="windows", |
| 63 |
), |
| 64 |
# Windows with `app_packages_path_mappings` (currently not used by briefcase, but principally possible) |
| 65 |
pytest.param( |
| 66 |
"nt", |
| 67 |
AppPathMappings( |
| 68 |
device_sys_path_regex="app$", |
| 69 |
device_subfolders=["helloworld"], |
| 70 |
host_folders=["C:\\PROJECT_ROOT\\src\\helloworld"], |
| 71 |
), |
| 72 |
AppPackagesPathMappings( |
| 73 |
sys_path_regex="app_packages$", |
| 74 |
host_folder="C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app_packages", |
| 75 |
), |
| 76 |
[ |
| 77 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\python313.zip", |
| 78 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src", |
| 79 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app", |
| 80 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app_packages", |
| 81 |
], |
| 82 |
[ |
| 83 |
( |
| 84 |
"C:\\PROJECT_ROOT\\src\\helloworld", |
| 85 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app\\helloworld", |
| 86 |
), |
| 87 |
( |
| 88 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app_packages", |
| 89 |
"C:\\PROJECT_ROOT\\build\\helloworld\\windows\\app\\src\\app_packages", |
| 90 |
), |
| 91 |
], |
| 92 |
id="windows-with-app-packages", |
| 93 |
), |
| 94 |
# macOS |
| 95 |
pytest.param( |
| 96 |
"posix", |
| 97 |
AppPathMappings( |
| 98 |
device_sys_path_regex="app$", |
| 99 |
device_subfolders=["helloworld"], |
| 100 |
host_folders=["/PROJECT_ROOT/src/helloworld"], |
| 101 |
), |
| 102 |
None, |
| 103 |
[ |
| 104 |
"/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Frameworks/Python.framework/Versions/3.13/lib/python3.13", |
| 105 |
"/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Frameworks/Python.framework/Versions/3.13/lib/python3.13/lib-dynload", |
| 106 |
"/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Resources/app", |
| 107 |
"/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages", |
| 108 |
"/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Resources/app_packages", |
| 109 |
], |
| 110 |
[ |
| 111 |
( |
| 112 |
"/PROJECT_ROOT/src/helloworld", |
| 113 |
"/PROJECT_ROOT/build/helloworld/macos/app/Hello World.app/Contents/Resources/app/helloworld", |
| 114 |
) |
| 115 |
], |
| 116 |
id="macos", |
| 117 |
), |
| 118 |
# iOS |
| 119 |
pytest.param( |
| 120 |
"posix", |
| 121 |
AppPathMappings( |
| 122 |
device_sys_path_regex="app$", |
| 123 |
device_subfolders=["helloworld"], |
| 124 |
host_folders=["/PROJECT_ROOT/src/helloworld"], |
| 125 |
), |
| 126 |
AppPackagesPathMappings( |
| 127 |
sys_path_regex="app_packages$", |
| 128 |
host_folder="/APP_PACKAGES_PATH/app_packages.iphonesimulator", |
| 129 |
), |
| 130 |
[ |
| 131 |
"CoreSimulator/Devices/RANDOM_NUMBER/data/Containers/Bundle/Application/RANDOM_NUMBER/Hello World.app/python/lib/python3.13", |
| 132 |
"CoreSimulator/Devices/RANDOM_NUMBER/data/Containers/Bundle/Application/RANDOM_NUMBER/Hello World.app/python/lib/python3.13/lib-dynload", |
| 133 |
"CoreSimulator/Devices/RANDOM_NUMBER/data/Containers/Bundle/Application/RANDOM_NUMBER/Hello World.app/app", |
| 134 |
"CoreSimulator/Devices/RANDOM_NUMBER/data/Containers/Bundle/Application/RANDOM_NUMBER/Hello World.app/python/lib/python3.13/site-packages", |
| 135 |
"CoreSimulator/Devices/RANDOM_NUMBER/data/Containers/Bundle/Application/RANDOM_NUMBER/Hello World.app/app_packages", |
| 136 |
], |
| 137 |
[ |
| 138 |
( |
| 139 |
"/PROJECT_ROOT/src/helloworld", |
| 140 |
"CoreSimulator/Devices/RANDOM_NUMBER/data/Containers/Bundle/Application/RANDOM_NUMBER/Hello World.app/app/helloworld", |
| 141 |
), |
| 142 |
( |
| 143 |
"/APP_PACKAGES_PATH/app_packages.iphonesimulator", |
| 144 |
"CoreSimulator/Devices/RANDOM_NUMBER/data/Containers/Bundle/Application/RANDOM_NUMBER/Hello World.app/app_packages", |
| 145 |
), |
| 146 |
], |
| 147 |
id="ios", |
| 148 |
), |
| 149 |
# Android (with VS Code running on Windows) |
| 150 |
pytest.param( |
| 151 |
"posix", |
| 152 |
AppPathMappings( |
| 153 |
device_sys_path_regex="app$", |
| 154 |
device_subfolders=["helloworld"], |
| 155 |
host_folders=["C:\\PROJECT_ROOT\\src\\helloworld"], |
| 156 |
), |
| 157 |
AppPackagesPathMappings( |
| 158 |
sys_path_regex="requirements$", |
| 159 |
host_folder="C:\\BUNDLE_PATH\\app\\build\\python\\pip\\debug\\common", |
| 160 |
), |
| 161 |
[ |
| 162 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/app", |
| 163 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/requirements", |
| 164 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/stdlib-x86_64", |
| 165 |
"/data/user/0/com.example.helloworld/files/chaquopy/stdlib-common.imy", |
| 166 |
"/data/user/0/com.example.helloworld/files/chaquopy/bootstrap.imy", |
| 167 |
"/data/user/0/com.example.helloworld/files/chaquopy/bootstrap-native/x86_64", |
| 168 |
], |
| 169 |
[ |
| 170 |
( |
| 171 |
"C:\\PROJECT_ROOT\\src\\helloworld", |
| 172 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/app/helloworld", |
| 173 |
), |
| 174 |
( |
| 175 |
"C:\\BUNDLE_PATH\\app\\build\\python\\pip\\debug\\common", |
| 176 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/requirements", |
| 177 |
), |
| 178 |
], |
| 179 |
id="android-on-windows-host", |
| 180 |
), |
| 181 |
# Android (with VS Code running on POSIX system) |
| 182 |
pytest.param( |
| 183 |
"posix", |
| 184 |
AppPathMappings( |
| 185 |
device_sys_path_regex="app$", |
| 186 |
device_subfolders=["helloworld"], |
| 187 |
host_folders=["/PROJECT_ROOT/src/helloworld"], |
| 188 |
), |
| 189 |
AppPackagesPathMappings( |
| 190 |
sys_path_regex="requirements$", |
| 191 |
host_folder="/BUNDLE_PATH/app/build/python/pip/debug/common", |
| 192 |
), |
| 193 |
[ |
| 194 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/app", |
| 195 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/requirements", |
| 196 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/stdlib-x86_64", |
| 197 |
"/data/user/0/com.example.helloworld/files/chaquopy/stdlib-common.imy", |
| 198 |
"/data/user/0/com.example.helloworld/files/chaquopy/bootstrap.imy", |
| 199 |
"/data/user/0/com.example.helloworld/files/chaquopy/bootstrap-native/x86_64", |
| 200 |
], |
| 201 |
[ |
| 202 |
( |
| 203 |
"/PROJECT_ROOT/src/helloworld", |
| 204 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/app/helloworld", |
| 205 |
), |
| 206 |
( |
| 207 |
"/BUNDLE_PATH/app/build/python/pip/debug/common", |
| 208 |
"/data/data/com.example.helloworld/files/chaquopy/AssetFinder/requirements", |
| 209 |
), |
| 210 |
], |
| 211 |
id="android-on-posix-host", |
| 212 |
), |
| 213 |
], |
| 214 |
) |
| 215 |
def test_mappings( |
| 216 |
os_name: str, |
| 217 |
app_path_mappings: AppPathMappings, |
| 218 |
app_packages_path_mappings: AppPackagesPathMappings | None, |
| 219 |
sys_path: list[str], |
| 220 |
expected_path_mappings: list[tuple[str, str]], |
| 221 |
monkeypatch, |
| 222 |
): |
| 223 |
if os.name != os_name: |
| 224 |
pytest.skip(f"Test only runs on {os_name} systems") |
| 225 |
|
| 226 |
config = DebuggerConfig( |
| 227 |
debugger="debugpy", |
| 228 |
host="", |
| 229 |
port=0, |
| 230 |
app_path_mappings=app_path_mappings, |
| 231 |
app_packages_path_mappings=app_packages_path_mappings, |
| 232 |
) |
| 233 |
|
| 234 |
monkeypatch.setattr(sys, "path", sys_path) |
| 235 |
|
| 236 |
path_mappings = briefcase_debugger.debugpy.load_path_mappings(config, False) |
| 237 |
|
| 238 |
assert path_mappings == expected_path_mappings |
| 239 |
|
| 240 |
|
| 241 |
@pytest.mark.parametrize( |
| 242 |
("os_name", "app_path_mappings"), |
| 243 |
[ |
| 244 |
# Windows |
| 245 |
pytest.param( |
| 246 |
"nt", |
| 247 |
AppPathMappings( |
| 248 |
device_sys_path_regex="app$", |
| 249 |
device_subfolders=["helloworld"], |
| 250 |
host_folders=["C:\\PROJECT_ROOT\\src\\helloworld"], |
| 251 |
), |
| 252 |
id="windows", |
| 253 |
), |
| 254 |
# POSIX (macOS/iOS/Android) |
| 255 |
pytest.param( |
| 256 |
"posix", |
| 257 |
AppPathMappings( |
| 258 |
device_sys_path_regex="app$", |
| 259 |
device_subfolders=["helloworld"], |
| 260 |
host_folders=["/PROJECT_ROOT/src/helloworld"], |
| 261 |
), |
| 262 |
id="posix", |
| 263 |
), |
| 264 |
], |
| 265 |
) |
| 266 |
def test_mappings_wrong_sys_path( |
| 267 |
os_name: str, |
| 268 |
app_path_mappings: AppPathMappings, |
| 269 |
monkeypatch, |
| 270 |
): |
| 271 |
"""Path mappings with a wrong sys path set.""" |
| 272 |
if os.name != os_name: |
| 273 |
pytest.skip(f"Test only runs on {os_name} systems") |
| 274 |
|
| 275 |
config = DebuggerConfig( |
| 276 |
debugger="debugpy", |
| 277 |
host="", |
| 278 |
port=0, |
| 279 |
app_path_mappings=app_path_mappings, |
| 280 |
app_packages_path_mappings=None, |
| 281 |
) |
| 282 |
|
| 283 |
sys_path = [] |
| 284 |
monkeypatch.setattr(sys, "path", sys_path) |
| 285 |
|
| 286 |
with pytest.raises(ValueError, match=r"No sys.path entry matches regex"): |
| 287 |
briefcase_debugger.debugpy.load_path_mappings(config, False) |