Skip to content

Commit

Permalink
Improve handling of missing component (#317)
Browse files Browse the repository at this point in the history
* Improve handling of missing component

* docstring
  • Loading branch information
cidrblock authored Jul 17, 2024
1 parent 6c99679 commit a01c138
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ansible_dev_tools/version_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Unit tests for the project."""
22 changes: 22 additions & 0 deletions tests/unit/test_version_builder.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a01c138

Please sign in to comment.