Show package version number on kanta CLI
This commit is contained in:
+18
-1
@@ -6,6 +6,7 @@ import argparse
|
||||
import asyncio
|
||||
import contextlib
|
||||
import importlib
|
||||
import importlib.metadata
|
||||
import importlib.util
|
||||
import logging
|
||||
import sys
|
||||
@@ -161,10 +162,26 @@ def _format_ts(dt) -> str:
|
||||
return dt.replace(tzinfo=None, microsecond=0).isoformat(sep=" ")
|
||||
|
||||
|
||||
def _package_version() -> str:
|
||||
"""Return the installed package version, or ``"unknown"`` from a source tree."""
|
||||
try:
|
||||
return importlib.metadata.version("kanta")
|
||||
except importlib.metadata.PackageNotFoundError:
|
||||
return "unknown"
|
||||
|
||||
|
||||
def _parse_args(argv: list[str] | None = None) -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="kanta",
|
||||
description="Read a kantadb file and print each change record to the console.",
|
||||
description=(
|
||||
f"kanta {_package_version()} - read a kantadb file and print each"
|
||||
" change record to the console."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--version",
|
||||
action="version",
|
||||
version=f"%(prog)s {_package_version()}",
|
||||
)
|
||||
parser.add_argument(
|
||||
"file",
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
import sys
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from kanta.__main__ import (
|
||||
_extra_import_paths,
|
||||
_format_ts,
|
||||
@@ -131,6 +133,23 @@ def test_cli_strips_ansi_without_color_support(tmp_path, capsys, monkeypatch):
|
||||
assert "snapshot s0" in err
|
||||
|
||||
|
||||
def test_cli_version_on_help_and_version_flag(capsys):
|
||||
"""--help and --version print the installed package version."""
|
||||
import importlib.metadata
|
||||
|
||||
version = importlib.metadata.version("kanta")
|
||||
|
||||
with pytest.raises(SystemExit) as help_exit:
|
||||
main(["--help"])
|
||||
assert help_exit.value.code == 0
|
||||
assert f"kanta {version}" in capsys.readouterr().out
|
||||
|
||||
with pytest.raises(SystemExit) as version_exit:
|
||||
main(["--version"])
|
||||
assert version_exit.value.code == 0
|
||||
assert capsys.readouterr().out.strip() == f"kanta {version}"
|
||||
|
||||
|
||||
def test_import_dotted_from_file_path(tmp_path):
|
||||
"""--data can be a filesystem path with an optional colon-separated symbol."""
|
||||
module = tmp_path / "models.py"
|
||||
|
||||
Reference in New Issue
Block a user