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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
|
import os
import platform
import shutil
import sys
from unittest.mock import MagicMock
import pytest
from briefcase.console import LogLevel
from briefcase.exceptions import (
BriefcaseCommandError,
IncompatibleToolError,
MissingToolError,
NetworkFailure,
UnsupportedHostError,
)
from briefcase.integrations.android_sdk import AndroidSDK
from briefcase.integrations.base import ToolCache
from ..conftest import SDK_MGR_DL_VER, SDK_MGR_VER
SDKMANAGER_FILENAME = (
"sdkmanager.bat" if platform.system() == "Windows" else "sdkmanager"
)
@pytest.fixture
def mock_tools(mock_tools) -> ToolCache:
# Mock the os environment, but copy over other key functions.
mock_tools.os.environ = {}
mock_tools.os.fsdecode = os.fsdecode
mock_tools.os.access = os.access
mock_tools.os.X_OK = os.X_OK
mock_tools.os.unlink = os.unlink
# Identify the host platform
mock_tools._test_download_tag = {
"Windows": "win",
"Darwin": "mac",
"Linux": "linux",
}[mock_tools.host_os]
# Use the original module rmtree implementation
mock_tools.shutil.rmtree = shutil.rmtree
return mock_tools
def mock_unpack(filename, extract_dir, filter=None):
# Create a file that would have been created by unpacking the archive
# This includes the duplicated "cmdline-tools" folder name
(extract_dir / "cmdline-tools/bin").mkdir(parents=True)
(extract_dir / "cmdline-tools/bin/sdkmanager").touch(mode=0o644)
(extract_dir / "cmdline-tools/bin/avdmanager").touch(mode=0o644)
# Include an extra tool that is already executable.
(extract_dir / "cmdline-tools/bin/other").touch(mode=0o755)
def accept_license(android_sdk_root_path):
"""Generate a side effect method that will accept a license."""
def _side_effect(*args, **kwargs):
license_dir = android_sdk_root_path / "licenses"
license_dir.mkdir(parents=True)
(license_dir / "android-sdk-license").touch()
return _side_effect
def test_short_circuit(mock_tools):
"""Tool is not created if already cached."""
mock_tools.android_sdk = "tool"
tool = AndroidSDK.verify(mock_tools)
assert tool == "tool"
assert tool == mock_tools.android_sdk
def test_unsupported_os(mock_tools):
"""When host OS is not supported, an error is raised."""
mock_tools.host_os = "wonky"
with pytest.raises(
UnsupportedHostError,
match=f"{AndroidSDK.name} is not supported on wonky",
):
AndroidSDK.verify(mock_tools)
def test_unsupported_arch(mock_tools):
"""When the architecture is not supported, an error is raised."""
mock_tools.host_arch = "IA-64"
with pytest.raises(
IncompatibleToolError,
match="Briefcase cannot install Android SDK on this machine.",
):
AndroidSDK.verify(mock_tools)
@pytest.mark.parametrize(
"host_os, host_arch",
[
("Darwin", "arm64"),
("Darwin", "x86_64"),
("Linux", "x86_64"),
("Linux", "aarch64"),
("Windows", "AMD64"),
],
)
def test_supported_os_arch(mock_tools, host_os, host_arch, tmp_path):
"""The supported OS/arch combinations do not error."""
mock_tools.host_os = host_os
mock_tools.host_arch = host_arch
# Create `sdkmanager` and the license file.
android_sdk_root_path = tmp_path / "tools/android_sdk"
tools_bin = android_sdk_root_path / f"cmdline-tools/{SDK_MGR_VER}/bin"
tools_bin.mkdir(parents=True, mode=0o755)
if host_os == "Windows":
sdk_manager = tools_bin / "sdkmanager.bat"
sdk_manager.touch()
else:
sdk_manager = tools_bin / "sdkmanager"
sdk_manager.touch(mode=0o755)
# Pre-accept the license
accept_license(android_sdk_root_path)()
assert isinstance(AndroidSDK.verify(mock_tools), AndroidSDK)
def test_succeeds_immediately_in_happy_path(mock_tools, tmp_path):
"""If verify is invoked on a path containing an Android SDK, it does nothing."""
# If `sdkmanager` exists and has the right permissions, and `android-sdk-license`
# exists, verify() should succeed, create no subprocesses, make no requests, and
# return an SDK wrapper.
# Create `sdkmanager` and the license file.
android_sdk_root_path = tmp_path / "tools/android_sdk"
tools_bin = android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(android_sdk_root_path)()
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download, run or unpack anything.
mock_tools.file.download.assert_not_called()
mock_tools.subprocess.run.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
@pytest.mark.parametrize("env_var", ["ANDROID_HOME", "ANDROID_SDK_ROOT"])
def test_user_provided_sdk(mock_tools, env_var, tmp_path, capsys):
"""If the user environment specifies a valid Android SDK, it is used."""
# Increase the log level.
mock_tools.console.verbosity = LogLevel.DEBUG
# Create `sdkmanager` and the license file.
existing_android_sdk_root_path = tmp_path / "other_sdk"
tools_bin = existing_android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(existing_android_sdk_root_path)()
# Set the environment to specify ANDROID_SDK_ROOT
mock_tools.os.environ = {env_var: os.fsdecode(existing_android_sdk_root_path)}
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download or unpack anything.
mock_tools.file.download.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# The returned SDK has the expected root path.
assert sdk.root_path == existing_android_sdk_root_path
# User is warned about using ANDROID_SDK_ROOT
if env_var == "ANDROID_SDK_ROOT":
assert "Using Android SDK from ANDROID_SDK_ROOT" in capsys.readouterr().out
else:
assert capsys.readouterr().out == (
"\n[Android SDK] Evaluating ANDROID_HOME...\n"
f"ANDROID_HOME={tmp_path / 'other_sdk'}\n"
f"Using Android SDK at {tmp_path / 'other_sdk'}\n"
)
def test_consistent_user_provided_sdk(mock_tools, tmp_path, capsys):
"""If the user environment specifies a valid Android SDK in both ANDROID_HOME and
ANDROID_SDK_ROOT, it is used."""
# Increase the log level.
mock_tools.console.verbosity = LogLevel.DEBUG
# Create `sdkmanager` and the license file.
existing_android_sdk_root_path = tmp_path / "other_sdk"
tools_bin = existing_android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(existing_android_sdk_root_path)()
# Set the environment to specify ANDROID_SDK_ROOT
mock_tools.os.environ = {
"ANDROID_HOME": os.fsdecode(existing_android_sdk_root_path),
"ANDROID_SDK_ROOT": os.fsdecode(existing_android_sdk_root_path),
}
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download or unpack anything.
mock_tools.file.download.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# The returned SDK has the expected root path.
assert sdk.root_path == existing_android_sdk_root_path
# User is not warned about configuration
assert capsys.readouterr().out == (
"\n[Android SDK] Evaluating ANDROID_HOME...\n"
f"ANDROID_HOME={tmp_path / 'other_sdk'}\n"
f"Using Android SDK at {tmp_path / 'other_sdk'}\n"
)
def test_inconsistent_user_provided_sdk(mock_tools, tmp_path, capsys):
"""A warning is logged if ANDROID_HOME and ANDROID_SDK_ROOT are not the same."""
# Increase the log level.
mock_tools.console.verbosity = LogLevel.DEBUG
# Create `sdkmanager` and the license file.
existing_android_sdk_root_path = tmp_path / "other_sdk"
tools_bin = existing_android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(existing_android_sdk_root_path)()
# Set the environment to specify ANDROID_HOME and ANDROID_SDK_ROOT
mock_tools.os.environ = {
"ANDROID_HOME": os.fsdecode(existing_android_sdk_root_path),
"ANDROID_SDK_ROOT": "/opt/sdk2",
}
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download or unpack anything.
mock_tools.file.download.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# The returned SDK has the expected root path.
assert sdk.root_path == existing_android_sdk_root_path
# User is warned about using ANDROID_SDK_ROOT
assert (
"ANDROID_HOME and ANDROID_SDK_ROOT are inconsistent" in capsys.readouterr().out
)
@pytest.mark.parametrize("env_var", ["ANDROID_HOME", "ANDROID_SDK_ROOT"])
def test_invalid_user_provided_sdk(mock_tools, env_var, tmp_path, capsys):
"""If the user's environment specifies an invalid Android SDK, it is ignored."""
# Create `sdkmanager` and the license file
# for the *briefcase* managed version of the SDK.
android_sdk_root_path = tmp_path / "tools/android_sdk"
tools_bin = android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(android_sdk_root_path)()
# Set the environment to specify an ANDROID_SDK_ROOT that doesn't exist
mock_tools.os.environ = {env_var: os.fsdecode(tmp_path / "other_sdk")}
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download, run or unpack anything.
mock_tools.file.download.assert_not_called()
mock_tools.subprocess.run.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
# User is informed about invalid env var setting
assert f"{env_var} does not point to an Android SDK" in capsys.readouterr().out
@pytest.mark.parametrize("env_var", ["ANDROID_HOME", "ANDROID_SDK_ROOT"])
def test_user_provided_sdk_wrong_cmdline_tools_ver(
mock_tools,
env_var,
tmp_path,
capsys,
):
"""If the user's environment specifies an Android SDK without the expected cmdline
tools and the cmdline-tools install fails, the Briefcase SDK is used."""
# Create `sdkmanager` and the license file for the *user's* version of the SDK
user_sdk_path = tmp_path / "other_sdk"
user_tools_bin = user_sdk_path / "cmdline-tools/6.0/bin"
user_tools_bin.mkdir(parents=True, mode=0o755)
(user_tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Create `sdkmanager` and the license file
# for the *briefcase* managed version of the SDK.
android_sdk_root_path = tmp_path / "tools/android_sdk"
tools_bin = android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(android_sdk_root_path)()
# Set the environment to specify an ANDROID_SDK_ROOT that doesn't exist
mock_tools.os.environ = {env_var: os.fsdecode(user_sdk_path)}
# Mock `cmdline-tools/latest` directory not existing
mock_tools.subprocess.run.side_effect = OSError
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download and nothing unpacked
mock_tools.file.download.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# Required Command-line Tools installed
mock_tools.subprocess.run.assert_called_once_with(
[
tmp_path
/ "other_sdk"
/ "cmdline-tools"
/ "latest"
/ "bin"
/ SDKMANAGER_FILENAME,
f"cmdline-tools;{SDK_MGR_VER}",
],
check=True,
stream_output=False,
)
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
# User is informed about failed install and invalid SDK
output = capsys.readouterr().out
assert f"Failed to install cmdline-tools;{SDK_MGR_VER}" in output
assert "Incompatible Command-Line Tools Version" in output
@pytest.mark.parametrize("env_var", ["ANDROID_HOME", "ANDROID_SDK_ROOT"])
def test_user_provided_sdk_with_latest_cmdline_tools(
mock_tools,
env_var,
tmp_path,
capsys,
):
"""If the user's environment specifies an Android SDK without the expected cmdline
tools, the required cmdline-tools is installed in to it."""
# Create `sdkmanager` and the license file for the *user's* version of the SDK
user_sdk_path = tmp_path / "other_sdk"
user_tools_bin = user_sdk_path / "cmdline-tools/latest/bin"
user_tools_bin.mkdir(parents=True, mode=0o755)
(user_tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(user_sdk_path)()
# Set the environment to specify an ANDROID_SDK_ROOT that doesn't exist
mock_tools.os.environ = {env_var: os.fsdecode(user_sdk_path)}
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download and nothing unpacked
mock_tools.file.download.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# Required Command-line Tools installed
mock_tools.subprocess.run.assert_called_once_with(
[
tmp_path
/ "other_sdk"
/ "cmdline-tools"
/ "latest"
/ "bin"
/ SDKMANAGER_FILENAME,
f"cmdline-tools;{SDK_MGR_VER}",
],
check=True,
stream_output=False,
)
# The returned SDK has the expected root path.
assert sdk.root_path == user_sdk_path
def test_consistent_invalid_user_provided_sdk(mock_tools, tmp_path, capsys):
"""If the user's environment specifies an invalid Android SDK in both ANDROID_HOME
and ANDROID_SDK_ROOT, they are ignored."""
# Create `sdkmanager` and the license file
# for the *briefcase* managed version of the SDK.
android_sdk_root_path = tmp_path / "tools/android_sdk"
tools_bin = android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(android_sdk_root_path)()
# Set the environment to specify an ANDROID_SDK_ROOT that doesn't exist
mock_tools.os.environ = {
"ANDROID_HOME": os.fsdecode(tmp_path / "other_sdk"),
"ANDROID_SDK_ROOT": os.fsdecode(tmp_path / "other_sdk"),
}
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download, run or unpack anything.
mock_tools.file.download.assert_not_called()
mock_tools.subprocess.run.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
# User is informed about invalid env var setting
assert "ANDROID_HOME does not point to an Android SDK" in capsys.readouterr().out
def test_inconsistent_invalid_user_provided_sdk(mock_tools, tmp_path, capsys):
"""If the user's environment specifies an invalid Android SDK in both ANDROID_HOME
and ANDROID_SDK_ROOT...and they are both different, they are ignored."""
# Create `sdkmanager` and the license file
# for the *briefcase* managed version of the SDK.
android_sdk_root_path = tmp_path / "tools/android_sdk"
tools_bin = android_sdk_root_path / "cmdline-tools" / SDK_MGR_VER / "bin"
tools_bin.mkdir(parents=True, mode=0o755)
(tools_bin / SDKMANAGER_FILENAME).touch(mode=0o755)
# Pre-accept the license
accept_license(android_sdk_root_path)()
# Set the environment to specify an ANDROID_SDK_ROOT that doesn't exist
mock_tools.os.environ = {
"ANDROID_HOME": os.fsdecode(tmp_path / "other_sdk1"),
"ANDROID_SDK_ROOT": os.fsdecode(tmp_path / "other_sdk2"),
}
# Expect verify() to succeed
sdk = AndroidSDK.verify(mock_tools)
# No calls to download, run or unpack anything.
mock_tools.file.download.assert_not_called()
mock_tools.subprocess.run.assert_not_called()
mock_tools.shutil.unpack_archive.assert_not_called()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
# User is informed about invalid env var setting
output = capsys.readouterr().out
assert "ANDROID_HOME and ANDROID_SDK_ROOT are inconsistent" in output
assert "ANDROID_HOME does not point to an Android SDK" in output
def test_download_sdk(mock_tools, tmp_path, capsys):
"""If an SDK is not available, one will be downloaded."""
android_sdk_root_path = tmp_path / "tools/android_sdk"
cmdline_tools_base_path = android_sdk_root_path / "cmdline-tools"
# The download will produce a cached file.
cache_file = MagicMock()
mock_tools.file.download = MagicMock(return_value=cache_file)
# Calling unpack will create files
mock_tools.shutil.unpack_archive.side_effect = mock_unpack
# Set up a side effect for accepting the license
mock_tools.subprocess.run.side_effect = accept_license(android_sdk_root_path)
# Call `verify()`
sdk = AndroidSDK.verify(mock_tools)
# Validate that the SDK was downloaded and unpacked
url = (
"https://dl.google.com/android/repository/"
f"commandlinetools-{mock_tools._test_download_tag}-{SDK_MGR_DL_VER}_latest.zip"
)
mock_tools.file.download.assert_called_once_with(
url=url,
download_path=mock_tools.base_path,
role="Android SDK Command-Line Tools",
)
mock_tools.shutil.unpack_archive.assert_called_once_with(
filename=cache_file,
extract_dir=cmdline_tools_base_path,
**({"filter": "data"} if sys.version_info >= (3, 12) else {}),
)
# The cached file will be deleted
cache_file.unlink.assert_called_once_with()
# The commandline tools path exists
assert sdk.cmdline_tools_path.is_dir()
if platform.system() != "Windows":
# On non-Windows, ensure the unpacked binary was made executable
assert os.access(
cmdline_tools_base_path / SDK_MGR_VER / "bin/sdkmanager",
os.X_OK,
)
# The license has been accepted
assert (android_sdk_root_path / "licenses/android-sdk-license").exists()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
assert "The Android SDK was not found" in capsys.readouterr().out
def test_upgrade_existing_sdk(mock_tools, tmp_path, capsys):
"""An existing SDK is upgraded if the required version of cmdline-tools isn't
installed."""
android_sdk_root_path = tmp_path / "tools/android_sdk"
cmdline_tools_base_path = android_sdk_root_path / "cmdline-tools"
# Mock an existing cmdline-tools install
(cmdline_tools_base_path / "latest").mkdir(parents=True)
(cmdline_tools_base_path / "8092744").touch()
# The download will produce a cached file.
cache_file = MagicMock()
mock_tools.file.download = MagicMock(return_value=cache_file)
# Calling unpack will create files
mock_tools.shutil.unpack_archive.side_effect = mock_unpack
# Set up a side effect for accepting the license
mock_tools.subprocess.run.side_effect = accept_license(android_sdk_root_path)
# Call `verify()`
sdk = AndroidSDK.verify(mock_tools)
# Validate that the SDK was downloaded and unpacked
url = (
"https://dl.google.com/android/repository/"
f"commandlinetools-{mock_tools._test_download_tag}-{SDK_MGR_DL_VER}_latest.zip"
)
mock_tools.file.download.assert_called_once_with(
url=url,
download_path=mock_tools.base_path,
role="Android SDK Command-Line Tools",
)
mock_tools.shutil.unpack_archive.assert_called_once_with(
filename=cache_file,
extract_dir=cmdline_tools_base_path,
**({"filter": "data"} if sys.version_info >= (3, 12) else {}),
)
# The cached file will be deleted
cache_file.unlink.assert_called_once_with()
# The commandline tools path exists
assert sdk.cmdline_tools_path.is_dir()
if platform.system() != "Windows":
# On non-Windows, ensure the unpacked binary was made executable
assert os.access(
cmdline_tools_base_path / SDK_MGR_VER / "bin/sdkmanager",
os.X_OK,
)
# The license has been accepted
assert (android_sdk_root_path / "licenses/android-sdk-license").exists()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
# Ensure old cmdline-tools installs are cleaned up
assert not (cmdline_tools_base_path / "latest").exists()
assert not (cmdline_tools_base_path / "8092744").exists()
assert "Upgrading Android SDK..." in capsys.readouterr().out
def test_download_sdk_legacy_install(mock_tools, tmp_path):
"""If the legacy SDK tools are present, they will be deleted."""
android_sdk_root_path = tmp_path / "tools/android_sdk"
cmdline_tools_base_path = android_sdk_root_path / "cmdline-tools"
# Create files that mock the existence of the *old* SDK tools.
sdk_tools_base_path = android_sdk_root_path / "tools"
(sdk_tools_base_path / "bin").mkdir(parents=True)
(sdk_tools_base_path / "bin/sdkmanager").touch(mode=0o755)
(sdk_tools_base_path / "bin/avdmanager").touch(mode=0o755)
# Create some of the tools that have locations that overlap
# between legacy and new.
emulator_path = android_sdk_root_path / "emulator"
emulator_path.mkdir(parents=True)
(emulator_path / "emulator").touch(mode=0o755)
# The download will produce a cached file.
cache_file = MagicMock()
mock_tools.file.download = MagicMock(return_value=cache_file)
# Calling unpack will create files
mock_tools.shutil.unpack_archive.side_effect = mock_unpack
# Set up a side effect for accepting the license
mock_tools.subprocess.run.side_effect = accept_license(android_sdk_root_path)
# Call `verify()`
sdk = AndroidSDK.verify(mock_tools)
# Validate that the SDK was downloaded and unpacked
url = (
"https://dl.google.com/android/repository/"
f"commandlinetools-{mock_tools._test_download_tag}-{SDK_MGR_DL_VER}_latest.zip"
)
mock_tools.file.download.assert_called_once_with(
url=url,
download_path=mock_tools.base_path,
role="Android SDK Command-Line Tools",
)
mock_tools.shutil.unpack_archive.assert_called_once_with(
filename=cache_file,
extract_dir=cmdline_tools_base_path,
**({"filter": "data"} if sys.version_info >= (3, 12) else {}),
)
# The cached file will be deleted
cache_file.unlink.assert_called_once_with()
# The commandline tools path exists, in both "latest" and versioned form
assert sdk.cmdline_tools_path.is_dir()
if platform.system() != "Windows":
# On non-Windows, ensure the unpacked binary was made executable
assert os.access(
cmdline_tools_base_path / SDK_MGR_VER / "bin/sdkmanager", os.X_OK
)
# The legacy SDK tools have been removed
assert not sdk_tools_base_path.exists()
assert not emulator_path.exists()
# The license has been accepted
assert (android_sdk_root_path / "licenses/android-sdk-license").exists()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
def test_no_install(mock_tools, tmp_path):
"""If an SDK is not available, and install is not requested, an error is raised."""
# Call `verify()`
with pytest.raises(MissingToolError):
AndroidSDK.verify(mock_tools, install=False)
assert mock_tools.file.download.call_count == 0
def test_download_sdk_if_sdkmanager_not_executable(mock_tools, tmp_path):
"""An SDK will be downloaded and unpacked if `tools/bin/sdkmanager` exists but does
not have its permissions set properly."""
android_sdk_root_path = tmp_path / "tools/android_sdk"
cmdline_tools_base_path = android_sdk_root_path / "cmdline-tools"
# Create pre-existing non-executable `sdkmanager`.
(cmdline_tools_base_path / SDK_MGR_VER / "bin").mkdir(parents=True)
(cmdline_tools_base_path / SDK_MGR_VER / "bin/sdkmanager").touch(mode=0o644)
(cmdline_tools_base_path / SDK_MGR_DL_VER).touch()
# The download will produce a cached file
cache_file = MagicMock()
mock_tools.file.download = MagicMock(return_value=cache_file)
# Calling unpack will create files
mock_tools.shutil.unpack_archive.side_effect = mock_unpack
# Set up a side effect for accepting the license
mock_tools.subprocess.run.side_effect = accept_license(android_sdk_root_path)
# Call `verify()`
sdk = AndroidSDK.verify(mock_tools)
# Validate that the SDK was downloaded and unpacked
url = (
"https://dl.google.com/android/repository/"
f"commandlinetools-{mock_tools._test_download_tag}-{SDK_MGR_DL_VER}_latest.zip"
)
mock_tools.file.download.assert_called_once_with(
url=url,
download_path=mock_tools.base_path,
role="Android SDK Command-Line Tools",
)
mock_tools.shutil.unpack_archive.assert_called_once_with(
filename=cache_file,
extract_dir=cmdline_tools_base_path,
**({"filter": "data"} if sys.version_info >= (3, 12) else {}),
)
# The cached file will be deleted
cache_file.unlink.assert_called_once_with()
# The license has been accepted
assert (android_sdk_root_path / "licenses/android-sdk-license").exists()
# The returned SDK has the expected root path.
assert sdk.root_path == android_sdk_root_path
def test_raises_networkfailure_on_connectionerror(mock_tools):
"""If an error occurs downloading the ZIP file, and error is raised."""
mock_tools.file.download = MagicMock(side_effect=NetworkFailure("mock"))
with pytest.raises(NetworkFailure, match="Unable to mock"):
AndroidSDK.verify(mock_tools)
# The download was attempted
url = (
"https://dl.google.com/android/repository/"
f"commandlinetools-{mock_tools._test_download_tag}-{SDK_MGR_DL_VER}_latest.zip"
)
mock_tools.file.download.assert_called_once_with(
url=url,
download_path=mock_tools.base_path,
role="Android SDK Command-Line Tools",
)
# But no unpack occurred
assert mock_tools.shutil.unpack_archive.call_count == 0
def test_detects_bad_zipfile(mock_tools, tmp_path):
"""If the ZIP file is corrupted, an error is raised."""
android_sdk_root_path = tmp_path / "tools/android_sdk"
cache_file = MagicMock()
mock_tools.file.download = MagicMock(return_value=cache_file)
# But the unpack will fail.
mock_tools.shutil.unpack_archive.side_effect = shutil.ReadError
with pytest.raises(BriefcaseCommandError):
AndroidSDK.verify(mock_tools)
# The download attempt was made.
url = (
"https://dl.google.com/android/repository/"
f"commandlinetools-{mock_tools._test_download_tag}-{SDK_MGR_DL_VER}_latest.zip"
)
mock_tools.file.download.assert_called_once_with(
url=url,
download_path=mock_tools.base_path,
role="Android SDK Command-Line Tools",
)
mock_tools.shutil.unpack_archive.assert_called_once_with(
filename=cache_file,
extract_dir=android_sdk_root_path / "cmdline-tools",
**({"filter": "data"} if sys.version_info >= (3, 12) else {}),
)
|