Skip to content

Commit

Permalink
Pass and use original sys.argv to/with workers
Browse files Browse the repository at this point in the history
This gets used e.g. by argparse for the "prog" part.

We could explicitly pass it through and/or set it on
config._parser.prog, but that is a bit tedious just for this use case,
and it looks like "simulating" the main prog here appears to not be that
bad of a hack after all.
  • Loading branch information
blueyed committed Dec 11, 2018
1 parent 5ecbf25 commit 31955ea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
54 changes: 54 additions & 0 deletions testing/test_remote.py
@@ -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,55 @@ 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():
assert sys.argv == ["-c"]
"""
)
result = testdir.runpytest("-n1")
assert result.ret == 0


def test_remote_usage_prog(testdir):
outer_argv = sys.argv[0]
prog = os.path.basename(outer_argv)

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")
assert request.config.workerinput["mainargv"] == {!r}
""".format(
outer_argv
)
)

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
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
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 31955ea

Please sign in to comment.