8
8
import types
9
9
import warnings
10
10
from functools import lru_cache
11
+ from pathlib import Path
11
12
13
+ import attr
12
14
import py
13
15
from packaging .version import Version
14
16
from pluggy import HookimplMarker
@@ -70,8 +72,6 @@ def main(args=None, plugins=None):
70
72
tw .line (line .rstrip (), red = True )
71
73
return 4
72
74
else :
73
- config .invocation_args = args
74
- config .invocation_plugins = plugins
75
75
try :
76
76
return config .hook .pytest_cmdline_main (config = config )
77
77
finally :
@@ -149,10 +149,15 @@ def directory_arg(path, optname):
149
149
builtin_plugins .add ("pytester" )
150
150
151
151
152
- def get_config (args = None ):
152
+ def get_config (args = None , plugins = None ):
153
153
# subsequent calls to main will create a fresh instance
154
154
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
+ )
156
161
157
162
if args is not None :
158
163
# Handle any "-p no:plugin" args.
@@ -185,7 +190,7 @@ def _prepareconfig(args=None, plugins=None):
185
190
msg = "`args` parameter expected to be a list or tuple of strings, got: {!r} (type: {})"
186
191
raise TypeError (msg .format (args , type (args )))
187
192
188
- config = get_config (args )
193
+ config = get_config (args , plugins )
189
194
pluginmanager = config .pluginmanager
190
195
try :
191
196
if plugins :
@@ -617,20 +622,36 @@ class Config:
617
622
618
623
:ivar argparse.Namespace option: access to command line option as attributes.
619
624
620
- :ivar invocation_args: list of command-line arguments as passed to pytest.main()
625
+ :ivar InvocationParams invocation_params:
621
626
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.
623
629
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.
625
635
"""
626
636
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 ):
628
646
from .argparsing import Parser , FILE_OR_DIR
629
647
648
+ if invocation_params is None :
649
+ invocation_params = self .InvocationParams (
650
+ args = (), plugins = None , dir = Path ().resolve ()
651
+ )
652
+
630
653
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
634
655
635
656
_a = FILE_OR_DIR
636
657
self ._parser = Parser (
@@ -648,6 +669,11 @@ def __init__(self, pluginmanager):
648
669
self ._configured = False
649
670
self .hook .pytest_addoption .call_historic (kwargs = dict (parser = self ._parser ))
650
671
672
+ @property
673
+ def invocation_dir (self ):
674
+ """Backward compatibility"""
675
+ return py .path .local (str (self .invocation_params .dir ))
676
+
651
677
def add_cleanup (self , func ):
652
678
""" Add a function to be called when the config object gets out of
653
679
use (usually coninciding with pytest_unconfigure)."""
0 commit comments