/
/
/
1"""Tests for the working-tree provider aliasing performed in conftest."""
2
3from __future__ import annotations
4
5import sys
6import types
7from pathlib import Path
8
9import pytest
10
11from .conftest import _PROVIDER_PKG, _alias_working_tree_provider
12
13
14def test_aliasing_is_noop_without_provider_working_tree(tmp_path: Path) -> None:
15 """
16 Skip aliasing when the ``provider/`` working tree does not exist.
17
18 That is the upstream repo layout: the aliasing must silently no-op there
19 instead of crashing test collection.
20 """
21 before = sys.modules.get(_PROVIDER_PKG)
22
23 _alias_working_tree_provider(tmp_path / "provider")
24
25 assert sys.modules.get(_PROVIDER_PKG) is before
26
27
28def test_aliasing_rejects_preimported_snapshot(
29 tmp_path: Path, monkeypatch: pytest.MonkeyPatch
30) -> None:
31 """Fail loudly when the provider was already imported from another location."""
32 provider_dir = tmp_path / "provider"
33 provider_dir.mkdir()
34 (provider_dir / "__init__.py").touch()
35 snapshot = types.ModuleType(_PROVIDER_PKG)
36 snapshot.__file__ = str(tmp_path / "venv_snapshot" / "__init__.py")
37 monkeypatch.setitem(sys.modules, _PROVIDER_PKG, snapshot)
38
39 with pytest.raises(RuntimeError, match="already imported"):
40 _alias_working_tree_provider(provider_dir)
41
42
43def test_aliasing_accepts_symlinked_working_tree(
44 tmp_path: Path, monkeypatch: pytest.MonkeyPatch
45) -> None:
46 """Treat a symlink to the already-imported working tree as the same location."""
47 real_dir = tmp_path / "real_provider"
48 real_dir.mkdir()
49 (real_dir / "__init__.py").touch()
50 link_dir = tmp_path / "provider"
51 link_dir.symlink_to(real_dir, target_is_directory=True)
52 already_imported = types.ModuleType(_PROVIDER_PKG)
53 already_imported.__file__ = str(real_dir / "__init__.py")
54 monkeypatch.setitem(sys.modules, _PROVIDER_PKG, already_imported)
55
56 _alias_working_tree_provider(link_dir)
57
58 assert sys.modules[_PROVIDER_PKG] is already_imported
59
60
61def test_aliasing_unregisters_module_when_exec_fails(
62 tmp_path: Path, monkeypatch: pytest.MonkeyPatch
63) -> None:
64 """Leave no half-initialized module behind when the package fails to execute."""
65 provider_dir = tmp_path / "provider"
66 provider_dir.mkdir()
67 (provider_dir / "__init__.py").write_text("raise ValueError('boom')\n")
68 monkeypatch.delitem(sys.modules, _PROVIDER_PKG)
69
70 with pytest.raises(ValueError, match="boom"):
71 _alias_working_tree_provider(provider_dir)
72
73 assert _PROVIDER_PKG not in sys.modules
74