def runParserTests(self, jsshell, testPath=None): """Get all .pjs in test/parser/ files as JSON, and run through the test harness, faking a DOM""" jsshell = os.path.abspath(jsshell) parsertestdir = os.path.join(self.toolsdir, '..', 'test', 'parser') if testPath: testPath = os.path.abspath(testPath) for root, dirs, filenames in os.walk(parsertestdir): for filename in filenames: # If a single test file name is given, only test that file fullpath = os.path.join(root, filename) if testPath and not fullpath.startswith(testPath): continue if filename.endswith('.pde'): tmpFile = jsshellhelper.createEscapedFile(fullpath) one_test = 'var parserTest = {name:"' + fullpath + '", body: __unescape_string()};\n' testCmd = [jsshell, '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', one_test, '-f', os.path.join(self.toolsdir, 'test-harness.js')] proc = Popen(testCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stderr: # we failed to parse, and died in the js shell self.testsFailed += 1 print "TEST-FAILED: " + fullpath print stderr elif stdout: # TEST-SUMMARY: passed/failed m = re.search('^TEST-SUMMARY: (\d+)/(\d+)', stdout, re.MULTILINE) if m and m.group: self.testsPassed += int(m.group(1)) self.testsFailed += int(m.group(2)) if int(m.group(2)) > 0: print "TEST-FAILED: " + fullpath print re.sub("\n?TEST-SUMMARY: (\d+)\/(\d+)\n?", "", stdout) print stderr else: print "TEST-PASSED: " + fullpath else: # Shouldn't happen! self.testsFailed += 1 print "TEST-FAILED: " + fullpath + ". Test died:" print stdout jsshellhelper.cleanUp(tmpFile)
def run(self, jsshell, filename): tmpFile = jsshellhelper.createEscapedFile(filename) cmd = [jsshell, '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', 'var input = __unescape_string();\n', '-f', os.path.join(self.toolsdir, 'jslint-cmdline.js')] proc = Popen(cmd) stdout, stderr = proc.communicate() if stdout: print stdout else: print stderr jsshellhelper.cleanUp(tmpFile)
def run(self, jsshell, filename): tmpFile = jsshellhelper.createEscapedFile(filename) cmd = [jsshell, '-f', os.path.join(self.toolsdir, 'packer.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', "var input = __unescape_string(); print(pack(input, 62, 1, 0));"] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stdout: print stdout else: print stderr tmpFile = jsshellhelper.cleanUp(tmpFile)
def run(self, jsshell, filename): tmpFile = jsshellhelper.createEscapedFile(filename) cmd = [jsshell, '-f', os.path.join(self.toolsdir, 'jsbeautify.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', "var input = __unescape_string(); print(js_beautify(input, {indent_size: 2}));"] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stdout: print stdout else: print stderr jsshellhelper.cleanUp(tmpFile)
def run(self, jsshell, filename): tmpFile = jsshellhelper.createEscapedFile(filename) cmd = [ jsshell, '-f', os.path.join(self.toolsdir, 'packer.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', "var input = __unescape_string(); print(pack(input, 62, 1, 0));" ] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stdout: print stdout else: print stderr tmpFile = jsshellhelper.cleanUp(tmpFile)
def run(self, jsshell, filename): tmpFile = jsshellhelper.createEscapedFile(filename) cmd = [ jsshell, '-f', os.path.join(self.toolsdir, 'jsbeautify.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', "var input = __unescape_string(); print(js_beautify(input, {indent_size: 2}));" ] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stdout: print stdout else: print stderr jsshellhelper.cleanUp(tmpFile)
def run(self, jsshell, filename, strip=None): tmpFile = jsshellhelper.createEscapedFile(filename) cmd = [jsshell, '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', os.path.join(self.toolsdir, 'jsbeautify.js'), '-f', tmpFile, '-e', 'var pcode = __unescape_string(); print(js_beautify(Processing.parse(pcode, Processing(canvas, pcode))));\n'] proc = Popen(cmd) # allow user to kill hung subprocess with SIGINT w/o killing this script # - don't move this line above Popen, or child will inherit the SIG_IGN signal.signal(signal.SIGINT, signal.SIG_IGN) # |stderr == None| as |pStderr| was either |None| or redirected to |stdout|. stdout, stderr = proc.communicate() signal.signal(signal.SIGINT, signal.SIG_DFL) jsshellhelper.cleanUp(tmpFile)
def run(self, jsshell, filename, strip=None): tmpFile = jsshellhelper.createEscapedFile(filename) cmd = [ jsshell, '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', os.path.join(self.toolsdir, 'jsbeautify.js'), '-f', tmpFile, '-e', 'var pcode = __unescape_string(); print(js_beautify(Processing.parse(pcode, Processing(canvas, pcode))));\n' ] proc = Popen(cmd) # allow user to kill hung subprocess with SIGINT w/o killing this script # - don't move this line above Popen, or child will inherit the SIG_IGN signal.signal(signal.SIGINT, signal.SIG_IGN) # |stderr == None| as |pStderr| was either |None| or redirected to |stdout|. stdout, stderr = proc.communicate() signal.signal(signal.SIGINT, signal.SIG_DFL) jsshellhelper.cleanUp(tmpFile)
def runParserTests(self, jsshell, testPattern=None, summaryOnly=False, processingPath=None): """Get all .pjs in test/parser/ files as JSON, and run through the test harness, faking a DOM""" jsshell = os.path.abspath(jsshell) parsertestdir = os.path.join(self.toolsdir, '..', 'test', 'parser') processing_js = None if processingPath: processing_js =os.path.join(self.toolsdir, '..', processingPath.replace('/', os.sep)) else: processing_js = os.path.join(self.toolsdir, '..', 'processing.js') for root, dirs, filenames in os.walk(parsertestdir): for filename in filenames: sys.stdout.flush() sys.stderr.flush() # If a single test file name is given, only test that file fullpath = os.path.abspath(os.path.join(root, filename)) if testPattern and self.shouldSkipTest(testPattern, fullpath): continue if filename.endswith('.pde'): tmpFile = jsshellhelper.createEscapedFile(fullpath) one_test = 'var parserTest = {name:"' + fullpath + '", body: __unescape_string()};\n' testCmd = [jsshell, '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', processing_js, #os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', one_test, '-f', os.path.join(self.toolsdir, 'test-harness.js')] proc = Popen(testCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stderr: # we failed to parse, and died in the js shell if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write('K') self.testsFailedKnown += 1 else: sys.stdout.write('F') self.testsFailed += 1 sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath self.testsFailedKnown += 1 else: print "TEST-FAILED: " + fullpath print stderr self.testsFailed += 1 elif stdout: # TEST-SUMMARY: passed/failed m = re.search('^TEST-SUMMARY: (\d+)/(\d+)', stdout, re.MULTILINE) if m and m.group: self.testsPassed += int(m.group(1)) if self.isKnownFailure(fullpath): self.testsFailedKnown += int(m.group(2)) else: self.testsFailed += int(m.group(2)) if int(m.group(2)) > 0: if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write('K') else: sys.stdout.write('F') sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath else: print "TEST-FAILED: " + fullpath print re.sub("\n?TEST-SUMMARY: (\d+)\/(\d+)(\nLINES-CALLED: ([\d,]+))?\n?", "", stdout) print stderr else: if summaryOnly: if self.isKnownFailure(fullpath): # we should pass if we are expecting to fail! sys.stdout.write('!') else: sys.stdout.write('.') sys.stdout.flush() else: if self.isKnownFailure(fullpath): # we shouldn't pass if we are expecting to fail! print "TEST-FAILED (known failure passed!): " + fullpath self.testsPassed -= 1 self.testsFailed += 1 else: print "TEST-PASSED: " + fullpath else: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write('F') sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test died:" print stdout m2 = re.search('^LINES-CALLED: ([\d,]+)', stdout, re.MULTILINE) if m2 and m2.group: for ln in m2.group(1).split(','): self.linesCalled.add(int(ln)) jsshellhelper.cleanUp(tmpFile)
def runUnitTests(self, jsshell, testPattern=None, summaryOnly=False, processingPath=None): """Run all .js unit tests in test/unit through the test harness.""" # TODO: add support for doing .pjs unit tests. unittestdir = os.path.join(self.toolsdir, '..', 'test', 'unit') jsshell = os.path.abspath(jsshell) processing_js = None if processingPath: processing_js =os.path.join(self.toolsdir, '..', processingPath.replace('/', os.sep)) else: processing_js = os.path.join(self.toolsdir, '..', 'processing.js') for root, dirs, filenames in os.walk(unittestdir): for filename in filenames: sys.stdout.flush() sys.stderr.flush() # If a single test file name is given, only test that file fullpath = os.path.abspath(os.path.join(root, filename)) if testPattern and self.shouldSkipTest(testPattern, fullpath): continue tmpFile = None testCmd = None if filename.endswith('.js'): # Read the test file so we can wrap it properly: f = open(fullpath, 'r') testFile = ''.join(f.readlines()).replace("'", "\'").replace('"', '\"'); f.close() # We wrap all tests in a function so as to replace the context with the Processing context wrapper = "function _testWrapper(ctx) { with (ctx) { %s \n _runTest(); }}\n" % testFile testCmd = [jsshell, '-e', 'var _testName = "%s"; %s;' % (fullpath, wrapper), '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', processing_js, #os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'test-harness.js')] elif filename.endswith('.pde'): tmpFile = jsshellhelper.createEscapedFile(fullpath) testCmd = [jsshell, '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', processing_js, #os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'test-harness-lib.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', 'eval(new Processing(canvas, \'UnitTests();\' + __unescape_string() + \'_printTestSummary();\'));'] else: continue proc = Popen(testCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stdout: # TEST-SUMMARY: passed/failed m = re.search('^TEST-SUMMARY: (\d+)/(\d+)', stdout, re.MULTILINE) if m and m.group: self.testsPassed += int(m.group(1)) if self.isKnownFailure(fullpath): self.testsFailedKnown += int(m.group(2)) else: self.testsFailed += int(m.group(2)) if int(m.group(2)) > 0: if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write('K') else: sys.stdout.write('F') sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath else: print "TEST-FAILED: " + fullpath print re.sub("\n?TEST-SUMMARY: (\d+)\/(\d+)(\nLINES-CALLED: ([\d,]+))?\n?", "", stdout) print stderr else: if summaryOnly: if self.isKnownFailure(fullpath): # we should pass if we are expecting to fail! sys.stdout.write('!') else: sys.stdout.write('.') sys.stdout.flush() else: if self.isKnownFailure(fullpath): # we shouldn't pass if we are expecting to fail! print "TEST-FAILED (known failure passed!): " + fullpath self.testsPassed -= 1 self.testsFailed += 1 else: print "TEST-PASSED: " + fullpath else: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write('F') sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test died:" print stdout elif stderr: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write('F') sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test exited early:" print stderr m2 = re.search('^LINES-CALLED: ([\d,]+)', stdout, re.MULTILINE) if m2 and m2.group: for ln in m2.group(1).split(','): self.linesCalled.add(int(ln)) if tmpFile: jsshellhelper.cleanUp(tmpFile)
def runParserTests(self, jsshell, testPattern=None, summaryOnly=False, processingPath=None): """Get all .pjs in test/parser/ files as JSON, and run through the test harness, faking a DOM""" jsshell = os.path.abspath(jsshell) parsertestdir = os.path.join(self.toolsdir, '..', 'test', 'parser') processing_js = None if processingPath: processing_js = os.path.join(self.toolsdir, '..', processingPath.replace('/', os.sep)) else: processing_js = os.path.join(self.toolsdir, '..', 'processing.js') for root, dirs, filenames in os.walk(parsertestdir): for filename in filenames: sys.stdout.flush() sys.stderr.flush() # If a single test file name is given, only test that file fullpath = os.path.abspath(os.path.join(root, filename)) if testPattern and self.shouldSkipTest(testPattern, fullpath): continue if filename.endswith('.pde'): tmpFile = jsshellhelper.createEscapedFile(fullpath) one_test = 'var parserTest = {name:"' + fullpath + '", body: __unescape_string()};\n' testCmd = [ jsshell, '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', processing_js, #os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', one_test, '-f', os.path.join(self.toolsdir, 'test-harness.js') ] proc = Popen(testCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stderr: # we failed to parse, and died in the js shell if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write('K') self.testsFailedKnown += 1 else: sys.stdout.write('F') self.testsFailed += 1 sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath self.testsFailedKnown += 1 else: print "TEST-FAILED: " + fullpath print stderr self.testsFailed += 1 elif stdout: # TEST-SUMMARY: passed/failed m = re.search('^TEST-SUMMARY: (\d+)/(\d+)', stdout, re.MULTILINE) if m and m.group: self.testsPassed += int(m.group(1)) if self.isKnownFailure(fullpath): self.testsFailedKnown += int(m.group(2)) else: self.testsFailed += int(m.group(2)) if int(m.group(2)) > 0: if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write('K') else: sys.stdout.write('F') sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath else: print "TEST-FAILED: " + fullpath print re.sub( "\n?TEST-SUMMARY: (\d+)\/(\d+)(\nLINES-CALLED: ([\d,]+))?\n?", "", stdout) print stderr else: if summaryOnly: if self.isKnownFailure(fullpath): # we should pass if we are expecting to fail! sys.stdout.write('!') else: sys.stdout.write('.') sys.stdout.flush() else: if self.isKnownFailure(fullpath): # we shouldn't pass if we are expecting to fail! print "TEST-FAILED (known failure passed!): " + fullpath self.testsPassed -= 1 self.testsFailed += 1 else: print "TEST-PASSED: " + fullpath else: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write('F') sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test died:" print stdout m2 = re.search('^LINES-CALLED: ([\d,]+)', stdout, re.MULTILINE) if m2 and m2.group: for ln in m2.group(1).split(','): self.linesCalled.add(int(ln)) jsshellhelper.cleanUp(tmpFile)
def runUnitTests(self, jsshell, testPattern=None, summaryOnly=False, processingPath=None): """Run all .js unit tests in test/unit through the test harness.""" # TODO: add support for doing .pjs unit tests. unittestdir = os.path.join(self.toolsdir, '..', 'test', 'unit') jsshell = os.path.abspath(jsshell) processing_js = None if processingPath: processing_js = os.path.join(self.toolsdir, '..', processingPath.replace('/', os.sep)) else: processing_js = os.path.join(self.toolsdir, '..', 'processing.js') for root, dirs, filenames in os.walk(unittestdir): for filename in filenames: sys.stdout.flush() sys.stderr.flush() # If a single test file name is given, only test that file fullpath = os.path.abspath(os.path.join(root, filename)) if testPattern and self.shouldSkipTest(testPattern, fullpath): continue tmpFile = None testCmd = None if filename.endswith('.js'): # Read the test file so we can wrap it properly: f = open(fullpath, 'r') testFile = ''.join(f.readlines()).replace("'", "\'").replace( '"', '\"') f.close() # We wrap all tests in a function so as to replace the context with the Processing context wrapper = "function _testWrapper(ctx) { with (ctx) { %s \n _runTest(); }}\n" % testFile testCmd = [ jsshell, '-e', 'var _testName = "%s"; %s;' % (fullpath, wrapper), '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', processing_js, #os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'test-harness.js') ] elif filename.endswith('.pde'): tmpFile = jsshellhelper.createEscapedFile(fullpath) testCmd = [ jsshell, '-f', os.path.join(self.toolsdir, 'fake-dom.js'), '-f', processing_js, #os.path.join(self.toolsdir, '..', 'processing.js'), '-f', os.path.join(self.toolsdir, 'test-harness-lib.js'), '-f', os.path.join(self.toolsdir, 'cleaner.js'), '-f', tmpFile, '-e', 'eval(new Processing(canvas, \'UnitTests();\' + __unescape_string() + \'_printTestSummary();\'));' ] else: continue proc = Popen(testCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stdout: # TEST-SUMMARY: passed/failed m = re.search('^TEST-SUMMARY: (\d+)/(\d+)', stdout, re.MULTILINE) if m and m.group: self.testsPassed += int(m.group(1)) if self.isKnownFailure(fullpath): self.testsFailedKnown += int(m.group(2)) else: self.testsFailed += int(m.group(2)) if int(m.group(2)) > 0: if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write('K') else: sys.stdout.write('F') sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath else: print "TEST-FAILED: " + fullpath print re.sub( "\n?TEST-SUMMARY: (\d+)\/(\d+)(\nLINES-CALLED: ([\d,]+))?\n?", "", stdout) print stderr else: if summaryOnly: if self.isKnownFailure(fullpath): # we should pass if we are expecting to fail! sys.stdout.write('!') else: sys.stdout.write('.') sys.stdout.flush() else: if self.isKnownFailure(fullpath): # we shouldn't pass if we are expecting to fail! print "TEST-FAILED (known failure passed!): " + fullpath self.testsPassed -= 1 self.testsFailed += 1 else: print "TEST-PASSED: " + fullpath else: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write('F') sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test died:" print stdout elif stderr: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write('F') sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test exited early:" print stderr m2 = re.search('^LINES-CALLED: ([\d,]+)', stdout, re.MULTILINE) if m2 and m2.group: for ln in m2.group(1).split(','): self.linesCalled.add(int(ln)) if tmpFile: jsshellhelper.cleanUp(tmpFile)
def runParserTests(self, jsshell, testPattern=None, summaryOnly=False): """Get all .pjs in test/parser/ files as JSON, and run through the test harness, faking a DOM""" jsshell = os.path.abspath(jsshell) parsertestdir = os.path.join(self.toolsdir, "..", "test", "parser") for root, dirs, filenames in os.walk(parsertestdir): for filename in filenames: sys.stdout.flush() sys.stderr.flush() # If a single test file name is given, only test that file fullpath = os.path.abspath(os.path.join(root, filename)) if testPattern and self.shouldSkipTest(testPattern, fullpath): continue if filename.endswith(".pde"): tmpFile = jsshellhelper.createEscapedFile(fullpath) one_test = 'var parserTest = {name:"' + fullpath + '", body: __unescape_string()};\n' testCmd = [ jsshell, "-f", os.path.join(self.toolsdir, "fake-dom.js"), "-f", os.path.join(self.toolsdir, "..", "processing.js"), "-f", os.path.join(self.toolsdir, "cleaner.js"), "-f", tmpFile, "-e", one_test, "-f", os.path.join(self.toolsdir, "test-harness.js"), ] proc = Popen(testCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stderr: # we failed to parse, and died in the js shell if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write("K") self.testsFailedKnown += 1 else: sys.stdout.write("F") self.testsFailed += 1 sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath self.testsFailedKnown += 1 else: print "TEST-FAILED: " + fullpath print stderr self.testsFailed += 1 elif stdout: # TEST-SUMMARY: passed/failed m = re.search("^TEST-SUMMARY: (\d+)/(\d+)", stdout, re.MULTILINE) if m and m.group: self.testsPassed += int(m.group(1)) if self.isKnownFailure(fullpath): self.testsFailedKnown += int(m.group(2)) else: self.testsFailed += int(m.group(2)) if int(m.group(2)) > 0: if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write("K") else: sys.stdout.write("F") sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath else: print "TEST-FAILED: " + fullpath print re.sub("\n?TEST-SUMMARY: (\d+)\/(\d+)\n?", "", stdout) print stderr else: if summaryOnly: if self.isKnownFailure(fullpath): # we should pass if we are expecting to fail! sys.stdout.write("!") else: sys.stdout.write(".") sys.stdout.flush() else: if self.isKnownFailure(fullpath): # we shouldn't pass if we are expecting to fail! print "TEST-FAILED (known failure passed!): " + fullpath self.testsPassed -= 1 self.testsFailed += 1 else: print "TEST-PASSED: " + fullpath else: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write("F") sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test died:" print stdout jsshellhelper.cleanUp(tmpFile)
def runUnitTests(self, jsshell, testPattern=None, summaryOnly=False): """Run all .js unit tests in test/unit through the test harness.""" # TODO: add support for doing .pjs unit tests. unittestdir = os.path.join(self.toolsdir, "..", "test", "unit") jsshell = os.path.abspath(jsshell) for root, dirs, filenames in os.walk(unittestdir): for filename in filenames: sys.stdout.flush() sys.stderr.flush() # If a single test file name is given, only test that file fullpath = os.path.abspath(os.path.join(root, filename)) if testPattern and self.shouldSkipTest(testPattern, fullpath): continue tmpFile = None testCmd = None if filename.endswith(".js"): # Read the test file so we can wrap it properly: f = open(fullpath, "r") testFile = "".join(f.readlines()).replace("'", "'").replace('"', '"') f.close() # We wrap all tests in a function so as to replace the context with the Processing context wrapper = "function _testWrapper(ctx) { with (ctx) { %s \n _runTest(); }}\n" % testFile testCmd = [ jsshell, "-e", 'var _testName = "%s"; %s;' % (fullpath, wrapper), "-f", os.path.join(self.toolsdir, "fake-dom.js"), "-f", os.path.join(self.toolsdir, "..", "processing.js"), "-f", os.path.join(self.toolsdir, "test-harness.js"), ] elif filename.endswith(".pde"): tmpFile = jsshellhelper.createEscapedFile(fullpath) testCmd = [ jsshell, "-f", os.path.join(self.toolsdir, "fake-dom.js"), "-f", os.path.join(self.toolsdir, "..", "processing.js"), "-f", os.path.join(self.toolsdir, "test-harness-lib.js"), "-f", os.path.join(self.toolsdir, "cleaner.js"), "-f", tmpFile, "-e", "eval(Processing(canvas, 'UnitTests();' + __unescape_string() + '_printTestSummary();'));", ] else: continue proc = Popen(testCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() if stdout: # TEST-SUMMARY: passed/failed m = re.search("^TEST-SUMMARY: (\d+)/(\d+)", stdout, re.MULTILINE) if m and m.group: self.testsPassed += int(m.group(1)) if self.isKnownFailure(fullpath): self.testsFailedKnown += int(m.group(2)) else: self.testsFailed += int(m.group(2)) if int(m.group(2)) > 0: if summaryOnly: if self.isKnownFailure(fullpath): sys.stdout.write("K") else: sys.stdout.write("F") sys.stdout.flush() else: if self.isKnownFailure(fullpath): print "KNOWN-FAILURE: " + fullpath else: print "TEST-FAILED: " + fullpath print re.sub("\n?TEST-SUMMARY: (\d+)\/(\d+)\n?", "", stdout) print stderr else: if summaryOnly: if self.isKnownFailure(fullpath): # we should pass if we are expecting to fail! sys.stdout.write("!") else: sys.stdout.write(".") sys.stdout.flush() else: if self.isKnownFailure(fullpath): # we shouldn't pass if we are expecting to fail! print "TEST-FAILED (known failure passed!): " + fullpath self.testsPassed -= 1 self.testsFailed += 1 else: print "TEST-PASSED: " + fullpath else: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write("F") sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test died:" print stdout elif stderr: # Shouldn't happen! self.testsFailed += 1 if summaryOnly: sys.stdout.write("F") sys.stdout.flush() else: print "TEST-FAILED: " + fullpath + ". Test exited early:" print stderr if tmpFile: jsshellhelper.cleanUp(tmpFile)