diff --git a/.gitignore b/.gitignore index 68bc17f..2dc53ca 100644 --- a/.gitignore +++ b/.gitignore @@ -157,4 +157,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/{{cookiecutter.package_name}}/pyproject.toml b/{{cookiecutter.package_name}}/pyproject.toml index 4da52a3..d85911d 100644 --- a/{{cookiecutter.package_name}}/pyproject.toml +++ b/{{cookiecutter.package_name}}/pyproject.toml @@ -38,6 +38,7 @@ DJANGO_SETTINGS_MODULE="tests.settings" test = [ "freezegun", "pytest", + "pytest-apiver @ git+https://github.com/reef-technologies/pytest_apiver.git", "pytest-asyncio", # COOKIECUTTER{%- if cookiecutter.is_django_package == "y" %} "pytest-django", diff --git a/{{cookiecutter.package_name}}/tests/apiver_fixtures.py b/{{cookiecutter.package_name}}/tests/apiver_fixtures.py deleted file mode 100644 index a171db6..0000000 --- a/{{cookiecutter.package_name}}/tests/apiver_fixtures.py +++ /dev/null @@ -1,105 +0,0 @@ -from __future__ import annotations - -import importlib.util -import itertools -import re -from pathlib import Path - -import pytest - - -def pytest_addoption(parser): - group = parser.getgroup("apiver", "Test API versions.") - group.addoption( - "--target-package-name", - action="store", - dest="target_package_name", - help="The name of the package to test API versions of.", - ) - parser.addini( - "target_package_name", - help="The name of the package to test API versions of.", - default=None, - ) - - -def get_package_name(config) -> str: - package_name = config.getoption("target_package_name") or config.getini("target_package_name") - assert package_name, "The --target-package-name CLI option or target_package_name INI option is required." - return package_name - - -def get_api_versions(package_name: str) -> list[int]: - versions = set() - - package_spec = importlib.util.find_spec(package_name) - if not package_spec: - raise ValueError(f"Package {package_name=!r} not found. Possibly `--target-package-name` is incorrect.") - assert package_spec.origin - package_path = Path(package_spec.origin).parent - - for apiver_package_path in itertools.chain( - package_path.glob("v*/__init__.py"), - package_path.glob("v*.py"), - ): - match = re.search(r"/_?v(\d+)(:?\.py|/__init__\.py)$", str(apiver_package_path)) - if match: - versions.add(int(match.group(1))) - assert versions, f"No API versions found in {package_path!r}" - return sorted(versions) - - -def pytest_generate_tests(metafunc): - if "apiver" in metafunc.fixturenames: - versions = metafunc.config.cache.get("apivers", None) - if versions is None: - package_name = get_package_name(metafunc.config) - versions = get_api_versions(package_name) - metafunc.config.cache.set("apivers", versions) - markers = [mark for mark in metafunc.definition.iter_markers(name="apiver")] - if markers: - applicable_versions = set() - for marker in markers: - for version in marker.args: - if isinstance(version, tuple): - applicable_versions.update(range(version[0], version[1] + 1)) - elif isinstance(version, int): - applicable_versions.add(version) - for key, value in marker.kwargs.items(): - if key == "from_ver": - applicable_versions.update(range(value, max(versions) + 1)) - elif key == "to_ver": - applicable_versions.update(range(min(versions), value + 1)) - applicable_versions = sorted(applicable_versions & set(versions)) - else: - applicable_versions = versions - metafunc.parametrize("apiver", [f"v{ver}" for ver in applicable_versions]) - - -@pytest.fixture(scope="session") -def apiver_tested_package_name(request): - return get_package_name(request.config) - - -@pytest.fixture -def apiver_int(apiver): - """Get apiver as an int, e.g., 2.""" - return int(apiver[1:]) - - -@pytest.fixture -def apiver_import(apiver, apiver_tested_package_name): - def importer(module_name: str | None = None): - module_name = ( - f"{apiver_tested_package_name}.{apiver}.{module_name}" - if module_name - else f"{apiver_tested_package_name}.{apiver}" - ) - return importlib.import_module(module_name) - - return importer - - -@pytest.fixture -def apiver_deps(apiver_import): - return apiver_import() diff --git a/{{cookiecutter.package_name}}/tests/conftest.py b/{{cookiecutter.package_name}}/tests/conftest.py index 8ff1531..4dd6d9c 100644 --- a/{{cookiecutter.package_name}}/tests/conftest.py +++ b/{{cookiecutter.package_name}}/tests/conftest.py @@ -1,4 +1,3 @@ -from .apiver_fixtures import * # noqa: F401, F403 # COOKIECUTTER{%- if cookiecutter.is_django_package == "y" -%} from .django_fixtures import * # noqa: F401, F403 # COOKIECUTTER{%- endif %} diff --git a/{{cookiecutter.package_name}}/tests/unit/api/test_setup.py b/{{cookiecutter.package_name}}/tests/unit/api/test_setup.py index ed3afa8..4343b66 100644 --- a/{{cookiecutter.package_name}}/tests/unit/api/test_setup.py +++ b/{{cookiecutter.package_name}}/tests/unit/api/test_setup.py @@ -1,2 +1,2 @@ -def test_apiver_exports(apiver_deps): - assert sorted(name for name in dir(apiver_deps) if not name.startswith("_")) == [] +def test_apiver_exports(apiver_module): + assert sorted(name for name in dir(apiver_module) if not name.startswith("_")) == []