#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Regress template staging through canonical and symlink-spelled checkouts.""" import importlib.util import os from pathlib import Path import subprocess import sys import tempfile import unittest SCRIPT = Path(__file__).with_name('prepare-development-templates.py') SPEC = importlib.util.spec_from_file_location('development_templates', SCRIPT) TEMPLATES = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(TEMPLATES) LANDER_FILES = ('main.c', 'Makefile', 'vccplus-project.json', 'README.md', '.gitignore') class DevelopmentTemplateTests(unittest.TestCase): def setUp(self): self.work = tempfile.TemporaryDirectory() self.addCleanup(self.work.cleanup) # tempfile can itself use a symlink-spelled macOS temporary directory. self.root = Path(self.work.name).resolve() self.checkout = self.root / 'physical-checkout' (self.checkout / 'platforms/macos').mkdir(parents=True) self.sources = self.checkout / 'examples/cmoc' for name, files in [('HelloDisk', TEMPLATES.HELLO_DISK_FILES), ('TalkingPlanetLander', LANDER_FILES)]: for relative in files: path = self.sources / name / relative path.parent.mkdir(parents=True, exist_ok=True) path.write_text(name + '/' + relative + '\n') self.alias = self.root / 'checkout-alias' self.alias.symlink_to(self.checkout, target_is_directory=True) self.destination = self.root / 'Result.app/Contents/Resources/DevelopmentTemplates' def test_default_templates_stage_through_ancestor_alias_without_build_products(self): (self.sources / 'TalkingPlanetLander/program.dsk').write_bytes(b'not a template') environment = dict(os.environ, SRCROOT=str(self.alias / 'platforms/macos')) subprocess.run([sys.executable, str(SCRIPT), '--destination', str(self.destination)], env=environment, check=True, capture_output=True, text=True) expected = {str(path.relative_to(self.sources)): path.read_bytes() for path in self.sources.rglob('*') if path.is_file() and path.suffix != '.dsk'} actual = {str(path.relative_to(self.destination)): path.read_bytes() for path in self.destination.rglob('*') if path.is_file()} self.assertEqual(actual, expected) def test_linked_template_file_stays_rejected_before_existing_output_changes(self): self.destination.mkdir(parents=True) marker = self.destination / 'previous' marker.write_bytes(b'existing bundle') leaf = self.sources / 'TalkingPlanetLander/main.c' leaf.unlink() outside = self.root / 'outside.c' outside.write_text('not an approved regular template file\n') leaf.symlink_to(outside) with self.assertRaisesRegex(ValueError, 'unlinked file'): TEMPLATES.stage(self.sources / 'HelloDisk', self.destination, self.sources / 'TalkingPlanetLander') self.assertEqual(marker.read_bytes(), b'existing bundle') if __name__ == '__main__': unittest.main()