team-ljyzipelo

공개
74줄3.2 KB
1import gzip
2import importlib.util
3import io
4from pathlib import Path
5import tarfile
6import tempfile
7import unittest
8import zipfile
9
10
11ROOT = Path(__file__).resolve().parents[2]
12SPEC = importlib.util.spec_from_file_location(
13 "large_fixture_generator", ROOT / "tests/large-archive-fixtures/generate.py"
14)
15GENERATOR = importlib.util.module_from_spec(SPEC)
16SPEC.loader.exec_module(GENERATOR)
17
18
19class LargeArchiveFixtureTests(unittest.TestCase):
20 def test_boundary_archives_are_valid_and_expectations_straddle_limit(self):
21 with tempfile.TemporaryDirectory(dir=ROOT / "tests/ci") as scratch:
22 directory = Path(scratch) / "corpus"
23 manifest = GENERATOR.build(directory)
24 self.assertEqual(len(manifest["cases"]), 6)
25 self.assertFalse(manifest["worker_execution_verified"])
26 for name, artifact in manifest["artifacts"].items():
27 path = directory / name
28 self.assertEqual(GENERATOR.digest(path), artifact["sha256"])
29 if name.endswith(".zip"):
30 with zipfile.ZipFile(path) as archive:
31 self.assertIsNone(archive.testzip())
32 payload = archive.read(GENERATOR.NAME)
33 decoded = len(payload)
34 else:
35 data = gzip.decompress(path.read_bytes())
36 decoded = len(data)
37 with tarfile.open(fileobj=io.BytesIO(data), mode="r:") as archive:
38 payload = archive.extractfile(GENERATOR.NAME).read()
39 self.assertEqual(payload, bytes(artifact["payload_bytes"]))
40 self.assertEqual(decoded, artifact["decoded_bytes"])
41 self.assertEqual(decoded, 65536)
42 self.assertGreater(decoded / artifact["archive_bytes"], 100)
43 for case in manifest["cases"]:
44 decoded = manifest["artifacts"][case["file"]]["decoded_bytes"]
45 failed = decoded > case["limits"]["output"]
46 self.assertEqual(case["expected"]["error"], "Limit" if failed else "None")
47 self.assertEqual(case["expected"]["created_count"], 0 if failed else 1)
48
49 def test_generator_rejects_existing_destination_without_changes(self):
50 with tempfile.TemporaryDirectory(dir=ROOT / "tests/ci") as scratch:
51 directory = Path(scratch)
52 marker = directory / "keep"
53 marker.write_bytes(b"preserve")
54 with self.assertRaises(FileExistsError):
55 GENERATOR.build(directory)
56 self.assertEqual(list(directory.iterdir()), [marker])
57 self.assertEqual(marker.read_bytes(), b"preserve")
58
59 def test_generator_requires_project_test_directory(self):
60 with self.assertRaises(ValueError):
61 GENERATOR.build(ROOT / "outside-tests")
62
63 def test_payload_source_enforces_bounded_reads(self):
64 source = GENERATOR.ZeroStream(GENERATOR.GIB)
65 with self.assertRaises(ValueError):
66 source.read()
67 with self.assertRaises(ValueError):
68 source.read(GENERATOR.CHUNK + 1)
69 self.assertEqual(source.read(GENERATOR.CHUNK), bytes(GENERATOR.CHUNK))
70 self.assertEqual(source.remaining, GENERATOR.GIB - GENERATOR.CHUNK)
71
72
73if __name__ == "__main__":
74 unittest.main()