-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·152 lines (117 loc) · 4.03 KB
/
test.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
148
149
150
151
152
#!/usr/bin/env python3
# SCRIPT TO TEST THE PROJECT
from glob import glob
import subprocess
import tempfile
import os
import argparse
import sys
import datetime
from pathlib import Path
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
dir_path = dir_path = os.path.dirname(os.path.realpath(__file__))
EXE = f"{dir_path}/../crimvm"
DIRS = next(os.walk(f"{dir_path}/inputs/."))[1]
def printError(string):
print(FAIL + BOLD + string + ENDC)
def printSucess(string):
print(OKGREEN + BOLD + string + ENDC)
def printProgressBar(n, progress):
percent = round((progress/n) * 100)
BAR_SIZE = 20
x = (progress * BAR_SIZE) // n
color = ""
if percent < 50:
color = FAIL
elif percent != 100:
color = WARNING
else:
color = OKGREEN
text = "[" + color
for i in range(x):
text += "#"
for i in range(BAR_SIZE - x):
text += "-"
text += ENDC + f"] ({color}{percent}%{ENDC})"
print(text)
n_files = 0
n_files_passed = 0
print()
ALL_TESTS_PASSED = True
for directory in DIRS:
if not os.path.exists(f"{dir_path}/inputs/{directory}/.section"): continue
section_tests_passed = True
INPUT_FILES = sorted(glob(f"{dir_path}/inputs/{directory}/*"))
t = str.maketrans("\n", " ")
section_name = ""
with open(f"{dir_path}/inputs/{directory}/.section") as f:
section_name = f.readlines()[0].translate(t)
if section_name[-1] == " ":
section_name = section_name[:-1]
section_text = ""
for file in INPUT_FILES:
if os.path.splitext(file)[-1] != ".cas": continue
# Check if test file needs input file
flg_needs_input = False
flg_test_error = False
test_flags = ""
with open(file, 'r') as f:
test_flags = f.readline().replace(" ", "").replace("\n", "")
if test_flags[0:3] == ";@[":
assert test_flags[-1] == ']'
test_flags = test_flags[3:-1].split(",")
for flag in test_flags:
if flag.upper() == "IN":
flg_needs_input = True
elif flag.upper() == "ERR":
flg_test_error = True
# GET FILE NAME
filename = os.path.basename(file)
n_files += 1
filename_no_ext = os.path.splitext(filename)[0]
out_file = f"{dir_path}/outputs/{directory}/{filename_no_ext}.out"
# EXECUTE PROGRAM
result = 0
if not flg_needs_input:
result = subprocess.run([EXE, "-ar", file], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
else:
with open(f"{dir_path}/inputs/{directory}/{filename_no_ext}.in", "r") as test_input:
result = subprocess.run([EXE, "-ar", file], stdin = test_input, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
ret_code = result.returncode
success = ret_code == 0
passed = False
if success:
with open(out_file, "r") as f:
file_text = f.read();
passed = file_text == result.stdout.decode('ascii')
else:
if flg_test_error:
with open(out_file, "r") as f:
file_text = f.read();
passed = file_text == result.stderr.decode('ascii')
if not passed:
section_text += FAIL + "fail " + ENDC + ":\t" + filename + "\n"
ALL_TESTS_PASSED = False
section_tests_passed = False
else:
section_text += OKGREEN + "ok " + ENDC + ":\t" + filename + "\n"
n_files_passed += 1
sec_color = ""
if section_tests_passed:
sec_color = OKGREEN
else:
sec_color = FAIL
print(WARNING, BOLD, f" SECTION [", ENDC + sec_color, section_name, ENDC + WARNING + BOLD, "]\n", ENDC)
print(section_text)
print("\n-------\n")
printProgressBar(n_files, n_files_passed)
if ALL_TESTS_PASSED:
printSucess("ALL TESTS PASSED!\n")