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