Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sys.path for local workers Fixes #421 #667

Merged
merged 3 commits into from Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/xdist/plugin.py
@@ -1,9 +1,12 @@
import os
import uuid
import sys

import py
import pytest

_sys_path = list(sys.path) # freeze a copy of sys.path at interpreter startup


def pytest_xdist_auto_num_workers():
try:
Expand Down
4 changes: 3 additions & 1 deletion src/xdist/remote.py
Expand Up @@ -219,12 +219,14 @@ def setup_config(config, basetemp):
channel = channel # noqa
workerinput, args, option_dict, change_sys_path = channel.receive()

if change_sys_path:
if change_sys_path is None:
importpath = os.getcwd()
sys.path.insert(0, importpath)
os.environ["PYTHONPATH"] = (
importpath + os.pathsep + os.environ.get("PYTHONPATH", "")
)
else:
sys.path = change_sys_path

os.environ["PYTEST_XDIST_TESTRUNUID"] = workerinput["testrunuid"]
os.environ["PYTEST_XDIST_WORKER"] = workerinput["workerid"]
Expand Down
4 changes: 3 additions & 1 deletion src/xdist/workermanage.py
Expand Up @@ -9,6 +9,7 @@
import execnet

import xdist.remote
from xdist.plugin import _sys_path


def parse_spec_config(config):
Expand Down Expand Up @@ -261,7 +262,8 @@ def setup(self):
remote_module = self.config.hook.pytest_xdist_getremotemodule()
self.channel = self.gateway.remote_exec(remote_module)
# change sys.path only for remote workers
change_sys_path = not self.gateway.spec.popen
# restore sys.path from a frozen copy for local workers
change_sys_path = _sys_path if self.gateway.spec.popen else None
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to use a frozen copy of sys.path because --import-mode=prepend changes the sys.path

self.channel.send((self.workerinput, args, option_dict, change_sys_path))

if self.putevent:
Expand Down
16 changes: 15 additions & 1 deletion testing/test_remote.py
Expand Up @@ -234,7 +234,7 @@ def test():


def test_remote_inner_argv(testdir):
"""Test/document the behavior due to execnet using `python -c`."""
"""Work around sys.path differences due to execnet using `python -c`."""
graingert marked this conversation as resolved.
Show resolved Hide resolved
testdir.makepyfile(
"""
import sys
Expand Down Expand Up @@ -292,3 +292,17 @@ def test(get_config_parser, request):
result = testdir.runpytest_subprocess("-n1")
assert result.ret == 1
result.stdout.fnmatch_lines(["*usage: *", "*error: my_usage_error"])


def test_remote_sys_path(testdir):
"""Test/document the behavior due to execnet using `python -c`."""
graingert marked this conversation as resolved.
Show resolved Hide resolved
testdir.makepyfile(
"""
import sys

def test_sys_path():
assert "" not in sys.path
"""
)
result = testdir.runpytest("-n1")
assert result.ret == 0