Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 42 additions & 0 deletions tests/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()