Skip to content

Commit 4a9e8eb

Browse files
committed
refactor(installer): improve import_package_bundle and tests
- Add guard to raise ValueError when both condition_env and priority are specified (mutually exclusive parameters) - Update docstring to document the mutual exclusivity constraint - Refactor tests to use pathlib.Path instead of os.path/os.makedirs - Add test for mutually exclusive parameter validation
1 parent 2d369da commit 4a9e8eb

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

installer/module/virtual_environment.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,17 @@ def import_package_bundle(
9292
:param bundle_site_package_path: Absolute path to the package bundle.
9393
:param condition_env: Optional environment variable name. If provided, the bundle
9494
will only be loaded when this env var is set to 'true'.
95+
Cannot be combined with priority.
9596
:param priority: If True, insert at front of sys.path to override other bundles.
97+
Cannot be combined with condition_env.
98+
:raises ValueError: If both condition_env and priority are specified.
9699
"""
100+
if condition_env and priority:
101+
raise ValueError(
102+
"condition_env and priority are mutually exclusive; "
103+
"specify only one of them"
104+
)
105+
97106
pth_file_path = os.path.join(self._site_packages_path, "deepnote.pth")
98107

99108
with open(pth_file_path, "a+", encoding="utf-8") as pth_file:

tests/unit/test_virtual_environment.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for VirtualEnvironment class."""
22

3-
import os
3+
from pathlib import Path
44
from unittest.mock import patch
55

66
import pytest
@@ -21,7 +21,7 @@ def venv(self, tmp_path):
2121
):
2222
venv = VirtualEnvironment(str(venv_path))
2323
# Create the site-packages directory
24-
os.makedirs(venv.site_packages_path, exist_ok=True)
24+
Path(venv.site_packages_path).mkdir(parents=True, exist_ok=True)
2525
return venv
2626

2727
def test_import_package_bundle_plain_path(self, venv):
@@ -30,9 +30,8 @@ def test_import_package_bundle_plain_path(self, venv):
3030

3131
venv.import_package_bundle(bundle_path)
3232

33-
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
34-
with open(pth_file, "r") as f:
35-
content = f.read()
33+
pth_file = Path(venv.site_packages_path) / "deepnote.pth"
34+
content = pth_file.read_text()
3635

3736
assert content == f"{bundle_path}\n"
3837

@@ -43,9 +42,8 @@ def test_import_package_bundle_with_condition_env(self, venv):
4342

4443
venv.import_package_bundle(bundle_path, condition_env=condition_env)
4544

46-
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
47-
with open(pth_file, "r") as f:
48-
content = f.read()
45+
pth_file = Path(venv.site_packages_path) / "deepnote.pth"
46+
content = pth_file.read_text()
4947

5048
assert "import os, sys" in content
5149
assert f"sys.path.insert(0, '{bundle_path}')" in content
@@ -57,9 +55,8 @@ def test_import_package_bundle_with_priority(self, venv):
5755

5856
venv.import_package_bundle(bundle_path, priority=True)
5957

60-
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
61-
with open(pth_file, "r") as f:
62-
content = f.read()
58+
pth_file = Path(venv.site_packages_path) / "deepnote.pth"
59+
content = pth_file.read_text()
6360

6461
assert "import sys" in content
6562
assert f"sys.path.insert(0, '{bundle_path}')" in content
@@ -77,9 +74,8 @@ def test_import_package_bundle_ordering(self, venv):
7774
venv.import_package_bundle(system_site, priority=True)
7875
venv.import_package_bundle(kernel_libs)
7976

80-
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
81-
with open(pth_file, "r") as f:
82-
lines = f.readlines()
77+
pth_file = Path(venv.site_packages_path) / "deepnote.pth"
78+
lines = pth_file.read_text().splitlines()
8379

8480
assert len(lines) == 3
8581
# Line 1: server-libs conditional
@@ -89,3 +85,16 @@ def test_import_package_bundle_ordering(self, venv):
8985
assert f"sys.path.insert(0, '{system_site}')" in lines[1]
9086
# Line 3: kernel plain path
9187
assert lines[2].strip() == kernel_libs
88+
89+
def test_import_package_bundle_condition_env_and_priority_mutually_exclusive(
90+
self, venv
91+
):
92+
"""Test that condition_env and priority cannot be used together."""
93+
bundle_path = "/some/bundle/site-packages"
94+
95+
with pytest.raises(ValueError, match="mutually exclusive"):
96+
venv.import_package_bundle(
97+
bundle_path,
98+
condition_env="SOME_ENV_VAR",
99+
priority=True,
100+
)

0 commit comments

Comments
 (0)