diff --git a/src/ansible_dev_tools/version_builder.py b/src/ansible_dev_tools/version_builder.py index cf1a14c9..22e23e25 100644 --- a/src/ansible_dev_tools/version_builder.py +++ b/src/ansible_dev_tools/version_builder.py @@ -26,7 +26,10 @@ def version_builder() -> str: """ lines = [] for pkg in sorted(PKGS): - version = importlib.metadata.version(pkg) + try: + version = importlib.metadata.version(pkg) + except importlib.metadata.PackageNotFoundError: + version = "not installed" lines.append(f"{pkg: <40} {version}") return "\n".join(lines) diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 00000000..3576137e --- /dev/null +++ b/tests/unit/__init__.py @@ -0,0 +1 @@ +"""Unit tests for the project.""" diff --git a/tests/unit/test_version_builder.py b/tests/unit/test_version_builder.py new file mode 100644 index 00000000..f50f3293 --- /dev/null +++ b/tests/unit/test_version_builder.py @@ -0,0 +1,22 @@ +"""Tests for the version builder.""" + +import re + +import pytest + +from ansible_dev_tools.version_builder import PKGS, version_builder + + +def test_version_builder_success() -> None: + """Test the version builder.""" + versions = version_builder() + assert all(p in versions for p in PKGS) + + +def test_version_builder_failure(monkeypatch: pytest.MonkeyPatch) -> None: + """Test the version builder.""" + monkeypatch.setattr("ansible_dev_tools.version_builder.PKGS", ["__invalid__"]) + + versions = version_builder() + + assert re.search(r"__invalid__\s+not installed", versions)