Skip to content

Commit

Permalink
testing: Add test to assert path hooks ignored in sys.path
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-hiatt committed Feb 29, 2024
1 parent 51ef944 commit 79fb6ff
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions testing/test_looponfail.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pathlib
import tempfile
import unittest.mock
from typing import List

Expand Down Expand Up @@ -191,6 +193,43 @@ def test_func():
control.loop_once()
assert control.failures

def test_ignore_sys_path_hook_entry(
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
# Modifying sys.path as seen by the worker process is a bit tricky,
# because any changes made in the current process do not carry over.
# However, we can leverage the `sitecustomize` behavior to run arbitrary
# code when the subprocess interpreter is starting up. We just need to
# install our module in the search path, which we can accomplish by
# adding a temporary directory to PYTHONPATH.
tmpdir = tempfile.TemporaryDirectory()
with open(pathlib.Path(tmpdir.name) / "sitecustomize.py", "w") as custom:
print(
textwrap.dedent(
"""
import sys
sys.path.append('dummy.__path_hook__')
"""
),
file=custom,
)

monkeypatch.setenv("PYTHONPATH", tmpdir.name, prepend=":")

item = pytester.getitem(
textwrap.dedent(
"""
def test_func():
import sys
assert "dummy.__path_hook__" in sys.path
"""
)
)
control = RemoteControl(item.config)
control.setup()
topdir, failures = control.runsession()[:2]
assert not failures


class TestLooponFailing:
def test_looponfail_from_fail_to_ok(self, pytester: pytest.Pytester) -> None:
Expand Down

0 comments on commit 79fb6ff

Please sign in to comment.