Skip to content

Commit

Permalink
feat: Add way for 3rd party resources to register into the PMR cli. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Feb 1, 2024
1 parent 7ef324e commit 23e40ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <oakhan3@gmail.com>",
Expand Down Expand Up @@ -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}
Expand Down
9 changes: 5 additions & 4 deletions src/pytest_mock_resources/cli.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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()

Expand Down
24 changes: 24 additions & 0 deletions src/pytest_mock_resources/plugin.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 23e40ad

Please sign in to comment.