-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
43 lines (34 loc) · 1.33 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import subprocess
import tempfile
import json
import pathlib
HERE_PATH = pathlib.Path(__file__).parent.resolve()
TEST_FILES_PATH = HERE_PATH / 'tests' / 'test_files'
def assert_equal_json(got_json_str, filename):
"""Assert that the JSON string is semantically identical to the contents of the file"""
norm_json_str = _normalize_json(got_json_str)
expected_json_str = _read_file(filename)
# assert norm_json_str == expected_json_str
if norm_json_str != expected_json_str:
print('Got JSON:')
print(norm_json_str)
print('Expected JSON:')
print(expected_json_str)
# assert False
meld(norm_json_str, filename)
# assert norm_json_str == expected_json_str
def _read_file(filename):
file_path = TEST_FILES_PATH / filename
if not file_path.exists():
file_path.touch()
return file_path.read_text()
def _normalize_json(json_str):
return json.dumps(json.loads(json_str), indent=4, sort_keys=True).strip() + '\n'
def meld(left_str, filename):
"""Open the meld command with left and right files"""
with tempfile.NamedTemporaryFile(delete=True, suffix='.json') as tmp_file:
tmp_file.write(left_str.encode('utf-8'))
tmp_file.seek(0)
subprocess.run(
('meld', tmp_file.name, (TEST_FILES_PATH / filename).as_posix()),
)