Skip to content

Commit 0ab14e8

Browse files
authored
Enable clearing environments with editable installs (#31)
Closes #30
2 parents 01aacaf + dc3c759 commit 0ab14e8

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/venv-cli/venv.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ venv::raise() {
3434
return "${_fail}"
3535
}
3636

37+
venv::_check_venv_activated() {
38+
if [ -z "${VIRTUAL_ENV}" ]; then
39+
venv::raise "No virtual environment activated. Please activate the virtual environment first"
40+
return "${_fail}"
41+
fi
42+
return "${_success}"
43+
}
44+
3745
venv::_check_if_help_requested() {
3846
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
3947
return "${_success}"
@@ -258,7 +266,9 @@ venv::install() {
258266

259267
# Clear the environment before running pip install to avoid orphaned packages
260268
# https://github.com/SallingGroup-AI-and-ML/venv-cli/issues/9
261-
venv::clear
269+
if ! venv::clear; then
270+
return "${_fail}"
271+
fi
262272

263273
venv::color_echo "${_green}" "Installing requirements from ${requirements_file}"
264274
if ! pip install --require-virtualenv --use-pep517 -r "${requirements_file}" "$@"; then
@@ -339,9 +349,16 @@ venv::clear() {
339349
return "${_success}"
340350
fi
341351

352+
if ! venv::_check_venv_activated; then
353+
return "${_fail}"
354+
fi
355+
342356
venv::color_echo "${_yellow}" "Removing all packages from virtual environment ..."
343-
pip freeze --require-virtualenv \
344-
| cut -d "@" -f1 \
357+
pip list --format freeze \
358+
--exclude pip \
359+
--exclude setuptools \
360+
--exclude wheel \
361+
| cut -d "=" -f1 \
345362
| xargs --no-run-if-empty pip uninstall --require-virtualenv -y
346363
venv::color_echo "${_green}" "All packages removed!"
347364
}

tests/test_venv_install.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import subprocess
23
from itertools import chain
34
from pathlib import Path
45

@@ -22,6 +23,12 @@ def _check_package_was_installed(requirement: str, installed_line: str) -> None:
2223
assert package_name in installed_line, f"Package {package_name} was not installed succesfully"
2324

2425

26+
def test_venv_install_not_activated(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
27+
with pytest.raises(subprocess.CalledProcessError), monkeypatch.context() as m:
28+
m.delenv("VIRTUAL_ENV", raising=False)
29+
run_command(["venv install"], cwd=tmp_path, activated=False)
30+
31+
2532
@pytest.mark.order(after="test_venv_activate.py::test_venv_activate")
2633
@parametrize_with_cases(argnames=["files", "requirements_base"], cases=CasesVenvInstallRequirementstxt)
2734
@pytest.mark.parametrize("use_file_name", [True, False])

tests/test_venv_internals.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import subprocess
2+
from pathlib import Path
23

34
import pytest
45

56
from tests.helpers import run_command
67

78

9+
def test_check_venv_activated_no_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
10+
with pytest.raises(subprocess.CalledProcessError), monkeypatch.context() as m:
11+
m.delenv("VIRTUAL_ENV", raising=False)
12+
run_command(["venv::_check_venv_activated"], cwd=tmp_path, activated=False)
13+
14+
15+
@pytest.mark.order(after="test_venv_activate.py::test_venv_activate")
16+
def test_check_venv_activated_yes_env(tmp_path: Path):
17+
run_command("venv::_check_venv_activated", cwd=tmp_path, activated=True)
18+
19+
820
@pytest.mark.order("first")
921
@pytest.mark.parametrize(
1022
["filename", "expected"],

0 commit comments

Comments
 (0)