|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import yaml |
| 4 | +import subprocess |
| 5 | +from django.core.management.base import BaseCommand |
| 6 | + |
| 7 | +class Command(BaseCommand): |
| 8 | + help = 'Run all tests referenced in notifications.yaml.' |
| 9 | + |
| 10 | + def handle(self, *args, **options): |
| 11 | + notifications_path = os.path.join(os.getcwd(), 'notifications.yaml') |
| 12 | + |
| 13 | + if not os.path.exists(notifications_path): |
| 14 | + self.stdout.write(self.style.ERROR(f'File not found: {notifications_path}')) |
| 15 | + return |
| 16 | + |
| 17 | + with open(notifications_path, 'r') as f: |
| 18 | + data = yaml.safe_load(f) or {} |
| 19 | + |
| 20 | + test_files = set() |
| 21 | + for nt in data.get('notification_types', []): |
| 22 | + for test in nt.get('tests', []): |
| 23 | + if test and test.strip(): |
| 24 | + test_files.add(test.strip()) |
| 25 | + |
| 26 | + if not test_files: |
| 27 | + self.stdout.write(self.style.WARNING('No test files found in notifications.yaml.')) |
| 28 | + return |
| 29 | + |
| 30 | + self.stdout.write(self.style.SUCCESS(f'Running tests in {len(test_files)} files...')) |
| 31 | + for test_file in sorted(test_files): |
| 32 | + self.stdout.write(f' - {test_file}') |
| 33 | + |
| 34 | + # Run pytest once for all test files |
| 35 | + result = subprocess.run( |
| 36 | + [sys.executable, '-m', 'pytest', *sorted(test_files)] |
| 37 | + ) |
| 38 | + |
| 39 | + if result.returncode != 0: |
| 40 | + self.stdout.write(self.style.ERROR('Some tests failed.')) |
| 41 | + sys.exit(result.returncode) |
| 42 | + else: |
| 43 | + self.stdout.write(self.style.SUCCESS('All tests passed.')) |
0 commit comments