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:
@@ -1,16 +1,16 @@
|
||||
import errno
|
||||
import gzip
|
||||
from unittest import mock
|
||||
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
|
||||
from failure import Failure
|
||||
|
||||
|
||||
class Compress(unittest.TestCase):
|
||||
@@ -26,6 +26,21 @@ class Compress(unittest.TestCase):
|
||||
self.assertEqual(packed.read_bytes(), gzip.compress(b'{"a": 1}\n', mtime=0))
|
||||
self.assertEqual(packed.stat().st_mtime, 2000)
|
||||
|
||||
def test_syncs_the_gzip_and_its_folder_before_removing_the_file(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
plain = pathlib.Path(root, "t.jsonl")
|
||||
plain.write_bytes(b"a\n")
|
||||
synced = []
|
||||
fsync = os.fsync
|
||||
|
||||
def record(handle):
|
||||
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.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")
|
||||
@@ -96,6 +111,13 @@ class Read(unittest.TestCase):
|
||||
self.assertEqual((raised.exception.errno, raised.exception.filename), (errno.EIO, str(pathlib.Path(root, "a.jsonl.gz"))))
|
||||
self.assertTrue(raised.exception.strerror.startswith("not a whole gzip: "), raised.exception.strerror)
|
||||
|
||||
def test_a_file_that_is_no_gzip_fails_as_an_os_error(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
pathlib.Path(root, "a.jsonl.gz").write_bytes(b"plain text\n")
|
||||
with self.assertRaises(OSError) as raised:
|
||||
compressed.read_stored(pathlib.Path(root, "a.jsonl"))
|
||||
self.assertEqual((raised.exception.errno, raised.exception.filename), (errno.EIO, str(pathlib.Path(root, "a.jsonl.gz"))))
|
||||
|
||||
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"):
|
||||
|
||||
Reference in New Issue
Block a user