Answer stability round 1: 1 🔴, 3 🟡 (0 misread); sort the new files' imports, sync the gzip before removing the file, read the plain file then its gzip without a check between, and age a file by the later of its mtime and ctime

This commit is contained in:
2026-11-17 02:33:15 +01:00
parent abe30ab893
commit 04904180d5
4 changed files with 86 additions and 36 deletions
@@ -1,26 +1,28 @@
import contextlib
import gzip
import io
from unittest import mock
import os
import pathlib
import sys
import tempfile
import time
import unittest
from unittest import mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from compress_archives import main
from failure import Failure
from compress_archives import main
NOW = 1790000000
DAY = 24 * 60 * 60
# A month ahead, so a file's ctime, which is when the test wrote it, lies over 30 days before NOW.
NOW = time.time() + 31 * DAY
OLD = 30 * DAY + 60
NEW = 30 * DAY - 60
class Main(unittest.TestCase):
"""A home whose archive and review folders each hold files last written a minute over 30 days ago, and one a minute under."""
"""A home whose archive and review folders each hold files that last changed a minute over 30 days ago, and one that changed a minute under."""
def setUp(self):
self.home = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()))
@@ -72,12 +74,16 @@ class Main(unittest.TestCase):
self.run_main()
self.assertTrue((self.archive / "b.jsonl").is_file())
def test_keeps_a_file_copied_today_with_its_old_times_plain(self):
self.assertEqual(self.run_main(now=time.time()), (0, "Compressed 0 files.\n", ""))
self.assertTrue((self.archive / "a.jsonl").is_file())
def test_measures_the_30_days_from_the_clock_by_default(self):
with mock.patch.object(main.time, "time", return_value=NOW):
self.assertEqual(self.run_main(now=None), (0, "Compressed 4 files.\n", ""))
def test_skips_a_file_removed_while_it_walks(self):
gone = mock.Mock(**{"is_file.return_value": True, "is_symlink.return_value": False, "stat.side_effect": FileNotFoundError})
gone = mock.Mock(**{"is_symlink.return_value": False, "stat.side_effect": FileNotFoundError})
# Mock(name=...) names the mock itself, so the path's name is set after.
gone.name = "gone.jsonl"
kept = self.archive / "a.jsonl"
@@ -106,7 +112,7 @@ class Main(unittest.TestCase):
for flag in ("--help", "-h"):
code, out, _ = self.run_main([flag])
self.assertEqual(code, 0)
self.assertTrue(out.startswith("Compress every file"), out)
self.assertTrue(out.startswith("Gzip each file"), out)
self.assertEqual(self.run_main(["x", "y"]), (1, "", "unknown arguments x y; usage: compress-archives\n"))