-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration.py
More file actions
142 lines (119 loc) · 3.8 KB
/
Copy pathtest_integration.py
File metadata and controls
142 lines (119 loc) · 3.8 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
import os
import sys
import subprocess
from subprocess import TimeoutExpired
import re
timeout_seconds = 15
GREEN = "\033[92m"
RED = "\033[91m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
RESET = "\033[0m"
test_dir = "tests_integration/"
if not os.path.isdir(test_dir):
print(
f"""
{RED}
=========================================================================
Error: Directory with integration tests doesn't exist. |
=========================================================================
{RESET}
"""
)
exit(1)
if len(sys.argv) > 1:
test_file = sys.argv[1] + ".swift"
if "test_" not in test_file:
test_file = "test_" + test_file
test_files = [test_file]
else:
test_files = [f for f in os.listdir(test_dir) if f.startswith("test_")]
main = "./main"
if not os.path.isfile(main):
print(
f"""
{RED}
=========================================================================
Error: 'main' doesn't exists.
=========================================================================
{RESET}"""
)
exit(1)
tests_passed = 0
failed_tests = []
for test_file in test_files:
test_file_path = os.path.join(test_dir, test_file)
run_test = f"{main} < {test_file_path}"
print(
f"""
{BLUE}
=========================================================================
Running test {test_file}...
{RESET}"""
)
try:
completed_process = subprocess.run(
run_test, shell=True, timeout=timeout_seconds, stdout=subprocess.DEVNULL
)
return_code = completed_process.returncode
except TimeoutExpired:
print(
f"\n{RED}Test '{test_file}' timed out after {timeout_seconds} seconds. Test failed.{RESET}"
)
failed_tests.append((test_file, -1))
continue
except Exception as e:
print(f"\n{RED}Encountered error while running test '{test_file}': {e}{RESET}")
continue
regex = re.compile(r"test_([0-9]+)_.+")
matches = regex.findall(test_file)
expected_code = 0
try:
expected_code = int(matches[0])
except (ValueError, IndexError):
print(f"{MAGENTA}Could not parse expected exit code{RESET}")
print(f"Expecting code: {expected_code}")
if return_code == expected_code:
print(f"\n{GREEN}Test '{test_file}' passed.{RESET}")
tests_passed += 1
# elif "test_lex_" in test_file and return_code == 1:
# print(
# f"\n{MAGENTA}Program should exit with 1 (testing lexical errors).{RESET}",
# end="",
# )
# print(f"\n{GREEN}Test '{test_file}' passed.{RESET}")
# tests_passed += 1
else:
failed_tests.append((test_file, return_code))
print(
f"\n{RED}Test '{test_file}' failed with return code {return_code}.{RESET}"
)
print(
f"""
{BLUE}
{test_file} finished with reutrn code {return_code}
=========================================================================
{RESET}"""
)
col = GREEN if tests_passed / len(test_files) > 0.5 else RED
if failed_tests and len(test_files) > 1:
print(
f"""
{RED}Failed tests:
========================================================================="""
)
for failed, ret_codes in failed_tests:
print(f"{failed.ljust(55)}Return code:{ret_codes}")
print(
f"""=========================================================================
{RESET}"""
)
print(
f"""
{BLUE}SUMMARY:
=========================================================================
| DON'T GIVE UP, YOU CAN DO IT! 💪{20*" "}{col}{tests_passed}/{len(test_files)} tests passed{BLUE}
| 💯😎🤑💲🐵🤖📈 {40*" "}{col}{tests_passed/len(test_files)*100:.2f}%{BLUE}
=========================================================================
{RESET}"""
)