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
|
import re
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from briefcase.exceptions import (
BriefcaseCommandError,
)
from briefcase.integrations.java import JDK
@pytest.mark.parametrize(
"host_os, java_home",
[
("Linux", Path("tools", "java17")),
("Windows", Path("tools", "java17")),
("Darwin", Path("tools", "java17", "Contents", "Home")),
],
)
def test_java_running(mock_tools, host_os, java_home, tmp_path):
"""If Java is running, uninstall will raise an error message."""
# Mock os
mock_tools.host_os = host_os
# Create a mock of a previously installed Java version.
(tmp_path / java_home / "bin").mkdir(parents=True)
# Create an JDK wrapper
jdk = JDK(mock_tools, java_home=tmp_path / java_home)
# Mock shutil.rmtree so that it gives a permission error
mock_tools.shutil.rmtree.side_effect = PermissionError(
"File is being used by other processes"
)
# Set correct path according to each OS
if host_os == "Darwin":
expected_loc = (tmp_path / java_home).parent.parent
else:
expected_loc = tmp_path / java_home
# Attempting to uninstall JDK which will fail due to Permission Error
with pytest.raises(
BriefcaseCommandError,
match=re.escape(f"Permission denied when trying to remove {expected_loc!r}"),
):
jdk.uninstall()
@pytest.mark.parametrize(
"host_os, java_home",
[
("Linux", Path("tools", "java17")),
("Windows", Path("tools", "java17")),
("Darwin", Path("tools", "java17", "Contents", "Home")),
],
)
def test_java_not_running(mock_tools, host_os, java_home, tmp_path):
"""If Java is not running, uninstalling should proceed smoothly."""
# Mock os
mock_tools.host_os = host_os
# Create a mock of a previously installed Java version.
(tmp_path / java_home / "bin").mkdir(parents=True)
# Create an JDK wrapper
jdk = JDK(mock_tools, java_home=tmp_path / java_home)
# Set correct path according to each OS
if host_os == "Darwin":
expected_loc = (tmp_path / java_home).parent.parent
else:
expected_loc = tmp_path / java_home
# Mock so that shutil behave normally
mock_tools.shutil.rmtree = MagicMock()
# Uninstall JDK
jdk.uninstall()
# Verify that shutil works normally and that correct location is passed
mock_tools.shutil.rmtree.assert_called_once_with(expected_loc)
|