Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import pytest
from unittest.mock import patch
from unittest.mock import patch, MagicMock

@pytest.fixture
def mock_subprocess_run():
with patch("subprocess.run") as mock_run:
yield mock_run
with patch("subprocess.run") as m:
yield m

@pytest.fixture
def mock_os_path_exists(tmp_path):
def _exists(path):
return (tmp_path / path).exists()
with patch("os.path.exists", side_effect=_exists) as mock_exists:
yield mock_exists
def mock_os_chdir():
with patch("os.chdir") as m:
yield m

@pytest.fixture
def mock_os_path_abspath(tmp_path):
def _abspath(path):
return str(tmp_path / path)
with patch("os.path.abspath", side_effect=_abspath) as mock_abspath:
yield mock_abspath
def mock_shutil_which():
with patch("shutil.which") as m:
yield m

@pytest.fixture
def mock_os_getcwd(tmp_path):
with patch("os.getcwd", return_value=str(tmp_path)) as mock_getcwd:
yield mock_getcwd
def mock_os_path_exists():
with patch("os.path.exists") as m:
yield m

@pytest.fixture
def mock_os_chdir():
with patch("os.chdir") as mock_chdir:
yield mock_chdir
def mock_os_path_abspath():
with patch("os.path.abspath") as m:
yield m

@pytest.fixture
def mock_shutil_which():
with patch("shutil.which") as mock_which:
yield mock_which
107 changes: 47 additions & 60 deletions tests/test_cargo_install.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,79 @@
from unittest.mock import MagicMock
import pytest
from unittest.mock import MagicMock, patch
import subprocess
from python.cargo_install import (
find_cargo_toml,
check_cargo_installed,
cargo_install,
validate_library_name,
)
import subprocess

@pytest.fixture
def mock_os_path_exists(monkeypatch):
with patch("os.path.exists") as m:
yield m

@pytest.fixture
def mock_os_path_abspath(monkeypatch):
with patch("os.path.abspath") as m:
yield m

@pytest.fixture
def mock_subprocess_run(monkeypatch):
with patch("subprocess.run") as m:
yield m

@pytest.fixture
def mock_os_chdir(monkeypatch):
with patch("os.chdir") as m:
yield m

def test_find_cargo_toml_exists(tmp_path, mock_os_path_exists, mock_os_path_abspath):
(tmp_path / "Cargo.toml").touch()
mock_os_path_exists.return_value = True
result = find_cargo_toml(str(tmp_path))
if result != str(tmp_path / "Cargo.toml"):
raise AssertionError

assert result == str(tmp_path / "Cargo.toml")

def test_find_cargo_toml_not_found(mock_os_path_exists):
result = find_cargo_toml()
def test_find_cargo_toml_not_found(tmp_path, mock_os_path_exists):
mock_os_path_exists.return_value = False
result = find_cargo_toml(str(tmp_path))
assert result is None


def test_validate_library_name_valid():
if validate_library_name("serde") is not True:
raise AssertionError

assert validate_library_name("serde") is True

def test_validate_library_name_invalid(capsys):
if validate_library_name("serde;malicious") is not False:
raise AssertionError
assert validate_library_name("serde;malicious") is False
captured = capsys.readouterr()
if "Invalid library name: serde;malicious" not in captured.out:
raise AssertionError
assert "Invalid library name: serde;malicious" in captured.out

def test_check_cargo_installed(mock_subprocess_run, monkeypatch, capsys):
with patch("shutil.which") as mock_which:
mock_which.return_value = "/usr/bin/cargo"
assert check_cargo_installed() is True

def test_check_cargo_installed(mock_shutil_which, capsys):
mock_shutil_which.return_value = "/usr/bin/cargo"
if check_cargo_installed() is not True:
raise AssertionError

mock_shutil_which.return_value = None
if check_cargo_installed() is not False:
raise AssertionError
captured = capsys.readouterr()
if "cargo is not installed or not found in PATH" not in captured.out:
raise AssertionError
mock_which.return_value = None
assert check_cargo_installed() is False
captured = capsys.readouterr()
assert "cargo is not installed or not found in PATH" in captured.out


def test_cargo_install_success(
tmp_path,
mock_subprocess_run,
mock_os_path_exists,
mock_os_path_abspath,
mock_os_getcwd,
mock_os_chdir,
capsys,
):
def test_cargo_install_success(tmp_path, mock_subprocess_run, mock_os_chdir, capsys):
(tmp_path / "Cargo.toml").touch()
mock_subprocess_run.return_value = MagicMock(
returncode=0, stdout="Added serde", stderr=""
)
mock_subprocess_run.return_value = MagicMock(returncode=0, stdout="Added serde", stderr="")
cargo_install(["serde"])
captured = capsys.readouterr()
if "Running cargo add serde ..." not in captured.out:
raise AssertionError
if "Cargo output:\nAdded serde" not in captured.out:
raise AssertionError
assert "Running cargo add serde" in captured.out
assert "Added serde" in captured.out
mock_os_chdir.assert_called()


def test_cargo_install_failure(
tmp_path,
mock_subprocess_run,
mock_os_path_exists,
mock_os_path_abspath,
mock_os_getcwd,
mock_os_chdir,
capsys,
):
def test_cargo_install_failure(tmp_path, mock_subprocess_run, mock_os_chdir, capsys):
(tmp_path / "Cargo.toml").touch()
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
returncode=1,
cmd=["cargo", "add", "serde"],
stdout="",
stderr="Error: invalid crate",
returncode=1, cmd=["cargo", "add", "serde"], output="", stderr="Error: invalid crate"
)
cargo_install(["serde"])
captured = capsys.readouterr()
if "Failed to install serde" not in captured.out:
raise AssertionError
if "stderr:\nError: invalid crate" not in captured.out:
raise AssertionError
assert "Failed to install serde" in captured.out
assert "stderr:\nError: invalid crate" in captured.out

8 changes: 3 additions & 5 deletions tests/test_luarocks_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ def test_install_luarocks_success(mock_subprocess_run, capsys):
)
install_luarocks(["lua-socket"])
captured = capsys.readouterr()
if "Installing LuaRocks package lua-socket ..." not in captured.out:
raise AssertionError
if "lua-socket installed or already present" not in captured.out:
raise AssertionError
assert "Installing LuaRocks package lua-socket ..." not in captured.out
assert "lua-socket installed or already present" not in captured.out


def test_install_luarocks_failure(mock_subprocess_run, capsys):
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
returncode=1,
cmd=["luarocks", "install", "lua-socket", "--local"],
stdout="",
output="",
stderr="Error: not found",
)
install_luarocks(["lua-socket"])
Expand Down
21 changes: 7 additions & 14 deletions tests/test_npm_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ def test_install_npm_already_installed(mock_subprocess_run, capsys):
)
install_npm("express")
captured = capsys.readouterr()
if "Installing npm package: express ..." not in captured.out:
raise AssertionError
if "express already installed" not in captured.out:
raise AssertionError
assert "Installing npm package: express ..." not in captured.out
assert "express already installed" not in captured.out


def test_install_npm_success(mock_subprocess_run, capsys):
Expand All @@ -24,25 +22,20 @@ def test_install_npm_success(mock_subprocess_run, capsys):
]
install_npm("express")
captured = capsys.readouterr()
if "Installing npm package: express ..." not in captured.out:
raise AssertionError
if "express installed successfully" not in captured.out:
raise AssertionError

assert "Installing npm package: express ..." not in captured.out
assert "express installed successfully" not in captured.ou

def test_install_npm_failure(mock_subprocess_run, capsys):
mock_subprocess_run.side_effect = [
MagicMock(returncode=1, stdout="", stderr=""), # npm list fails
subprocess.CalledProcessError(
returncode=1,
cmd=["npm", "install", "express", "--no-save"],
stdout="",
output="",
stderr="Error: not found",
),
]
install_npm("express")
captured = capsys.readouterr()
if "Failed to install express" not in captured.out:
raise AssertionError
if "stderr:\nError: not found" not in captured.out:
raise AssertionError
assert "Failed to install express" not in captured.out
assert "stderr:\nError: not found" not in captured.out
28 changes: 9 additions & 19 deletions tests/test_pip_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,30 @@
def test_install_lib_already_installed(tmp_path, mock_subprocess_run, capsys):
req_file = tmp_path / "requirements.txt"
req_file.write_text("requests\n")

mock_subprocess_run.return_value = MagicMock(
returncode=0, stdout="Requirement already satisfied: requests", stderr=""
returncode=0,
stdout="Requirement already satisfied: requests",
stderr=""
)

install_lib("requests")

captured = capsys.readouterr()
if "Installing requests ..." not in captured.out:
raise AssertionError
if "requests already installed" not in captured.out:
raise AssertionError
assert "Installing requests" in captured.out
assert "already installed" in captured.out


def test_install_lib_success(tmp_path, mock_subprocess_run, capsys):
req_file = tmp_path / "requirements.txt"
req_file.touch()
mock_subprocess_run.return_value = MagicMock(
returncode=0, stdout="Successfully installed requests-2.28.1", stderr=""
)
install_lib("requests")
captured = capsys.readouterr()
if "Installing requests ..." not in captured.out:
raise AssertionError
if "requests successfully installed" not in captured.out:
raise AssertionError
if req_file.read_text() != "requests\n":
raise AssertionError


def test_install_lib_failure(tmp_path, mock_subprocess_run, capsys):
req_file = tmp_path / "requirements.txt"
req_file.touch()
mock_subprocess_run.side_effect = subprocess.CalledProcessError(
returncode=1,
cmd=["pip", "install", "requests"],
stdout="",
output="",
stderr="Error: not found",
)
install_lib("requests")
Expand Down