51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import zipfile
|
|
|
|
spec = importlib.util.spec_from_file_location('resource_catalog', Path(__file__).resolve().parents[1] / 'tools/build_catalog.py')
|
|
builder = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(builder)
|
|
|
|
|
|
def source(root):
|
|
directory = root / 'plugins/example'
|
|
directory.mkdir(parents=True)
|
|
(root / 'themes').mkdir()
|
|
info = {'id': 'example', 'name': 'Example', 'description': 'Test resource', 'version': '0.1.0'}
|
|
(directory / 'plugin.json').write_text(json.dumps(info))
|
|
(directory / 'plugin.py').write_text('# plugin source\n')
|
|
(directory / 'tests').mkdir()
|
|
(directory / 'tests/test_example.py').write_text('# excluded\n')
|
|
return directory
|
|
|
|
|
|
def test_builder_checksums_exclusions_and_reproducibility(tmp_path):
|
|
source(tmp_path)
|
|
entries = builder.build(tmp_path)
|
|
archive = tmp_path / entries[0]['archive']
|
|
first = archive.read_bytes()
|
|
with zipfile.ZipFile(archive) as result:
|
|
assert set(result.namelist()) == {'plugin.json', 'plugin.py'}
|
|
assert hashlib.sha256(first).hexdigest() == entries[0]['sha256']
|
|
assert builder.build(tmp_path) == entries
|
|
assert archive.read_bytes() == first
|
|
|
|
|
|
def test_builder_preserves_published_bytes_and_previous_versions(tmp_path):
|
|
directory = source(tmp_path)
|
|
archives = tmp_path / 'archives'
|
|
archives.mkdir()
|
|
older = archives / 'plugin-example-0.0.1.zip'
|
|
older.write_bytes(b'previous published version')
|
|
published = archives / 'plugin-example-0.1.0.zip'
|
|
with zipfile.ZipFile(published, 'w') as output:
|
|
for name in ('plugin.py', 'plugin.json'):
|
|
output.writestr(name, (directory / name).read_bytes())
|
|
original = published.read_bytes()
|
|
entries = builder.build(tmp_path)
|
|
assert published.read_bytes() == original
|
|
assert older.read_bytes() == b'previous published version'
|
|
assert entries[0]['sha256'] == hashlib.sha256(original).hexdigest()
|