summaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2018-11-24 15:12:56 -0500
committerNed Batchelder <ned@nedbatchelder.com>2018-11-25 15:11:54 -0500
commit20998572517413d52e4ca63a79d5a790578d19cb (patch)
treed5d6d7f9ed7e0c9dfb19fceb27374d0823b6f970 /tests/conftest.py
parent3c57eda03b6a1a40f480db30a19978e95dfbed31 (diff)
downloadpython-coveragepy-git-20998572517413d52e4ca63a79d5a790578d19cb.tar.gz
Control the sys.path that tests see
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index e9802517..2c30a2d7 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -7,6 +7,8 @@ Pytest auto configuration.
This module is run automatically by pytest, to define and enable fixtures.
"""
+import os
+import sys
import warnings
import pytest
@@ -33,3 +35,36 @@ def set_warnings():
# pypy3 warns about unclosed files a lot.
# pylint: disable=undefined-variable
warnings.filterwarnings("ignore", r".*unclosed file", category=ResourceWarning)
+
+
+@pytest.fixture(autouse=True)
+def reset_sys_path():
+ """Clean up sys.path changes around every test."""
+ sys_path = list(sys.path)
+ yield
+ sys.path[:] = sys_path
+
+
+@pytest.fixture(autouse=True)
+def fix_xdist_sys_path():
+ """Prevent xdist from polluting the Python path.
+
+ We run tests that care a lot about the contents of sys.path. Pytest-xdist
+ changes sys.path, so running with xdist, vs without xdist, sets sys.path
+ differently. With xdist, sys.path[1] is an empty string, without xdist,
+ it's the virtualenv bin directory. We don't want the empty string, so
+ clobber that entry.
+
+ See: https://github.com/pytest-dev/pytest-xdist/issues/376
+
+ """
+ if os.environ.get('PYTEST_XDIST_WORKER', ''):
+ # We are running in an xdist worker.
+ if sys.path[1] == '':
+ # xdist has set sys.path[1] to ''. Clobber it.
+ del sys.path[1]
+ # Also, don't let it sneak stuff in via PYTHONPATH.
+ try:
+ del os.environ['PYTHONPATH']
+ except KeyError:
+ pass