Skip to content

Commit fcc29f1

Browse files
committed
testing: Add test to assert path hooks ignored in sys.path
1 parent 51ef944 commit fcc29f1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

testing/test_looponfail.py

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import pathlib
2+
import sys
3+
import tempfile
14
import unittest.mock
25
from typing import List
36

@@ -191,6 +194,43 @@ def test_func():
191194
control.loop_once()
192195
assert control.failures
193196

197+
def test_ignore_sys_path_hook_entry(
198+
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
199+
) -> None:
200+
# Modifying sys.path as seen by the worker process is a bit tricky,
201+
# because any changes made in the current process do not carry over.
202+
# However, we can leverage the `sitecustomize` behavior to run arbitrary
203+
# code when the subprocess interpreter is starting up. We just need to
204+
# install our module in the search path, which we can accomplish by
205+
# adding a temporary directory to PYTHONPATH.
206+
tmpdir = tempfile.TemporaryDirectory()
207+
with open(pathlib.Path(tmpdir.name) / "sitecustomize.py", "w") as custom:
208+
print(
209+
textwrap.dedent(
210+
"""
211+
import sys
212+
sys.path.append('dummy.__path_hook__')
213+
"""
214+
),
215+
file=custom,
216+
)
217+
218+
monkeypatch.setenv("PYTHONPATH", tmpdir.name, prepend=":")
219+
220+
item = pytester.getitem(
221+
textwrap.dedent(
222+
"""
223+
def test_func():
224+
import sys
225+
assert "dummy.__path_hook__" in sys.path
226+
"""
227+
)
228+
)
229+
control = RemoteControl(item.config)
230+
control.setup()
231+
topdir, failures = control.runsession()[:2]
232+
assert not failures
233+
194234

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

0 commit comments

Comments
 (0)