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:
@@ -1,6 +1,5 @@
|
||||
import errno
|
||||
import gzip
|
||||
from unittest import mock
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
@@ -9,6 +8,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from text_files.doubles import compress
|
||||
from text_files import compressed
|
||||
from failure import Failure
|
||||
|
||||
@@ -19,7 +19,7 @@ class Compress(unittest.TestCase):
|
||||
plain = pathlib.Path(root, "t.jsonl")
|
||||
plain.write_bytes(b'{"a": 1}\n')
|
||||
os.utime(plain, (1000, 2000))
|
||||
self.assertIsNone(compressed.compress(plain))
|
||||
self.assertIsNone(compress(plain))
|
||||
self.assertFalse(plain.exists())
|
||||
packed = pathlib.Path(root, "t.jsonl.gz")
|
||||
# A gzip header's own time is 0, so compressing the same bytes twice writes the same file.
|
||||
@@ -37,31 +37,28 @@ class Compress(unittest.TestCase):
|
||||
synced.append((os.readlink(f"/proc/self/fd/{handle}"), plain.exists()))
|
||||
fsync(handle)
|
||||
|
||||
with mock.patch.object(compressed.os, "fsync", record):
|
||||
self.assertIsNone(compressed.compress(plain))
|
||||
self.assertIsNone(compress(plain, sync=record))
|
||||
self.assertEqual(synced, [(os.path.realpath(f"{plain}.gz"), True), (os.path.realpath(root), True)])
|
||||
|
||||
def test_a_gzip_it_cannot_write_keeps_the_original_and_says_why(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
plain = pathlib.Path(root, "t.jsonl")
|
||||
plain.write_bytes(b"kept\n")
|
||||
with mock.patch.object(compressed, "replace_file", return_value=Failure("disk full")):
|
||||
self.assertEqual(compressed.compress(plain), Failure(f"could not compress {plain}: disk full"))
|
||||
self.assertEqual(compress(plain, replace=lambda path, data: Failure("disk full")), Failure(f"could not compress {plain}: disk full"))
|
||||
self.assertEqual(plain.read_bytes(), b"kept\n")
|
||||
|
||||
def test_a_file_it_cannot_read_fails_naming_it(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
folder = pathlib.Path(root, "t.jsonl")
|
||||
folder.mkdir()
|
||||
failed = compressed.compress(folder)
|
||||
failed = compress(folder)
|
||||
self.assertTrue(failed.reason.startswith(f"could not compress {folder}: [Errno {errno.EISDIR}]"), failed.reason)
|
||||
|
||||
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)
|
||||
failed = compress(plain, pack=lambda data, **kwargs: gzip.compress(b"other\n"))
|
||||
self.assertIsInstance(failed, Failure)
|
||||
self.assertIn(str(plain), failed.reason)
|
||||
self.assertEqual(plain.read_bytes(), b"kept\n")
|
||||
@@ -70,15 +67,12 @@ class Compress(unittest.TestCase):
|
||||
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)
|
||||
return gzip.compress(data, **kwargs)
|
||||
|
||||
with mock.patch.object(compressed.gzip, "compress", grow_meanwhile):
|
||||
self.assertIsNone(compressed.compress(plain))
|
||||
self.assertIsNone(compress(plain, pack=grow_meanwhile))
|
||||
self.assertEqual(plain.read_bytes(), b"one\ntwo\n")
|
||||
self.assertEqual(compressed.read_stored(plain), b"one\ntwo\n")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user