test_profiles.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env python3
  2. """Explicit software test profiles for live repo / compact detached / reference regen.
  3. Profiles (distinct from packaging handoff profiles):
  4. - ``live-repository``: full checkout with raw ``.git`` and reference originals.
  5. - ``review-compact-detached``: extracted review-compact tree (no BRD/PDF/legacy_rtl).
  6. - ``reference-regeneration``: local BRD regeneration / inventory refresh path.
  7. Override with ``PRODU2_TEST_PROFILE``. When unset, infer from git mode + tree shape.
  8. """
  9. from __future__ import annotations
  10. import argparse
  11. import json
  12. import os
  13. import sys
  14. from pathlib import Path
  15. from typing import Any
  16. TOOLS = Path(__file__).resolve().parent
  17. ROOT = TOOLS.parent
  18. if str(TOOLS) not in sys.path:
  19. sys.path.insert(0, str(TOOLS))
  20. from git_worktree_mode import ( # noqa: E402
  21. DETACHED,
  22. LIVE_GIT,
  23. SNAPSHOT,
  24. detect_git_mode,
  25. )
  26. LIVE_REPOSITORY = "live-repository"
  27. REVIEW_COMPACT_DETACHED = "review-compact-detached"
  28. REFERENCE_REGENERATION = "reference-regeneration"
  29. TEST_PROFILES = frozenset(
  30. {LIVE_REPOSITORY, REVIEW_COMPACT_DETACHED, REFERENCE_REGENERATION}
  31. )
  32. ENV_TEST_PROFILE = "PRODU2_TEST_PROFILE"
  33. # Paths that live only in the full repository / reference volume — never required
  34. # under review-compact-detached.
  35. REFERENCE_ONLY_PATHS = (
  36. "reference/original/legacy_rtl/prod2u_p4_normal_v3_top_legacy.v",
  37. "reference/original/legacy_rtl/prod2u_p4_normal_v3_top_legacy_tb.v",
  38. )
  39. def normalize_profile(value: str | None) -> str | None:
  40. if value is None:
  41. return None
  42. text = value.strip().casefold().replace("_", "-")
  43. aliases = {
  44. "live": LIVE_REPOSITORY,
  45. "live-repo": LIVE_REPOSITORY,
  46. "live-repository": LIVE_REPOSITORY,
  47. "compact": REVIEW_COMPACT_DETACHED,
  48. "detached": REVIEW_COMPACT_DETACHED,
  49. # Contract / THREAD-AUTH underscore form → hyphenated after normalize.
  50. "detached-review-compact": REVIEW_COMPACT_DETACHED,
  51. "review-compact": REVIEW_COMPACT_DETACHED,
  52. "review-compact-detached": REVIEW_COMPACT_DETACHED,
  53. "reference": REFERENCE_REGENERATION,
  54. "reference-regen": REFERENCE_REGENERATION,
  55. "reference-regeneration": REFERENCE_REGENERATION,
  56. }
  57. return aliases.get(text)
  58. def detect_test_profile(root: Path | None = None) -> str:
  59. """Return active test profile from env or tree shape."""
  60. override = normalize_profile(os.environ.get(ENV_TEST_PROFILE))
  61. if override is not None:
  62. return override
  63. base = (root or ROOT).resolve()
  64. # Explicit regeneration requests (BRD present + inventory tooling) win only
  65. # when env asks; otherwise prefer git/compact shape.
  66. git_mode = detect_git_mode(base)
  67. if git_mode in {DETACHED, SNAPSHOT}:
  68. return REVIEW_COMPACT_DETACHED
  69. if git_mode == LIVE_GIT:
  70. return LIVE_REPOSITORY
  71. # Fallback: compact-shaped trees without .git.
  72. if (base / "source").is_dir() and (base / "meta").is_dir():
  73. return REVIEW_COMPACT_DETACHED
  74. return LIVE_REPOSITORY
  75. def requires_reference_legacy_rtl(profile: str | None = None) -> bool:
  76. """legacy_rtl is excluded from review-compact; only live/reference need it."""
  77. active = profile or detect_test_profile()
  78. return active in {LIVE_REPOSITORY, REFERENCE_REGENERATION}
  79. def allows_reference_regeneration(profile: str | None = None) -> bool:
  80. active = profile or detect_test_profile()
  81. return active == REFERENCE_REGENERATION
  82. def is_compact_detached(profile: str | None = None) -> bool:
  83. active = profile or detect_test_profile()
  84. return active == REVIEW_COMPACT_DETACHED
  85. # Unit modules safe for detached review-compact replay (no raw .git / no
  86. # historical receipts / no reference originals). Live-repository runs the full
  87. # verification matrix separately.
  88. DETACHED_REPLAY_UNIT_MODULES = (
  89. "tests.unit.test_protocol",
  90. "tests.unit.test_project_config",
  91. "tests.unit.test_product_contract_coverage",
  92. )
  93. # Tests that must not be implied by detached compact replay (git provenance,
  94. # historical receipts, raw reference originals, full matrix).
  95. LIVE_REPOSITORY_ONLY_UNIT_HINTS = (
  96. "tests.unit.test_review_git_provenance",
  97. "tests.unit.test_release_set_provenance",
  98. "tests.unit.test_build_firmware_package",
  99. "tests.unit.test_activity_docs_truth",
  100. )
  101. def detached_replay_unit_modules() -> tuple[str, ...]:
  102. """Modules promised by review-compact detached software replay."""
  103. return DETACHED_REPLAY_UNIT_MODULES
  104. def structure_required_paths(profile: str | None = None) -> tuple[str, ...]:
  105. """Canonical files that structure tests must find for ``profile``."""
  106. common = (
  107. "projects/prod2u-door-temp/project.json",
  108. "projects/prod2u-door-temp/requirements.zh-CN.md",
  109. "schemas/project.schema.json",
  110. "src/common/frame_rx.v",
  111. "src/common/frame_tx.v",
  112. "src/common/crc16_serial.v",
  113. "src/modules/door_motion_controller.v",
  114. "src/modules/temperature_reader.v",
  115. "src/project/project_adapter.v",
  116. "src/top/prod2u_p4_normal_v3_top.v",
  117. "src/generated/prod2u_door_temp_params.vh",
  118. "tests/sim/prod2u_p4_normal_v3_top_tb.v",
  119. "td/prod2u_p4_normal_v3.prj",
  120. "td/syn/prod2u_p4_normal_v3.tcl",
  121. "td/phy/prod2u_p4_normal_v3.tcl",
  122. "td/bitgen/prod2u_p4_normal_v3.tcl",
  123. "constraints/prod2u_p4_normal_v3.adc",
  124. "constraints/prod2u_p4_normal_v3.sdc",
  125. "tools/run_verification_matrix.py",
  126. )
  127. active = profile or detect_test_profile()
  128. if requires_reference_legacy_rtl(active):
  129. return common + REFERENCE_ONLY_PATHS
  130. return common
  131. def describe_test_profile(root: Path | None = None) -> dict[str, Any]:
  132. base = (root or ROOT).resolve()
  133. profile = detect_test_profile(base)
  134. return {
  135. "schema": "cpld-test-profile-v1",
  136. "profile": profile,
  137. "env_override": os.environ.get(ENV_TEST_PROFILE) or None,
  138. "git_mode": detect_git_mode(base),
  139. "requires_reference_legacy_rtl": requires_reference_legacy_rtl(profile),
  140. "allows_reference_regeneration": allows_reference_regeneration(profile),
  141. "is_compact_detached": is_compact_detached(profile),
  142. "root": str(base),
  143. }
  144. def build_parser() -> argparse.ArgumentParser:
  145. parser = argparse.ArgumentParser(description=__doc__)
  146. parser.add_argument(
  147. "--root",
  148. type=Path,
  149. default=ROOT,
  150. help="repository or extracted CPLD/source root",
  151. )
  152. parser.add_argument(
  153. "--json",
  154. action="store_true",
  155. help="print profile description JSON",
  156. )
  157. return parser
  158. def main(argv: list[str] | None = None) -> int:
  159. args = build_parser().parse_args(argv)
  160. payload = describe_test_profile(args.root)
  161. if args.json:
  162. print(json.dumps(payload, ensure_ascii=False, indent=2))
  163. else:
  164. print(payload["profile"])
  165. return 0
  166. if __name__ == "__main__":
  167. raise SystemExit(main())