-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-tests.py
More file actions
147 lines (118 loc) · 4.79 KB
/
run-tests.py
File metadata and controls
147 lines (118 loc) · 4.79 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import subprocess
import xml.etree.ElementTree as ET
import colorama
"""
Get all tests from the /tests/ directory, run them, and compare to expected outputs
WARNING: this is super finicky at the moment. if you can find a better way to compare the compiled XML output,
you'll probably find it incredibly valuable to do so
"""
def print_difference(exp, act):
print(
f"\033[91mUnexpected difference found. Something in the below two lines of output is different:\033[0m"
)
print(f'\033[91mExpected:\033[0m "{exp}"')
print(f'\033[91mActual:\033[0m "{act}"')
def elements_equal(exp, act):
"""source: https://stackoverflow.com/questions/7905380/testing-equivalence-of-xml-etree-elementtree"""
if exp.tag != act.tag:
print_difference(exp.tag, act.tag)
return False
if exp.text != act.text:
print_difference(exp.text, act.text)
return False
if (exp.tail or "").strip() != (act.tail or "").strip():
print_difference((exp.tail or "").strip(), (act.tail or "").strip())
return False
if exp.attrib != act.attrib:
print_difference(exp.attrib, act.attrib)
return False
if len(exp) != len(act):
return False
return all(elements_equal(c1, c2) for c1, c2 in zip(exp, act))
def handle_test_failure(test_name, expected_out, actual_out):
print(f"\n\033[91mTest failed: {test_name}\033[0m")
print(f"\n\033[91mGranular error data should be logged above this message.\033[0m")
print(
f"\033[91mMore data below is included to help you to debug the issue.\033[0m\n"
)
e_lines = expected_out.splitlines()
a_lines = actual_out.splitlines()
if len(e_lines) != len(a_lines):
if len(e_lines) > len(a_lines):
print("Expected more lines of output than given.")
elif len(e_lines) < len(a_lines):
print("Expected less lines of output than given.")
print(f"\033[91mExpected\033[0m")
print(expected_out)
print(f"\033[91mActual\033[0m")
print(actual_out)
exit()
for i in range(len(e_lines)):
if e_lines[i] != a_lines[i]:
print(f"\033[91mUnexpected difference on line {i}:\033[0m")
print(f"\033[91mExpected:\033[0m")
print(e_lines[i])
print(f"\033[91mActual:\033[0m")
print(a_lines[i])
exit()
def run_test(test_name: str):
"""Runs the given test"""
print(f"Running test: {test_name}")
directory = os.path.join(test_directory, test_name)
infile = os.path.join(directory, "import.xml")
expected_main_articles_output_filepath = os.path.join(
directory, "main_articles_issue.xml"
)
expected_secondary_articles_output_filepath = os.path.join(
directory, "secondary_articles_issue.xml"
)
prepress_result = subprocess.run(
["python", os.path.join(os.getcwd(), "prepress.py"), "v1xxiy", infile],
stdout=subprocess.DEVNULL,
)
if prepress_result.returncode != 0:
print(colorama.Fore.YELLOW)
print(f"prepress failed to run test: {test_name}")
print(
"There is likely debug output above. If not, try running the test file directly"
+ colorama.Fore.RESET
)
exit()
generated_main_articles_output_filepath = os.path.join(
os.getcwd(), "main_articles_issue.xml"
)
expected_main_xml = ET.parse(expected_main_articles_output_filepath)
actual_main_xml = ET.parse(generated_main_articles_output_filepath)
expected_main_out = ET.tostring(expected_main_xml.getroot())
actual_main_out = ET.tostring(actual_main_xml.getroot())
if elements_equal(expected_main_xml.getroot(), actual_main_xml.getroot()) == False:
handle_test_failure(
test_name + " (main articles)", expected_main_out, actual_main_out
)
generated_secondary_articles_output_filepath = os.path.join(
os.getcwd(), "secondary_articles_issue.xml"
)
expected_secondary_xml = ET.parse(expected_secondary_articles_output_filepath)
actual_secondary_xml = ET.parse(generated_secondary_articles_output_filepath)
expected_secondary_out = ET.tostring(expected_secondary_xml.getroot())
actual_secondary_out = ET.tostring(actual_secondary_xml.getroot())
if (
elements_equal(expected_secondary_xml.getroot(), actual_secondary_xml.getroot())
== False
):
handle_test_failure(
test_name + " (secondary articles)",
expected_secondary_out,
actual_secondary_out,
)
colorama.init()
test_directory = os.path.join(os.getcwd(), "test-cases")
test_suites = [
d
for d in os.listdir(test_directory)
if os.path.isdir(os.path.join(test_directory, d))
]
for directory in test_suites:
run_test(directory)
print("\033[92mAll tests passed :)\033[0m")