Datasets:
Download scripts/test_context_materializer.py from CrossSWE/CrossSWE-Bench: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/scripts/test_context_materializer.py
- Command line
-
hf download hf://datasets/CrossSWE/CrossSWE-Bench/scripts/test_context_materializer.py
-
curl -L -o test_context_materializer.py https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/scripts/test_context_materializer.py
2.86 kB
| import hashlib | |
| import io | |
| import json | |
| from pathlib import Path | |
| import tarfile | |
| import tempfile | |
| import unittest | |
| from materialize_agent_context import materialize | |
| class MaterializeTests(unittest.TestCase): | |
| def fixture(self, root, member='Dockerfile', payload=b'FROM scratch\n'): | |
| root = Path(root) | |
| archive = root / 'context.tar.gz' | |
| with tarfile.open(archive, 'w:gz') as tar: | |
| info = tarfile.TarInfo(member) | |
| info.size = len(payload) | |
| info.mode = 0o644 | |
| tar.addfile(info, io.BytesIO(payload)) | |
| data = archive.read_bytes() | |
| record = {'archive': {'path': archive.name, 'sha256': hashlib.sha256(data).hexdigest(), 'bytes': len(data)}, | |
| 'files': [{'path': member, 'sha256': hashlib.sha256(payload).hexdigest(), 'bytes': len(payload), 'mode': 0o644}]} | |
| (root / 'context-manifest.json').write_text(json.dumps(record)) | |
| return root / 'context-manifest.json' | |
| def test_exact_context_is_restored(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| record = self.fixture(temp) | |
| out = Path(temp) / 'out' | |
| materialize(record, out) | |
| self.assertEqual((out / 'Dockerfile').read_bytes(), b'FROM scratch\n') | |
| def test_tampered_archive_is_rejected_before_extraction(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| record = self.fixture(temp) | |
| with (Path(temp) / 'context.tar.gz').open('ab') as stream: | |
| stream.write(b'changed') | |
| with self.assertRaises(ValueError): | |
| materialize(record, Path(temp) / 'out') | |
| self.assertFalse((Path(temp) / 'out').exists()) | |
| def test_traversal_member_is_rejected(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| record = self.fixture(temp, '../escape') | |
| with self.assertRaises(ValueError): | |
| materialize(record, Path(temp) / 'out') | |
| self.assertFalse((Path(temp) / 'escape').exists()) | |
| def test_existing_output_is_preserved(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| record = self.fixture(temp) | |
| out = Path(temp) / 'out' | |
| out.mkdir() | |
| (out / 'keep').write_text('preserve') | |
| with self.assertRaises(FileExistsError): | |
| materialize(record, out) | |
| self.assertEqual((out / 'keep').read_text(), 'preserve') | |
| def test_unlisted_member_is_rejected(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| record = self.fixture(temp) | |
| data = json.loads(record.read_text()) | |
| data['files'] = [] | |
| record.write_text(json.dumps(data)) | |
| with self.assertRaises(ValueError): | |
| materialize(record, Path(temp) / 'out') | |
| if __name__ == '__main__': | |
| unittest.main() | |