1
1
#!/usr/bin/env python
2
2
3
+ import os
3
4
import os .path
4
5
import unittest
5
6
import subprocess
6
7
import glob
8
+ import sys
7
9
8
10
class RunTests (unittest .TestCase ):
9
11
def _runTestFile (self , shortName , fileName , interpreterPath ):
10
12
print ("\n // %s" % shortName )
11
- exitCode = subprocess .call ([interpreterPath , fileName ])
13
+ sys .stdout .flush ()
14
+
15
+ logPath = fileName .replace ("test/" , "test/output/" ).replace (".wasm" , ".wasm.log" )
16
+ try :
17
+ os .remove (logPath )
18
+ except OSError :
19
+ pass
20
+
21
+ commandStr = ("%s %s | tee %s" ) % (interpreterPath , fileName , logPath )
22
+ exitCode = subprocess .call (commandStr , shell = True )
12
23
self .assertEqual (0 , exitCode , "test runner failed with exit code %i" % exitCode )
13
24
25
+ try :
26
+ expected = open (fileName .replace ("test/" , "test/expected-output/" ).replace (".wasm" , ".wasm.log" ))
27
+ except IOError :
28
+ print ("// WARNING: No expected output found for this test" )
29
+ return
30
+
31
+ output = open (logPath )
32
+
33
+ with expected :
34
+ with output :
35
+ expectedText = expected .read ()
36
+ actualText = output .read ()
37
+ self .assertEqual (expectedText , actualText )
38
+
39
+ def generate_test_case (rec ):
40
+ return lambda self : self ._runTestFile (* rec )
41
+
42
+
14
43
def generate_test_cases (cls , interpreterPath , files ):
15
44
for fileName in files :
16
- absFileName = os .path .abspath (fileName )
17
45
attrName = fileName
18
- testCase = lambda self : self ._runTestFile (attrName , absFileName , interpreterPath )
46
+ rec = (attrName , fileName , interpreterPath )
47
+ testCase = generate_test_case (rec )
19
48
setattr (cls , attrName , testCase )
20
49
21
50
def rebuild_interpreter ():
22
51
interpreterPath = os .path .abspath ("src/main.native" )
23
52
24
53
print ("// building main.native" )
54
+ sys .stdout .flush ()
25
55
exitCode = subprocess .call (["ocamlbuild" , "-libs" , "bigarray" , "main.native" ], cwd = os .path .abspath ("src" ))
26
56
if (exitCode != 0 ):
27
57
raise Exception ("ocamlbuild failed with exit code %i" % exitCode )
28
58
29
59
return interpreterPath
30
60
31
- if __name__ == "__main__" :
61
+ if __name__ == "__main__" :
62
+ try :
63
+ os .makedirs ("test/output/" )
64
+ except OSError :
65
+ pass
66
+
32
67
interpreterPath = rebuild_interpreter ()
33
- generate_test_cases (RunTests , interpreterPath , glob .glob ("test/*.wasm" ))
68
+ testFiles = glob .glob ("test/*.wasm" )
69
+ generate_test_cases (RunTests , interpreterPath , testFiles )
34
70
unittest .main ()
0 commit comments