Skip to content

Commit

Permalink
Merge pull request #388 from blueyed/pass-mainargv
Browse files Browse the repository at this point in the history
Pass and use original sys.argv to/with workers
  • Loading branch information
nicoddemus authored Dec 12, 2018
2 parents 33d5441 + a70dace commit 95aaaec
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
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")
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 @@ -264,6 +264,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

0 comments on commit 95aaaec

Please sign in to comment.