Skip to content

Fix incompatibility error with --pdb despite using -n0 #938

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

Merged
merged 2 commits into from
Mar 5, 2024
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
1 change: 1 addition & 0 deletions changelog/937.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where plugin would raise an incompatibility error with ``--pdb`` despite using ``-n0``.
22 changes: 18 additions & 4 deletions src/xdist/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def pytest_configure(config):

# Create the distributed session in case we have a valid distribution
# mode and test environments.
if config.getoption("dist") != "no" and config.getoption("tx"):
if _is_distribution_mode(config):
from xdist.dsession import DSession

session = DSession(config)
Expand All @@ -252,8 +252,19 @@ def pytest_configure(config):
config.issue_config_time_warning(warning, 2)


def _is_distribution_mode(config):
"""Return `True` if distribution mode is on, `False` otherwise.

:param config: the `pytest` `config` object
"""
return config.getoption("dist") != "no" and config.getoption("tx")


@pytest.hookimpl(tryfirst=True)
def pytest_cmdline_main(config):
if config.option.distload:
config.option.dist = "load"

usepdb = config.getoption("usepdb", False) # a core option
if config.option.numprocesses in ("auto", "logical"):
if usepdb:
Expand All @@ -270,10 +281,13 @@ def pytest_cmdline_main(config):
if config.option.maxprocesses:
numprocesses = min(numprocesses, config.option.maxprocesses)
config.option.tx = ["popen"] * numprocesses
if config.option.distload:
config.option.dist = "load"

if config.option.numprocesses == 0:
config.option.dist = "no"
config.option.tx = []

val = config.getvalue
if not val("collectonly") and val("dist") != "no" and usepdb:
if not val("collectonly") and _is_distribution_mode(config) and usepdb:
raise pytest.UsageError(
"--pdb is incompatible with distributing tests; try using -n0 or -nauto."
) # noqa: E501
Expand Down
34 changes: 23 additions & 11 deletions testing/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ def test_dist_options(pytester: pytest.Pytester) -> None:
check_options(config)
assert config.option.dist == "load"

config = pytester.parseconfigure("--numprocesses", "0")
check_options(config)
assert config.option.dist == "no"
assert config.option.tx == []

config = pytester.parseconfigure("--numprocesses", "0", "-d")
check_options(config)
assert config.option.dist == "no"
assert config.option.tx == []

config = pytester.parseconfigure(
"--numprocesses", "0", "--dist", "each", "--tx", "2*popen"
)
check_options(config)
assert config.option.dist == "no"
assert config.option.tx == []
Copy link
Member

Choose a reason for hiding this comment

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

Don't we want to also test passing -n0 and --pdb explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Surely we do, thanks! Addressed in 0de6180.



def test_auto_detect_cpus(
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
Expand Down Expand Up @@ -77,17 +94,12 @@ def test_auto_detect_cpus(
check_options(config)
assert config.getoption("numprocesses") == 99

config = pytester.parseconfigure("-nauto", "--pdb")
check_options(config)
assert config.getoption("usepdb")
assert config.getoption("numprocesses") == 0
assert config.getoption("dist") == "no"

config = pytester.parseconfigure("-nlogical", "--pdb")
check_options(config)
assert config.getoption("usepdb")
assert config.getoption("numprocesses") == 0
assert config.getoption("dist") == "no"
for numprocesses in (0, "auto", "logical"):
config = pytester.parseconfigure(f"-n{numprocesses}", "--pdb")
check_options(config)
assert config.getoption("usepdb")
assert config.getoption("numprocesses") == 0
assert config.getoption("dist") == "no"

monkeypatch.delattr(os, "sched_getaffinity", raising=False)
monkeypatch.setenv("TRAVIS", "true")
Expand Down