Files
VCCPlusOSS/tests/toolchain/test_development_templates.py
T

58 lines
2.5 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Ensure distributed project templates contain authored sources, not local builds."""
import importlib.util
from pathlib import Path
import shutil
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location('development_templates', ROOT / 'platforms/macos/Tools/prepare-development-templates.py')
TEMPLATES = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(TEMPLATES)
class DevelopmentTemplateTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory(prefix='vcc-template-')
self.root = Path(self.temp.name).resolve()
self.source = self.root / 'source'
shutil.copytree(ROOT / 'examples/cmoc/HelloDisk', self.source)
self.destination = self.root / 'VCC Plus.app/Contents/Resources/DevelopmentTemplates'
def tearDown(self):
self.temp.cleanup()
def test_source_only_copy_removes_stale_payload_and_ignores_local_products(self):
(self.source / 'HELLO.bin').write_bytes(b'private local program')
(self.source / 'HELLO.dsk').write_bytes(b'private local disk')
self.destination.mkdir(parents=True)
(self.destination / 'stale.bin').write_bytes(b'old')
TEMPLATES.stage(self.source, self.destination)
actual = {p.relative_to(self.destination / 'HelloDisk').as_posix()
for p in (self.destination / 'HelloDisk').rglob('*') if p.is_file()}
self.assertEqual(actual, set(TEMPLATES.HELLO_DISK_FILES))
self.assertFalse((self.destination / 'stale.bin').exists())
for relative in actual:
self.assertEqual((self.source / relative).read_bytes(), (self.destination / 'HelloDisk' / relative).read_bytes())
def test_linked_or_missing_source_fails_before_replacing_existing_template(self):
self.destination.mkdir(parents=True)
marker = self.destination / 'unchanged'
marker.write_text('preserve')
(self.source / 'main.c').unlink()
(self.source / 'main.c').symlink_to(ROOT / 'examples/cmoc/HelloDisk/main.c')
with self.assertRaises(ValueError):
TEMPLATES.stage(self.source, self.destination)
self.assertEqual(marker.read_text(), 'preserve')
def test_cleanup_is_restricted_to_app_template_directory(self):
with self.assertRaises(ValueError):
TEMPLATES.stage(self.source, self.source)
self.assertTrue((self.source / 'main.c').is_file())
if __name__ == '__main__':
unittest.main()