Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions commit_check/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__version__ (package version)
"""

from importlib.metadata import version
from importlib.metadata import version, PackageNotFoundError

# Exit codes used across the package
PASS = 0
Expand Down Expand Up @@ -76,4 +76,7 @@
DEFAULT_AI_ATTRIBUTION = "ignore" # "ignore" | "forbid"


__version__ = version("commit-check")
try:
__version__ = version("commit-check")
except PackageNotFoundError:
__version__ = "0.0.0.dev"
28 changes: 28 additions & 0 deletions tests/version_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Tests for commit_check.__version__."""

import importlib
import pytest
from unittest.mock import patch
from importlib.metadata import PackageNotFoundError
import commit_check


class TestVersion:
"""Tests for __version__ resolution."""

@pytest.mark.benchmark
def test_version_is_string_when_installed(self):
"""When the package is installed, __version__ must be a non-empty string."""
assert isinstance(commit_check.__version__, str)
assert len(commit_check.__version__) > 0

@pytest.mark.benchmark
def test_version_fallback_when_not_installed(self):
"""When PackageNotFoundError is raised, __version__ must fall back to '0.0.0.dev'."""
with patch("importlib.metadata.version", side_effect=PackageNotFoundError):
importlib.reload(commit_check)
assert commit_check.__version__ == "0.0.0.dev"

# Reload again to restore original version
importlib.reload(commit_check)
assert isinstance(commit_check.__version__, str)