-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestcasesScript.py
More file actions
149 lines (121 loc) · 5.57 KB
/
testcasesScript.py
File metadata and controls
149 lines (121 loc) · 5.57 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
148
import os
import sys
import glob
import shutil
import subprocess
def buildAndTest(submissionpath, sourceTestPath):
script_path = os.path.dirname(os.path.realpath(__file__))
# create temporary directory so that previous students' results will not affect subsequent tests
testCasePath = sourceTestPath
testCases = glob.glob(os.path.join(testCasePath, "*.simplec"))
for i in glob.glob(os.path.join(submissionpath, "*.o")):
if os.path.exists(i):
os.remove(i)
progname = os.path.join(submissionpath, "simplec")
if os.path.exists(progname):
os.remove(progname)
if len(testCases) == 0:
print("# no tests found. double-check your path: " + testCasePath)
sys.exit()
print("# the following are all the commands run by this test script. you can cut-and-paste them to run them by hand.")
if os.path.exists(submissionpath + "/simplec"):
os.remove(submissionpath + "/simplec")
print("# building your simplec compiler")
print("make")
out = subprocess.run(['make'], cwd = submissionpath,
stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
output = ""
err = ""
if out.returncode != 0:
output += "# ERROR running make failed."
print(output + " Do you have a Makefile?") # can't even compile the compiler
return None, None, output
simpleCfile = os.path.join(submissionpath, "simplec")
totalCount = len(testCases)
errorCount = 0
# simpleC compilers lives so lets go through every test case now
for case in testCases:
caseTestFile = case
caseGroundTruth = case.replace(".simplec",".groundtruth")
caseGroundTruthErr = case.replace(".simplec",".groundtrutherr")
caseLLfile = case.replace(".simplec",".ll")
caseBinary = case.replace(".simplec","")
outFile = case.replace(".simplec",".out")
errFile = case.replace(".simplec",".err")
print("\n# TESTING " + caseTestFile)
try:
args = ["bash", os.path.join(script_path, "compile.sh"), simpleCfile, case]
command = " ".join(args)
print(command)
out = subprocess.run(args,
timeout=5, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
if out.returncode != 0:
err = error("compile.sh", case)
print(err)
output += err
errorCount += 1
continue
else: print ("# SUCCESS")
if os.path.exists(caseGroundTruth):
args = ["bash", os.path.join(script_path, "run.sh"), caseLLfile]
command = " ".join(args)
print(command)
out = subprocess.run(args,
timeout=5, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
if out.returncode != 0:
err = error("run.sh", caseLLfile)
print(err)
output += err
errorCount += 1
continue
else: print ("# SUCCESS")
args = ["diff", "--strip-trailing-cr", "-Z", caseGroundTruth, outFile]
command = " ".join(args)
print(command)
out = subprocess.run(args) #, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
if out.returncode != 0: #if the test case fails diff, increment error counter
err = error("diff", outFile)
print(err)
output += err
errorCount += 1
else: print ("# SUCCESS")
elif os.path.exists(caseGroundTruthErr):
with open(caseGroundTruthErr) as f:
lines = f.readlines()
match = '^' + lines[0].strip() + '$'
# args = ["diff", "--strip-trailing-cr", "-Z", '--unchanged-group-format=""', r'--old-group-format="%<"', '--new-group-format=""', caseGroundTruthErr, errFile]
# args = ["diff", "--strip-trailing-cr", "-Z", '--unchanged-group-format=', r'--old-group-format=%<', '--new-group-format=', caseGroundTruthErr, errFile]
args = ["egrep", match, errFile]
command = " ".join(["egrep", '"' + match + '"', errFile])
print(command)
out = subprocess.run(args) #, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
if out.returncode != 0: #if the test case fails diff, increment error counter
err = error("grep", errFile)
print(err)
output += err
errorCount += 1
else: print ("# SUCCESS")
except Exception as e:
print(str(e))
output += str(e) + "\n"
err = error("compile.sh", case)
print(err)
output += err
errorCount += 1
continue
value = totalCount - errorCount
test_pass = repr(value) + " test cases passed out of " + repr(totalCount)
print(test_pass)
output += test_pass
return totalCount, value, output
def error(app, f):
return "# ERROR " + app + " failed on " + f + "\n"
if __name__ == "__main__":
try:
submissionDirectory = sys.argv[1]
sourceTestPath = sys.argv[2]
except:
print("USAGE: path/to/your/repo path/to/the/tests")
print("example: ./ ../syllabus/projects/tests/proj0/")
sys.exit()
buildAndTest(submissionDirectory, sourceTestPath)