Files

105 lines
4.4 KiB
Python

import hashlib
import importlib.util
import json
from pathlib import Path
import zipfile
import pytest
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()
def test_builder_rejects_missing_dependencies_before_publishing(tmp_path):
directory = source(tmp_path)
manifest = directory / 'plugin.json'
manifest.write_text(json.dumps({**json.loads(manifest.read_text()), 'dependencies': ['missing']}))
with pytest.raises(ValueError, match='Unknown plugin dependency'):
builder.build(tmp_path)
assert not (tmp_path / 'archives').exists()
assert not (tmp_path / 'catalog.json').exists()
def test_builder_rejects_cycles(tmp_path):
directory = source(tmp_path)
manifest = directory / 'plugin.json'
info = json.loads(manifest.read_text())
manifest.write_text(json.dumps({**info, 'dependencies': ['second']}))
other = tmp_path / 'plugins/second'
other.mkdir()
(other / 'plugin.json').write_text(json.dumps({**info, 'id': 'second', 'dependencies': ['example']}))
with pytest.raises(ValueError, match='Dependency cycle'):
builder.build(tmp_path)
def test_builder_requires_version_bump_for_changed_published_source(tmp_path):
directory = source(tmp_path)
entries = builder.build(tmp_path)
archive = tmp_path / entries[0]['archive']
before = archive.read_bytes()
(directory / 'plugin.py').write_text('# updated source\n')
with pytest.raises(ValueError, match='bump the resource version'):
builder.build(tmp_path)
assert archive.read_bytes() == before
def test_real_catalog_dependencies_match_runtime_contracts_versions_and_archives():
from web_framework.plugins import discover
root = Path(__file__).resolve().parents[1]
plugins = {plugin.id: plugin for plugin in discover(root / 'plugins')}
for entry in json.loads((root / 'catalog.json').read_text())['resources']:
if entry['type'] != 'plugin':
continue
plugin = plugins[entry['id']]
expected = set(plugin.requires)
expected.update(setting.default for setting in plugin.settings_schema.values()
if setting.kind == 'service' and setting.required and setting.default)
assert set(entry['dependencies']) == expected, entry['id']
assert plugin.version == entry['version'], entry['id']
assert expected <= plugins.keys()
source_info = json.loads((root / 'plugins' / entry['id'] / 'plugin.json').read_text())
assert source_info['dependencies'] == entry['dependencies']
with zipfile.ZipFile(root / entry['archive']) as archive:
assert json.loads(archive.read('plugin.json')) == source_info