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

Pass and use original sys.argv to/with workers #388

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/388.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``mainargv`` is made available in ``workerinput`` from the host's ``sys.argv``.

This can be used via ``request.config.workerinput["mainargv"]``.
67 changes: 67 additions & 0 deletions testing/test_remote.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import py
import pprint
import pytest
import sys

from xdist.workermanage import WorkerController, unserialize_report
from xdist.remote import serialize_report
Expand Down Expand Up @@ -397,3 +399,68 @@ def test():
)
result = testdir.runpytest("-n2", "--max-worker-restart=0")
assert result.ret == 0


def test_remote_inner_argv(testdir):
"""Test/document the behavior due to execnet using `python -c`."""
testdir.makepyfile(
"""
import sys

def test_argv():
assert sys.argv == ["-c"]
"""
)
result = testdir.runpytest("-n1")
assert result.ret == 0


def test_remote_mainargv(testdir):
outer_argv = sys.argv

testdir.makepyfile(
"""
def test_mainargv(request):
assert request.config.workerinput["mainargv"] == {!r}
""".format(
outer_argv
)
)
result = testdir.runpytest("-n1")
assert result.ret == 0


def test_remote_usage_prog(testdir, request):
if not hasattr(request.config._parser, "prog"):
pytest.skip("prog not available in config parser")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

prog = os.path.basename(sys.argv[0])

testdir.makeconftest(
"""
import pytest

config_parser = None

@pytest.fixture
def get_config_parser():
return config_parser

def pytest_configure(config):
global config_parser
config_parser = config._parser
"""
)
testdir.makepyfile(
"""
import sys

def test(get_config_parser, request):
get_config_parser._getparser().error("my_usage_error")
"""
)

result = testdir.runpytest_subprocess("-n1")
assert result.ret == 1
result.stdout.fnmatch_lines(
["usage: %s *" % prog, "%s: error: my_usage_error" % prog]
)
1 change: 1 addition & 0 deletions xdist/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def remote_initconfig(option_dict, args):
import py

config = remote_initconfig(option_dict, args)
config._parser.prog = os.path.basename(workerinput["mainargv"][0])
config.workerinput = workerinput
config.workeroutput = {}
# TODO: deprecated name, backward compatibility only. Remove it in future
Expand Down
2 changes: 2 additions & 0 deletions xdist/workermanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fnmatch
import os
import re
import sys
import threading

import py
Expand Down Expand Up @@ -213,6 +214,7 @@ def __init__(self, nodemanager, gateway, config, putevent):
"workercount": len(nodemanager.specs),
"slaveid": gateway.id,
"slavecount": len(nodemanager.specs),
"mainargv": sys.argv,
}
# TODO: deprecated name, backward compatibility only. Remove it in future
self.slaveinput = self.workerinput
Expand Down