tools: add atomic I/O, input-overwrite protection and CLI tests for gen_bcpack_runtime_tables.py #1811

Merged
kirigayakazima merged 2 commits from upstream-pr/1811 into main 2026-11-28 15:01:20 +00:00
kirigayakazima commented 2026-11-25 12:23:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-25T12:23:09Z.


Claimed in the coordination issue as: GEN-BCPACK-TABLES-ATOMIC-IO-AND-CLI-TESTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162264899)

What and why

  • Stage both text (generate) and binary (write_profile) outputs in tools/gen_bcpack_runtime_tables.py through _atomic_write() (tempfile.mkstemp beside os.path.realpath(output), parent directory creation, explicit newline="\n" for text, os.fsync, published_mode, and replace_retry) so write failures never truncate existing files.
  • Validate output paths (_validate_output_path) to reject empty/whitespace paths, trailing separators, existing directories, and non-directory parent components cleanly before writing.
  • Refuse --profile JSON OUT when OUT aliases the source JSON (or bc6h.json/bc7.json in a source directory) via same_report_path.
  • Extract main(argv=None) to reject combining positional output with --profile, catch (OSError, ValueError, KeyError, TypeError, RecursionError) with exit 2 instead of tracebacks, and broaden the stdout reconfigure guard to (AttributeError, ValueError, OSError).
  • Add synthetic standard-library CLI/roundtrip unit tests in tests/test_gen_bcpack_runtime_tables.py.

Tested

  • python -I tests/test_gen_bcpack_runtime_tables.py -v -> Ran 6 tests in 0.629s, OK (verified in a detached upstream/main worktree that all new tests fail before the fix: FAILED (failures=8)).
  • python -I tests/test_ci_registration.py -> Ran 16 tests in 4.413s, OK.
  • python -I tests/test_text_write_line_endings.py -> Ran 3 tests in 12.495s, OK.
  • python -I scripts/check_no_game_data.py tools/gen_bcpack_runtime_tables.py tests/test_gen_bcpack_runtime_tables.py -> exit 0.
  • python -I -m compileall -q tools/gen_bcpack_runtime_tables.py tests/test_gen_bcpack_runtime_tables.py and git diff --check -> clean.

GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5491246761 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1811). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-25T12:23:09Z. --- Claimed in the coordination issue as: GEN-BCPACK-TABLES-ATOMIC-IO-AND-CLI-TESTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162264899) ### What and why - Stage both text (`generate`) and binary (`write_profile`) outputs in `tools/gen_bcpack_runtime_tables.py` through `_atomic_write()` (`tempfile.mkstemp` beside `os.path.realpath(output)`, parent directory creation, explicit `newline="\n"` for text, `os.fsync`, `published_mode`, and `replace_retry`) so write failures never truncate existing files. - Validate output paths (`_validate_output_path`) to reject empty/whitespace paths, trailing separators, existing directories, and non-directory parent components cleanly before writing. - Refuse `--profile JSON OUT` when `OUT` aliases the source JSON (or `bc6h.json`/`bc7.json` in a source directory) via `same_report_path`. - Extract `main(argv=None)` to reject combining positional output with `--profile`, catch `(OSError, ValueError, KeyError, TypeError, RecursionError)` with exit `2` instead of tracebacks, and broaden the stdout `reconfigure` guard to `(AttributeError, ValueError, OSError)`. - Add synthetic standard-library CLI/roundtrip unit tests in `tests/test_gen_bcpack_runtime_tables.py`. ### Tested - `python -I tests/test_gen_bcpack_runtime_tables.py -v` -> `Ran 6 tests in 0.629s`, `OK` (verified in a detached `upstream/main` worktree that all new tests fail before the fix: `FAILED (failures=8)`). - `python -I tests/test_ci_registration.py` -> `Ran 16 tests in 4.413s`, `OK`. - `python -I tests/test_text_write_line_endings.py` -> `Ran 3 tests in 12.495s`, `OK`. - `python -I scripts/check_no_game_data.py tools/gen_bcpack_runtime_tables.py tests/test_gen_bcpack_runtime_tables.py` -> exit `0`. - `python -I -m compileall -q tools/gen_bcpack_runtime_tables.py tests/test_gen_bcpack_runtime_tables.py` and `git diff --check` -> clean. GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64) AI-assisted: yes ### Checklist - [x] Based on current main and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
madvillain91 commented 2026-11-25 13:19:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by madvillain91 on 2026-11-25T13:19:45Z.

Review: COMMENTED


Independent Windows 11 26H2 (26300.9711) check at 8dfb5cd58 against main 384abe5b0 (PR base 0d80bc5df), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. All 25 tests passed across test_gen_bcpack_runtime_tables.py (6/6 in 1.067s), test_ci_registration.py (16/16 in 7.907s), and test_text_write_line_endings.py (3/3 in 16.304s), with check_no_game_data.py and compileall clean. Verified LF line endings on generated C++ tables, BCPHIST1 profile roundtrip, atomic replace_retry writes with cleanup on failure, and exit-2 rejection for self-overwrite, directory paths, and combining positional output with --profile. LGTM.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6058349937 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1811#pullrequestreview-6058349937). Originally posted by [madvillain91](https://github.com/madvillain91) on 2026-11-25T13:19:45Z. **Review:** COMMENTED --- Independent Windows 11 26H2 (26300.9711) check at 8dfb5cd58 against main 384abe5b0 (PR base 0d80bc5df), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. All 25 tests passed across test_gen_bcpack_runtime_tables.py (6/6 in 1.067s), test_ci_registration.py (16/16 in 7.907s), and test_text_write_line_endings.py (3/3 in 16.304s), with check_no_game_data.py and compileall clean. Verified LF line endings on generated C++ tables, BCPHIST1 profile roundtrip, atomic replace_retry writes with cleanup on failure, and exit-2 rejection for self-overwrite, directory paths, and combining positional output with --profile. LGTM.
DeadlyChirp commented 2026-11-25 15:52:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-25T15:52:09Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_gen_bcpack_runtime_tables.py:

  • PR: 6 passed.
  • The new test file run against main's tool: 8 failed, 1 passed. So the tests catch the problem, and the change fixes it on Windows.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167384316 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1811#issuecomment-7167384316). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-25T15:52:09Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_gen_bcpack_runtime_tables.py`: - PR: 6 passed. - The new test file run against main's tool: 8 failed, 1 passed. So the tests catch the problem, and the change fixes it on Windows. (AI-assisted test run; output checked by hand.)
aslan2142 commented 2026-11-25 17:27:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T17:27:30Z.


Thanks, the atomic write helps the parallel build. One thing:

  • main() also catches TypeError/KeyError around generate(). CMake runs the generator at build time, so a bug in it now shows as one line with exit 2 and no traceback.
  • Please limit that except to the --profile path (user JSON), or let errors from generate() raise.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7169531770 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1811#issuecomment-7169531770). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T17:27:30Z. --- Thanks, the atomic write helps the parallel build. One thing: - `main()` also catches `TypeError`/`KeyError` around `generate()`. CMake runs the generator at build time, so a bug in it now shows as one line with exit 2 and no traceback. - Please limit that `except` to the `--profile` path (user JSON), or let errors from `generate()` raise. AI-assisted (Claude Code, Claude Opus 5.5).
kirigayakazima commented 2026-11-27 11:56:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-27T11:56:57Z.


Thanks @aslan2142 — updated main() so TypeError/KeyError/ValueError/RecursionError are caught only around --profile JSON serialization, while generate() lets internal bugs raise normally with a traceback (only _validate_output_path and OSError write failures map to exit 2). Added regression coverage in tests/test_gen_bcpack_runtime_tables.py (7 passed, 5 repo checks clean).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7217660514 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1811#issuecomment-7217660514). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-27T11:56:57Z. --- Thanks @aslan2142 — updated `main()` so `TypeError`/`KeyError`/`ValueError`/`RecursionError` are caught only around `--profile` JSON serialization, while `generate()` lets internal bugs raise normally with a traceback (only `_validate_output_path` and `OSError` write failures map to exit 2). Added regression coverage in `tests/test_gen_bcpack_runtime_tables.py` (7 passed, 5 repo checks clean).
MoonEyes commented 2026-11-28 15:01:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T15:01:16Z.


Red/green: classified READY by cloud-4 (red on main 09c153948, green on head merged with main); head 8b2839dd5f unchanged since 2026-11-27, before that run.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7241134293 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1811#issuecomment-7241134293). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T15:01:16Z. --- Red/green: classified READY by cloud-4 (red on main 09c153948, green on head merged with main); head 8b2839dd5f8b unchanged since 2026-11-27, before that run. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1811
No description provided.