| 1 |
import subprocess |
| 2 |
from pathlib import Path |
| 3 |
from unittest import mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import briefcase.platforms.windows.visualstudio |
| 8 |
from briefcase.console import LogLevel |
| 9 |
from briefcase.exceptions import BriefcaseCommandError |
| 10 |
from briefcase.integrations.subprocess import Subprocess |
| 11 |
from briefcase.integrations.visualstudio import VisualStudio |
| 12 |
from briefcase.platforms.windows.visualstudio import WindowsVisualStudioBuildCommand |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture |
| 16 |
def build_command(dummy_console, tmp_path): |
| 17 |
command = WindowsVisualStudioBuildCommand( |
| 18 |
console=dummy_console, |
| 19 |
base_path=tmp_path / "base_path", |
| 20 |
data_path=tmp_path / "briefcase", |
| 21 |
) |
| 22 |
command.tools.subprocess = mock.MagicMock(spec_set=Subprocess) |
| 23 |
command.tools.visualstudio = VisualStudio( |
| 24 |
tools=command.tools, |
| 25 |
msbuild_path=tmp_path / "Visual Studio/MSBuild.exe", |
| 26 |
) |
| 27 |
return command |
| 28 |
|
| 29 |
|
| 30 |
def test_verify(build_command, monkeypatch): |
| 31 |
"""Verifying on Windows creates a VisualStudio wrapper.""" |
| 32 |
build_command.tools.host_os = "Windows" |
| 33 |
|
| 34 |
mock_visualstudio_verify = mock.MagicMock(wraps=VisualStudio.verify) |
| 35 |
monkeypatch.setattr( |
| 36 |
briefcase.platforms.windows.visualstudio.VisualStudio, |
| 37 |
"verify", |
| 38 |
mock_visualstudio_verify, |
| 39 |
) |
| 40 |
|
| 41 |
build_command.verify_tools() |
| 42 |
|
| 43 |
# VisualStudio tool was verified |
| 44 |
mock_visualstudio_verify.assert_called_once_with(tools=build_command.tools) |
| 45 |
assert isinstance(build_command.tools.visualstudio, VisualStudio) |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.parametrize("tool_debug_mode", [True, False]) |
| 49 |
def test_build_app(build_command, first_app_config, tool_debug_mode, tmp_path): |
| 50 |
"""The solution will be compiled when the project is built.""" |
| 51 |
# Enable verbose tool logging |
| 52 |
if tool_debug_mode: |
| 53 |
build_command.tools.console.verbosity = LogLevel.DEEP_DEBUG |
| 54 |
|
| 55 |
build_command.build_app(first_app_config) |
| 56 |
|
| 57 |
build_command.tools.subprocess.run.assert_has_calls( |
| 58 |
[ |
| 59 |
# Collect manifest |
| 60 |
mock.call( |
| 61 |
[ |
| 62 |
Path(tmp_path) / "Visual Studio/MSBuild.exe", |
| 63 |
"First App.sln", |
| 64 |
"-target:restore", |
| 65 |
"-property:RestorePackagesConfig=true", |
| 66 |
"-target:First App", |
| 67 |
"-property:Configuration=Release", |
| 68 |
"-verbosity:detailed" if tool_debug_mode else "-verbosity:normal", |
| 69 |
], |
| 70 |
check=True, |
| 71 |
cwd=tmp_path |
| 72 |
/ "base_path" |
| 73 |
/ "build" |
| 74 |
/ "first-app" |
| 75 |
/ "windows" |
| 76 |
/ "visualstudio", |
| 77 |
), |
| 78 |
] |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
def test_build_app_failure(build_command, first_app_config, tmp_path): |
| 83 |
"""If the stub binary cannot be updated, an error is raised.""" |
| 84 |
|
| 85 |
build_command.tools.subprocess.run.side_effect = subprocess.CalledProcessError( |
| 86 |
returncode=1, |
| 87 |
cmd="MSBuild.exe", |
| 88 |
) |
| 89 |
|
| 90 |
with pytest.raises( |
| 91 |
BriefcaseCommandError, |
| 92 |
match=r"Unable to build solution for first-app.", |
| 93 |
): |
| 94 |
build_command.build_app(first_app_config) |