-
Notifications
You must be signed in to change notification settings - Fork 20
/
runtests.py
executable file
·184 lines (136 loc) · 4.81 KB
/
runtests.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
#!/usr/bin/env python
import platform
import os
import os.path
import unittest
import subprocess
import glob
import sys
import shutil
import re
def get_libs(fileName):
with open(fileName, "r") as f:
text = f.read()
libComments = re.finditer("//#use (.*)", text)
return list(
os.path.join(os.path.dirname(fileName), match.group(1)) for match in libComments
)
def get_stdout_path(fileName):
with open(fileName, "r") as f:
text = f.read()
m = re.search("SetStdout\\(\"([^\"]*)\"\\);", text)
if m:
return m.group(1)
else:
return None
def compile_cs(fileName):
if platform.system() == "Windows":
csc = "csc"
else:
csc = "mcs"
testName = os.path.basename(fileName)
compiledPath = os.path.join("output", testName.replace(".cs", ".exe"))
libs = get_libs(fileName)
inTime = os.path.getmtime(fileName)
try:
outTime = os.path.getmtime(compiledPath)
except OSError:
outTime = 0
pass
isNewer = (outTime > inTime)
for libName in libs:
if os.path.getmtime(libName) > outTime:
isNewer = False
break
if isNewer:
return (0, compiledPath, get_stdout_path(fileName))
fileNames = [fileName] + libs
commandStr = ("%s /nologo /langversion:6 /debug+ /unsafe+ /debug:full %s /reference:%s /out:%s") % (
csc, " ".join(fileNames),
os.path.join("WasmMeta", "bin", "WasmMeta.dll"),
compiledPath
)
exitCode = subprocess.call(commandStr, shell=True)
if exitCode != 0:
print("failed while running '%s'" % commandStr)
print("")
return (exitCode, compiledPath, get_stdout_path(fileName))
def translate(compiledPath):
wasmPath = compiledPath.replace(".exe", ".wasm")
inTime = os.path.getmtime(compiledPath)
try:
outTime = os.path.getmtime(wasmPath)
if (outTime > inTime):
return (0, wasmPath)
except OSError:
pass
commandStr = ("mono %s ilwasm.jsilconfig --quiet --nodefaults --e=WasmSExpr --outputFile=%s %s") % (
os.path.join("third_party", "JSIL", "bin", "JSILc.exe"),
wasmPath, compiledPath
)
exitCode = subprocess.call(commandStr, shell=True)
if exitCode != 0:
print("failed while running '%s'" % commandStr)
print("")
return (exitCode, wasmPath)
def run_csharp(compiledPath):
commandStr = ("mono %s --quiet") % (compiledPath)
exitCode = subprocess.call(commandStr, shell=True)
if exitCode != 0:
print("failed while running '%s'" % commandStr)
print("")
return exitCode
def run_wasm(wasmPath, stdoutPath):
interpreterPath = os.path.realpath(os.path.join("third_party", "wasm-spec", "ml-proto", "_build", "host", "main.d.byte"))
commandStr = ("%s %s") % (interpreterPath, wasmPath)
csDataPath = None
wasmDataPath = None
csBytes = None
wasmBytes = None
if stdoutPath:
csDataPath = os.path.join("output", "cs-data", stdoutPath)
wasmDataPath = os.path.join("output", "wasm-data", stdoutPath)
commandStr += " > " + wasmDataPath
exitCode = subprocess.call(commandStr, shell=True)
if exitCode != 0:
print("failed while running '%s'" % commandStr)
print("")
if stdoutPath:
with open(csDataPath, "rb") as f:
csBytes = f.read()
with open(wasmDataPath, "rb") as f:
wasmBytes = f.read()
return (exitCode, csBytes, wasmBytes)
class RunTests(unittest.TestCase):
def _runTestFile(self, fileName):
(exitCode, compiledPath, stdoutPath) = compile_cs(fileName)
self.assertEqual(0, exitCode, "C# compiler failed with exit code %i" % exitCode)
(exitCode, wasmPath) = translate(compiledPath)
if exitCode != 0:
# HACK: If JSILc fails ensure that the C# compile and JSILc compile are repeated next run
os.unlink(compiledPath)
self.assertEqual(0, exitCode, "JSILc failed with exit code %i" % exitCode)
exitCode = run_csharp(compiledPath)
self.assertEqual(0, exitCode, "C# test case failed with exit code %i" % exitCode)
(exitCode, csBytes, wasmBytes) = run_wasm(wasmPath, stdoutPath)
self.assertEqual(csBytes, wasmBytes, "C# and wasm output did not match (%s)" % stdoutPath)
if exitCode != 0:
# HACK: If JSILc fails ensure that the C# compile and JSILc compile are repeated next run
os.unlink(compiledPath)
self.assertEqual(0, exitCode, "wasm interpreter failed with exit code %i" % exitCode)
def generate_test_case(fileName):
return lambda self : self._runTestFile(fileName)
def generate_test_cases(cls, files):
for fileName in files:
testCase = generate_test_case(fileName)
setattr(cls, fileName, testCase)
if __name__ == "__main__":
try:
os.makedirs("output/wasm-data")
except OSError:
pass
testFiles = glob.glob(os.path.join("third_party", "tests", "*.cs"))
unittest.TestLoader.testMethodPrefix = os.path.join("third_party", "tests")
generate_test_cases(RunTests, testFiles)
shutil.copy2(os.path.join("WasmMeta", "bin", "WasmMeta.dll"), "output")
unittest.main()