Skip to content

Commit 2d369da

Browse files
committed
test(installer): add tests for import_package_bundle priority param
Add unit tests for VirtualEnvironment.import_package_bundle covering: - Plain path appending to .pth file - Conditional import with env var check - Priority flag using sys.path.insert(0, ...) - Ordering of bundles matching __main__.py usage
1 parent cc4498a commit 2d369da

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

installer/module/virtual_environment.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def start_server(self, command: str, cwd: Optional[str] = None) -> ServerProcess
8181
return server_proc
8282

8383
def import_package_bundle(
84-
self, bundle_site_package_path: str,
84+
self,
85+
bundle_site_package_path: str,
8586
condition_env: Optional[str] = None,
8687
priority: bool = False,
8788
) -> None:
@@ -110,7 +111,9 @@ def import_package_bundle(
110111
)
111112
elif priority:
112113
# Insert at front of sys.path for higher priority (overrides other bundles)
113-
pth_content = f"import sys; sys.path.insert(0, '{bundle_site_package_path}')"
114+
pth_content = (
115+
f"import sys; sys.path.insert(0, '{bundle_site_package_path}')"
116+
)
114117
pth_file.write(pth_content + "\n")
115118
logger.info(
116119
"Bundle was imported with priority to the virtual environment."
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""Tests for VirtualEnvironment class."""
2+
3+
import os
4+
from unittest.mock import patch
5+
6+
import pytest
7+
8+
from installer.module.virtual_environment import VirtualEnvironment
9+
10+
11+
class TestImportPackageBundle:
12+
"""Tests for import_package_bundle method."""
13+
14+
@pytest.fixture
15+
def venv(self, tmp_path):
16+
"""Create a VirtualEnvironment with a temporary path."""
17+
venv_path = tmp_path / "venv"
18+
with patch(
19+
"installer.module.virtual_environment.get_current_python_version",
20+
return_value="3.11",
21+
):
22+
venv = VirtualEnvironment(str(venv_path))
23+
# Create the site-packages directory
24+
os.makedirs(venv.site_packages_path, exist_ok=True)
25+
return venv
26+
27+
def test_import_package_bundle_plain_path(self, venv):
28+
"""Test that plain path is appended to .pth file."""
29+
bundle_path = "/some/bundle/site-packages"
30+
31+
venv.import_package_bundle(bundle_path)
32+
33+
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
34+
with open(pth_file, "r") as f:
35+
content = f.read()
36+
37+
assert content == f"{bundle_path}\n"
38+
39+
def test_import_package_bundle_with_condition_env(self, venv):
40+
"""Test that conditional import uses sys.path.insert(0, ...)."""
41+
bundle_path = "/server/libs/site-packages"
42+
condition_env = "DEEPNOTE_INCLUDE_SERVER_PACKAGES"
43+
44+
venv.import_package_bundle(bundle_path, condition_env=condition_env)
45+
46+
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
47+
with open(pth_file, "r") as f:
48+
content = f.read()
49+
50+
assert "import os, sys" in content
51+
assert f"sys.path.insert(0, '{bundle_path}')" in content
52+
assert f"os.environ.get('{condition_env}', '').lower() == 'true'" in content
53+
54+
def test_import_package_bundle_with_priority(self, venv):
55+
"""Test that priority=True uses sys.path.insert(0, ...)."""
56+
bundle_path = "/usr/local/lib/python3.11/site-packages"
57+
58+
venv.import_package_bundle(bundle_path, priority=True)
59+
60+
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
61+
with open(pth_file, "r") as f:
62+
content = f.read()
63+
64+
assert "import sys" in content
65+
assert f"sys.path.insert(0, '{bundle_path}')" in content
66+
assert "os.environ.get" not in content
67+
68+
def test_import_package_bundle_ordering(self, venv):
69+
"""Test .pth file content matches expected order from __main__.py."""
70+
server_libs = "/tmp/python3.11/server-libs/lib/python3.11/site-packages"
71+
system_site = "/usr/local/lib/python3.11/site-packages"
72+
kernel_libs = "/tmp/python3.11/kernel-libs/lib/python3.11/site-packages"
73+
74+
venv.import_package_bundle(
75+
server_libs, condition_env="DEEPNOTE_INCLUDE_SERVER_PACKAGES"
76+
)
77+
venv.import_package_bundle(system_site, priority=True)
78+
venv.import_package_bundle(kernel_libs)
79+
80+
pth_file = os.path.join(venv.site_packages_path, "deepnote.pth")
81+
with open(pth_file, "r") as f:
82+
lines = f.readlines()
83+
84+
assert len(lines) == 3
85+
# Line 1: server-libs conditional
86+
assert "DEEPNOTE_INCLUDE_SERVER_PACKAGES" in lines[0]
87+
assert f"sys.path.insert(0, '{server_libs}')" in lines[0]
88+
# Line 2: system with priority
89+
assert f"sys.path.insert(0, '{system_site}')" in lines[1]
90+
# Line 3: kernel plain path
91+
assert lines[2].strip() == kernel_libs

0 commit comments

Comments
 (0)