Skip to content

Commit c2d8470

Browse files
committed
Refactor json tests
1 parent 237b784 commit c2d8470

1 file changed

Lines changed: 11 additions & 23 deletions

File tree

travis/test_json.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,21 @@
33
Test JSON files for errors.
44
"""
55

6-
import codecs
7-
import fnmatch
86
import json
9-
import os
10-
import sys
117
from collections import OrderedDict
8+
from pathlib import Path
129

13-
exit_code = 0
14-
print("Travis: Testing all JSON files in source")
1510

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)))
2115

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'
2916

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()
3320

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

Comments
 (0)