fix(tests): replace os.chmod with mock for unreadable-file tests
test / unit (pull_request) Successful in 59s
test / integration (pull_request) Successful in 20s
test / coverage (pull_request) Successful in 1m13s
lint / lint (push) Successful in 2m1s
test / unit (push) Successful in 59s
test / integration (push) Successful in 17s
test / coverage (push) Successful in 1m4s
Update Quality Badges / update-badges (push) Successful in 57s

CI runs as root, so chmod 0o000 doesn't prevent reads. Both
test_skips_unreadable_file (TestFindRepoBottleFile) and
test_returns_false_when_file_unreadable_on_write now mock
Path.read_text to raise OSError directly.
This commit was merged in pull request #335.
This commit is contained in:
2026-07-09 17:35:28 +00:00
parent e27bd66080
commit fa7c6ab9d8
+6 -16
View File
@@ -342,9 +342,8 @@ git-gate:
os.chmod(path, 0o644) os.chmod(path, 0o644)
def test_returns_false_when_file_unreadable_on_write(self) -> None: def test_returns_false_when_file_unreadable_on_write(self) -> None:
# find_repo_bottle_file succeeds, but the file becomes unreadable # find_repo_bottle_file succeeds, but the second read (for writing)
# before find_and_update_bottle_file can re-read it. # raises OSError — e.g. file removed or permissions changed.
import os
content = """\ content = """\
--- ---
git-gate: git-gate:
@@ -361,12 +360,8 @@ git-gate:
with patch( with patch(
"bot_bottle.git_gate_host_key.find_repo_bottle_file", "bot_bottle.git_gate_host_key.find_repo_bottle_file",
return_value=path, return_value=path,
): ), patch.object(Path, "read_text", side_effect=OSError("Permission denied")):
os.chmod(path, 0o000) result = find_and_update_bottle_file(Path(d), "myrepo", "ssh-ed25519 AAAA")
try:
result = find_and_update_bottle_file(Path(d), "myrepo", "ssh-ed25519 AAAA")
finally:
os.chmod(path, 0o644)
self.assertFalse(result) self.assertFalse(result)
def test_skips_when_update_produces_no_change(self) -> None: def test_skips_when_update_produces_no_change(self) -> None:
@@ -440,15 +435,10 @@ git-gate:
self.assertIsNone(find_repo_bottle_file(Path("/nonexistent"), "myrepo")) self.assertIsNone(find_repo_bottle_file(Path("/nonexistent"), "myrepo"))
def test_skips_unreadable_file(self) -> None: def test_skips_unreadable_file(self) -> None:
import os
with tempfile.TemporaryDirectory() as d: with tempfile.TemporaryDirectory() as d:
path = Path(d) / "unreadable.md" (Path(d) / "unreadable.md").write_text(self._CONTENT)
path.write_text(self._CONTENT) with patch.object(Path, "read_text", side_effect=OSError("Permission denied")):
os.chmod(path, 0o000)
try:
self.assertIsNone(find_repo_bottle_file(Path(d), "myrepo")) self.assertIsNone(find_repo_bottle_file(Path(d), "myrepo"))
finally:
os.chmod(path, 0o644)
def test_skips_file_with_non_dict_git_gate(self) -> None: def test_skips_file_with_non_dict_git_gate(self) -> None:
with tempfile.TemporaryDirectory() as d: with tempfile.TemporaryDirectory() as d: