Files
cutdex/tool/gitea/test_package_app.py
T

113 lines
5.3 KiB
Python

import importlib.util
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import zipfile
from test_release_artifacts import fixture
from release_artifacts import METADATA, validate_artifact, validate_metadata
spec = importlib.util.spec_from_file_location('package_app', Path(__file__).with_name('package-app.py'))
packaging = importlib.util.module_from_spec(spec)
spec.loader.exec_module(packaging)
def apk_bytes(abis=('arm64-v8a', 'x86_64')):
stream = io.BytesIO()
with zipfile.ZipFile(stream, 'w') as archive:
for abi in abis:
archive.writestr(f'lib/{abi}/libflutter.so', b'flutter')
return stream.getvalue()
class SplitPackagingTests(unittest.TestCase):
def run_cli(self, root, *args):
with patch.object(packaging, 'ROOT', root), patch('sys.argv', ['package-app.py', *args]), patch('sys.stdout', io.StringIO()):
packaging.main()
def prepare(self, root):
(root / 'pubspec.yaml').write_text('version: 0.1.0+2\n')
apk = root / 'build/app/outputs/flutter-apk/app-production-release.apk'
apk.parent.mkdir(parents=True)
apk.write_bytes(apk_bytes())
packages = root / 'windows-packages'
packages.mkdir()
for name, body in fixture().items():
if name not in {'cutdex-0.1.0-android.apk', 'SHA256SUMS', 'release-manifest.json'}:
(packages / name).write_bytes(body)
return packages
def test_assemble_requires_no_windows_build_or_resolved_dependencies(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
packages = self.prepare(root)
self.run_cli(root, '--platform', 'assemble', '--windows-packages', str(packages))
contents = {p.name: p.read_bytes() for p in (root / 'dist').iterdir()}
validate_artifact(contents, '0.1.0', '0.1.0-build.2')
metadata = {p.name: p.read_bytes() for p in (root / '.build/release-metadata').iterdir()}
self.assertEqual(set(metadata), METADATA)
validate_metadata(metadata, '0.1.0', '0.1.0-build.2')
self.assertTrue(all(metadata[name] == contents[name] for name in metadata))
self.assertEqual(contents['cutdex-0.1.0-android.apk'],
(root / 'build/app/outputs/flutter-apk/app-production-release.apk').read_bytes())
def test_assemble_rejects_tampered_update_package_before_writing(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
packages = self.prepare(root)
next(packages.glob('*.nupkg')).write_bytes(b'tampered')
with self.assertRaisesRegex(ValueError, 'index checksums'):
self.run_cli(root, '--platform', 'assemble', '--windows-packages', str(packages))
self.assertFalse((root / 'dist').exists())
def test_assemble_rejects_missing_stale_or_nested_windows_packages(self):
for change in ('missing', 'stale', 'nested'):
with self.subTest(change=change), tempfile.TemporaryDirectory() as directory:
root = Path(directory)
packages = self.prepare(root)
if change == 'missing':
(packages / packaging.INSTALLER).unlink()
elif change == 'stale':
(packages / 'old.zip').write_bytes(b'old')
else:
(packages / 'nested').mkdir()
with self.assertRaises(ValueError):
self.run_cli(root, '--platform', 'assemble', '--windows-packages', str(packages))
self.assertFalse((root / 'dist').exists())
def test_windows_packaging_does_not_require_android(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / 'pubspec.yaml').write_text('version: 0.1.0+2\n')
windows = root / 'build/windows/x64/production/runner/Release'
windows.mkdir(parents=True)
(windows / 'cutdex.exe').write_bytes(b'exe')
with patch.object(packaging, 'package_windows', return_value={'windows.zip': b'zip'}) as pack:
self.run_cli(root, '--platform', 'windows', '--vpk', 'vpk.exe')
pack.assert_called_once_with(windows, Path('vpk.exe'), '0.1.0-build.2')
self.assertEqual({p.name for p in (root / 'dist').iterdir()}, {'windows.zip'})
def test_apk_rejects_missing_abi_and_32_bit_libraries(self):
for abis in [('arm64-v8a',), ('arm64-v8a', 'x86_64', 'armeabi-v7a')]:
with tempfile.TemporaryDirectory() as directory:
apk = Path(directory) / 'app.apk'
apk.write_bytes(apk_bytes(abis))
with self.assertRaises(ValueError):
packaging.validate_apk(apk)
def test_assemble_refuses_existing_output(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
packages = self.prepare(root)
(root / 'dist').mkdir()
(root / 'dist/stale.zip').write_bytes(b'stale')
with self.assertRaisesRegex(ValueError, 'dist must be empty'):
self.run_cli(root, '--platform', 'assemble', '--windows-packages', str(packages))
if __name__ == '__main__':
unittest.main()