|
3 | 3 | Test JSON files for errors. |
4 | 4 | """ |
5 | 5 |
|
6 | | -import codecs |
7 | | -import fnmatch |
8 | 6 | import json |
9 | | -import os |
10 | | -import sys |
11 | 7 | from collections import OrderedDict |
| 8 | +from pathlib import Path |
12 | 9 |
|
13 | | -exit_code = 0 |
14 | | -print("Travis: Testing all JSON files in source") |
15 | 10 |
|
16 | | -for root, dirs, files in os.walk('.'): |
17 | | - for filename in fnmatch.filter(files, '*.json'): |
18 | | - file = os.path.join(root, filename) |
19 | | - with codecs.open(file, encoding="utf-8") as f: |
20 | | - text = f.read() |
| 11 | +def pytest_generate_tests(metafunc): |
| 12 | + if 'json_file' in metafunc.fixturenames: |
| 13 | + paths = list(Path().rglob("*.json")) |
| 14 | + metafunc.parametrize('json_file', paths, ids=list(map(str, paths))) |
21 | 15 |
|
22 | | - try: |
23 | | - data = json.loads(text, object_pairs_hook=OrderedDict) |
24 | | - except Exception as e: |
25 | | - exit_code |= 1 |
26 | | - print("Travis: {} is not a valid JSON file, json.load threw exception:\n{}".format(file, e)) |
27 | | - else: |
28 | | - formatted_text = json.dumps(data, indent=4) + '\n' |
29 | 16 |
|
30 | | - if text != formatted_text: |
31 | | - exit_code |= 2 |
32 | | - print("Travis: {} is not a properly formatted JSON file".format(file)) |
| 17 | +def test_json(json_file): |
| 18 | + with json_file.open(encoding="utf-8") as f: |
| 19 | + text = f.read() |
33 | 20 |
|
34 | | -if exit_code != 0: |
35 | | - sys.exit(exit_code) |
| 21 | + data = json.loads(text, object_pairs_hook=OrderedDict) |
| 22 | + formatted_text = json.dumps(data, indent=4) + '\n' |
| 23 | + assert formatted_text == text, "Improperly formatted JSON file" |
0 commit comments