Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
68 lines (58 loc) · 2.68 KB
/
Copy pathconftest.py
File metadata and controls
68 lines (58 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import annotations
import sys
from pathlib import Path
# Ensure pytest collection can resolve repository-root imports such as
# ``src.phasmid`` and ``tests.scenarios`` without requiring manual PYTHONPATH.
_REPO_ROOT = Path(__file__).resolve().parent
_repo_root_str = str(_REPO_ROOT)
if _repo_root_str not in sys.path:
sys.path.insert(0, _repo_root_str)
# The WebUI resolves its target Vessel from the operator registry, which is
# real, global, and outlives any single test. A Vessel left registered by an
# earlier run therefore silently changes which container the WebUI acts on,
# and tests written against the legacy fallback start failing for reasons
# that have nothing to do with the code under test.
#
# Point the override at a path that cannot exist, which resolve_web_vessel()
# treats as "nothing registered". Tests that want a real target set the
# variable themselves; tests that want the registry branch pop it.
import os # noqa: E402
import tempfile # noqa: E402
import pytest # noqa: E402
_NO_WEB_VESSEL = "/nonexistent/phasmid-test-no-vessel.vessel"
@pytest.fixture(autouse=True)
def _isolate_web_vessel_target():
previous = os.environ.get("PHASMID_WEB_VESSEL")
os.environ["PHASMID_WEB_VESSEL"] = _NO_WEB_VESSEL
try:
yield
finally:
if previous is None:
os.environ.pop("PHASMID_WEB_VESSEL", None)
else:
os.environ["PHASMID_WEB_VESSEL"] = previous
# The same problem one level up: the Vessel registry itself lives in the
# config directory, so a test that builds a VesselService without an override
# reads and rewrites the operator's real `vessel_registry.json`. On a machine
# where Phasmid is actually used, a test run mutates their registry, and
# Vessels registered there leak into unrelated assertions - which is exactly
# how a stale registry from a previous session turned into five failures in
# test_web_server and test_scenarios that pointed at a purge call rather than
# at the real cause.
#
# TestCase-based suites isolate this in setUp, but module-level test functions
# have no setUp to hang it on, so it is done here for every test. A test that
# wants a specific config directory still sets the variable itself; the
# per-test patches nest on top of this one.
@pytest.fixture(autouse=True)
def _isolate_config_dir():
previous = os.environ.get("PHASMID_CONFIG_DIR")
with tempfile.TemporaryDirectory() as isolated:
os.environ["PHASMID_CONFIG_DIR"] = os.path.join(isolated, "config")
try:
yield
finally:
if previous is None:
os.environ.pop("PHASMID_CONFIG_DIR", None)
else:
os.environ["PHASMID_CONFIG_DIR"] = previous