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

Open
github-bot wants to merge 2 commits from github-bot/gh-1811 into main AGit
Owner

@⁠kirigayakazima on GitHub · 2026-11-25 12:23 UTC · original
kirigayakazima:fix/gen-bcpack-tables-safety → main · +269 −17 · 2 files · 2 commits · open

Claimed in the coordination issue as: GEN-BCPACK-TABLES-ATOMIC-IO-AND-CLI-TESTS (#1608)

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)
**@⁠kirigayakazima** on GitHub · 2026-11-25 12:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1811) `kirigayakazima:fix/gen-bcpack-tables-safety` → `main` · +269 −17 · 2 files · 2 commits · open Claimed in the coordination issue as: GEN-BCPACK-TABLES-ATOMIC-IO-AND-CLI-TESTS (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:06:33 +00:00
Author
Owner

@⁠madvillain91 on GitHub · 2026-11-25 13:19 UTC · original · reviewed

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.

**@⁠madvillain91** on GitHub · 2026-11-25 13:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1811#pullrequestreview-6058349937) · **reviewed** 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.
Author
Owner

@⁠DeadlyChirp on GitHub · 2026-11-25 15:52 UTC · original

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.)

**@⁠DeadlyChirp** on GitHub · 2026-11-25 15:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1811#issuecomment-7167384316) 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.)
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 17:27 UTC · original

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).

**@⁠aslan2142** on GitHub · 2026-11-25 17:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1811#issuecomment-7169531770) 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).
Author
Owner

@⁠kirigayakazima on GitHub · 2026-11-27 11:56 UTC · original

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).

**@⁠kirigayakazima** on GitHub · 2026-11-27 11:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1811#issuecomment-7217660514) 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).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1811/head:github-bot/gh-1811'
git switch 'github-bot/gh-1811'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1811'
git switch 'github-bot/gh-1811'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1811'
git switch 'github-bot/gh-1811'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1811'
git switch 'main'
git merge --squash 'github-bot/gh-1811'
git switch 'main'
git merge --ff-only 'github-bot/gh-1811'
git switch 'main'
git merge 'github-bot/gh-1811'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.