|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """ |
3 | | -Verify that template initialization created the expected files. |
| 3 | +Validate templates.json and verify that all referenced template files exist. |
4 | 4 |
|
5 | | -Usage: python verify_template_output.py <template-name> |
| 5 | +Usage: python verify_template_output.py |
6 | 6 | """ |
7 | 7 |
|
8 | 8 | import json |
|
11 | 11 |
|
12 | 12 |
|
13 | 13 | def main(): |
14 | | - if len(sys.argv) != 2: |
15 | | - print("Usage: python verify_template_output.py <template-name>") |
16 | | - sys.exit(1) |
17 | | - |
18 | | - template_name = sys.argv[1] |
19 | 14 | repo_root = Path(__file__).parent.parent.parent |
20 | 15 | templates_json = repo_root / 'templates.json' |
21 | 16 |
|
22 | | - # Load templates.json |
23 | | - with open(templates_json) as f: |
24 | | - templates = json.load(f) |
25 | | - |
26 | | - if template_name not in templates: |
27 | | - print(f"✗ Template '{template_name}' not found in templates.json") |
| 17 | + # Validate templates.json is valid JSON |
| 18 | + try: |
| 19 | + with open(templates_json) as f: |
| 20 | + templates = json.load(f) |
| 21 | + print(f"✓ templates.json is valid JSON") |
| 22 | + except json.JSONDecodeError as e: |
| 23 | + print(f"✗ templates.json is invalid JSON: {e}") |
28 | 24 | sys.exit(1) |
29 | | - |
30 | | - config = templates[template_name] |
31 | | - template_dir = repo_root / 'test-env' / template_name |
32 | | - |
33 | | - # Check if template directory was created |
34 | | - if not template_dir.exists(): |
35 | | - print(f"✗ Template directory '{template_dir}' was not created") |
| 25 | + except FileNotFoundError: |
| 26 | + print(f"✗ templates.json not found") |
36 | 27 | sys.exit(1) |
37 | 28 |
|
38 | | - print(f"✓ Template directory created: {template_dir}") |
| 29 | + errors = [] |
39 | 30 |
|
40 | | - # For simple templates, just check the output file exists |
41 | | - if 'files' not in config: |
42 | | - # Simple template - should have created test_output.py |
43 | | - output_file = template_dir / 'test_output.py' |
44 | | - if output_file.exists(): |
45 | | - print(f"✓ Output file created: {output_file}") |
46 | | - sys.exit(0) |
47 | | - else: |
48 | | - print(f"✗ Output file not found: {output_file}") |
49 | | - sys.exit(1) |
| 31 | + # Validate each template |
| 32 | + for template_name, config in templates.items(): |
| 33 | + print(f"\nValidating template: {template_name}") |
50 | 34 |
|
51 | | - # For complex templates, check all expected files |
52 | | - errors = [] |
53 | | - for file_spec in config['files']: |
54 | | - dest = file_spec['dest'] |
55 | | - expected_file = template_dir / dest |
| 35 | + # Check required fields |
| 36 | + if 'file' not in config: |
| 37 | + errors.append(f"✗ {template_name}: missing 'file' field") |
| 38 | + print(errors[-1]) |
| 39 | + continue |
| 40 | + |
| 41 | + if 'description' not in config: |
| 42 | + errors.append(f"✗ {template_name}: missing 'description' field") |
| 43 | + print(errors[-1]) |
56 | 44 |
|
57 | | - if expected_file.exists(): |
58 | | - print(f"✓ File created: {dest}") |
| 45 | + # Check main file exists |
| 46 | + main_file = repo_root / config['file'] |
| 47 | + if main_file.exists(): |
| 48 | + print(f" ✓ Main file exists: {config['file']}") |
| 49 | + |
| 50 | + # Try to compile Python files |
| 51 | + if config['file'].endswith('.py'): |
| 52 | + try: |
| 53 | + import py_compile |
| 54 | + py_compile.compile(main_file, doraise=True) |
| 55 | + print(f" ✓ Python file compiles: {config['file']}") |
| 56 | + except py_compile.PyCompileError as e: |
| 57 | + errors.append(f"✗ {template_name}: Python compilation error in {config['file']}: {e}") |
| 58 | + print(errors[-1]) |
59 | 59 | else: |
60 | | - errors.append(f"✗ Missing file: {dest}") |
| 60 | + errors.append(f"✗ {template_name}: main file not found: {config['file']}") |
61 | 61 | print(errors[-1]) |
62 | 62 |
|
| 63 | + # Check all files in complex templates |
| 64 | + if 'files' in config: |
| 65 | + for file_spec in config['files']: |
| 66 | + source = file_spec.get('source') |
| 67 | + if not source: |
| 68 | + errors.append(f"✗ {template_name}: file spec missing 'source' field") |
| 69 | + print(errors[-1]) |
| 70 | + continue |
| 71 | + |
| 72 | + source_file = repo_root / source |
| 73 | + if source_file.exists(): |
| 74 | + print(f" ✓ File exists: {source}") |
| 75 | + |
| 76 | + # Try to compile Python files |
| 77 | + if source.endswith('.py'): |
| 78 | + try: |
| 79 | + import py_compile |
| 80 | + py_compile.compile(source_file, doraise=True) |
| 81 | + print(f" ✓ Python file compiles: {source}") |
| 82 | + except py_compile.PyCompileError as e: |
| 83 | + errors.append(f"✗ {template_name}: Python compilation error in {source}: {e}") |
| 84 | + print(errors[-1]) |
| 85 | + else: |
| 86 | + errors.append(f"✗ {template_name}: source file not found: {source}") |
| 87 | + print(errors[-1]) |
| 88 | + |
| 89 | + # Print summary |
| 90 | + print("\n" + "="*50) |
63 | 91 | if errors: |
64 | | - print(f"\n✗ {len(errors)} file(s) missing") |
| 92 | + print(f"✗ Validation failed with {len(errors)} error(s)") |
| 93 | + for error in errors: |
| 94 | + print(f" {error}") |
65 | 95 | sys.exit(1) |
66 | 96 | else: |
67 | | - print(f"\n✓ All {len(config['files'])} expected files created") |
| 97 | + print(f"✓ All {len(templates)} template(s) validated successfully") |
68 | 98 | sys.exit(0) |
69 | 99 |
|
70 | 100 |
|
|
0 commit comments