|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
| 3 | +import difflib |
3 | 4 | import json
|
4 | 5 | import jsonschema
|
| 6 | +import sys |
5 | 7 |
|
6 | 8 | schema = json.load(open('implementation.schema.json'))
|
7 | 9 |
|
| 10 | +errors = [] |
8 | 11 | raw_data = open('implementations.json').read()
|
9 |
| -if not raw_data.endswith(']\n'): |
10 |
| - raise ValueError('implementations.json should end in one \\n') |
11 | 12 | data = json.loads(raw_data)
|
12 | 13 |
|
13 | 14 | for impl in data:
|
14 |
| - jsonschema.validate(impl, schema) |
| 15 | + try: |
| 16 | + jsonschema.validate(impl, schema) |
| 17 | + except jsonschema.exceptions.ValidationError as e: |
| 18 | + errors.append(e) |
15 | 19 |
|
16 | 20 | sorted_data = sorted(data, key=lambda impl: impl['repoURL'])
|
17 | 21 | for expected, actual in zip(sorted_data, data):
|
18 | 22 | if expected['repoURL'] != actual['repoURL']:
|
19 |
| - print('Expected:', expected['repoURL']) |
20 |
| - print('Found: ', actual['repoURL']) |
21 |
| - raise ValueError('Implementations should be sorted by repoURL') |
| 23 | + errors.append(ValueError( |
| 24 | + 'Implementations should be sorted by repoURL.\n' + |
| 25 | + f"Expected: {expected['repoURL']}\n" + |
| 26 | + f"Found: {actual['repoURL']}")) |
| 27 | + break |
| 28 | + |
| 29 | +diff = ''.join(difflib.unified_diff( |
| 30 | + raw_data.splitlines(keepends=True), |
| 31 | + (json.dumps(data, indent=4) + '\n').splitlines(keepends=True), |
| 32 | + fromfile='implementations.json (current)', |
| 33 | + tofile='implementations.json (expected formatting)')) |
| 34 | +if diff != '': |
| 35 | + errors.append(ValueError('Formatting error:\n' + diff)) |
| 36 | + |
| 37 | +# Saving the file without a trailing newline is a common error. It's already |
| 38 | +# caught by difflib above, but this error message is clearer. |
| 39 | +if not raw_data.endswith(']\n'): |
| 40 | + errors.append(ValueError('implementations.json should end in one \\n')) |
22 | 41 |
|
| 42 | +if errors: |
| 43 | + print(f'Encountered {len(errors)} errors:') |
| 44 | + for error in errors: |
| 45 | + print() |
| 46 | + print(error) |
| 47 | + sys.exit(1) |
0 commit comments