75 lines
4.4 KiB
Python
75 lines
4.4 KiB
Python
"""Check component selection and real Maven bootstrapping with generated/ absent."""
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
HOME = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location('generator', HOME / 'create-project.py')
|
|
generator = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(generator)
|
|
|
|
|
|
class GeneratorTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.temporary = tempfile.TemporaryDirectory()
|
|
cls.addClassCleanup(cls.temporary.cleanup)
|
|
cls.root = Path(cls.temporary.name)
|
|
cls.source = cls.root / 'engineering'
|
|
shutil.copytree(HOME, cls.source, ignore=shutil.ignore_patterns('.git','target','__pycache__'))
|
|
for args in (['init', '-q'], ['config','user.name','Recipe test'],
|
|
['config','user.email','test@example.invalid'], ['add','.'],
|
|
['commit','-qm','Test release'], ['tag','v1.0.0']):
|
|
subprocess.run(['git','-C',str(cls.source),*args], check=True, stdout=subprocess.DEVNULL)
|
|
generator.HOME = cls.source
|
|
|
|
def test_component_choices_and_bootstrap_without_generated_files(self):
|
|
for components in (['backend'], ['frontend'], ['backend','frontend']):
|
|
with self.subTest(components=components):
|
|
project = self.root / ('consumer-' + '-'.join(components))
|
|
generator.create(project, 'sample', components, str(self.source), 'v1.0.0', 'test-runner', 'https://landscape.invalid')
|
|
self.assertEqual('backend' in components, (project / 'backend/pom.xml').exists())
|
|
self.assertEqual('frontend' in components, (project / 'frontend/package.json').exists())
|
|
self.assertIn('test-runner', (project / '.gitea/workflows/build.yaml').read_text())
|
|
self.assertIn('RUNNER_SCOPE=user', (project / 'engineering.properties').read_text())
|
|
self.assertIn('.local/share/test-runner-runner', (project / 'ci/runner/runner.service').read_text())
|
|
self.assertIn('landscape', (project / 'AGENTS.md').read_text())
|
|
self.assertFalse((project / 'generated/skills').exists())
|
|
shutil.rmtree(project / 'generated')
|
|
result = subprocess.run(['mvn','-N','-B','-ntp','-Pregenerate-engineering','validate'], cwd=project,
|
|
text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=180)
|
|
self.assertEqual(0, result.returncode, result.stdout)
|
|
self.assertTrue((project / 'generated/ci/Ci.java').exists())
|
|
subprocess.run(['python3','scripts/engineering.py','--check'], cwd=project, check=True, stdout=subprocess.DEVNULL)
|
|
if components == ['frontend']:
|
|
for args in (['npm','ci'], ['npm','run','lint'], ['npm','test'], ['npm','run','build']):
|
|
subprocess.run(args, cwd=project / 'frontend', check=True, stdout=subprocess.DEVNULL)
|
|
self.assertTrue((project / 'frontend/dist/index.html').exists())
|
|
|
|
def test_cli_defaults_to_shared_mini_pc(self):
|
|
project = self.root / 'default-runner'
|
|
subprocess.run(['python3', str(self.source / 'create-project.py'), 'default-runner', str(project),
|
|
'--components', 'frontend', '--repository', str(self.source), '--version', 'v1.0.0'],
|
|
check=True, stdout=subprocess.DEVNULL)
|
|
self.assertIn('runs-on: mini-pc', (project / '.gitea/workflows/build.yaml').read_text())
|
|
self.assertIn('RUNNER_NAME=mini-pc', (project / 'engineering.properties').read_text())
|
|
self.assertIn('RUNNER_SCOPE=user', (project / 'engineering.properties').read_text())
|
|
self.assertIn('.config/mini-pc-runner/environment', (project / 'ci/runner/runner.service').read_text())
|
|
self.assertFalse((project / 'generated/skills').exists())
|
|
|
|
def test_generator_preserves_existing_destination(self):
|
|
project = self.root / 'already-exists'
|
|
project.mkdir()
|
|
(project / 'important').write_text('preserve')
|
|
with self.assertRaisesRegex(ValueError, 'must not exist'):
|
|
generator.create(project, 'sample', ['backend'], str(self.source), 'v1.0.0', 'runner', 'landscape')
|
|
self.assertEqual('preserve', (project / 'important').read_text())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|