Example #1
0
    def __init__(
        self,
        name: str,
        parent: "Optional[Node]" = None,
        config: Optional[Config] = None,
        session: "Optional[Session]" = None,
        fspath: Optional[py.path.local] = None,
        nodeid: Optional[str] = None,
    ) -> None:
        #: A unique name within the scope of the parent node.
        self.name = name

        #: The parent collector node.
        self.parent = parent

        #: The pytest config object.
        if config:
            self.config: Config = config
        else:
            if not parent:
                raise TypeError("config or parent must be provided")
            self.config = parent.config

        #: The pytest session this node is part of.
        if session:
            self.session = session
        else:
            if not parent:
                raise TypeError("session or parent must be provided")
            self.session = parent.session

        #: Filesystem path where this node was collected from (can be None).
        self.fspath = fspath or getattr(parent, "fspath", None)

        #: Keywords/markers collected from all scopes.
        self.keywords = NodeKeywords(self)

        #: The marker objects belonging to this node.
        self.own_markers: List[Mark] = []

        #: Allow adding of extra keywords to use for matching.
        self.extra_keyword_matches: Set[str] = set()

        if nodeid is not None:
            assert "::()" not in nodeid
            self._nodeid = nodeid
        else:
            if not self.parent:
                raise TypeError("nodeid or parent must be provided")
            self._nodeid = self.parent.nodeid
            if self.name != "()":
                self._nodeid += "::" + self.name

        # A place where plugins can store information on the node for their
        # own use. Currently only intended for internal plugins.
        self._store = Store()
Example #2
0
    def __init__(
        self,
        pluginmanager: PytestPluginManager,
        *,
        invocation_params: Optional[InvocationParams] = None
    ) -> None:
        from .argparsing import Parser, FILE_OR_DIR

        if invocation_params is None:
            invocation_params = self.InvocationParams(
                args=(), plugins=None, dir=Path.cwd()
            )

        self.option = argparse.Namespace()
        """Access to command line option as attributes.

        :type: argparse.Namespace
        """

        self.invocation_params = invocation_params

        _a = FILE_OR_DIR
        self._parser = Parser(
            usage="%(prog)s [options] [{}] [{}] [...]".format(_a, _a),
            processopt=self._processopt,
        )
        self.pluginmanager = pluginmanager
        """The plugin manager handles plugin registration and hook invocation.

        :type: PytestPluginManager.
        """

        self.trace = self.pluginmanager.trace.root.get("config")
        self.hook = self.pluginmanager.hook
        self._inicache = {}  # type: Dict[str, Any]
        self._override_ini = ()  # type: Sequence[str]
        self._opt2dest = {}  # type: Dict[str, str]
        self._cleanup = []  # type: List[Callable[[], None]]
        # A place where plugins can store information on the config for their
        # own use. Currently only intended for internal plugins.
        self._store = Store()
        self.pluginmanager.register(self, "pytestconfig")
        self._configured = False
        self.hook.pytest_addoption.call_historic(
            kwargs=dict(parser=self._parser, pluginmanager=self.pluginmanager)
        )

        if TYPE_CHECKING:
            from _pytest.cacheprovider import Cache

            self.cache = None  # type: Optional[Cache]
Example #3
0
def test_store() -> None:
    store = Store()

    key1 = StoreKey[str]()
    key2 = StoreKey[int]()

    # Basic functionality - single key.
    assert key1 not in store
    store[key1] = "hello"
    assert key1 in store
    assert store[key1] == "hello"
    assert store.get(key1, None) == "hello"
    store[key1] = "world"
    assert store[key1] == "world"
    # Has correct type (no mypy error).
    store[key1] + "string"

    # No interaction with another key.
    assert key2 not in store
    assert store.get(key2, None) is None
    with pytest.raises(KeyError):
        store[key2]
    with pytest.raises(KeyError):
        del store[key2]
    store[key2] = 1
    assert store[key2] == 1
    # Has correct type (no mypy error).
    store[key2] + 20
    del store[key1]
    with pytest.raises(KeyError):
        del store[key1]
    with pytest.raises(KeyError):
        store[key1]

    # Can't accidentally add attributes to store object itself.
    with pytest.raises(AttributeError):
        store.foo = "nope"  # type: ignore[attr-defined] # noqa: F821

    # No interaction with anoter store.
    store2 = Store()
    key3 = StoreKey[int]()
    assert key2 not in store2
    store2[key2] = 100
    store2[key3] = 200
    assert store2[key2] + store2[key3] == 300
    assert store[key2] == 1
    assert key3 not in store