diff options
| author | Tayar <358041433+lucastayaralvarenga-ui@users.noreply.github.com> | 2026-11-22 05:05:17 -0300 |
|---|---|---|
| committer | Tayar <358041433+lucastayaralvarenga-ui@users.noreply.github.com> | 2026-11-22 05:05:17 -0300 |
| commit | 4c85973eeaee4e2fa2f056f7983915afd8b8e2de (patch) | |
| tree | dd31063d44d81e3f23022f9385022f54622e85ba | |
| parent | d3841c0c0ee41ec014699a985846c6dc76026440 (diff) | |
world_names: isolate JSON report staging files
| -rw-r--r-- | tests/test_world_names.py | 15 | ||||
| -rw-r--r-- | tools/world_names.py | 15 |
2 files changed, 23 insertions, 7 deletions
diff --git a/tests/test_world_names.py b/tests/test_world_names.py index ad40feaad..8b9aa663d 100644 --- a/tests/test_world_names.py +++ b/tests/test_world_names.py | |||
| @@ -231,6 +231,21 @@ class ResolveTest(unittest.TestCase): | |||
| 231 | 231 | ||
| 232 | 232 | ||
| 233 | class CliTest(unittest.TestCase): | 233 | class CliTest(unittest.TestCase): |
| 234 | def test_write_json_preserves_existing_temp_sibling(self): | ||
| 235 | with tempfile.TemporaryDirectory() as tmp: | ||
| 236 | out = os.path.join(tmp, "names.json") | ||
| 237 | sibling = out + ".tmp" | ||
| 238 | with open(sibling, "w", encoding="utf-8") as f: | ||
| 239 | f.write("keep me") | ||
| 240 | |||
| 241 | WN._write_json(out, {"names": {}}) | ||
| 242 | |||
| 243 | with open(sibling, encoding="utf-8") as f: | ||
| 244 | self.assertEqual(f.read(), "keep me") | ||
| 245 | with open(out, encoding="utf-8") as f: | ||
| 246 | self.assertEqual(json.load(f), {"names": {}}) | ||
| 247 | self.assertCountEqual(os.listdir(tmp), ["names.json", "names.json.tmp"]) | ||
| 248 | |||
| 234 | def test_xml_unterminated_comment_keeps_later_files(self): | 249 | def test_xml_unterminated_comment_keeps_later_files(self): |
| 235 | with tempfile.TemporaryDirectory() as tmp: | 250 | with tempfile.TemporaryDirectory() as tmp: |
| 236 | xml = os.path.join(tmp, "concat.xml") | 251 | xml = os.path.join(tmp, "concat.xml") |
diff --git a/tools/world_names.py b/tools/world_names.py index 0b00bf255..2039f6801 100644 --- a/tools/world_names.py +++ b/tools/world_names.py | |||
| @@ -34,6 +34,7 @@ import json | |||
| 34 | import os | 34 | import os |
| 35 | import re | 35 | import re |
| 36 | import sys | 36 | import sys |
| 37 | import tempfile | ||
| 37 | 38 | ||
| 38 | MASK32 = 0xFFFFFFFF | 39 | MASK32 = 0xFFFFFFFF |
| 39 | MASK64 = 0xFFFFFFFFFFFFFFFF | 40 | MASK64 = 0xFFFFFFFFFFFFFFFF |
| @@ -580,13 +581,13 @@ def _resolve(args) -> int: | |||
| 580 | 581 | ||
| 581 | def _write_json(path: str, data: dict) -> None: | 582 | def _write_json(path: str, data: dict) -> None: |
| 582 | directory = os.path.dirname(os.path.abspath(path)) | 583 | directory = os.path.dirname(os.path.abspath(path)) |
| 583 | if directory: | 584 | os.makedirs(directory, exist_ok=True) |
| 584 | os.makedirs(directory, exist_ok=True) | 585 | with tempfile.TemporaryDirectory(dir=directory, prefix=".world-names-") as staging: |
| 585 | tmp = path + ".tmp" | 586 | tmp = os.path.join(staging, "report.json") |
| 586 | with open(tmp, "w", encoding="utf-8") as handle: | 587 | with open(tmp, "w", encoding="utf-8") as handle: |
| 587 | json.dump(data, handle, indent=1, sort_keys=True) | 588 | json.dump(data, handle, indent=1, sort_keys=True) |
| 588 | handle.write("\n") | 589 | handle.write("\n") |
| 589 | os.replace(tmp, path) | 590 | os.replace(tmp, path) |
| 590 | 591 | ||
| 591 | 592 | ||
| 592 | def main(argv=None) -> int: | 593 | def main(argv=None) -> int: |