Download tools/test_rebuild_publication.py from Lexmount/LexBench-Web-Fetch: direct link, hf CLI and curl.
- Browser
- Download file 3.82 kB
-
https://huggingface.co/datasets/Lexmount/LexBench-Web-Fetch/resolve/refs%2Fpr%2F3/tools/test_rebuild_publication.py
- Command line
-
hf download hf://datasets/Lexmount/LexBench-Web-Fetch@refs/pr/3/tools/test_rebuild_publication.py
-
curl -L -o test_rebuild_publication.py https://huggingface.co/datasets/Lexmount/LexBench-Web-Fetch/resolve/refs%2Fpr%2F3/tools/test_rebuild_publication.py
3.82 kB
| """Controls for population preservation and publication integrity.""" | |
| import json | |
| from pathlib import Path | |
| import tempfile | |
| import unittest | |
| from copy import deepcopy | |
| from rebuild_publication import encoded, sha, rebuild, check | |
| class PublicationTests(unittest.TestCase): | |
| def setUp(self): | |
| self.temp = tempfile.TemporaryDirectory() | |
| self.addCleanup(self.temp.cleanup) | |
| self.root = Path(self.temp.name) | |
| (self.root/'definitions').mkdir() | |
| (self.root/'definitions/tasks.json').write_text('{}\n') | |
| for name in ['LICENSE','NOTICE','THIRD_PARTY.md','VERSIONING.md']: | |
| (self.root/name).write_text(name+'\n') | |
| (self.root/'data').mkdir() | |
| self.task = {'id':'case-1','task_prompt':'Fetch the target.'} | |
| row = {'id':'case-1','source_suite':'test','task':json.dumps(self.task)} | |
| (self.root/'data/test.jsonl').write_text(json.dumps(row)+'\n') | |
| population = [{'suite':'test','id':'case-1','task_sha256':sha(encoded(self.task))}] | |
| self.edition = {'tag':'v2.1.0','version':'2.1.0','task_count':1, | |
| 'configurations':[{'name':'test','count':1}], | |
| 'population_sha256':sha(encoded(population))} | |
| self.manifest = {'edition':deepcopy(self.edition),'files':{}} | |
| (self.root/'manifest.json').write_bytes(encoded(self.manifest)) | |
| def test_rebuild_preserves_edition_and_is_deterministic(self): | |
| first = rebuild(self.root,'a'*40) | |
| before = (self.root/'definitions.tar.gz').read_bytes() | |
| second = rebuild(self.root,'a'*40) | |
| self.assertEqual(first['edition'],self.edition) | |
| self.assertEqual(first,second) | |
| self.assertEqual(before,(self.root/'definitions.tar.gz').read_bytes()) | |
| self.assertEqual(check(self.root)['task_count'],1) | |
| def test_missing_edition_is_rejected(self): | |
| (self.root/'manifest.json').write_text('{}') | |
| with self.assertRaisesRegex(ValueError,'edition'): | |
| rebuild(self.root,'a'*40) | |
| def test_changed_population_is_rejected(self): | |
| (self.root/'data/test.jsonl').write_text((self.root/'data/test.jsonl').read_text().replace('Fetch the target.','Changed scope.')) | |
| with self.assertRaisesRegex(ValueError,'population'): | |
| rebuild(self.root,'a'*40) | |
| def test_short_source_revision_is_rejected(self): | |
| with self.assertRaisesRegex(ValueError,'full uploaded'): | |
| rebuild(self.root,'local-branch') | |
| def test_tampered_archive_is_rejected(self): | |
| rebuild(self.root,'a'*40) | |
| (self.root/'definitions.tar.gz').write_bytes(b'bad') | |
| with self.assertRaisesRegex(ValueError,'archive'): | |
| check(self.root) | |
| def test_changed_definition_is_rejected(self): | |
| rebuild(self.root,'a'*40) | |
| (self.root/'definitions/tasks.json').write_text('{"changed":true}') | |
| with self.assertRaisesRegex(ValueError,'publication file'): | |
| check(self.root) | |
| def test_undeclared_capture_is_rejected(self): | |
| (self.root/'definitions/capture.html').write_text('private capture') | |
| with self.assertRaisesRegex(ValueError,'Unexpected'): | |
| rebuild(self.root,'a'*40) | |
| def test_omitted_manifest_file_is_rejected(self): | |
| rebuild(self.root,'a'*40) | |
| manifest=json.loads((self.root/'manifest.json').read_text()) | |
| del manifest['files']['definitions/tasks.json'] | |
| (self.root/'manifest.json').write_bytes(encoded(manifest)) | |
| with self.assertRaisesRegex(ValueError,'inventory'): | |
| check(self.root) | |
| def test_symlink_is_rejected(self): | |
| (self.root/'definitions/link.json').symlink_to(self.root/'NOTICE') | |
| with self.assertRaisesRegex(ValueError,'symbolic'): | |
| rebuild(self.root,'a'*40) | |
| if __name__=='__main__': | |
| unittest.main() | |