Public Access
111 lines
4.0 KiB
Python
111 lines
4.0 KiB
Python
from __future__ import annotations
|
|
|
|
import platform
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from click import ClickException
|
|
|
|
import devx.tools.install_checkmake as install_checkmake
|
|
from devx.tools._shared import arch_string
|
|
|
|
|
|
class TestArchString:
|
|
def test_amd64(self) -> None:
|
|
with patch.object(platform, "machine", return_value="x86_64"):
|
|
assert arch_string() == "amd64"
|
|
|
|
def test_arm64(self) -> None:
|
|
with patch.object(platform, "machine", return_value="aarch64"):
|
|
assert arch_string() == "arm64"
|
|
|
|
def test_unsupported(self) -> None:
|
|
with patch.object(platform, "machine", return_value="riscv64"):
|
|
with pytest.raises(ClickException):
|
|
arch_string()
|
|
|
|
|
|
class TestInstallWithGo:
|
|
def test_no_go(self) -> None:
|
|
with patch("shutil.which", return_value=None):
|
|
assert install_checkmake._install_with_go() is False
|
|
|
|
def test_with_go(self) -> None:
|
|
with patch("shutil.which", return_value="/usr/bin/go"):
|
|
with patch("subprocess.run") as mock_run:
|
|
assert install_checkmake._install_with_go() is True
|
|
mock_run.assert_called_once_with(
|
|
[
|
|
"/usr/bin/go",
|
|
"install",
|
|
"github.com/checkmake/checkmake/cmd/checkmake@latest",
|
|
],
|
|
check=True,
|
|
)
|
|
|
|
|
|
class TestDownloadBinary:
|
|
def test_download(self, tmp_path: Path) -> None:
|
|
target = tmp_path / "checkmake"
|
|
|
|
def _write_file(url: str, path: str) -> tuple[str, None]:
|
|
Path(path).write_bytes(b"binary")
|
|
return path, None
|
|
|
|
with patch.object(install_checkmake, "TARGET_PATH", target):
|
|
with patch.object(platform, "machine", return_value="x86_64"):
|
|
with patch("urllib.request.urlretrieve", side_effect=_write_file) as mock_retrieve:
|
|
install_checkmake._download_binary()
|
|
mock_retrieve.assert_called_once()
|
|
assert target.exists()
|
|
assert target.stat().st_mode & 0o111
|
|
|
|
|
|
class TestMain:
|
|
def test_already_installed(self) -> None:
|
|
from click.testing import CliRunner
|
|
|
|
with (
|
|
patch("shutil.which", return_value="/usr/bin/checkmake"),
|
|
patch("devx.tools.install_checkmake._install_with_go"),
|
|
):
|
|
runner = CliRunner()
|
|
runner.invoke(install_checkmake.cli, [])
|
|
|
|
def test_install_with_go(self) -> None:
|
|
from click.testing import CliRunner
|
|
|
|
with patch("shutil.which", side_effect=[None, "/usr/bin/go"]):
|
|
with patch("subprocess.run") as mock_run:
|
|
runner = CliRunner()
|
|
runner.invoke(install_checkmake.cli, [])
|
|
mock_run.assert_called_once_with(
|
|
[
|
|
"/usr/bin/go",
|
|
"install",
|
|
"github.com/checkmake/checkmake/cmd/checkmake@latest",
|
|
],
|
|
check=True,
|
|
)
|
|
|
|
def test_download_when_no_go(self, tmp_path: Path) -> None:
|
|
from click.testing import CliRunner
|
|
|
|
target = tmp_path / "checkmake"
|
|
|
|
def _write_file(url: str, path: str) -> tuple[str, None]:
|
|
Path(path).write_bytes(b"binary")
|
|
return path, None
|
|
|
|
with patch.object(install_checkmake, "TARGET_PATH", target):
|
|
with patch("shutil.which", side_effect=[None, None]):
|
|
with patch.object(platform, "machine", return_value="x86_64"):
|
|
with (
|
|
patch("urllib.request.urlretrieve", side_effect=_write_file) as mock_retrieve,
|
|
patch("devx.tools.install_checkmake._install_with_go", return_value=False),
|
|
):
|
|
runner = CliRunner()
|
|
runner.invoke(install_checkmake.cli, [])
|
|
mock_retrieve.assert_called_once()
|