feat(dirty-content-fingerprint): xceadmin shows fingerprint in --version

Adds DIRTY_FINGERPRINT = "" to _build_info.py's committed fallback stub
and cli.py's separate ImportError fallback class. New
_compose_version_display() applies the strip-and-append rule via a
genuine anchored removesuffix("-dirty"), mirroring the C/Go
implementations (tasks 3.1/4.4/4.5).

Updates one pre-existing test whose hard-coded literal-GIT_COMMIT
assertion no longer holds once a fingerprint is present, to compute its
expected value via the production function instead -- independently
assessed by review as a legitimate strengthening, not a weakened check.
4 new dedicated unit tests independently guard the transform itself with
hard-coded literal assertions, including an anchored-suffix-not-blind-
substring regression case.

Verified independently by review (including a second, differently-
constructed mutation test) across both install paths via a full real
/xce-build xceadmin cycle: editable install shows "unknown, unknown" with
no import error; wheel built from the dirty tree shows <hash>-<fingerprint>,
never <hash>-dirty-<fingerprint>. 267 passed/3 skipped.

This completes all xceadmin work (tasks 5.1-5.2) for this spec.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Rongsong Shen 2026-08-21 09:20:27 +00:00
parent eeb7a9b4db
commit c79ccf0fae
No known key found for this signature in database
GPG key ID: 5BCDAAE0123E9FE2
4 changed files with 142 additions and 23 deletions

View file

@ -93,18 +93,31 @@ def test_cli_version_flag_exits_zero_and_shows_package_version() -> None:
def test_cli_version_flag_includes_build_info_fields() -> None:
"""The printed --version string must include BOTH _build_info fields
(whichever variant -- "unknown" fallback or meson-generated real values
-- is currently installed), not just the bare package version, so
--version and the eventual startup log line (out of this task's scope)
can never disagree (Requirement 2.2)."""
"""The printed --version string must include BUILD_TIME verbatim, plus
the commit display -- whichever _build_info variant ("unknown"/""
fallback, or meson-generated real values) is currently installed -- not
just the bare package version (Requirement 2.2). dirty-content-
fingerprint task 5.2 note: when DIRTY_FINGERPRINT is empty, the commit
display is GIT_COMMIT verbatim (unchanged from the original
build-time-versioning feature's behavior, asserted directly below by
``test_compose_version_omits_suffix_when_build_info_fingerprint_empty``);
when non-empty (e.g. a wheel built from this dirty tree),
``_compose_version_display()``'s strip-and-append rule means the
composed display -- not the raw GIT_COMMIT string, which may still carry
an unstripped ``-dirty`` suffix -- is what appears in --version output.
This test therefore computes the same expected display the production
code does, rather than asserting GIT_COMMIT literally, so it holds for
both fallback and real-value installs."""
from xceadmin import _build_info
runner = CliRunner()
result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0, result.output
assert _build_info.GIT_COMMIT in result.output
expected_commit_display = cli_module._compose_version_display(
_build_info.GIT_COMMIT, _build_info.DIRTY_FINGERPRINT
)
assert expected_commit_display in result.output
assert _build_info.BUILD_TIME in result.output
@ -121,6 +134,82 @@ def test_build_info_module_is_importable_with_string_fields() -> None:
assert isinstance(_build_info.BUILD_TIME, str) and _build_info.BUILD_TIME
# ---------------------------------------------------------------------------
# dirty-content-fingerprint task 5.2: DIRTY_FINGERPRINT is a third
# _build_info field (empty-string fallback, not "unknown" -- see
# _build_info.py's docstring) and cli.py's _compose_version_display()
# applies the same strip-and-append rule already approved for the C daemons
# (xce_compose_version_ex(), src/lib/utils.c task 3.1) and the Go binaries
# (composeVersionDisplay(), apiserver/cmd/main.go and xcecli/cmd/main.go
# tasks 4.4/4.5). Tested directly as a pure function (mirroring this file's
# own _format_timestamp_ns precedent) since _compose_version() is evaluated
# once at module-import time to build click.version_option()'s default, so
# monkeypatching _build_info after import would not be observed by a
# `runner.invoke(cli, ["--version"])` call.
# ---------------------------------------------------------------------------
def test_compose_version_display_strips_dirty_suffix_and_appends_fingerprint() -> None:
"""dirty_fingerprint non-empty + git_commit ending in '-dirty' -> the
suffix is stripped and the fingerprint appended in its place
(<hash>-<fingerprint>, not <hash>-dirty-<fingerprint>) -- Requirements
1.1, 1.2, 1.3."""
assert cli_module._compose_version_display("abc1234-dirty", "f00dcafe") == "abc1234-f00dcafe"
def test_compose_version_display_empty_fingerprint_clean_commit_passthrough() -> None:
"""dirty_fingerprint empty + a clean (no -dirty suffix) git_commit ->
git_commit returned exactly as-is -- Requirement 2.1."""
assert cli_module._compose_version_display("abc1234", "") == "abc1234"
def test_compose_version_display_empty_fingerprint_dirty_commit_unchanged() -> None:
"""dirty_fingerprint empty + git_commit already ending in '-dirty'
(fingerprint computation failed, tree still dirty) -> git_commit passed
through completely unchanged, '-dirty' left in place -- this is
Requirement 5.2's fallback, satisfied by simply not touching commit_id
when dirty_fingerprint is empty."""
assert cli_module._compose_version_display("abc1234-dirty", "") == "abc1234-dirty"
def test_compose_version_display_anchored_suffix_not_blind_substring() -> None:
"""A regression case matching the pattern already established in the Go
tests (tasks 4.4/4.5): '-dirty' appearing elsewhere in the commit
string (not as the trailing suffix) must NOT be stripped -- only a
literal trailing '-dirty' is anchored-matched and removed."""
assert (
cli_module._compose_version_display("abc-dirty1234", "f00dcafe") == "abc-dirty1234-f00dcafe"
)
def test_compose_version_includes_fingerprint_when_build_info_has_one(monkeypatch) -> None:
"""_compose_version() (the function that actually feeds
click.version_option()) applies _compose_version_display() against the
live _build_info module's GIT_COMMIT/DIRTY_FINGERPRINT fields --
Requirement 4.1."""
monkeypatch.setattr(cli_module._build_info, "GIT_COMMIT", "abc1234-dirty")
monkeypatch.setattr(cli_module._build_info, "DIRTY_FINGERPRINT", "f00dcafe")
version_str = cli_module._compose_version()
assert "abc1234-f00dcafe" in version_str
assert "abc1234-dirty-f00dcafe" not in version_str
assert "-dirty" not in version_str
def test_compose_version_omits_suffix_when_build_info_fingerprint_empty(monkeypatch) -> None:
"""The committed fallback stub's DIRTY_FINGERPRINT = "" case: --version
shows GIT_COMMIT exactly as _build_info carries it, no fingerprint
suffix appended -- e.g. the editable-install path's GIT_COMMIT =
"unknown" stays "unknown" verbatim (Requirement 2.1, 5.1)."""
monkeypatch.setattr(cli_module._build_info, "GIT_COMMIT", "unknown")
monkeypatch.setattr(cli_module._build_info, "DIRTY_FINGERPRINT", "")
version_str = cli_module._compose_version()
assert "(unknown," in version_str
# ---------------------------------------------------------------------------
# config build
# ---------------------------------------------------------------------------