-
Notifications
You must be signed in to change notification settings - Fork 13
/
conftest.py
59 lines (43 loc) · 1.56 KB
/
conftest.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pytest
import os
import tempfile
import filecmp
import difflib
def get_tests_dir():
return os.path.join(os.path.dirname(__file__), "tests")
def get_gold_dir():
return os.path.join(get_tests_dir(), "gold")
def get_vectors_dir():
return os.path.join(get_tests_dir(), "vectors")
def check_file_fn(src_str: str, vector_filename: str):
vector_filename = os.path.join(get_gold_dir(), vector_filename)
with tempfile.TemporaryDirectory() as temp:
temp_filename = os.path.join(temp, "file")
with open(temp_filename, "w+") as f:
f.write(src_str)
equal = filecmp.cmp(vector_filename, temp_filename)
if not equal:
with open(vector_filename) as f:
vector_content = f.read()
src_lines = src_str.splitlines()
vector_lines = vector_content.splitlines()
for line in difflib.unified_diff(vector_lines, src_lines,
fromfile="gold_vector",
tofile="test_output", lineterm=""):
print(line)
assert equal, "content are not equal"
@pytest.fixture
def check_file():
return check_file_fn
def get_vector_filename_fn(filename):
filename = os.path.join(get_vectors_dir(), filename)
assert os.path.isfile(filename)
return filename
@pytest.fixture
def get_vector_filename():
return get_vector_filename_fn
@pytest.fixture
def temp():
temp_dir = tempfile.TemporaryDirectory()
yield temp_dir.name
temp_dir.__exit__(None, None, None)