| 1 |
import re |
| 2 |
import shutil |
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
from pathlib import Path |
| 6 |
from unittest import mock |
| 7 |
|
| 8 |
import pytest |
| 9 |
import tomli_w |
| 10 |
|
| 11 |
import briefcase.platforms.windows.app |
| 12 |
from briefcase.exceptions import BriefcaseCommandError |
| 13 |
from briefcase.integrations.rcedit import RCEdit |
| 14 |
from briefcase.integrations.subprocess import Subprocess |
| 15 |
from briefcase.integrations.windows_sdk import WindowsSDK |
| 16 |
from briefcase.platforms.windows.app import WindowsAppBuildCommand |
| 17 |
|
| 18 |
from ....utils import create_file |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture |
| 22 |
def build_command(dummy_console, tmp_path): |
| 23 |
command = WindowsAppBuildCommand( |
| 24 |
console=dummy_console, |
| 25 |
base_path=tmp_path / "base_path", |
| 26 |
data_path=tmp_path / "briefcase", |
| 27 |
) |
| 28 |
command.tools.host_os = "Windows" |
| 29 |
command.tools.host_arch = "AMD64" |
| 30 |
command.tools.subprocess = mock.MagicMock(spec_set=Subprocess) |
| 31 |
command.tools.shutil = mock.MagicMock(spec_set=shutil) |
| 32 |
command.tools.file.download = mock.MagicMock() |
| 33 |
command.tools.rcedit = RCEdit(command.tools) |
| 34 |
return command |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def windows_sdk(build_command, tmp_path): |
| 39 |
return WindowsSDK( |
| 40 |
tools=build_command.tools, |
| 41 |
root_path=tmp_path / "win_sdk", |
| 42 |
version="86.1.1", |
| 43 |
arch="x64", |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def test_verify_without_windows_sdk(build_command, monkeypatch): |
| 48 |
"""Verifying on Windows creates an RCEdit wrapper.""" |
| 49 |
mock_sdk = mock.MagicMock(spec_set=WindowsSDK) |
| 50 |
monkeypatch.setattr(briefcase.platforms.windows.app, "WindowsSDK", mock_sdk) |
| 51 |
mock_sdk.verify.side_effect = BriefcaseCommandError("Windows SDK") |
| 52 |
|
| 53 |
mock_rcedit_verify = mock.MagicMock(wraps=RCEdit.verify) |
| 54 |
monkeypatch.setattr( |
| 55 |
briefcase.platforms.windows.app.RCEdit, |
| 56 |
"verify", |
| 57 |
mock_rcedit_verify, |
| 58 |
) |
| 59 |
|
| 60 |
build_command.verify_tools() |
| 61 |
|
| 62 |
# RCEdit tool was verified |
| 63 |
mock_rcedit_verify.assert_called_once_with(tools=build_command.tools) |
| 64 |
assert isinstance(build_command.tools.rcedit, RCEdit) |
| 65 |
# Windows SDK tool not created |
| 66 |
assert not hasattr(build_command.tools, "windows_sdk") |
| 67 |
|
| 68 |
|
| 69 |
def test_verify_with_windows_sdk(build_command, windows_sdk, monkeypatch): |
| 70 |
"""Verifying on Windows creates an RCEdit and Windows SDK wrapper.""" |
| 71 |
build_command.tools.windows_sdk = windows_sdk |
| 72 |
|
| 73 |
mock_windows_sdk_verify = mock.MagicMock(wraps=WindowsSDK.verify) |
| 74 |
monkeypatch.setattr( |
| 75 |
briefcase.platforms.windows.app.WindowsSDK, |
| 76 |
"verify", |
| 77 |
mock_windows_sdk_verify, |
| 78 |
) |
| 79 |
|
| 80 |
mock_rcedit_verify = mock.MagicMock(wraps=RCEdit.verify) |
| 81 |
monkeypatch.setattr( |
| 82 |
briefcase.platforms.windows.app.RCEdit, |
| 83 |
"verify", |
| 84 |
mock_rcedit_verify, |
| 85 |
) |
| 86 |
|
| 87 |
build_command.verify_tools() |
| 88 |
|
| 89 |
# RCEdit tool was verified |
| 90 |
mock_rcedit_verify.assert_called_once_with(tools=build_command.tools) |
| 91 |
assert isinstance(build_command.tools.rcedit, RCEdit) |
| 92 |
# WindowsSDK tool was verified |
| 93 |
mock_windows_sdk_verify.assert_called_once_with(tools=build_command.tools) |
| 94 |
assert isinstance(build_command.tools.windows_sdk, WindowsSDK) |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") |
| 98 |
@pytest.mark.parametrize("pre_existing", [True, False]) |
| 99 |
@pytest.mark.parametrize("console_app", [True, False]) |
| 100 |
def test_build_app_without_windows_sdk( |
| 101 |
build_command, |
| 102 |
first_app_templated, |
| 103 |
pre_existing, |
| 104 |
console_app, |
| 105 |
tmp_path, |
| 106 |
): |
| 107 |
"""The stub binary will be updated when a Windows app is built.""" |
| 108 |
first_app_templated.console_app = console_app |
| 109 |
|
| 110 |
exec_path = tmp_path / "base_path/build/first-app/windows/app/src" |
| 111 |
if pre_existing: |
| 112 |
# If this is a pre-existing app, the stub has already been renamed |
| 113 |
if console_app: |
| 114 |
(exec_path / "Stub.exe").rename(exec_path / "first-app.exe") |
| 115 |
else: |
| 116 |
(exec_path / "Stub.exe").rename(exec_path / "First App.exe") |
| 117 |
|
| 118 |
build_command.build_app(first_app_templated) |
| 119 |
|
| 120 |
# The stub binary has been renamed |
| 121 |
assert not (exec_path / "Stub.exe").is_file() |
| 122 |
if console_app: |
| 123 |
assert (exec_path / "first-app.exe").is_file() |
| 124 |
else: |
| 125 |
assert (exec_path / "First App.exe").is_file() |
| 126 |
|
| 127 |
# update the app binary resources |
| 128 |
build_command.tools.subprocess.run.assert_called_once_with( |
| 129 |
[ |
| 130 |
tmp_path / "briefcase/tools/rcedit-x64.exe", |
| 131 |
Path("src/first-app.exe") if console_app else Path("src/First App.exe"), |
| 132 |
"--set-version-string", |
| 133 |
"CompanyName", |
| 134 |
"Megacorp", |
| 135 |
"--set-version-string", |
| 136 |
"FileDescription", |
| 137 |
"First App", |
| 138 |
"--set-version-string", |
| 139 |
"FileVersion", |
| 140 |
"0.0.1", |
| 141 |
"--set-version-string", |
| 142 |
"InternalName", |
| 143 |
"first_app", |
| 144 |
"--set-version-string", |
| 145 |
"OriginalFilename", |
| 146 |
"first-app.exe" if console_app else "First App.exe", |
| 147 |
"--set-version-string", |
| 148 |
"ProductName", |
| 149 |
"First App", |
| 150 |
"--set-version-string", |
| 151 |
"ProductVersion", |
| 152 |
"0.0.1", |
| 153 |
"--set-icon", |
| 154 |
"icon.ico", |
| 155 |
], |
| 156 |
check=True, |
| 157 |
cwd=tmp_path / "base_path/build/first-app/windows/app", |
| 158 |
) |
| 159 |
|
| 160 |
|
| 161 |
@pytest.mark.parametrize("console_app", [True, False]) |
| 162 |
def test_build_app_with_windows_sdk( |
| 163 |
build_command, |
| 164 |
windows_sdk, |
| 165 |
first_app_templated, |
| 166 |
console_app, |
| 167 |
tmp_path, |
| 168 |
): |
| 169 |
"""The stub binary will be updated when a Windows app is built.""" |
| 170 |
build_command.tools.windows_sdk = windows_sdk |
| 171 |
first_app_templated.console_app = console_app |
| 172 |
|
| 173 |
build_command.build_app(first_app_templated) |
| 174 |
|
| 175 |
# remove any digital signatures on the app binary |
| 176 |
build_command.tools.subprocess.check_output.assert_called_once_with( |
| 177 |
[ |
| 178 |
tmp_path / "win_sdk/bin/86.1.1/x64/signtool.exe", |
| 179 |
"remove", |
| 180 |
"-s", |
| 181 |
Path("src/first-app.exe") if console_app else Path("src/First App.exe"), |
| 182 |
], |
| 183 |
cwd=tmp_path / "base_path/build/first-app/windows/app", |
| 184 |
quiet=1, |
| 185 |
) |
| 186 |
# update the app binary resources |
| 187 |
build_command.tools.subprocess.run.assert_called_once_with( |
| 188 |
[ |
| 189 |
tmp_path / "briefcase/tools/rcedit-x64.exe", |
| 190 |
Path("src/first-app.exe") if console_app else Path("src/First App.exe"), |
| 191 |
"--set-version-string", |
| 192 |
"CompanyName", |
| 193 |
"Megacorp", |
| 194 |
"--set-version-string", |
| 195 |
"FileDescription", |
| 196 |
"First App", |
| 197 |
"--set-version-string", |
| 198 |
"FileVersion", |
| 199 |
"0.0.1", |
| 200 |
"--set-version-string", |
| 201 |
"InternalName", |
| 202 |
"first_app", |
| 203 |
"--set-version-string", |
| 204 |
"OriginalFilename", |
| 205 |
"first-app.exe" if console_app else "First App.exe", |
| 206 |
"--set-version-string", |
| 207 |
"ProductName", |
| 208 |
"First App", |
| 209 |
"--set-version-string", |
| 210 |
"ProductVersion", |
| 211 |
"0.0.1", |
| 212 |
"--set-icon", |
| 213 |
"icon.ico", |
| 214 |
], |
| 215 |
check=True, |
| 216 |
cwd=tmp_path / "base_path/build/first-app/windows/app", |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def test_build_app_without_any_digital_signatures( |
| 221 |
build_command, |
| 222 |
windows_sdk, |
| 223 |
first_app_templated, |
| 224 |
tmp_path, |
| 225 |
): |
| 226 |
"""If the app binary is not already signed, then attempt to remove signatures fails |
| 227 |
but app build succeeds.""" |
| 228 |
build_command.tools.windows_sdk = windows_sdk |
| 229 |
|
| 230 |
build_command.tools.subprocess.check_output.side_effect = ( |
| 231 |
subprocess.CalledProcessError( |
| 232 |
returncode=1, |
| 233 |
cmd="signtool.exe remove -s app.exe", |
| 234 |
output=""" |
| 235 |
Number of errors: 1 |
| 236 |
SignTool Error: CryptSIPRemoveSignedDataMsg returned error: 0x00000057 |
| 237 |
The parameter is incorrect. |
| 238 |
""", |
| 239 |
) |
| 240 |
) |
| 241 |
|
| 242 |
build_command.build_app(first_app_templated) |
| 243 |
|
| 244 |
# remove any digital signatures on the app binary |
| 245 |
build_command.tools.subprocess.check_output.assert_called_once_with( |
| 246 |
[ |
| 247 |
tmp_path / "win_sdk/bin/86.1.1/x64/signtool.exe", |
| 248 |
"remove", |
| 249 |
"-s", |
| 250 |
Path("src/First App.exe"), |
| 251 |
], |
| 252 |
cwd=tmp_path / "base_path/build/first-app/windows/app", |
| 253 |
quiet=1, |
| 254 |
) |
| 255 |
# update the app binary resources |
| 256 |
build_command.tools.subprocess.run.assert_called_once_with( |
| 257 |
[ |
| 258 |
tmp_path / "briefcase/tools/rcedit-x64.exe", |
| 259 |
Path("src/First App.exe"), |
| 260 |
"--set-version-string", |
| 261 |
"CompanyName", |
| 262 |
"Megacorp", |
| 263 |
"--set-version-string", |
| 264 |
"FileDescription", |
| 265 |
"First App", |
| 266 |
"--set-version-string", |
| 267 |
"FileVersion", |
| 268 |
"0.0.1", |
| 269 |
"--set-version-string", |
| 270 |
"InternalName", |
| 271 |
"first_app", |
| 272 |
"--set-version-string", |
| 273 |
"OriginalFilename", |
| 274 |
"First App.exe", |
| 275 |
"--set-version-string", |
| 276 |
"ProductName", |
| 277 |
"First App", |
| 278 |
"--set-version-string", |
| 279 |
"ProductVersion", |
| 280 |
"0.0.1", |
| 281 |
"--set-icon", |
| 282 |
"icon.ico", |
| 283 |
], |
| 284 |
check=True, |
| 285 |
cwd=tmp_path / "base_path/build/first-app/windows/app", |
| 286 |
) |
| 287 |
|
| 288 |
|
| 289 |
def test_build_app_error_remove_signature( |
| 290 |
build_command, |
| 291 |
windows_sdk, |
| 292 |
first_app_templated, |
| 293 |
tmp_path, |
| 294 |
): |
| 295 |
"""If the attempt to remove any exist digital signatures fails because signtool |
| 296 |
raises an unexpected error, then the build fails.""" |
| 297 |
build_command.tools.windows_sdk = windows_sdk |
| 298 |
|
| 299 |
build_command.tools.subprocess.check_output.side_effect = ( |
| 300 |
subprocess.CalledProcessError( |
| 301 |
returncode=1, |
| 302 |
cmd="signtool.exe remove /s filepath", |
| 303 |
output=""" |
| 304 |
Number of errors: 1 |
| 305 |
Unknown and unexpected error |
| 306 |
""", |
| 307 |
) |
| 308 |
) |
| 309 |
|
| 310 |
error_message = ( |
| 311 |
"Failed to remove any existing digital signatures from the stub app.\n" |
| 312 |
"\n" |
| 313 |
"Recreating the app layout may also help resolve this issue:\n" |
| 314 |
"\n" |
| 315 |
" $ briefcase create windows app\n" |
| 316 |
"\n" |
| 317 |
) |
| 318 |
with pytest.raises(BriefcaseCommandError, match=re.escape(error_message)): |
| 319 |
build_command.build_app(first_app_templated) |
| 320 |
|
| 321 |
# remove any digital signatures on the app binary |
| 322 |
build_command.tools.subprocess.check_output.assert_called_once_with( |
| 323 |
[ |
| 324 |
tmp_path / "win_sdk/bin/86.1.1/x64/signtool.exe", |
| 325 |
"remove", |
| 326 |
"-s", |
| 327 |
Path("src/First App.exe"), |
| 328 |
], |
| 329 |
cwd=tmp_path / "base_path/build/first-app/windows/app", |
| 330 |
quiet=1, |
| 331 |
) |
| 332 |
# update the app binary resources not called |
| 333 |
build_command.tools.subprocess.run.assert_not_called() |
| 334 |
|
| 335 |
|
| 336 |
def test_build_app_failure(build_command, first_app_templated): |
| 337 |
"""If the stub binary cannot be updated, an error is raised.""" |
| 338 |
|
| 339 |
build_command.tools.subprocess.run.side_effect = subprocess.CalledProcessError( |
| 340 |
returncode=1, |
| 341 |
cmd="rcedit-x64.exe", |
| 342 |
) |
| 343 |
|
| 344 |
with pytest.raises( |
| 345 |
BriefcaseCommandError, |
| 346 |
match=r"Unable to update details on stub app for first-app.", |
| 347 |
): |
| 348 |
build_command.build_app(first_app_templated) |
| 349 |
|
| 350 |
|
| 351 |
def test_build_app_with_support_package_update( |
| 352 |
build_command, |
| 353 |
first_app_templated, |
| 354 |
tmp_path, |
| 355 |
windows_sdk, |
| 356 |
capsys, |
| 357 |
): |
| 358 |
"""If a support package update is performed, the user is warned.""" |
| 359 |
|
| 360 |
# To trigger the app package update logic, we need to invoke the full build |
| 361 |
# command, and fake being on a verified Windows install with a generated |
| 362 |
# app. |
| 363 |
build_command.tools.host_os = "Windows" |
| 364 |
build_command.tools.windows_sdk = windows_sdk |
| 365 |
|
| 366 |
# Hard code a support revision so that the download support package is fixed |
| 367 |
first_app_templated.support_revision = "1" |
| 368 |
|
| 369 |
# Fake the existence of some source files. |
| 370 |
create_file( |
| 371 |
tmp_path / "base_path/src/first_app/app.py", |
| 372 |
"print('an app')", |
| 373 |
) |
| 374 |
|
| 375 |
# Populate a briefcase.toml that mirrors a real Windows app |
| 376 |
with (build_command.bundle_path(first_app_templated) / "briefcase.toml").open( |
| 377 |
"wb" |
| 378 |
) as f: |
| 379 |
index = { |
| 380 |
"briefcase": { |
| 381 |
"target_version": "0.3.24", |
| 382 |
}, |
| 383 |
"paths": { |
| 384 |
"app_path": "src/app", |
| 385 |
"app_package_path": "src/app_packages", |
| 386 |
"support_path": "src", |
| 387 |
}, |
| 388 |
} |
| 389 |
tomli_w.dump(index, f) |
| 390 |
|
| 391 |
# Build the app with a support package update |
| 392 |
build_command(first_app_templated, update_support=True) |
| 393 |
|
| 394 |
# update the app binary resources |
| 395 |
build_command.tools.subprocess.run.assert_called_once_with( |
| 396 |
[ |
| 397 |
tmp_path / "briefcase/tools/rcedit-x64.exe", |
| 398 |
Path("src/First App.exe"), |
| 399 |
"--set-version-string", |
| 400 |
"CompanyName", |
| 401 |
"Megacorp", |
| 402 |
"--set-version-string", |
| 403 |
"FileDescription", |
| 404 |
"First App", |
| 405 |
"--set-version-string", |
| 406 |
"FileVersion", |
| 407 |
"0.0.1", |
| 408 |
"--set-version-string", |
| 409 |
"InternalName", |
| 410 |
"first_app", |
| 411 |
"--set-version-string", |
| 412 |
"OriginalFilename", |
| 413 |
"First App.exe", |
| 414 |
"--set-version-string", |
| 415 |
"ProductName", |
| 416 |
"First App", |
| 417 |
"--set-version-string", |
| 418 |
"ProductVersion", |
| 419 |
"0.0.1", |
| 420 |
"--set-icon", |
| 421 |
"icon.ico", |
| 422 |
], |
| 423 |
check=True, |
| 424 |
cwd=tmp_path / "base_path/build/first-app/windows/app", |
| 425 |
) |
| 426 |
|
| 427 |
# No attempt was made to clean up the support package. |
| 428 |
build_command.tools.shutil.rmtree.assert_not_called() |
| 429 |
|
| 430 |
# The user was warned that support package update may not work. |
| 431 |
assert "WARNING: Support package update may be imperfect" in capsys.readouterr().out |