diff --git a/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py b/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py index 2ad83e4..b742fad 100644 --- a/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py +++ b/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py @@ -1,14 +1,10 @@ import pytest from functools import singledispatch -from collections.abc import Iterable +from collections.abc import IterableABC as IterableABC from collections.abc import MappingView from collections import ChainMap from dataclasses import dataclass -from typing import ( - Generic, - Optional, - TypeVar, -) +from typing import Any, Dict, Generic, Optional, TypeVar, Iterable from enum import Enum, auto @@ -105,7 +101,7 @@ def help(self) -> str: class Group: - def __init__(self, name: str, settings: Iterable[Setting]): + def __init__(self, name: str, settings: IterableABC[Setting]): group_name = Name.normalize(name) self._name = group_name self._settings = tuple( @@ -137,12 +133,22 @@ class Category(Enum): class Resolver(MappingView): def __init__( - self, settings: Iterable[Setting], environment=None, cli_arguments=None + self, + settings: IterableABC[Setting], + cli_arguments=None, + environment=None, + config=None, ): self._settings = tuple(settings) self._default = {s.normalized_name: s.default for s in self._settings} - self._env = environment or {} - self._cli = cli_arguments or {} + self._config = {} + self._env = {} + self._cli = {} + + self.update(config, Category.ConfigFile) + self.update(environment, Category.Environment) + self.update(cli_arguments, Category.CommandLine) + self._kwargs = ChainMap(self._cli, self._env, self._default) def __len__(self): @@ -158,7 +164,16 @@ def __iter__(self): def settings(self): return self._kwargs - def _environment(self, environment): + def _update_cli(self, cli_arguments): + cli = { + setting.normalized_name: getattr(cli_arguments, setting.pytest) + for setting in self.options + if hasattr(cli_arguments, setting.pytest) + and getattr(cli_arguments, setting.pytest) is not None + } + self._cli.update(cli) + + def _update_environment(self, environment): env = { setting.normalized_name: setting.type(environment[setting.env]) for setting in self._settings @@ -166,24 +181,36 @@ def _environment(self, environment): } self._env.update(env) - def _cli(self, cli_arguments): - cli = { - setting.normalized_name: getattr(cli_arguments, setting.pytest) - for setting in self.options - if hasattr(cli_arguments, setting.pytest) - and getattr(cli_arguments, setting.pytest) is not None + def _update_config_file(self, config): + cfg = { + setting.normalized_name: setting.type(config[setting.ini]) + for setting in self._settings + if setting.ini in config } - self._cli.update(cli) + self._config.update(cfg) def update(self, mapping, category): dispatcher = { - Category.Environment: self._environment, - Category.CommandLine: self._cli, + Category.CommandLine: self._update_cli, + Category.Environment: self._update_environment, + Category.ConfigFile: self._update_config_file, } method = dispatcher[category] method(mapping) +def from_pytest(settings: Iterable[Setting]) -> Dict[str, Any]: + pass + + +def from_env(settings: Iterable[Setting]) -> Dict[str, Any]: + pass + + +def from_pytest_ini(settings: Iterable[Setting]) -> Dict[str, Any]: + pass + + @singledispatch def add_to_pytest_settings(obj, parser): raise TypeError("Unsupported type") @@ -196,7 +223,7 @@ def _(setting: Setting, parser): @add_to_pytest_settings.register -def _(settings: Iterable, parser): +def _(settings: IterableABC, parser): for setting in settings: add_to_pytest_settings(setting, parser) diff --git a/pytest-plugin-settings/test/unit/resolver_test.py b/pytest-plugin-settings/test/unit/resolver_test.py index e69de29..501117d 100644 --- a/pytest-plugin-settings/test/unit/resolver_test.py +++ b/pytest-plugin-settings/test/unit/resolver_test.py @@ -0,0 +1,33 @@ +import pytest + + +def test_constructor(): + pass + + +def test_default(): + pass + + +def test_config(): + pass + + +def test_env(): + pass + + +def test_cli(): + pass + + +def test_update_config(): + pass + + +def test_update_environment(): + pass + + +def test_update_cli(): + pass