Skip to content
6 changes: 5 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import pytest
from unittest.mock import patch


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


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


@pytest.fixture
def mock_shutil_which():
with patch("shutil.which") as m:
yield m


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


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

18 changes: 8 additions & 10 deletions tests/test_pip_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import subprocess
from python.pip_install import install_lib


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")
Expand All @@ -18,18 +17,17 @@ def test_install_lib_already_installed(tmp_path, mock_subprocess_run, capsys):
assert "Installing requests ..." in captured.out
assert "requests 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()
assert "📦 Installing requests ..." in captured.out
assert "✅ requests successfully installed" in captured.out or "already installed" in captured.out
assert (
"✅ requests successfully installed" in captured.out
or "already installed" in captured.out
)
assert req_file.read_text() == "requests\n"


def test_install_lib_failure(tmp_path, mock_subprocess_run, capsys):
req_file = tmp_path / "requirements.txt"
req_file.touch()
Expand Down