52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import unittest
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from validate_download_index import validate
|
|
|
|
|
|
class DownloadIndexTests(unittest.TestCase):
|
|
def write(self, row):
|
|
directory = TemporaryDirectory()
|
|
path = Path(directory.name) / "index.jsonl"
|
|
path.write_text(json.dumps(row) + "\n", encoding="utf-8")
|
|
return directory, path
|
|
|
|
def test_empty_project_index_is_valid(self):
|
|
self.assertEqual(validate(Path("vendor/downloads/index.jsonl")), [])
|
|
|
|
def test_china_mirror_requires_origin(self):
|
|
directory, path = self.write({
|
|
"record_type": "artifact",
|
|
"name": "demo",
|
|
"version": "1",
|
|
"source_url": "https://mirror.example/demo",
|
|
"sha256": "0" * 64,
|
|
"bytes": 1,
|
|
"license": "MIT",
|
|
"mirror_kind": "china-mirror",
|
|
})
|
|
self.addCleanup(directory.cleanup)
|
|
errors = validate(path)
|
|
self.assertTrue(any("origin_url" in error for error in errors))
|
|
|
|
def test_valid_local_cache_record(self):
|
|
directory, path = self.write({
|
|
"record_type": "artifact",
|
|
"name": "demo",
|
|
"version": "1",
|
|
"source_url": "demo.tar",
|
|
"sha256": "0" * 64,
|
|
"bytes": 1,
|
|
"license": "MIT",
|
|
"mirror_kind": "local-cache",
|
|
})
|
|
self.addCleanup(directory.cleanup)
|
|
self.assertEqual(validate(path), [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|