Skip to content

Commit 6a9bf28

Browse files
committed
Apply review suggestions: use a simple struct for invocation params
1 parent 3e669a2 commit 6a9bf28

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/_pytest/config/__init__.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import types
99
import warnings
1010
from functools import lru_cache
11+
from pathlib import Path
1112

13+
import attr
1214
import py
1315
from packaging.version import Version
1416
from pluggy import HookimplMarker
@@ -70,8 +72,6 @@ def main(args=None, plugins=None):
7072
tw.line(line.rstrip(), red=True)
7173
return 4
7274
else:
73-
config.invocation_args = args
74-
config.invocation_plugins = plugins
7575
try:
7676
return config.hook.pytest_cmdline_main(config=config)
7777
finally:
@@ -149,10 +149,15 @@ def directory_arg(path, optname):
149149
builtin_plugins.add("pytester")
150150

151151

152-
def get_config(args=None):
152+
def get_config(args=None, plugins=None):
153153
# subsequent calls to main will create a fresh instance
154154
pluginmanager = PytestPluginManager()
155-
config = Config(pluginmanager)
155+
config = Config(
156+
pluginmanager,
157+
invocation_params=Config.InvocationParams(
158+
args=args, plugins=plugins, dir=Path().resolve()
159+
),
160+
)
156161

157162
if args is not None:
158163
# Handle any "-p no:plugin" args.
@@ -185,7 +190,7 @@ def _prepareconfig(args=None, plugins=None):
185190
msg = "`args` parameter expected to be a list or tuple of strings, got: {!r} (type: {})"
186191
raise TypeError(msg.format(args, type(args)))
187192

188-
config = get_config(args)
193+
config = get_config(args, plugins)
189194
pluginmanager = config.pluginmanager
190195
try:
191196
if plugins:
@@ -617,20 +622,36 @@ class Config:
617622
618623
:ivar argparse.Namespace option: access to command line option as attributes.
619624
620-
:ivar invocation_args: list of command-line arguments as passed to pytest.main()
625+
:ivar InvocationParams invocation_params:
621626
622-
:ivar invocation_plugins: list of extra plugins passed to pytest.main(), might be None
627+
Object containing the parameters regarding the ``pytest.main``
628+
invocation.
623629
624-
:ivar py.path.local invocation_dir: directory where pytest.main() was invoked from
630+
Contains the followinig read-only attributes:
631+
632+
* ``args``: list of command-line arguments as passed to ``pytest.main()``.
633+
* ``plugins``: list of extra plugins, might be None
634+
* ``dir``: directory where ``pytest.main()`` was invoked from.
625635
"""
626636

627-
def __init__(self, pluginmanager):
637+
@attr.s(frozen=True)
638+
class InvocationParams:
639+
"""Holds parameters passed during ``pytest.main()``"""
640+
641+
args = attr.ib()
642+
plugins = attr.ib()
643+
dir = attr.ib()
644+
645+
def __init__(self, pluginmanager, *, invocation_params=None):
628646
from .argparsing import Parser, FILE_OR_DIR
629647

648+
if invocation_params is None:
649+
invocation_params = self.InvocationParams(
650+
args=(), plugins=None, dir=Path().resolve()
651+
)
652+
630653
self.option = argparse.Namespace()
631-
self.invocation_args = None
632-
self.invocation_plugins = None
633-
self.invocation_dir = py.path.local()
654+
self.invocation_params = invocation_params
634655

635656
_a = FILE_OR_DIR
636657
self._parser = Parser(
@@ -648,6 +669,11 @@ def __init__(self, pluginmanager):
648669
self._configured = False
649670
self.hook.pytest_addoption.call_historic(kwargs=dict(parser=self._parser))
650671

672+
@property
673+
def invocation_dir(self):
674+
"""Backward compatibility"""
675+
return py.path.local(str(self.invocation_params.dir))
676+
651677
def add_cleanup(self, func):
652678
""" Add a function to be called when the config object gets out of
653679
use (usually coninciding with pytest_unconfigure)."""

testing/test_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import textwrap
3+
from pathlib import Path
34

45
import _pytest._code
56
import pytest
@@ -1212,9 +1213,10 @@ class DummyPlugin:
12121213
call = calls[0]
12131214
config = call.item.config
12141215

1215-
assert config.invocation_args == [p, "-v"]
1216+
assert config.invocation_params.args == [p, "-v"]
1217+
assert config.invocation_params.dir == Path(testdir.tmpdir)
12161218

1217-
plugins = config.invocation_plugins
1219+
plugins = config.invocation_params.plugins
12181220
assert len(plugins) == 2
12191221
assert plugins[0] is plugin
12201222
assert type(plugins[1]).__name__ == "Collect" # installed by testdir.inline_run()

0 commit comments

Comments
 (0)