test_prod2u_p4_wrapper_v3.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. """Static routing contracts for the minimal package PowerShell wrappers."""
  3. from __future__ import annotations
  4. import re
  5. import subprocess
  6. import unittest
  7. from pathlib import Path
  8. ROOT = Path(__file__).resolve().parents[2]
  9. TEMPLATES = ROOT / "tools" / "package_templates"
  10. DISABLE_HEX = "A55A02040500000000000000000426000000C4AE"
  11. class Prod2UP4WrapperV3Tests(unittest.TestCase):
  12. def test_run_wrapper_exposes_only_four_package_operations(self) -> None:
  13. source = (TEMPLATES / "run.ps1").read_text(encoding="utf-8")
  14. validate_set = re.search(r'\[ValidateSet\((.*?)\)\]', source, flags=re.DOTALL)
  15. self.assertIsNotNone(validate_set)
  16. commands = set(re.findall(r'"([^"]+)"', validate_set.group(1)))
  17. self.assertEqual(
  18. commands,
  19. {"selftest", "send-hex", "initialize-disable", "program-sram"},
  20. )
  21. for retired in (
  22. "normal-actuator-trace-gate",
  23. "set-all-outputs",
  24. "jog",
  25. "mark-closed",
  26. "read-temperatures",
  27. ):
  28. self.assertNotIn(f'"{retired}"', source)
  29. def test_run_wrapper_rejects_direct_motion_before_any_child_script(self) -> None:
  30. completed = subprocess.run(
  31. [
  32. "powershell.exe",
  33. "-NoProfile",
  34. "-ExecutionPolicy",
  35. "Bypass",
  36. "-File",
  37. str(TEMPLATES / "run.ps1"),
  38. "jog",
  39. ],
  40. cwd=ROOT,
  41. capture_output=True,
  42. text=True,
  43. check=False,
  44. )
  45. self.assertNotEqual(completed.returncode, 0)
  46. self.assertIn("ValidateSet", completed.stderr + completed.stdout)
  47. def test_initialize_disable_uses_the_frozen_request_through_send_hex(self) -> None:
  48. source = (TEMPLATES / "INITIALIZE_DISABLE.ps1").read_text(encoding="utf-8")
  49. compact = re.sub(r"\s+", "", source).upper()
  50. self.assertIn("SEND_HEX.PS1", compact)
  51. self.assertIn(DISABLE_HEX, compact)
  52. self.assertIn('"COM5"', source)
  53. self.assertIn("921600", source)
  54. def test_programmer_is_one_attempt_sram_only_at_200_khz(self) -> None:
  55. source = (TEMPLATES / "PROGRAM_SRAM.ps1").read_text(encoding="utf-8")
  56. self.assertIn('[ValidateSet("PROGRAM_SRAM")]', source)
  57. self.assertEqual(source.count("download -bit"), 1)
  58. self.assertIn("-mode jtag -spd 2", source)
  59. self.assertIn("attempt_count = 1", source)
  60. self.assertIn("speed_hz = 200000", source)
  61. self.assertIn("no retry was attempted", source)
  62. self.assertIn("$info.commit -notmatch '^[0-9a-f]{40}$'", source)
  63. self.assertIn('$stage = Join-Path "C:\\P2U" ([string]$info.commit)', source)
  64. self.assertNotRegex(source, r"(?i)build[_-]?id")
  65. for forbidden in (
  66. "PROGRAM_FLASH",
  67. "ERASE_FLASH",
  68. "PROGRAM_SPIBIN",
  69. "DIRECT_FLASH_WRITE",
  70. ):
  71. self.assertNotIn(forbidden, source)
  72. if __name__ == "__main__":
  73. unittest.main()