From b1629f032c735c518050638143898e100c2f207f Mon Sep 17 00:00:00 2001 From: Pavel Kotek <334189328+kotekompotek@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:19:54 +0500 Subject: [PATCH 1/3] test: cover probe collision retries and fallback in file_mode --- tests/test_file_mode_extra.py | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/test_file_mode_extra.py diff --git a/tests/test_file_mode_extra.py b/tests/test_file_mode_extra.py new file mode 100644 index 0000000000..97e76e735d --- /dev/null +++ b/tests/test_file_mode_extra.py @@ -0,0 +1,50 @@ +"""Additional unit tests for tools/file_mode.py (probe retry, exhaustion, fallback).""" + +from __future__ import annotations + +import stat +import sys +import unittest +from pathlib import Path +from tempfile import TemporaryDirectory +from unittest import mock + +REPO = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(REPO / "tools")) + +import file_mode # noqa: E402 + + +class TestFileModeExtra(unittest.TestCase): + def test_new_file_mode_retries_on_file_exists_then_succeeds(self) -> None: + with TemporaryDirectory() as tmp: + real_open = file_mode.os.open + calls = 0 + + def flaky_open(path, flags, mode): + nonlocal calls + calls += 1 + if calls < 3: + raise FileExistsError("probe collision") + return real_open(path, flags, mode) + + with mock.patch.object(file_mode.os, "open", side_effect=flaky_open): + mode = file_mode.new_file_mode(tmp) + self.assertEqual(calls, 3) + self.assertEqual(mode & ~0o777, 0) + + def test_new_file_mode_raises_after_exhausting_retries(self) -> None: + with TemporaryDirectory() as tmp: + with mock.patch.object(file_mode.os, "open", side_effect=FileExistsError("always collides")): + with self.assertRaises(FileExistsError): + file_mode.new_file_mode(tmp) + + def test_published_mode_falls_back_when_target_missing(self) -> None: + with TemporaryDirectory() as tmp: + missing = Path(tmp) / "new_output.json" + mode = file_mode.published_mode(str(missing)) + self.assertEqual(stat.S_IMODE(mode), mode) + + +if __name__ == "__main__": + unittest.main() From 7d05a02deacbe773f899c65984845ae4116d6263 Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:35:10 +0100 Subject: [PATCH 2/3] tests: make the published_mode fallback test check what it claims The old assertion (S_IMODE(mode) == mode) passes for any 9-bit integer, even 0. The test now mocks new_file_mode, checks the target's own directory is probed and the sentinel is returned, and a second test checks an existing file keeps its mode. Co-Authored-By: Claude Sonnet 5.5 --- tests/test_file_mode_extra.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test_file_mode_extra.py b/tests/test_file_mode_extra.py index 97e76e735d..05758b9cea 100644 --- a/tests/test_file_mode_extra.py +++ b/tests/test_file_mode_extra.py @@ -42,8 +42,19 @@ def test_new_file_mode_raises_after_exhausting_retries(self) -> None: def test_published_mode_falls_back_when_target_missing(self) -> None: with TemporaryDirectory() as tmp: missing = Path(tmp) / "new_output.json" - mode = file_mode.published_mode(str(missing)) - self.assertEqual(stat.S_IMODE(mode), mode) + with mock.patch.object(file_mode, "new_file_mode", return_value=0o640) as new_mode: + mode = file_mode.published_mode(str(missing)) + # A missing target gets a new file's mode, probed in the target's own directory. + self.assertEqual(mode, 0o640) + new_mode.assert_called_once_with(file_mode.os.path.dirname(file_mode.os.path.abspath(str(missing)))) + + def test_published_mode_keeps_the_mode_of_the_file_it_replaces(self) -> None: + with TemporaryDirectory() as tmp: + existing = Path(tmp) / "output.json" + existing.write_text("{}", encoding="utf-8", newline="\n") + with mock.patch.object(file_mode, "new_file_mode", side_effect=AssertionError("must not probe")): + mode = file_mode.published_mode(str(existing)) + self.assertEqual(mode, stat.S_IMODE(existing.stat().st_mode)) if __name__ == "__main__": From facd4fb122e6676489b18338b4445ae53f311322 Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Wed, 2 Dec 2026 00:05:41 +0100 Subject: [PATCH 3/3] tests: the retry test needs a real collision and a fresh probe name The mocked open succeeded on its third call whatever the path, so generating the probe name once before the loop still passed. The test now pre-creates the first probe, patches secrets.token_hex with two names and keeps the real os.open. Reported by andrew-ai on the carry PR. Co-Authored-By: Claude Sonnet 5.5 --- tests/test_file_mode_extra.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tests/test_file_mode_extra.py b/tests/test_file_mode_extra.py index 05758b9cea..a82bc747e1 100644 --- a/tests/test_file_mode_extra.py +++ b/tests/test_file_mode_extra.py @@ -16,22 +16,18 @@ class TestFileModeExtra(unittest.TestCase): - def test_new_file_mode_retries_on_file_exists_then_succeeds(self) -> None: + def test_new_file_mode_retries_with_a_fresh_probe_name_after_a_collision(self) -> None: with TemporaryDirectory() as tmp: - real_open = file_mode.os.open - calls = 0 - - def flaky_open(path, flags, mode): - nonlocal calls - calls += 1 - if calls < 3: - raise FileExistsError("probe collision") - return real_open(path, flags, mode) - - with mock.patch.object(file_mode.os, "open", side_effect=flaky_open): + taken = Path(tmp) / ".mode-aaaa.probe" + taken.write_bytes(b"sentinel") + # A real collision: the first name exists, the second does not. A retry that reused the + # first name would exhaust its attempts on the same file and raise. + with mock.patch.object(file_mode.secrets, "token_hex", side_effect=["aaaa", "bbbb"]) as names: mode = file_mode.new_file_mode(tmp) - self.assertEqual(calls, 3) + self.assertEqual(names.call_count, 2) self.assertEqual(mode & ~0o777, 0) + self.assertEqual(taken.read_bytes(), b"sentinel") + self.assertEqual([p.name for p in Path(tmp).iterdir()], [".mode-aaaa.probe"]) def test_new_file_mode_raises_after_exhausting_retries(self) -> None: with TemporaryDirectory() as tmp: