diff --git a/tests/conftest.py b/tests/conftest.py index ad2bb2e..26b1d8e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,15 +4,34 @@ import pytest -def _engine_available(): +def _engine_available(generation=2): + """Whether ``needle._library_path`` would find an engine without downloading. + + Every location this looks in has to be one that function looks in, or the + gate skips tests an installed engine could have run. + """ try: import needle from needle.agent import fetch + + override = os.environ.get(f"NEEDLE{generation}_LIB_PATH") + if generation == 2 and not override: + override = os.environ.get("NEEDLE_LIB_PATH") + if override: + return os.path.exists(override) + here = os.path.dirname(needle.__file__) name = fetch._lib_name() - cache = os.path.join(os.path.expanduser("~"), ".cache", - "cactus-needle", fetch.ENGINE_VERSION, name) - return os.path.exists(os.path.join(here, name)) or os.path.exists(cache) + stem, suffix = os.path.splitext(name) + local_names = [f"{stem}{generation}{suffix}"] + if generation == 2: + local_names.append(name) + if any(os.path.exists(os.path.join(here, local)) for local in local_names): + return True + + cache = os.path.join(os.path.expanduser("~"), ".cache", "cactus-needle", + f"v{generation}", fetch.engine_version(generation), name) + return os.path.exists(cache) except Exception: return False diff --git a/tests/test_fetch.py b/tests/test_fetch.py index 9c579f7..520e071 100644 --- a/tests/test_fetch.py +++ b/tests/test_fetch.py @@ -62,3 +62,45 @@ def test_fetch_library_creates_destination(tmp_path, monkeypatch): assert (tmp_path / "new" / "libneedle.so").read_bytes() == b"engine" assert out == str(tmp_path / "new" / "libneedle.so") + + +def test_engine_gate_finds_the_cache_the_runtime_loads_from(tmp_path, monkeypatch): + import needle + from needle.agent import fetch + from conftest import _engine_available + + package = tmp_path / "pkg" + package.mkdir() + monkeypatch.setattr(needle, "__file__", str(package / "__init__.py")) + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("USERPROFILE", str(tmp_path)) + monkeypatch.delenv("NEEDLE_LIB_PATH", raising=False) + monkeypatch.delenv("NEEDLE2_LIB_PATH", raising=False) + + assert not _engine_available() + + cache = tmp_path / ".cache" / "cactus-needle" / "v2" / fetch.engine_version(2) + cache.mkdir(parents=True) + (cache / fetch._lib_name()).write_bytes(b"") + + assert _engine_available() + + +def test_engine_gate_honours_the_library_override(tmp_path, monkeypatch): + import needle + from needle.agent import fetch + from conftest import _engine_available + + package = tmp_path / "pkg" + package.mkdir() + monkeypatch.setattr(needle, "__file__", str(package / "__init__.py")) + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("USERPROFILE", str(tmp_path)) + + engine = tmp_path / fetch._lib_name() + engine.write_bytes(b"") + monkeypatch.setenv("NEEDLE_LIB_PATH", str(engine)) + assert _engine_available() + + monkeypatch.setenv("NEEDLE_LIB_PATH", str(tmp_path / "gone")) + assert not _engine_available()