|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from importlib.metadata import PackageNotFoundError, requires, version |
| 4 | + |
3 | 5 | from dandischema.models import DandiBaseModel |
| 6 | +from packaging.requirements import Requirement |
4 | 7 | from pytest import Config, Item, Parser |
5 | 8 |
|
6 | 9 | from .tests.fixtures import * # noqa: F401, F403 # lgtm [py/polluting-import] |
@@ -32,6 +35,29 @@ def pytest_configure(config): |
32 | 35 | config.addinivalue_line("markers", marker) |
33 | 36 |
|
34 | 37 |
|
| 38 | +def pytest_report_header(config: Config) -> list[str]: |
| 39 | + """Add version information for key dependencies to the pytest header.""" |
| 40 | + try: |
| 41 | + # Extract package names from requirement strings. |
| 42 | + # Format: "package-name >= 1.0" or "package-name ; condition" |
| 43 | + # and by regex thus we skip extras (in square brackets like [test]) |
| 44 | + deps = {Requirement(dep).name for dep in (requires("dandi") or [])} |
| 45 | + except PackageNotFoundError: |
| 46 | + # Use defaults if we didn't get deps from metadata |
| 47 | + deps = {"dandischema", "h5py", "hdmf"} |
| 48 | + |
| 49 | + # Format versions for display (sorted for consistent output) |
| 50 | + versions = [] |
| 51 | + for pkg in sorted(deps): |
| 52 | + try: |
| 53 | + version_str = f"-{version(pkg)}" |
| 54 | + except PackageNotFoundError: |
| 55 | + version_str = " NOT INSTALLED" |
| 56 | + versions.append(f"{pkg}{version_str}") |
| 57 | + |
| 58 | + return [f"dependencies: {', '.join(versions)}"] if versions else [] |
| 59 | + |
| 60 | + |
35 | 61 | def pytest_collection_modifyitems(items: list[Item], config: Config) -> None: |
36 | 62 | # Based on <https://pythontesting.net/framework/pytest/pytest-run-tests |
37 | 63 | # -using-particular-fixture/> |
|
0 commit comments