|
| 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