Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -14,31 +14,28 @@ its plain name. README.md → Install has the crontab line that runs it daily.
|
||||
It prints how many files it compressed. It exits 1, saying why, on any argument but --help or -h,
|
||||
and at the first file it cannot compress, leaving that file and every one after it plain.
|
||||
"""
|
||||
import os
|
||||
import pathlib
|
||||
import stat
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from transcript_archive.location import archive_root
|
||||
from text_files.compressed import compress, SUFFIX
|
||||
from failure import Failure
|
||||
from review_rules.review_folder import REPORTS
|
||||
from text_files.compressed import SUFFIX
|
||||
|
||||
# Why 30 days: docs/decisions.md → `archives: gzip each transcript in loop-claude's archive and each file in the review folders 30 days after it last changed, keeping all of them`.
|
||||
AGE_DAYS = 30
|
||||
|
||||
|
||||
def main(argv: list[str], env: typing.Mapping[str, str] = os.environ, home: pathlib.Path | None = None, now: float | None = None) -> int:
|
||||
def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, now: typing.Callable[[], float], compress: typing.Callable[[pathlib.Path], Failure | None]) -> int:
|
||||
if argv in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
return 0
|
||||
if argv:
|
||||
print(f"unknown arguments {' '.join(argv)}; usage: compress-archives", file=sys.stderr)
|
||||
return 1
|
||||
home = home or pathlib.Path.home()
|
||||
cutoff = (time.time() if now is None else now) - AGE_DAYS * 24 * 60 * 60
|
||||
cutoff = now() - AGE_DAYS * 24 * 60 * 60
|
||||
count = 0
|
||||
for path in [*old_files(archive_root(home, env), "*.jsonl", cutoff), *old_files(home / REPORTS, "*", cutoff)]:
|
||||
if failed := compress(path):
|
||||
|
||||
@@ -11,6 +11,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from text_files.doubles import compress
|
||||
from failure import Failure
|
||||
from compress_archives import main
|
||||
|
||||
@@ -43,10 +44,10 @@ class Main(unittest.TestCase):
|
||||
path.write_bytes(f"{path.name}\n".encode())
|
||||
os.utime(path, (NOW - seconds_ago, NOW - seconds_ago))
|
||||
|
||||
def run_main(self, argv=(), env=None, now=NOW):
|
||||
def run_main(self, argv=(), env=None, now=NOW, compress=compress):
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = main.main(list(argv), env or {}, self.home, now)
|
||||
code = main.main(list(argv), env or {}, self.home, lambda: now, compress)
|
||||
return code, out.getvalue(), err.getvalue()
|
||||
|
||||
def kept(self):
|
||||
@@ -80,9 +81,8 @@ class Main(unittest.TestCase):
|
||||
self.assertEqual(self.run_main(now=now), (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_measures_the_30_days_from_the_clock_it_is_given(self):
|
||||
self.assertEqual(self.run_main(now=NOW), (0, "Compressed 4 files.\n", ""))
|
||||
|
||||
def test_walks_on_past_a_gzip_a_link_and_a_file_removed_while_it_walks(self):
|
||||
gone = mock.Mock(**{"is_symlink.return_value": False, "stat.side_effect": FileNotFoundError})
|
||||
@@ -109,8 +109,7 @@ class Main(unittest.TestCase):
|
||||
self.assertEqual(self.run_main(), (0, "Compressed 0 files.\n", ""))
|
||||
|
||||
def test_stops_at_the_first_failure_keeping_that_file_and_every_later_one(self):
|
||||
with mock.patch.object(main, "compress", return_value=Failure("could not compress x: full")):
|
||||
code, out, err = self.run_main()
|
||||
code, out, err = self.run_main(compress=lambda path: Failure("could not compress x: full"))
|
||||
self.assertEqual((code, out, err), (1, "", "could not compress x: full\n"))
|
||||
self.assertTrue((self.archive / "a.jsonl").is_file())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user