Skip to content

Commit

Permalink
Fix unit test borkage due to loading from /dls_sw
Browse files Browse the repository at this point in the history
  • Loading branch information
rtuck99 committed Oct 25, 2024
1 parent c89282a commit 74a344a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
PathProvider,
)

from dodal.common.beamlines import beamline_utils
from dodal.common.beamlines import beamline_parameters, beamline_utils
from dodal.common.visit import (
DirectoryServiceClient,
LocalDirectoryServiceClient,
Expand All @@ -37,8 +37,29 @@
"s03": mock_paths,
"i04": mock_paths,
"s04": mock_paths,
"i24": mock_paths,
}

BANNED_PATHS = [Path("/dls"), Path("/dls_sw")]


@pytest.fixture(autouse=True)
def patch_open_to_prevent_dls_reads_in_tests():
unpatched_open = open

def patched_open(*args, **kwargs):
requested_path = Path(args[0])
if requested_path.is_absolute():
for p in BANNED_PATHS:
assert not requested_path.is_relative_to(
p
), f"Attempt to open {requested_path} from inside a unit test"
return unpatched_open(*args, **kwargs)

with patch("builtins.open", side_effect=patched_open):
yield []


# Prevent pytest from catching exceptions when debugging in vscode so that break on
# exception works correctly (see: https://github.com/pytest-dev/pytest/issues/7409)
if os.getenv("PYTEST_RAISE", "0") == "1":
Expand All @@ -55,6 +76,9 @@ def pytest_internalerror(excinfo):
def mock_beamline_module_filepaths(bl_name, bl_module):
if mock_attributes := mock_attributes_table.get(bl_name):
[bl_module.__setattr__(attr[0], attr[1]) for attr in mock_attributes]
beamline_parameters.BEAMLINE_PARAMETER_PATHS[bl_name] = (
"tests/test_data/i04_beamlineParameters"
)


@pytest.fixture(scope="function")
Expand Down

0 comments on commit 74a344a

Please sign in to comment.