From 23e40adad366f4bf941228aa70f311462f417d38 Mon Sep 17 00:00:00 2001 From: DanCardin Date: Thu, 1 Feb 2024 10:48:34 -0500 Subject: [PATCH] feat: Add way for 3rd party resources to register into the PMR cli. (#199) --- pyproject.toml | 3 ++- src/pytest_mock_resources/cli.py | 9 +++++---- src/pytest_mock_resources/plugin.py | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/pytest_mock_resources/plugin.py diff --git a/pyproject.toml b/pyproject.toml index 00fa0b3..2357738 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pytest-mock-resources" -version = "2.9.2" +version = "2.10.0" description = "A pytest plugin for easily instantiating reproducible mock resources." authors = [ "Omar Khan ", @@ -28,6 +28,7 @@ pytest = {version = ">=1.0"} sqlalchemy = {version = ">1.0, !=1.4.0, !=1.4.1, !=1.4.2, !=1.4.3, !=1.4.4, !=1.4.5, !=1.4.6, !=1.4.7, !=1.4.8, !=1.4.9, !=1.4.10, !=1.4.11, !=1.4.12, !=1.4.13, !=1.4.14, !=1.4.15, !=1.4.16, !=1.4.17, !=1.4.18, !=1.4.19, !=1.4.20, !=1.4.21, !=1.4.22, !=1.4.23"} typing_extensions = "*" +importlib-metadata = {version = "*", python = "<3.8"} # extra [postgres] psycopg2 = {version = "*", optional = true} diff --git a/src/pytest_mock_resources/cli.py b/src/pytest_mock_resources/cli.py index 5514119..cde8e93 100644 --- a/src/pytest_mock_resources/cli.py +++ b/src/pytest_mock_resources/cli.py @@ -1,12 +1,12 @@ from __future__ import annotations import argparse -import importlib import sys from pytest_mock_resources.config import DockerContainerConfig from pytest_mock_resources.container.base import container_name, get_container from pytest_mock_resources.hooks import get_docker_client +from pytest_mock_resources.plugin import find_entrypoints, load_entrypoints class StubPytestConfig: @@ -22,12 +22,13 @@ def getini(self, attr): def main(): + entrypoints = find_entrypoints() + load_entrypoints(entrypoints) + parser = create_parser() args = parser.parse_args() - if args.load: - for module in args.load: - importlib.import_module(module) + load_entrypoints(args.load) pytestconfig = StubPytestConfig() diff --git a/src/pytest_mock_resources/plugin.py b/src/pytest_mock_resources/plugin.py new file mode 100644 index 0000000..d3bac84 --- /dev/null +++ b/src/pytest_mock_resources/plugin.py @@ -0,0 +1,24 @@ +import importlib +import sys +from typing import Iterable + +if sys.version_info < (3, 8): + import importlib_metadata +else: + import importlib.metadata as importlib_metadata + + +def find_entrypoints() -> Iterable[str]: + modules = set() + for dist in importlib_metadata.distributions(): + for ep in dist.entry_points: + if ep.group.lower() != "pmr": + continue + + modules.add(ep.value) + return sorted(modules) + + +def load_entrypoints(modules: Iterable[str]): + for module in modules: + importlib.import_module(module)