-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
147 lines (107 loc) · 4.88 KB
/
main.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
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 argparse
import subprocess
from termcolor import colored
from lxml import etree
from xmldiff import main as xmldiff
def help():
parser = argparse.ArgumentParser(
prog="main.py",
description="Testing tool for the first IPP Project",
epilog="Author: Nick Settler (xmoise01)",
add_help=False
)
parser.add_argument("--help", "-h", action="help", help=argparse.SUPPRESS)
parser.add_argument("-i", "--interpret", action="store", help="PHP 8.1 interpreter", required=True)
parser.add_argument("-s", "--source", action="store", help="Path to parse.php file", required=True)
parser.add_argument("-f", "--filter",
action="store",
help="""Filter tests by category. Categories are divided by comma. Categories can be anything
what will be in the test path.""")
args = parser.parse_args()
return [args.interpret, args.source, args.filter]
class Test:
def __init__(self, src):
self.name = src.replace(".src", "").split('/')[-1]
self.src_file = src
self.out_file = src.replace(".src", ".xml")
self.rc_file = src.replace(".src", ".code")
self.actual_rc = ""
self.actual_out = ""
with open(self.out_file, "r") as file:
self.expected_out = file.read()
with open(self.rc_file, "r") as file:
self.expected_rc = file.read()
def __str__(self):
return self.name
class TestRunner:
def __init__(self, interpret, source):
self.interpret = interpret
self.source = source
def run(self, test):
with open(test.src_file, "r") as file:
try:
result = subprocess.run([self.interpret, self.source], stdin=file, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
test.actual_rc = result.returncode
test.actual_out = result.stdout.decode("utf-8")
except subprocess.CalledProcessError as e:
test.actual_rc = e.returncode
pass
def get_tests():
tests = []
for root, dirs, files in os.walk("tests"):
for file in files:
if file.endswith(".src"):
tests.append(Test(os.path.join(os.path.dirname(os.path.abspath(__file__)), root, file)))
return tests
def main():
interpret, source, filters = help()
tests = get_tests()
tests.sort(key=lambda x: x.src_file)
if filters:
filters = filters.split(',')
tests = [test for test in tests if all(f in test.src_file for f in filters)]
tests_runner = TestRunner(interpret, source)
succeed = 0
failed = 0
xml_parser = etree.XMLParser(ns_clean=True, recover=True, encoding='utf-8', remove_blank_text=True)
for test in tests:
tests_runner.run(test)
if int(test.actual_rc) != int(test.expected_rc):
failed += 1
print(colored(f"❌ Test {test.name} failed: RC {test.actual_rc} != {test.expected_rc}", "red"))
continue
if test.actual_out.strip() and test.expected_out.strip():
left_tree = etree.XML(bytes(bytearray(test.actual_out, encoding='utf-8')), parser=xml_parser)
right_tree = etree.XML(bytes(bytearray(test.expected_out, encoding='utf-8')), parser=xml_parser)
output_diff = xmldiff.diff_trees(left_tree, right_tree)
if len(output_diff):
failed += 1
print(colored(f"❌ Test {test.name} failed: OUT {test.actual_out} != {test.expected_out}", "red"))
continue
elif test.actual_out.strip() != test.expected_out.strip():
failed += 1
print(colored(f"❌ Test {test.name} failed: OUT {test.actual_out} != {test.expected_out}", "red"))
continue
succeed += 1
print(colored(f"✅ Test {test.name} passed", "green"))
percent = succeed / (succeed + failed) * 100
print(colored(f"Passed: {succeed}\t\tFailed: {failed}\t\tPercent: {percent:.2f}%",
"green" if failed == 0 else percent > 50 and "yellow" or "red"))
stats_length = 54
red_zone = 5
orange_zone = 8
yellow_zone = 10
green_zone = 4
pattern = "🟥" * int(red_zone) + "🟧" * int(orange_zone) + "🟨" * int(yellow_zone) + "🟩" * int(green_zone)
stats = int(percent / 100 * stats_length)
success_quotient = int(succeed / (succeed + failed) * stats_length) // 2
visible_pattern = pattern[:success_quotient]
print(colored(f"[{visible_pattern}{' ' * (stats_length - len(visible_pattern) * 2)}] {percent:.2f}%",
"green" if failed == 0 else percent > 50 and "yellow" or "red"))
print(colored(f"[{'=' * stats}{' ' * (stats_length - stats)}] {percent:.2f}%",
"green" if failed == 0 else percent > 50 and "yellow" or "red"))
pass
if __name__ == '__main__':
main()