Skip to content

Commit 7939e53

Browse files
authored
Merge pull request #4957 from blueyed/config-handle-pno-with-default-plugins
config: handle `-p no:plugin` with default plugins
2 parents 8dda561 + f717103 commit 7939e53

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

changelog/4957.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``-p no:plugin`` is handled correctly for default (internal) plugins now, e.g. with ``-p no:capture``.
2+
3+
Previously they were loaded (imported) always, making e.g. the ``capfd`` fixture available.

src/_pytest/config/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,15 @@ def directory_arg(path, optname):
147147
builtin_plugins.add("pytester")
148148

149149

150-
def get_config():
150+
def get_config(args=None):
151151
# subsequent calls to main will create a fresh instance
152152
pluginmanager = PytestPluginManager()
153153
config = Config(pluginmanager)
154+
155+
if args is not None:
156+
# Handle any "-p no:plugin" args.
157+
pluginmanager.consider_preparse(args)
158+
154159
for spec in default_plugins:
155160
pluginmanager.import_plugin(spec)
156161
return config
@@ -178,7 +183,7 @@ def _prepareconfig(args=None, plugins=None):
178183
msg = "`args` parameter expected to be a list or tuple of strings, got: {!r} (type: {})"
179184
raise TypeError(msg.format(args, type(args)))
180185

181-
config = get_config()
186+
config = get_config(args)
182187
pluginmanager = config.pluginmanager
183188
try:
184189
if plugins:
@@ -713,7 +718,7 @@ def cwd_relative_nodeid(self, nodeid):
713718
@classmethod
714719
def fromdictargs(cls, option_dict, args):
715720
""" constructor useable for subprocesses. """
716-
config = get_config()
721+
config = get_config(args)
717722
config.option.__dict__.update(option_dict)
718723
config.parse(args, addopts=False)
719724
for x in config.option.plugins:

src/_pytest/terminal.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def __init__(self, config, file=None):
246246
def _determine_show_progress_info(self):
247247
"""Return True if we should display progress information based on the current config"""
248248
# do not show progress if we are not capturing output (#3038)
249-
if self.config.getoption("capture") == "no":
249+
if self.config.getoption("capture", "no") == "no":
250250
return False
251251
# do not show progress if we are showing fixture setup/teardown
252252
if self.config.getoption("setupshow"):
@@ -456,8 +456,6 @@ def pytest_runtest_logfinish(self, nodeid):
456456
self._tw.write(msg + "\n", cyan=True)
457457

458458
def _get_progress_information_message(self):
459-
if self.config.getoption("capture") == "no":
460-
return ""
461459
collected = self._session.testscollected
462460
if self.config.getini("console_output_style") == "count":
463461
if collected:

testing/test_config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from _pytest.config.findpaths import get_common_ancestor
1616
from _pytest.config.findpaths import getcfg
1717
from _pytest.main import EXIT_NOTESTSCOLLECTED
18+
from _pytest.main import EXIT_TESTSFAILED
1819
from _pytest.main import EXIT_USAGEERROR
1920

2021

@@ -1176,3 +1177,15 @@ def pytest_addoption(parser):
11761177
["*pytest*{}*imported from*".format(pytest.__version__)]
11771178
)
11781179
assert result.ret == EXIT_USAGEERROR
1180+
1181+
1182+
def test_config_does_not_load_blocked_plugin_from_args(testdir):
1183+
"""This tests that pytest's config setup handles "-p no:X"."""
1184+
p = testdir.makepyfile("def test(capfd): pass")
1185+
result = testdir.runpytest(str(p), "-pno:capture")
1186+
result.stdout.fnmatch_lines(["E fixture 'capfd' not found"])
1187+
assert result.ret == EXIT_TESTSFAILED
1188+
1189+
result = testdir.runpytest(str(p), "-pno:capture", "-s")
1190+
result.stderr.fnmatch_lines(["*: error: unrecognized arguments: -s"])
1191+
assert result.ret == EXIT_USAGEERROR

0 commit comments

Comments
 (0)