diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 915fc1c..32b77e6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,61 @@ +pytest-xdist 1.26.1 (2019-01-28) +================================ + +Bug Fixes +--------- + +- `#406 `_: Do not implement deprecated ``pytest_logwarning`` hook in pytest versions where it is deprecated. + + +pytest-xdist 1.26.0 (2019-01-11) +================================ + +Features +-------- + +- `#376 `_: The current directory is no longer added ``sys.path`` for local workers, only for remote connections. + + This behavior is surprising because it makes xdist runs and non-xdist runs to potentially behave differently. + + +Bug Fixes +--------- + +- `#379 `_: Warning attributes are checked to make sure they can be dumped prior to + serializing the warning for submission to the master node. + + +pytest-xdist 1.25.0 (2018-12-12) +================================ + +Deprecations and Removals +------------------------- + +- `#372 `_: Pytest versions older than 3.6 are no longer supported. + + +Features +-------- + +- `#373 `_: Node setup information is hidden when pytest is run in quiet mode to reduce noise on many-core machines. + +- `#388 `_: ``mainargv`` is made available in ``workerinput`` from the host's ``sys.argv``. + + This can be used via ``request.config.workerinput["mainargv"]``. + + +Bug Fixes +--------- + +- `#332 `_: Fix report of module-level skips (``pytest.skip(reason, allow_module_level=True)``). + +- `#378 `_: Fix support for gevent monkeypatching + +- `#384 `_: pytest 4.1 support: ``ExceptionInfo`` API changes. + +- `#390 `_: pytest 4.1 support: ``pytest_logwarning`` hook removed. + + pytest-xdist 1.24.1 (2018-11-09) ================================ diff --git a/README.rst b/README.rst index a863421..072e5b0 100644 --- a/README.rst +++ b/README.rst @@ -223,6 +223,12 @@ defined: The information about the worker_id in a test is stored in the ``TestReport`` as well, under the ``worker_id`` attribute. +Acessing ``sys.argv`` from the master node in workers +----------------------------------------------------- + +To access the ``sys.argv`` passed to the command-line of the master node, use +``request.config.workerinput["mainargv"]``. + Specifying test exec environments in an ini file ------------------------------------------------ diff --git a/changelog/332.bugfix.rst b/changelog/332.bugfix.rst deleted file mode 100644 index 467c0e5..0000000 --- a/changelog/332.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Fix report of module-level skips (``pytest.skip(reason, allow_module_level=True)``). diff --git a/changelog/372.removal.rst b/changelog/372.removal.rst deleted file mode 100644 index ea14c5b..0000000 --- a/changelog/372.removal.rst +++ /dev/null @@ -1 +0,0 @@ -Pytest versions older than 3.6 are no longer supported. diff --git a/changelog/415.feature b/changelog/415.feature new file mode 100644 index 0000000..bb63455 --- /dev/null +++ b/changelog/415.feature @@ -0,0 +1 @@ +Improve behavior of ``--numprocesses=auto`` to work well with ``--pdb`` option. diff --git a/setup.py b/setup.py index b830492..03ee47a 100644 --- a/setup.py +++ b/setup.py @@ -3,12 +3,15 @@ from setuptools import setup, find_packages install_requires = ["execnet>=1.1", "pytest>=3.6.0", "pytest-forked", "six"] +with open("README.rst") as f: + long_description = f.read() + setup( name="pytest-xdist", use_scm_version={"write_to": "xdist/_version.py"}, description="pytest xdist plugin for distributed testing" " and loop-on-failing modes", - long_description=open("README.rst").read(), + long_description=long_description, license="MIT", author="holger krekel and contributors", author_email="pytest-dev@python.org,holger@merlinux.eu", diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index be4c7d8..9a8e42b 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -1,5 +1,6 @@ import os import re +import sys import textwrap import py @@ -358,6 +359,30 @@ class TestDistEach: class TestTerminalReporting: + @pytest.mark.parametrize("verbosity", ["", "-q", "-v"]) + def test_output_verbosity(self, testdir, verbosity): + testdir.makepyfile( + """ + def test_ok(): + pass + """ + ) + args = ["-n1"] + if verbosity: + args.append(verbosity) + result = testdir.runpytest(*args) + out = result.stdout.str() + if verbosity == "-v": + assert "scheduling tests" in out + assert "gw" in out + elif verbosity == "-q": + assert "scheduling tests" not in out + assert "gw" not in out + assert "bringing up nodes..." in out + else: + assert "scheduling tests" not in out + assert "gw" in out + def test_pass_skip_fail(self, testdir): testdir.makepyfile( """ @@ -465,9 +490,8 @@ def test_session_hooks(testdir): name = "worker" else: name = "master" - f = open(name, "w") - f.write("xy") - f.close() + with open(name, "w") as f: + f.write("xy") # let's fail on the worker if name == "worker": raise ValueError(42) @@ -517,16 +541,15 @@ def test_session_testscollected(testdir): assert collected_file.read() == "collected = 3" -def test_funcarg_teardown_failure(testdir): +def test_fixture_teardown_failure(testdir): p = testdir.makepyfile( """ import pytest - @pytest.fixture + @pytest.fixture(scope="module") def myarg(request): - def teardown(val): - raise ValueError(val) - return request.cached_setup(setup=lambda: 42, teardown=teardown, - scope="module") + yield 42 + raise ValueError(42) + def test_hello(myarg): pass """ @@ -612,6 +635,11 @@ def test_skipping(testdir): def test_issue34_pluginloading_in_subprocess(testdir): + import _pytest.hookspec + + if not hasattr(_pytest.hookspec, "pytest_namespace"): + pytest.skip("this pytest version no longer supports pytest_namespace()") + testdir.tmpdir.join("plugin123.py").write( textwrap.dedent( """ @@ -710,10 +738,12 @@ def test_sub_plugins_disabled(testdir, plugin): class TestWarnings: @pytest.mark.parametrize("n", ["-n0", "-n1"]) @pytest.mark.parametrize("warn_type", ["pytest", "builtin"]) - def test_warnings(self, testdir, n, warn_type): + def test_warnings(self, testdir, n, request, warn_type): if warn_type == "builtin": warn_code = """warnings.warn(UserWarning('this is a warning'))""" elif warn_type == "pytest": + if not hasattr(request.config, "warn"): + pytest.skip("config.warn has been removed in pytest 4.1") warn_code = """request.config.warn('', 'this is a warning', fslocation=py.path.local())""" else: @@ -773,6 +803,39 @@ class TestWarnings: result = testdir.runpytest(n) result.stdout.fnmatch_lines(["*UserWarning*foo.txt*", "*1 passed, 1 warnings*"]) + @pytest.mark.parametrize("n", ["-n0", "-n1"]) + def test_unserializable_warning_details(self, testdir, n): + """Check that warnings with unserializable _WARNING_DETAILS are + handled correctly (#379). + """ + if sys.version_info[0] < 3: + # The issue is only present in Python 3 warnings + return + testdir.makepyfile( + """ + import warnings, pytest + import socket + import gc + def abuse_socket(): + s = socket.socket() + del s + + # Deliberately provoke a ResourceWarning for an unclosed socket. + # The socket itself will end up attached as a value in + # _WARNING_DETAIL. We need to test that it is not serialized + # (it can't be, so the test will fail if we try to). + @pytest.mark.filterwarnings('always') + def test_func(tmpdir): + abuse_socket() + gc.collect() + """ + ) + testdir.syspathinsert() + result = testdir.runpytest(n) + result.stdout.fnmatch_lines( + ["*ResourceWarning*unclosed*", "*1 passed, 1 warnings*"] + ) + class TestNodeFailure: def test_load_single(self, testdir): diff --git a/testing/test_plugin.py b/testing/test_plugin.py index 0ea5b74..dc0fc51 100644 --- a/testing/test_plugin.py +++ b/testing/test_plugin.py @@ -36,6 +36,7 @@ def test_dist_options(testdir): def test_auto_detect_cpus(testdir, monkeypatch): import os + from xdist.plugin import pytest_cmdline_main as check_options if hasattr(os, "sched_getaffinity"): monkeypatch.setattr(os, "sched_getaffinity", lambda _pid: set(range(99))) @@ -52,6 +53,11 @@ def test_auto_detect_cpus(testdir, monkeypatch): config = testdir.parseconfigure("-nauto") assert config.getoption("numprocesses") == 99 + config = testdir.parseconfigure("-nauto", "--pdb") + check_options(config) + assert config.getoption("usepdb") + assert config.getoption("numprocesses") == 0 + monkeypatch.delattr(os, "sched_getaffinity", raising=False) monkeypatch.setenv("TRAVIS", "true") config = testdir.parseconfigure("-nauto") diff --git a/testing/test_remote.py b/testing/test_remote.py index dd6ee87..c134a9e 100644 --- a/testing/test_remote.py +++ b/testing/test_remote.py @@ -1,6 +1,7 @@ import py import pprint import pytest +import sys from xdist.workermanage import WorkerController, unserialize_report from xdist.remote import serialize_report @@ -152,7 +153,6 @@ class TestReportSerialization: for i in range(len(a_entries)): assert isinstance(rep_entries[i], ReprEntry) assert rep_entries[i].lines == a_entries[i].lines - assert rep_entries[i].localssep == a_entries[i].localssep assert rep_entries[i].reprfileloc.lineno == a_entries[i].reprfileloc.lineno assert ( rep_entries[i].reprfileloc.message == a_entries[i].reprfileloc.message @@ -397,3 +397,64 @@ def test_remote_env_vars(testdir): ) 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") + 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: *", "*error: my_usage_error"]) diff --git a/xdist/dsession.py b/xdist/dsession.py index be3ba77..8bb559c 100644 --- a/xdist/dsession.py +++ b/xdist/dsession.py @@ -229,9 +229,10 @@ class DSession(object): if self.terminal and not self.sched.has_pending: self.trdist.ensure_show_status() self.terminal.write_line("") - self.terminal.write_line( - "scheduling tests via %s" % (self.sched.__class__.__name__) - ) + if self.config.option.verbose > 0: + self.terminal.write_line( + "scheduling tests via %s" % (self.sched.__class__.__name__) + ) self.sched.schedule() def worker_logstart(self, node, nodeid, location): @@ -344,8 +345,11 @@ class TerminalDistReporter(object): self.rewrite(self.getstatus()) def getstatus(self): - parts = ["%s %s" % (spec.id, self._status[spec.id]) for spec in self._specs] - return " / ".join(parts) + if self.config.option.verbose >= 0: + parts = ["%s %s" % (spec.id, self._status[spec.id]) for spec in self._specs] + return " / ".join(parts) + else: + return "bringing up nodes..." def rewrite(self, line, newline=False): pline = line + " " * max(self._lastlen - len(line), 0) diff --git a/xdist/looponfail.py b/xdist/looponfail.py index d49a05b..ef68c6e 100644 --- a/xdist/looponfail.py +++ b/xdist/looponfail.py @@ -32,7 +32,7 @@ def pytest_cmdline_main(config): if config.getoption("looponfail"): usepdb = config.getoption("usepdb") # a core option if usepdb: - raise pytest.UsageError("--pdb incompatible with --looponfail.") + raise pytest.UsageError("--pdb is incompatible with --looponfail.") looponfail_main(config) return 2 # looponfail only can get stop with ctrl-C anyway diff --git a/xdist/plugin.py b/xdist/plugin.py index ce8f643..c727594 100644 --- a/xdist/plugin.py +++ b/xdist/plugin.py @@ -27,10 +27,14 @@ def auto_detect_cpus(): return n if n else 1 +class AutoInt(int): + """Mark value as auto-detected.""" + + def parse_numprocesses(s): if s == "auto": - return auto_detect_cpus() - else: + return AutoInt(auto_detect_cpus()) + elif s is not None: return int(s) @@ -45,7 +49,7 @@ def pytest_addoption(parser): type=parse_numprocesses, help="shortcut for '--dist=load --tx=NUM*popen', " "you can use 'auto' here for auto detection CPUs number on " - "host system", + "host system and it will be 0 when used with --pdb", ) group.addoption( "--maxprocesses", @@ -126,12 +130,12 @@ def pytest_addoption(parser): ) parser.addini( "rsyncdirs", - "list of (relative) paths to be rsynced for" " remote distributed testing.", + "list of (relative) paths to be rsynced for remote distributed testing.", type="pathlist", ) parser.addini( "rsyncignore", - "list of (relative) glob-style paths to be ignored " "for rsyncing.", + "list of (relative) glob-style paths to be ignored for rsyncing.", type="pathlist", ) parser.addini( @@ -177,6 +181,10 @@ def pytest_configure(config): @pytest.mark.tryfirst def pytest_cmdline_main(config): + usepdb = config.getoption("usepdb") # a core option + if isinstance(config.option.numprocesses, AutoInt): + config.option.numprocesses = 0 if usepdb else int(config.option.numprocesses) + if config.option.numprocesses: if config.option.dist == "no": config.option.dist = "load" @@ -188,11 +196,10 @@ def pytest_cmdline_main(config): config.option.dist = "load" val = config.getvalue if not val("collectonly"): - usepdb = config.getoption("usepdb") # a core option if val("dist") != "no": if usepdb: raise pytest.UsageError( - "--pdb is incompatible with distributing tests; try using -n0." + "--pdb is incompatible with distributing tests; try using -n0 or -nauto." ) # noqa: E501 diff --git a/xdist/remote.py b/xdist/remote.py index 0aadff9..120eadd 100644 --- a/xdist/remote.py +++ b/xdist/remote.py @@ -116,14 +116,19 @@ class WorkerInteractor(object): data = serialize_report(report) self.sendevent("collectreport", data=data) - def pytest_logwarning(self, message, code, nodeid, fslocation): - self.sendevent( - "logwarning", - message=message, - code=code, - nodeid=nodeid, - fslocation=str(fslocation), - ) + # the pytest_logwarning hook was deprecated since pytest 4.0 + if hasattr( + _pytest.hookspec, "pytest_logwarning" + ) and not _pytest.hookspec.pytest_logwarning.pytest_spec.get("warn_on_impl"): + + def pytest_logwarning(self, message, code, nodeid, fslocation): + self.sendevent( + "logwarning", + message=message, + code=code, + nodeid=nodeid, + fslocation=str(fslocation), + ) # the pytest_warning_captured hook was introduced in pytest 3.8 if hasattr(_pytest.hookspec, "pytest_warning_captured"): @@ -216,7 +221,15 @@ def serialize_warning_message(warning_message): for attr_name in warning_message._WARNING_DETAILS: if attr_name in ("message", "category"): continue - result[attr_name] = getattr(warning_message, attr_name) + attr = getattr(warning_message, attr_name) + # Check if we can serialize the warning detail, marking `None` otherwise + # Note that we need to define the attr (even as `None`) to allow deserializing + try: + dumps(attr) + except DumpError: + result[attr_name] = repr(attr) + else: + result[attr_name] = attr return result @@ -249,18 +262,23 @@ def remote_initconfig(option_dict, args): if __name__ == "__channelexec__": + import py + channel = channel # noqa - workerinput, args, option_dict = channel.receive() - importpath = os.getcwd() - sys.path.insert(0, importpath) # XXX only for remote situations - os.environ["PYTHONPATH"] = ( - importpath + os.pathsep + os.environ.get("PYTHONPATH", "") - ) + workerinput, args, option_dict, change_sys_path = channel.receive() + + if change_sys_path: + importpath = os.getcwd() + sys.path.insert(0, importpath) + os.environ["PYTHONPATH"] = ( + importpath + os.pathsep + os.environ.get("PYTHONPATH", "") + ) + os.environ["PYTEST_XDIST_WORKER"] = workerinput["workerid"] os.environ["PYTEST_XDIST_WORKER_COUNT"] = str(workerinput["workercount"]) - # os.environ['PYTHONPATH'] = importpath 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 diff --git a/xdist/scheduler/loadscope.py b/xdist/scheduler/loadscope.py index 2917fc0..426d340 100644 --- a/xdist/scheduler/loadscope.py +++ b/xdist/scheduler/loadscope.py @@ -187,7 +187,7 @@ class LoadScopeScheduling(object): break else: raise RuntimeError( - "Unable to identify crashitem on a workload with " "pending items" + "Unable to identify crashitem on a workload with pending items" ) # Made uncompleted work unit available again diff --git a/xdist/workermanage.py b/xdist/workermanage.py index a25243a..72665a6 100644 --- a/xdist/workermanage.py +++ b/xdist/workermanage.py @@ -2,6 +2,7 @@ from __future__ import print_function import fnmatch import os import re +import sys import threading import py @@ -219,6 +220,7 @@ class WorkerController(object): "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 @@ -248,9 +250,13 @@ class WorkerController(object): basetemp = self.config._tmpdirhandler.getbasetemp() option_dict["basetemp"] = str(basetemp.join(name)) self.config.hook.pytest_configure_node(node=self) + remote_module = self.config.hook.pytest_xdist_getremotemodule() - self.channel = self.gateway.remote_exec(remote_module) - self.channel.send((self.workerinput, args, option_dict)) + self.channel = self.gateway.remote_exec(remote_module) + # change sys.path only for remote workers + change_sys_path = not self.gateway.spec.popen + self.channel.send((self.workerinput, args, option_dict, change_sys_path)) + if self.putevent: self.channel.setcallback(self.process_from_remote, endmarker=self.ENDMARK) @@ -275,7 +281,7 @@ class WorkerController(object): if not self._down: try: self.sendcommand("shutdown") - except IOError: + except (IOError, OSError): pass self._shutdown_sent = True @@ -352,7 +358,11 @@ class WorkerController(object): except: # noqa from _pytest._code import ExceptionInfo - excinfo = ExceptionInfo() + # ExceptionInfo API changed in pytest 4.1 + if hasattr(ExceptionInfo, "from_current"): + excinfo = ExceptionInfo.from_current() + else: + excinfo = ExceptionInfo() print("!" * 20, excinfo) self.config.notify_exception(excinfo) self.shutdown()