Files
claude-config/bin/text_files/test_compressed.py
T

88 lines
3.5 KiB
Python

import gzip
import os
import pathlib
import sys
import tempfile
import unittest
from unittest import mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from text_files import compressed
class Compress(unittest.TestCase):
def test_replaces_the_file_with_a_gzip_of_the_same_bytes_and_times(self):
with tempfile.TemporaryDirectory() as root:
plain = pathlib.Path(root, "t.jsonl")
plain.write_bytes(b'{"a": 1}\n')
os.utime(plain, (1000, 2000))
self.assertIsNone(compressed.compress(plain))
self.assertFalse(plain.exists())
packed = pathlib.Path(root, "t.jsonl.gz")
self.assertEqual(gzip.decompress(packed.read_bytes()), b'{"a": 1}\n')
self.assertEqual(packed.stat().st_mtime, 2000)
def test_a_copy_that_reads_back_other_bytes_keeps_the_original_and_fails(self):
with tempfile.TemporaryDirectory() as root:
plain = pathlib.Path(root, "t.jsonl")
plain.write_bytes(b"kept\n")
with mock.patch.object(compressed.gzip, "compress", return_value=gzip.compress(b"other\n")):
failed = compressed.compress(plain)
self.assertIsInstance(failed, Failure)
self.assertIn(str(plain), failed.reason)
self.assertEqual(plain.read_bytes(), b"kept\n")
def test_a_file_written_while_it_compressed_stays_plain(self):
with tempfile.TemporaryDirectory() as root:
plain = pathlib.Path(root, "t.jsonl")
plain.write_bytes(b"one\n")
packing = compressed.gzip.compress
def grow_meanwhile(data, **kwargs):
with open(plain, "ab") as handle:
handle.write(b"two\n")
return packing(data, **kwargs)
with mock.patch.object(compressed.gzip, "compress", grow_meanwhile):
self.assertIsNone(compressed.compress(plain))
self.assertEqual(plain.read_bytes(), b"one\ntwo\n")
self.assertEqual(compressed.read_stored(plain), b"one\ntwo\n")
class Read(unittest.TestCase):
def test_reads_a_file_kept_plain_or_gzipped_by_its_plain_name(self):
with tempfile.TemporaryDirectory() as root:
plain = pathlib.Path(root, "a.jsonl")
plain.write_bytes(b"a\n")
pathlib.Path(root, "b.jsonl.gz").write_bytes(gzip.compress(b"b\n"))
self.assertEqual(compressed.read_stored(plain), b"a\n")
self.assertEqual(compressed.read_stored(pathlib.Path(root, "b.jsonl")), b"b\n")
self.assertEqual(compressed.stored(pathlib.Path(root, "b.jsonl")), pathlib.Path(root, "b.jsonl.gz"))
self.assertIsNone(compressed.stored(pathlib.Path(root, "c.jsonl")))
with self.assertRaises(FileNotFoundError):
compressed.read_stored(pathlib.Path(root, "c.jsonl"))
def test_the_plain_file_wins_over_an_older_gzip_beside_it(self):
with tempfile.TemporaryDirectory() as root:
pathlib.Path(root, "a.jsonl").write_bytes(b"a\nmore\n")
pathlib.Path(root, "a.jsonl.gz").write_bytes(gzip.compress(b"a\n"))
self.assertEqual(compressed.read_stored(pathlib.Path(root, "a.jsonl")), b"a\nmore\n")
def test_a_truncated_gzip_fails_as_an_os_error(self):
with tempfile.TemporaryDirectory() as root:
pathlib.Path(root, "a.jsonl.gz").write_bytes(gzip.compress(b"a\n" * 100)[:20])
with self.assertRaises(OSError):
compressed.read_stored(pathlib.Path(root, "a.jsonl"))
def test_globs_each_file_once_by_its_plain_name(self):
with tempfile.TemporaryDirectory() as root:
for name in ("a.jsonl", "b.jsonl.gz", "c.jsonl", "c.jsonl.gz", "d.md"):
pathlib.Path(root, name).write_bytes(b"")
self.assertEqual(compressed.stored_glob(pathlib.Path(root), "*.jsonl"), [pathlib.Path(root, name) for name in ("a.jsonl", "b.jsonl", "c.jsonl")])
if __name__ == "__main__":
unittest.main()