|
| 1 | +"""Import-time type gate (SPEC N-7b, #183): development-mode import hook.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import importlib |
| 5 | +import sys |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from typedpython import gate, importgate |
| 10 | + |
| 11 | +BAD = """\ |
| 12 | +from pathlib import Path |
| 13 | +Path(__file__).with_name("marker.txt").write_text("ran") |
| 14 | +
|
| 15 | +def f(x: int) -> int: |
| 16 | + return x |
| 17 | +
|
| 18 | +f("not an int") |
| 19 | +""" |
| 20 | + |
| 21 | +GOOD = "VALUE: int = 1\n" |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def project(tmp_path, monkeypatch): |
| 26 | + """A fresh source root on sys.path; the hook and imported modules are cleaned up afterwards.""" |
| 27 | + root = tmp_path / "src" |
| 28 | + root.mkdir() |
| 29 | + monkeypatch.syspath_prepend(str(root)) |
| 30 | + before = set(sys.modules) |
| 31 | + yield root |
| 32 | + importgate.uninstall() |
| 33 | + for name in set(sys.modules) - before: |
| 34 | + del sys.modules[name] |
| 35 | + importlib.invalidate_caches() |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def calls(monkeypatch): |
| 40 | + """Paths handed to the checker, one list per checker invocation (the seam).""" |
| 41 | + seen: list[list[str]] = [] |
| 42 | + real = importgate._run_gate |
| 43 | + |
| 44 | + def counting(paths, search_paths): |
| 45 | + seen.append([str(p) for p in paths]) |
| 46 | + return real(paths, search_paths) |
| 47 | + |
| 48 | + monkeypatch.setattr(importgate, "_run_gate", counting) |
| 49 | + return seen |
| 50 | + |
| 51 | + |
| 52 | +def test_type_error_stops_import_before_module_code_runs(project): |
| 53 | + (project / "bad_mod.py").write_text(BAD) |
| 54 | + importgate.install([project]) |
| 55 | + with pytest.raises(ImportError) as info: |
| 56 | + importlib.import_module("bad_mod") |
| 57 | + assert isinstance(info.value, importgate.ImportGateError) |
| 58 | + assert info.value.diagnostics |
| 59 | + assert all(d.severity == "error" for d in info.value.diagnostics) |
| 60 | + assert "bad_mod.py" in str(info.value) |
| 61 | + assert not (project / "marker.txt").exists() |
| 62 | + assert "bad_mod" not in sys.modules |
| 63 | + |
| 64 | + |
| 65 | +def test_same_module_imports_without_the_hook(project): |
| 66 | + (project / "bad_mod2.py").write_text(BAD) |
| 67 | + importlib.import_module("bad_mod2") |
| 68 | + assert (project / "marker.txt").exists() |
| 69 | + |
| 70 | + |
| 71 | +def test_uninstall_restores_normal_import(project): |
| 72 | + (project / "bad_mod3.py").write_text(BAD) |
| 73 | + importgate.install([project]) |
| 74 | + importgate.uninstall() |
| 75 | + importlib.import_module("bad_mod3") |
| 76 | + assert (project / "marker.txt").exists() |
| 77 | + |
| 78 | + |
| 79 | +def test_clean_module_imports_and_is_cached_by_content(project, calls): |
| 80 | + path = project / "good_mod.py" |
| 81 | + path.write_text(GOOD) |
| 82 | + importgate.install([project]) |
| 83 | + mod = importlib.import_module("good_mod") |
| 84 | + assert mod.VALUE == 1 |
| 85 | + assert len(calls) == 1 |
| 86 | + |
| 87 | + del sys.modules["good_mod"] |
| 88 | + importlib.import_module("good_mod") |
| 89 | + assert len(calls) == 1, "an unchanged module must not be checked again" |
| 90 | + |
| 91 | + del sys.modules["good_mod"] |
| 92 | + path.write_text("VALUE: int = 2\n") |
| 93 | + assert importlib.import_module("good_mod").VALUE == 2 |
| 94 | + assert len(calls) == 2, "changed content must be re-checked" |
| 95 | + |
| 96 | + |
| 97 | +def test_failing_result_is_cached_too(project, calls): |
| 98 | + (project / "bad_mod4.py").write_text(BAD) |
| 99 | + importgate.install([project]) |
| 100 | + for _ in range(2): |
| 101 | + with pytest.raises(importgate.ImportGateError): |
| 102 | + importlib.import_module("bad_mod4") |
| 103 | + assert len(calls) == 1 |
| 104 | + |
| 105 | + |
| 106 | +def test_modules_outside_source_roots_and_stdlib_are_not_checked(project, tmp_path, monkeypatch, calls): |
| 107 | + other = tmp_path / "deps" |
| 108 | + other.mkdir() |
| 109 | + (other / "dep_mod.py").write_text(BAD.replace("marker.txt", "dep_marker.txt")) |
| 110 | + monkeypatch.syspath_prepend(str(other)) |
| 111 | + importgate.install([project]) |
| 112 | + importlib.import_module("dep_mod") # a type error, but outside the roots |
| 113 | + importlib.import_module("colorsys") # stdlib |
| 114 | + assert calls == [] |
| 115 | + assert (other / "dep_marker.txt").exists() |
| 116 | + |
| 117 | + |
| 118 | +def test_package_submodule_under_root_is_checked(project, calls): |
| 119 | + pkg = project / "pkg" |
| 120 | + pkg.mkdir() |
| 121 | + (pkg / "__init__.py").write_text("") |
| 122 | + (pkg / "sub.py").write_text("def f(x):\n return x\n") |
| 123 | + importgate.install([project]) |
| 124 | + with pytest.raises(importgate.ImportGateError): |
| 125 | + importlib.import_module("pkg.sub") |
| 126 | + assert any(p.endswith("sub.py") for c in calls for p in c) |
| 127 | + |
| 128 | + |
| 129 | +def test_install_twice_installs_one_finder(project): |
| 130 | + importgate.install([project]) |
| 131 | + importgate.install([project]) |
| 132 | + assert sum(isinstance(f, importgate.ImportGateFinder) for f in sys.meta_path) == 1 |
| 133 | + importgate.uninstall() |
| 134 | + assert not any(isinstance(f, importgate.ImportGateFinder) for f in sys.meta_path) |
| 135 | + |
| 136 | + |
| 137 | +def test_install_from_env(project, monkeypatch): |
| 138 | + monkeypatch.delenv(importgate.ENV_VAR, raising=False) |
| 139 | + assert importgate.install_from_env() is False |
| 140 | + monkeypatch.setenv(importgate.ENV_VAR, str(project)) |
| 141 | + assert importgate.install_from_env() is True |
| 142 | + (project / "bad_mod5.py").write_text(BAD) |
| 143 | + with pytest.raises(importgate.ImportGateError): |
| 144 | + importlib.import_module("bad_mod5") |
0 commit comments