-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestFile.py
203 lines (169 loc) · 7.65 KB
/
TestFile.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import platform
import sys
import subprocess
import json
import glob
import configparser
_DEBUG = False
class KozaError(Exception):
def __init__(self, output, error):
self.message = output
self.error = error
def __str__(self):
return f"Output: {self.message} - Error: {self.error}"
class Test:
pythonDir = sys.executable
currentOs = ""
testPath = os.path.join(os.getcwd(), "Testcases")
exPath = os.path.join(os.getcwd(), "Exercises")
timeout = 0
# name = nome dell'esercizio
# diffPath = "nomePath", se ha una path diversa dalla default 'Exercises'
# samePath = True, se il .py si trova nella cartella dei Testcases, con lo stesso nome
def __init__(self, name, type="py"):
self.name = name
self.type = type
self.curPath = os.getcwd()
self.getTimeout()
self.currentOs = self.whatOs()
self.testcase = self.getTestcase(name)
self.error = False
self.risultati = {} # Se si fa con i thread, per tenere traccia dei vari testcase
self.title = name
self.exPath = os.path.join(os.getcwd(), "Exercises")
self.testPath = os.path.join(os.getcwd(), "Testcases")
# if self.currentOs == "Mac" or self.currentOs == "Linux":
# self.exPath = os.getcwd() + "/Exercises"
# self.testPath = os.getcwd() + "/Testcases"
self.exist = self.fileExists()
def testExercise(self):
if self.exist:
execString = [self.pythonDir, os.path.join(self.exPath, f"{self.name}.py")]
print(f"\t### TESTING {self.title} ###")
print("------------------------------------")
for i, v in self.testcase.items():
_namefile = v["namefile"]
_input = v["input"]
_output = v["output"]
p = subprocess.Popen(execString, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
try:
stdout, stderr = p.communicate(input=_input, timeout=self.timeout)
if p.returncode != 0: # Esercizio ha dato errore
# raise con i thread
# print(failed con i testcase passati)
# return normale con il for
# raise KozaError(stdout, stderr)
self.error = True
if i < 9:
print(f"Testcase: 0{i+1}, File: {_namefile} - ERROR ❌ \nExpected output: {_output} Actual output: {str(stdout)} \nError: {stderr}".ljust(30))
else:
print(
f"Testcase: {i+1}, File: {_namefile} - ERROR ❌ \nExpected output: {_output} Actual output: {str(stdout)} \nError: {stderr}".ljust(
30))
else:
if _output == stdout:
if i < 9:
print(f"Testcase: 0{i+1} - CORRECT ✅".ljust(30))
else:
print(f"Testcase: {i+1} - CORRECT ✅".ljust(30))
elif (_output + "\n") == stdout:
self.error = True
if i < 9:
print(f"Testcase: 0{i+1}, File: {_namefile} - SEMI-CORRECT ⚠️ Check for the end = \"\" in the print")
else:
print(f"Testcase: {i+1}, File: {_namefile} - SEMI-CORRECT ⚠️ Check for the end = \"\" in the print")
else:
self. error = True
if i < 9:
print(
f"Testcase: 0{i+1}, File: {_namefile} - WRONG ❌ \nExpected output: {_output} Actual output: {str(stdout)}".ljust(
30))
else:
print(
f"Testcase: {i+1}, File: {_namefile} - WRONG ❌ \nExpected output: {_output} Actual output: {str(stdout)}".ljust(
30))
except subprocess.TimeoutExpired:
p.kill()
self.error = True
if i < 9:
print(f"Testcase: {v[i]} - TIMEOUT EXPIRED ⏰".ljust(30))
else:
print(f"Testcase: {v[i]} - TIMEOUT EXPIRED ⏰".ljust(30))
if not self.error and len(self.testcase) >= 1:
print("------------------------------------")
print("\tALL TESTCASES PASSED! 🥳")
elif not self.error and len(self.testcase) == 0:
print("------------------------------------")
print("\tNO TESTCASE FOUND! ")
else:
print("------------------------------------")
print("\tNOT ALL TESTCASES PASSED! 😱")
else:
print(f"Exercise with the name: {self.name} NOT FOUND!")
# Windows, Linux or Mac
def whatOs(self):
currentOs = platform.system()
windos = ["win32", "Windows"]
mec = ["Darwin", "mac"]
if currentOs in windos:
return "Windows"
elif currentOs in mec:
return "Mac"
else:
return "Linux"
def getTestcase(self, name: str) -> dict:
Testcases = {}
ain = []
aout = []
nomeIn = os.path.join(self.testPath, name, "*.in")
nomeOut = os.path.join(self.testPath, name, "*.out")
for file in glob.glob(nomeIn):
ain.append(file)
for file in glob.glob(nomeOut):
aout.append(file)
ain.sort()
aout.sort()
for i in range(len(ain)):
innt = ""
ouut = ""
with open(ain[i]) as f:
innt = "".join(f.readlines())
with open(aout[i]) as f:
ouut = "".join(f.readlines())
# Find name file testcase
nomefile = ain[i]
char = '/' if (self.currentOs == "Linux" or self.currentOs == "Mac") else '\\'
nomefile = nomefile[nomefile.rindex(char)+1:]
Testcases[i] = {
"namefile": nomefile,
"input": innt,
"output": ouut,
}
return Testcases
def getTimeout(self):
path = self.testPath + "\\" + self.name
config = configparser.ConfigParser()
if os.path.exists(os.path.join(path, "domjudge-problem.ini")):
with open(os.path.join(path, "domjudge-problem.ini")) as stream:
# Little trick to cheat the parser.
config.read_string("[top]\n" + stream.read())
self.timeout = float(config["top"]["timelimit"].rstrip("'").lstrip("'"))
if len(config["top"]["name"].rstrip("'").lstrip("'")) >= 3:
self.title = config["top"]["name"].rstrip("'").lstrip("'")
else:
self.timeout = 45.0
def fileExists(self):
esiste = False
if os.path.exists(self.testPath) and os.path.exists(self.exPath):
if os.path.exists(os.path.join(self.exPath, f"{self.name}.py")):
esiste = True
return esiste
def prova():
ciao = Test("Lab2Set")
ciao.testExercise()
if __name__ == "__main__":
if _DEBUG:
prova()
else:
pass