Merge pull request #415 from jirikuncar/numprocesses-auto-with-pdb

Improve behavior of --numprocesses=auto and --pdb
This commit is contained in:
Bruno Oliveira
2019-02-15 09:20:51 -02:00
committed by GitHub
4 changed files with 20 additions and 8 deletions

1
changelog/415.feature Normal file
View File

@@ -0,0 +1 @@
Improve behavior of ``--numprocesses=auto`` to work well with ``--pdb`` option.

View File

@@ -36,6 +36,7 @@ def test_dist_options(testdir):
def test_auto_detect_cpus(testdir, monkeypatch): def test_auto_detect_cpus(testdir, monkeypatch):
import os import os
from xdist.plugin import pytest_cmdline_main as check_options
if hasattr(os, "sched_getaffinity"): if hasattr(os, "sched_getaffinity"):
monkeypatch.setattr(os, "sched_getaffinity", lambda _pid: set(range(99))) 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") config = testdir.parseconfigure("-nauto")
assert config.getoption("numprocesses") == 99 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.delattr(os, "sched_getaffinity", raising=False)
monkeypatch.setenv("TRAVIS", "true") monkeypatch.setenv("TRAVIS", "true")
config = testdir.parseconfigure("-nauto") config = testdir.parseconfigure("-nauto")

View File

@@ -457,6 +457,4 @@ def test_remote_usage_prog(testdir, request):
result = testdir.runpytest_subprocess("-n1") result = testdir.runpytest_subprocess("-n1")
assert result.ret == 1 assert result.ret == 1
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(["*usage: *", "*error: my_usage_error"])
["usage: pytest.py *", "pytest.py: error: my_usage_error"]
)

View File

@@ -27,10 +27,14 @@ def auto_detect_cpus():
return n if n else 1 return n if n else 1
class AutoInt(int):
"""Mark value as auto-detected."""
def parse_numprocesses(s): def parse_numprocesses(s):
if s == "auto": if s == "auto":
return auto_detect_cpus() return AutoInt(auto_detect_cpus())
else: elif s is not None:
return int(s) return int(s)
@@ -45,7 +49,7 @@ def pytest_addoption(parser):
type=parse_numprocesses, type=parse_numprocesses,
help="shortcut for '--dist=load --tx=NUM*popen', " help="shortcut for '--dist=load --tx=NUM*popen', "
"you can use 'auto' here for auto detection CPUs number on " "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( group.addoption(
"--maxprocesses", "--maxprocesses",
@@ -177,6 +181,10 @@ def pytest_configure(config):
@pytest.mark.tryfirst @pytest.mark.tryfirst
def pytest_cmdline_main(config): 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.numprocesses:
if config.option.dist == "no": if config.option.dist == "no":
config.option.dist = "load" config.option.dist = "load"
@@ -188,11 +196,10 @@ def pytest_cmdline_main(config):
config.option.dist = "load" config.option.dist = "load"
val = config.getvalue val = config.getvalue
if not val("collectonly"): if not val("collectonly"):
usepdb = config.getoption("usepdb") # a core option
if val("dist") != "no": if val("dist") != "no":
if usepdb: if usepdb:
raise pytest.UsageError( 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 ) # noqa: E501