📄 test.py.svn-base
字号:
#!/usr/bin/env python## Copyright 2008 the V8 project authors. All rights reserved.# Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions are# met:## * Redistributions of source code must retain the above copyright# notice, this list of conditions and the following disclaimer.# * Redistributions in binary form must reproduce the above# copyright notice, this list of conditions and the following# disclaimer in the documentation and/or other materials provided# with the distribution.# * Neither the name of Google Inc. nor the names of its# contributors may be used to endorse or promote products derived# from this software without specific prior written permission.## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.import impimport optparseimport osfrom os.path import join, dirname, abspath, basenameimport platformimport reimport signalimport subprocessimport sysimport tempfileimport timeimport utilsVERBOSE = False# ---------------------------------------------# --- P r o g r e s s I n d i c a t o r s ---# ---------------------------------------------class ProgressIndicator(object): def __init__(self, cases): self.cases = cases self.succeeded = 0 self.failed = 0 self.remaining = len(self.cases) self.total = len(self.cases) self.failed_tests = [ ] def Run(self): self.Starting() for test in self.cases: case = test.case self.AboutToRun(case) output = case.Run() if output.UnexpectedOutput(): self.failed += 1 self.failed_tests.append(output) else: self.succeeded += 1 self.remaining -= 1 self.HasRun(output) self.Done() return self.failed == 0def EscapeCommand(command): parts = [] for part in command: if ' ' in part: # Escape spaces. We may need to escape more characters for this # to work properly. parts.append('"%s"' % part) else: parts.append(part) return " ".join(parts)class SimpleProgressIndicator(ProgressIndicator): def Starting(self): print 'Running %i tests' % len(self.cases) def Done(self): print for failed in self.failed_tests: print "=== %s (%s) ===" % (failed.test.GetLabel(), "/".join(failed.test.path)) if failed.output.stderr: print "--- stderr ---" print failed.output.stderr.strip() if failed.output.stdout: print "--- stdout ---" print failed.output.stdout.strip() print "Command: %s" % EscapeCommand(failed.command) if len(self.failed_tests) == 0: print "===" print "=== All tests succeeded" print "===" else: print print "===" print "=== %i tests failed" % len(self.failed_tests) print "==="class VerboseProgressIndicator(SimpleProgressIndicator): def AboutToRun(self, case): print '%s:' % case.GetLabel(), sys.stdout.flush() def HasRun(self, output): if output.UnexpectedOutput(): print "FAIL" else: print "pass"class DotsProgressIndicator(SimpleProgressIndicator): def AboutToRun(self, case): pass def HasRun(self, output): total = self.succeeded + self.failed if (total > 1) and (total % 50 == 1): sys.stdout.write('\n') if output.UnexpectedOutput(): sys.stdout.write('F') sys.stdout.flush() else: sys.stdout.write('.') sys.stdout.flush()class CompactProgressIndicator(ProgressIndicator): def __init__(self, cases, templates): super(CompactProgressIndicator, self).__init__(cases) self.templates = templates self.last_status_length = 0 self.start_time = time.time() def Starting(self): pass def Done(self): self.PrintProgress('Done') def AboutToRun(self, case): self.PrintProgress(case.GetLabel()) def HasRun(self, output): if output.UnexpectedOutput(): print "=== %s (%s) ===" % (output.test.GetLabel(), "/".join(output.test.path)) print "Command: %s" % EscapeCommand(output.command) stdout = output.output.stdout.strip() if len(stdout): print self.templates['stdout'] % stdout stderr = output.output.stderr.strip() if len(stderr): print self.templates['stderr'] % stderr def Truncate(self, str, length): if length and (len(str) > (length - 3)): return str[:(length-3)] + "..." else: return str def PrintProgress(self, name): self.ClearLine(self.last_status_length) elapsed = time.time() - self.start_time status = self.templates['status_line'] % { 'passed': self.succeeded, 'remaining': (((self.total - self.remaining) * 100) // self.total), 'failed': self.failed, 'test': name, 'mins': int(elapsed) / 60, 'secs': int(elapsed) % 60 } status = self.Truncate(status, 78) self.last_status_length = len(status) print status, sys.stdout.flush()class ColorProgressIndicator(CompactProgressIndicator): def __init__(self, cases): templates = { 'status_line': "[%(mins)02i:%(secs)02i|\033[34m%%%(remaining) 4d\033[0m|\033[32m+%(passed) 4d\033[0m|\033[31m-%(failed) 4d\033[0m]: %(test)s", 'stdout': "\033[1m%s\033[0m", 'stderr': "\033[31m%s\033[0m", } super(ColorProgressIndicator, self).__init__(cases, templates) def ClearLine(self, last_line_length): print "\033[1K\r",class MonochromeProgressIndicator(CompactProgressIndicator): def __init__(self, cases): templates = { 'status_line': "[%(mins)02i:%(secs)02i|%%%(remaining) 4d|+%(passed) 4d|-%(failed) 4d]: %(test)s", 'stdout': '%s', 'stderr': '%s', 'clear': lambda last_line_length: ("\r" + (" " * last_line_length) + "\r"), 'max_length': 78 } super(MonochromeProgressIndicator, self).__init__(cases, templates) def ClearLine(self, last_line_length): print ("\r" + (" " * last_line_length) + "\r"),PROGRESS_INDICATORS = { 'verbose': VerboseProgressIndicator, 'dots': DotsProgressIndicator, 'color': ColorProgressIndicator, 'mono': MonochromeProgressIndicator}# -------------------------# --- F r a m e w o r k ---# -------------------------class CommandOutput(object): def __init__(self, exit_code, stdout, stderr): self.exit_code = exit_code self.stdout = stdout self.stderr = stderrclass TestCase(object): def __init__(self, context, path): self.path = path self.context = context def IsNegative(self): return False def IsFailureOutput(self, output): return output.exit_code != 0 def GetSource(self): return "(no source available)" def Run(self): command = self.GetCommand() full_command = self.context.processor(command) output = Execute(full_command, self.context, self.context.timeout) return TestOutput(self, full_command, output)class TestOutput(object): def __init__(self, test, command, output): self.test = test self.command = command self.output = output def UnexpectedOutput(self): if self.HasFailed(): outcome = FAIL else: outcome = PASS return not outcome in self.test.outcomes def HasFailed(self): execution_failed = self.test.IsFailureOutput(self.output) if self.test.IsNegative(): return not execution_failed else: return execution_faileddef KillProcessWithID(pid): if platform.system() == 'Windows': os.popen('taskkill /T /F /PID %d' % pid) else: os.kill(pid, signal.SIGTERM)MAX_SLEEP_TIME = 0.1INITIAL_SLEEP_TIME = 0.0001SLEEP_TIME_FACTOR = 1.25def RunProcess(context, timeout, args, **rest): if context.verbose: print "#", " ".join(args) popen_args = args if platform.system() == 'Windows': popen_args = '"' + subprocess.list2cmdline(args) + '"' process = subprocess.Popen( shell = (platform.system() == 'Windows'), args = popen_args, **rest ) # Compute the end time - if the process crosses this limit we # consider it timed out. if timeout is None: end_time = None else: end_time = time.time() + timeout timed_out = False # Repeatedly check the exit code from the process in a # loop and keep track of whether or not it times out. exit_code = None sleep_time = INITIAL_SLEEP_TIME while exit_code is None: if (not end_time is None) and (time.time() >= end_time): # Kill the process and wait for it to exit. KillProcessWithID(process.pid) exit_code = process.wait() timed_out = True else: exit_code = process.poll() time.sleep(sleep_time) sleep_time = sleep_time * SLEEP_TIME_FACTOR if sleep_time > MAX_SLEEP_TIME: sleep_time = MAX_SLEEP_TIME return (process, exit_code, timed_out)def PrintError(str): sys.stderr.write(str) sys.stderr.write('\n')def Execute(args, context, timeout=None): (fd_out, outname) = tempfile.mkstemp() (fd_err, errname) = tempfile.mkstemp() (process, exit_code, timed_out) = RunProcess( context, timeout, args = args, stdout = fd_out, stderr = fd_err, ) os.close(fd_out) os.close(fd_err) output = file(outname).read() errors = file(errname).read() def CheckedUnlink(name): try: os.unlink(name) except OSError, e: PrintError(str(e)) CheckedUnlink(outname) CheckedUnlink(errname) return CommandOutput(exit_code, output, errors)def ExecuteNoCapture(args, context, timeout=None): (process, exit_code, timed_out) = RunProcess( context, timeout, args = args, ) return CommandOutput(exit_code, "", "")def CarCdr(path): if len(path) == 0: return (None, [ ]) else:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -