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

Open
opened 2026-11-26 21:19:05 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1811
Author: @kirigayakazima
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/gen-bcpack-tables-safety


📝 Commits (1)

  • 8dfb5cd tools: add atomic I/O, input-overwrite protection and CLI tests for gen_bcpack_runtime_tables.py

📊 Changes

2 files changed (+250 additions, -18 deletions)

View changed files

➕ tests/test_gen_bcpack_runtime_tables.py (+150 -0)
📝 tools/gen_bcpack_runtime_tables.py (+100 -18)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1811 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/gen-bcpack-tables-safety` --- ### 📝 Commits (1) - [`8dfb5cd`](https://github.com/ubiquitarianism/leonida/commit/8dfb5cd58517057860ebf9f91a1ebc7a1097aec5) tools: add atomic I/O, input-overwrite protection and CLI tests for gen_bcpack_runtime_tables.py ### 📊 Changes **2 files changed** (+250 additions, -18 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_gen_bcpack_runtime_tables.py` (+150 -0) 📝 `tools/gen_bcpack_runtime_tables.py` (+100 -18) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1824
No description provided.