From f5c9b0f08c9bd67a748b9d7cb269461155ee4c78 Mon Sep 17 00:00:00 2001 From: Nicola Coretti Date: Tue, 18 Jun 2024 18:03:05 +0200 Subject: [PATCH] WIP --- .../exasol/pytest_plugin_settings/__init__.py | 31 ++++--- .../test/integration/conftest.py | 90 +++++++++++++++++++ .../integration/register_setting_on_pytest.py | 4 + 3 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 pytest-plugin-settings/test/integration/conftest.py create mode 100644 pytest-plugin-settings/test/integration/register_setting_on_pytest.py diff --git a/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py b/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py index 180c2bb..e440f49 100644 --- a/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py +++ b/pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py @@ -1,7 +1,7 @@ import os import pytest from functools import singledispatch -from collections.abc import IterableABC as IterableAbc +from collections.abc import Iterable as IterableAbc from collections.abc import MappingView from collections import ChainMap from dataclasses import dataclass @@ -74,25 +74,29 @@ class Setting(Generic[T]): default: Optional[T] = None help_text: str = "" + @property + def _name(self) -> Name: + return Name(f"{self.prefix}-{self.name}") + @property def normalized_name(self) -> str: - return str(Name(f"{self.prefix}-{self.name}")) + return str(self._name) @property def env(self) -> str: - return self.normalized_name.env() + return self._name.env @property def cli(self) -> str: - return self.normalized_name.cli() + return self._name.cli @property def ini(self) -> str: - return self.normalized_name.ini() + return self._name.ini @property def pytest(self) -> str: - return self.normalized_name.pytest() + return self._name.pytest @property def help(self) -> str: @@ -177,7 +181,7 @@ def update(self, mapping, category): def _update_cli(self, cli_arguments): cli = { setting.normalized_name: getattr(cli_arguments, setting.pytest) - for setting in self.options + for setting in self._settings if hasattr(cli_arguments, setting.pytest) and getattr(cli_arguments, setting.pytest) is not None } @@ -205,32 +209,33 @@ def __init__(self, settings: Iterable[Setting], config: pytest.Config): cli = from_pytest(settings, config) cfg = from_pytest_ini(settings, config) env = os.environ - super().__init__(cli_arguments=cli, environment=env, config=cfg) + super().__init__(settings, cli_arguments=cli, environment=env, config=cfg) def from_pytest(settings: Iterable[Setting], config: pytest.Config) -> Dict[str, Any]: - pass + return {} def from_env(settings: Iterable[Setting], env: Dict[str, str]) -> Dict[str, Any]: - pass + return {} def from_pytest_ini( settings: Iterable[Setting], config: pytest.Config ) -> Dict[str, Any]: - pass + return {} @singledispatch def add_to_pytest_settings(obj, parser): - raise TypeError("Unsupported type") + raise TypeError(f"Unsupported type: {obj.__class__.__name__}, Object name: {obj.__name__}") @add_to_pytest_settings.register def _(setting: Setting, parser): parser.addoption(setting.cli, help=setting.help) - parser.addini(name=setting.ini, help=setting.help) + p = parser if hasattr(parser, "addini") else parser.parser + p.addini(name=setting.ini, help=setting.help) @add_to_pytest_settings.register diff --git a/pytest-plugin-settings/test/integration/conftest.py b/pytest-plugin-settings/test/integration/conftest.py new file mode 100644 index 0000000..6467ac8 --- /dev/null +++ b/pytest-plugin-settings/test/integration/conftest.py @@ -0,0 +1,90 @@ +import pytest +from inspect import cleandoc + +pytest_plugins = "pytester" + + +@pytest.fixture() +def conftest(pytester): + file = pytester.path.joinpath("conftest.py") + file.write_text( + cleandoc( + """ + import pytest + from exasol.pytest_plugin_settings import Group, Setting, PytestResolver, add_to_pytest_settings + + PORT = Setting(prefix='', name="port", type=int, default=9999, help_text="Port to connect to") + ADDRESS = Setting(prefix='', name="ip", type=str, default="127.0.0.1", help_text="IP to connect to") + + CREDENTIALS = Group(name='credentials', settings=[ + Setting(prefix=None, name="user", type=str, default="johndoe", help_text="Username"), + Setting(prefix=None, name="pw", type=str, default="password", help_text="Password") + ]) + + SETTINGS = [ + Setting(prefix=None, name="db-name", type=str, default="test.db", help_text="Name of the database"), + Setting(prefix=None, name="db-schema", type=str, default="test_schema", help_text="name of the schema"), + ] + + def pytest_addoption(parser): + # add a single settings to pytest + add_to_pytest_settings(PORT, parser) + add_to_pytest_settings(ADDRESS, parser) + # add a group of settings to pytest + add_to_pytest_settings(CREDENTIALS, parser) + # add a list of settings to pytest + add_to_pytest_settings(SETTINGS, parser) + + def pytest_configure(config): + all_settings = [ADDRESS, PORT] + [ s for s in CREDENTIALS.settings ] + SETTINGS + settings = PytestResolver(all_settings, config) + config.stash["my-plugin-settings"] = settings + + @pytest.fixture() + def my_settings(request): + settings = request.config.stash["my-plugin-settings"] + yield settings + + @pytest.fixture() + def port(my_settings): + yield my_settings["port"] + + @pytest.fixture() + def ip(my_settings): + yield my_settings["ip"] + + @pytest.fixture() + def user(my_settings): + yield my_settings["credentials_user"] + + @pytest.fixture() + def password(my_settings): + yield my_settings["credentials_pw"] + """ + ) + ) + file = pytester.path.joinpath("test_smoke.py") + file.write_text( + cleandoc( + """ + def test_settings(user, port): + print() + print(f"username: {user}") + print(f"port: {port}") + + def test_smoke_fail(): + assert False + + def test_smoke_succeed(): + assert True + """ + ) + ) + + +def test_basic_pytester(pytester, conftest): + result = pytester.runpytest("-s", "-vvv", ".") + #config = pytester.parseconfig() + #configure = pytester.parseconfigure() + result2 = pytester.runpytest("--help") + print(result) diff --git a/pytest-plugin-settings/test/integration/register_setting_on_pytest.py b/pytest-plugin-settings/test/integration/register_setting_on_pytest.py new file mode 100644 index 0000000..755db9c --- /dev/null +++ b/pytest-plugin-settings/test/integration/register_setting_on_pytest.py @@ -0,0 +1,4 @@ +from pytest import Pytester + + +pytest_plugins = "pytester"