Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions pacta/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ._version import _detect_version

PACTA_VERSION = _detect_version()

__all__ = ["PACTA_VERSION"]
15 changes: 15 additions & 0 deletions pacta/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from importlib.metadata import version
from importlib.metadata import PackageNotFoundError


def _detect_version() -> str:
"""
Detect pacta version.

Falls back to a development placeholder if the package metadata
is not available.
"""
try:
return version("pacta")
except PackageNotFoundError:
return "0.0.0-dev"
3 changes: 2 additions & 1 deletion pacta/cli/_engine_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from typing import Any

from pacta import PACTA_VERSION
from pacta.core.config import EngineConfig
from pacta.core.engine import DefaultPactaEngine
from pacta.reporting.builder import DefaultReportBuilder
Expand Down Expand Up @@ -95,7 +96,7 @@ def run_engine_scan(
metadata={},
)

builder = DefaultReportBuilder(tool="pacta", version=tool_version or "0.0.7")
builder = DefaultReportBuilder(tool="pacta", version=tool_version or PACTA_VERSION)

if isinstance(res, Mapping):
violations = res.get("violations", ())
Expand Down
3 changes: 2 additions & 1 deletion pacta/reporting/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime, timezone
from typing import Any

from pacta import PACTA_VERSION
from pacta.reporting._extract import get_field
from pacta.reporting.types import (
DiffSummary,
Expand Down Expand Up @@ -71,7 +72,7 @@ class DefaultReportBuilder:
- Summary counts
"""

def __init__(self, *, tool: str = "pacta", version: str = "0.0.7") -> None:
def __init__(self, *, tool: str = "pacta", version: str = PACTA_VERSION) -> None:
self._tool = tool
self._version = version

Expand Down
5 changes: 3 additions & 2 deletions tests/cli/test_check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import patch

from pacta import PACTA_VERSION
from pacta.cli.main import main
from pacta.core.engine import CheckResult
from pacta.reporting.types import Report, RuleRef, RunInfo, Severity, Summary, Violation
Expand Down Expand Up @@ -31,7 +32,7 @@ def _make_report(repo_root: str, violations=None, engine_errors=None) -> Report:

return Report(
tool="pacta",
version="0.0.7",
version=PACTA_VERSION,
run=RunInfo(
repo_root=repo_root,
commit=None,
Expand All @@ -41,7 +42,7 @@ def _make_report(repo_root: str, violations=None, engine_errors=None) -> Report:
baseline_ref=None,
mode="full",
created_at=None,
tool_version="0.0.7",
tool_version=PACTA_VERSION,
metadata={},
),
summary=Summary(
Expand Down
5 changes: 3 additions & 2 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from unittest.mock import patch

from pacta import PACTA_VERSION
from pacta.cli.main import main
from pacta.reporting.types import EngineError, Report, RuleRef, RunInfo, Severity, Summary, Violation

Expand Down Expand Up @@ -36,7 +37,7 @@ def create_test_report(repo_root, violations=None, engine_errors=None, **overrid

return Report(
tool="pacta",
version="0.0.7",
version=PACTA_VERSION,
run=RunInfo(
repo_root=str(repo_root),
commit=None,
Expand All @@ -46,7 +47,7 @@ def create_test_report(repo_root, violations=None, engine_errors=None, **overrid
baseline_ref=overrides.get("baseline_ref"),
mode=overrides.get("mode", "full"),
created_at=None,
tool_version="0.0.7",
tool_version=PACTA_VERSION,
metadata={},
),
summary=summary,
Expand Down
9 changes: 5 additions & 4 deletions tests/reporting/test_github_renderer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pacta import PACTA_VERSION
from pacta.reporting.renderers.github import GitHubReportRenderer
from pacta.reporting.types import (
DiffSummary,
Expand All @@ -23,7 +24,7 @@ def _make_run(**overrides):
baseline_ref="baseline",
mode="full",
created_at="2025-01-22T12:00:00+00:00",
tool_version="0.0.7",
tool_version=PACTA_VERSION,
metadata={},
)
defaults.update(overrides)
Expand Down Expand Up @@ -71,7 +72,7 @@ def _make_report(*, violations=(), diff=None, trends=None, baseline_ref="baselin

return Report(
tool="pacta",
version="0.0.7",
version=PACTA_VERSION,
run=_make_run(baseline_ref=baseline_ref),
summary=Summary(
total_violations=len(all_v),
Expand Down Expand Up @@ -99,7 +100,7 @@ def test_header_includes_branch_and_commit(self):
def test_no_branch_or_commit(self):
report = Report(
tool="pacta",
version="0.0.7",
version=PACTA_VERSION,
run=_make_run(branch=None, commit=None, baseline_ref=None),
summary=Summary(total_violations=0, by_severity={}, by_status={}, by_rule={}, engine_errors=0),
violations=(),
Expand Down Expand Up @@ -239,7 +240,7 @@ def test_footer_includes_version(self):
report = _make_report()
out = GitHubReportRenderer().render(report)
assert "Pacta" in out
assert "v0.0.7" in out
assert PACTA_VERSION in out


class TestGitHubRendererFullOutput:
Expand Down
Loading