1#!/usr/bin/env python3 2 3from __future__ import print_function 4import argparse 5import os 6import os.path 7import unittest 8import subprocess 9import glob 10import sys 11 12 13ownDir = os.path.dirname(os.path.abspath(sys.argv[0])) 14inputDir = ownDir 15outputDir = os.path.join(inputDir, "_output") 16 17parser = argparse.ArgumentParser() 18parser.add_argument("--wasm", metavar="<wasm-command>", default=os.path.join(os.getcwd(), "wasm")) 19parser.add_argument("--js", metavar="<js-command>") 20parser.add_argument("--out", metavar="<out-dir>", default=outputDir) 21parser.add_argument("file", nargs='*') 22arguments = parser.parse_args() 23sys.argv = sys.argv[:1] 24 25wasmCommand = arguments.wasm 26jsCommand = arguments.js 27outputDir = arguments.out 28inputFiles = arguments.file if arguments.file else glob.glob(os.path.join(inputDir, "*.wast")) 29 30if not os.path.exists(wasmCommand): 31 sys.stderr.write("""\ 32Error: The executable '%s' does not exist. 33Provide the correct path with the '--wasm' flag. 34 35""" % (wasmCommand)) 36 parser.print_help() 37 sys.exit(1) 38 39 40class RunTests(unittest.TestCase): 41 def _runCommand(self, command, logPath, expectedExitCode = 0): 42 with open(logPath, 'w+') as out: 43 exitCode = subprocess.call(command, shell=True, stdout=out, stderr=subprocess.STDOUT) 44 self.assertEqual(expectedExitCode, exitCode, "failed with exit code %i (expected %i) for %s" % (exitCode, expectedExitCode, command)) 45 46 def _auxFile(self, path): 47 if os.path.exists(path): 48 os.remove(path) 49 return path 50 51 def _compareFile(self, expectFile, actualFile): 52 if os.path.exists(expectFile): 53 with open(expectFile) as expect: 54 with open(actualFile) as actual: 55 expectText = expect.read() 56 actualText = actual.read() 57 self.assertEqual(expectText, actualText) 58 59 def _runTestFile(self, inputPath): 60 dir, inputFile = os.path.split(inputPath) 61 outputPath = os.path.join(outputDir, inputFile) 62 63 # Run original file 64 expectedExitCode = 1 if ".fail." in inputFile else 0 65 logPath = self._auxFile(outputPath + ".log") 66 self._runCommand(('%s "%s"') % (wasmCommand, inputPath), logPath, expectedExitCode) 67 68 if expectedExitCode != 0: 69 return 70 71 # Convert to binary and run again 72 wasmPath = self._auxFile(outputPath + ".bin.wast") 73 logPath = self._auxFile(wasmPath + ".log") 74 self._runCommand(('%s -d "%s" -o "%s"') % (wasmCommand, inputPath, wasmPath), logPath) 75 self._runCommand(('%s "%s"') % (wasmCommand, wasmPath), logPath) 76 77 # Convert back to text and run again 78 wastPath = self._auxFile(wasmPath + ".wast") 79 logPath = self._auxFile(wastPath + ".log") 80 self._runCommand(('%s -d "%s" -o "%s"') % (wasmCommand, wasmPath, wastPath), logPath) 81 self._runCommand(('%s "%s"') % (wasmCommand, wastPath), logPath) 82 83 # Convert back to binary once more and compare 84 wasm2Path = self._auxFile(wastPath + ".bin.wast") 85 logPath = self._auxFile(wasm2Path + ".log") 86 self._runCommand(('%s -d "%s" -o "%s"') % (wasmCommand, wastPath, wasm2Path), logPath) 87 self._compareFile(wasmPath, wasm2Path) 88 89 # Convert back to text once more and compare 90 wast2Path = self._auxFile(wasm2Path + ".wast") 91 logPath = self._auxFile(wast2Path + ".log") 92 self._runCommand(('%s -d "%s" -o "%s"') % (wasmCommand, wasm2Path, wast2Path), logPath) 93 self._compareFile(wastPath, wast2Path) 94 95 # Convert to JavaScript 96 jsPath = self._auxFile(outputPath.replace(".wast", ".js")) 97 logPath = self._auxFile(jsPath + ".log") 98 self._runCommand(('%s -d "%s" -o "%s"') % (wasmCommand, inputPath, jsPath), logPath) 99 if jsCommand != None: 100 self._runCommand(('%s "%s"') % (jsCommand, jsPath), logPath) 101 102 103if __name__ == "__main__": 104 if not os.path.exists(outputDir): 105 os.makedirs(outputDir) 106 for fileName in inputFiles: 107 testName = 'test ' + os.path.basename(fileName) 108 setattr(RunTests, testName, lambda self, file=fileName: self._runTestFile(file)) 109 unittest.main() 110