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
|
import subprocess
import sys
import pytest
from briefcase.exceptions import BriefcaseCommandError
from ....utils import create_file
def create_emulator(root_path):
# Create `emulator` within `root_path`.
if sys.platform == "win32":
emulator_bin = "emulator.exe"
else:
emulator_bin = "emulator"
create_file(root_path / "emulator" / emulator_bin, "The Emulator", chmod=0o755)
def test_succeeds_immediately_if_emulator_installed(mock_tools, android_sdk):
"""`verify_emulator()` exits early if the emulator exists in its root_path."""
# Create `emulator` within `root_path`.
create_emulator(android_sdk.root_path)
# Also create the platforms folder
(android_sdk.root_path / "platforms").mkdir(parents=True)
android_sdk.verify_emulator()
# Platforms folder still exists
assert (android_sdk.root_path / "platforms").exists()
# No extra calls made
mock_tools.subprocess.run.assert_not_called()
mock_tools.file.download.assert_not_called()
def test_creates_platforms_folder(mock_tools, android_sdk):
"""If the platforms folder doesn't exist, it is created."""
# Create `emulator` within `root_path`.
create_emulator(android_sdk.root_path)
# Verify the emulator. This should create the missing "platforms" folder.
android_sdk.verify_emulator()
# Platforms folder now exists
assert (android_sdk.root_path / "platforms").exists()
# No extra calls made
mock_tools.subprocess.run.assert_not_called()
mock_tools.file.download.assert_not_called()
def test_installs_android_emulator(mock_tools, android_sdk):
"""The emulator tools will be installed if needed."""
android_sdk.verify_emulator()
# Platforms folder now exists
assert (android_sdk.root_path / "platforms").exists()
mock_tools.subprocess.run.assert_called_once_with(
[
android_sdk.sdkmanager_path,
"platform-tools",
"emulator",
],
env=android_sdk.env,
check=True,
stream_output=False,
)
def test_partial_android_emulator_install(mock_tools, android_sdk):
"""If the Android emulator is only partially installed, it's not detected."""
# Create the emulator *directory*, but not the actual binary.
(android_sdk.root_path / "emulator").mkdir(parents=True)
android_sdk.verify_emulator()
# Platforms folder now exists
assert (android_sdk.root_path / "platforms").exists()
mock_tools.subprocess.run.assert_called_once_with(
[
android_sdk.sdkmanager_path,
"platform-tools",
"emulator",
],
env=android_sdk.env,
check=True,
stream_output=False,
)
def test_install_problems_are_reported(mock_tools, android_sdk):
"""If the sdkmanager fails to properly install the Android emulator, an exception is
raised."""
# Configure `subprocess` module to crash as though it were a sad sdkmanager.
mock_tools.subprocess.run.side_effect = subprocess.CalledProcessError(
returncode=1,
cmd=["ignored"],
)
with pytest.raises(BriefcaseCommandError):
android_sdk.verify_emulator()
|