60 import Queue as queue |
60 import Queue as queue |
61 |
61 |
62 processlock = threading.Lock() |
62 processlock = threading.Lock() |
63 |
63 |
64 closefds = os.name == 'posix' |
64 closefds = os.name == 'posix' |
65 def Popen4(cmd, wd, timeout): |
65 def Popen4(cmd, wd, timeout, env=None): |
66 processlock.acquire() |
66 processlock.acquire() |
67 p = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=wd, |
67 p = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=wd, env=env, |
68 close_fds=closefds, |
68 close_fds=closefds, |
69 stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
69 stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
70 stderr=subprocess.STDOUT) |
70 stderr=subprocess.STDOUT) |
71 processlock.release() |
71 processlock.release() |
72 |
72 |
547 adir = os.path.join(TESTDIR, 'annotated') |
547 adir = os.path.join(TESTDIR, 'annotated') |
548 if not os.path.isdir(adir): |
548 if not os.path.isdir(adir): |
549 os.mkdir(adir) |
549 os.mkdir(adir) |
550 covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit) |
550 covrun('-i', '-a', '"--directory=%s"' % adir, '"--omit=%s"' % omit) |
551 |
551 |
552 def pytest(test, wd, options, replacements): |
552 def pytest(test, wd, options, replacements, env): |
553 py3kswitch = options.py3k_warnings and ' -3' or '' |
553 py3kswitch = options.py3k_warnings and ' -3' or '' |
554 cmd = '%s%s "%s"' % (PYTHON, py3kswitch, test) |
554 cmd = '%s%s "%s"' % (PYTHON, py3kswitch, test) |
555 vlog("# Running", cmd) |
555 vlog("# Running", cmd) |
556 if os.name == 'nt': |
556 if os.name == 'nt': |
557 replacements.append((r'\r\n', '\n')) |
557 replacements.append((r'\r\n', '\n')) |
558 return run(cmd, wd, options, replacements) |
558 return run(cmd, wd, options, replacements, env) |
559 |
559 |
560 needescape = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search |
560 needescape = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search |
561 escapesub = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub |
561 escapesub = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub |
562 escapemap = dict((chr(i), r'\x%02x' % i) for i in range(256)) |
562 escapemap = dict((chr(i), r'\x%02x' % i) for i in range(256)) |
563 escapemap.update({'\\': '\\\\', '\r': r'\r'}) |
563 escapemap.update({'\\': '\\\\', '\r': r'\r'}) |
613 if (el.endswith(" (re)\n") and rematch(el[:-6], l) or |
613 if (el.endswith(" (re)\n") and rematch(el[:-6], l) or |
614 el.endswith(" (glob)\n") and globmatch(el[:-8], l)): |
614 el.endswith(" (glob)\n") and globmatch(el[:-8], l)): |
615 return True |
615 return True |
616 return False |
616 return False |
617 |
617 |
618 def tsttest(test, wd, options, replacements): |
618 def tsttest(test, wd, options, replacements, env): |
619 # We generate a shell script which outputs unique markers to line |
619 # We generate a shell script which outputs unique markers to line |
620 # up script results with our source. These markers include input |
620 # up script results with our source. These markers include input |
621 # line number and the last return code |
621 # line number and the last return code |
622 salt = "SALT" + str(time.time()) |
622 salt = "SALT" + str(time.time()) |
623 def addsalt(line, inpython): |
623 def addsalt(line, inpython): |
739 os.write(fd, l) |
739 os.write(fd, l) |
740 os.close(fd) |
740 os.close(fd) |
741 |
741 |
742 cmd = '%s "%s"' % (options.shell, name) |
742 cmd = '%s "%s"' % (options.shell, name) |
743 vlog("# Running", cmd) |
743 vlog("# Running", cmd) |
744 exitcode, output = run(cmd, wd, options, replacements) |
744 exitcode, output = run(cmd, wd, options, replacements, env) |
745 # do not merge output if skipped, return hghave message instead |
745 # do not merge output if skipped, return hghave message instead |
746 # similarly, with --debug, output is None |
746 # similarly, with --debug, output is None |
747 if exitcode == SKIPPED_STATUS or output is None: |
747 if exitcode == SKIPPED_STATUS or output is None: |
748 return exitcode, output |
748 return exitcode, output |
749 finally: |
749 finally: |
789 postout += after.pop(pos) |
789 postout += after.pop(pos) |
790 |
790 |
791 return exitcode, postout |
791 return exitcode, postout |
792 |
792 |
793 wifexited = getattr(os, "WIFEXITED", lambda x: False) |
793 wifexited = getattr(os, "WIFEXITED", lambda x: False) |
794 def run(cmd, wd, options, replacements): |
794 def run(cmd, wd, options, replacements, env): |
795 """Run command in a sub-process, capturing the output (stdout and stderr). |
795 """Run command in a sub-process, capturing the output (stdout and stderr). |
796 Return a tuple (exitcode, output). output is None in debug mode.""" |
796 Return a tuple (exitcode, output). output is None in debug mode.""" |
797 # TODO: Use subprocess.Popen if we're running on Python 2.4 |
797 # TODO: Use subprocess.Popen if we're running on Python 2.4 |
798 if options.debug: |
798 if options.debug: |
799 proc = subprocess.Popen(cmd, shell=True, cwd=wd) |
799 proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env) |
800 ret = proc.wait() |
800 ret = proc.wait() |
801 return (ret, None) |
801 return (ret, None) |
802 |
802 |
803 proc = Popen4(cmd, wd, options.timeout) |
803 proc = Popen4(cmd, wd, options.timeout, env) |
804 def cleanup(): |
804 def cleanup(): |
805 terminate(proc) |
805 terminate(proc) |
806 ret = proc.wait() |
806 ret = proc.wait() |
807 if ret == 0: |
807 if ret == 0: |
808 ret = signal.SIGTERM << 8 |
808 ret = signal.SIGTERM << 8 |
928 '\\' + c |
928 '\\' + c |
929 for c in testtmp), '$TESTTMP')) |
929 for c in testtmp), '$TESTTMP')) |
930 else: |
930 else: |
931 replacements.append((re.escape(testtmp), '$TESTTMP')) |
931 replacements.append((re.escape(testtmp), '$TESTTMP')) |
932 |
932 |
|
933 env = os.environ.copy() |
|
934 |
933 if options.time: |
935 if options.time: |
934 starttime = time.time() |
936 starttime = time.time() |
935 ret, out = runner(testpath, testtmp, options, replacements) |
937 ret, out = runner(testpath, testtmp, options, replacements, env) |
936 if options.time: |
938 if options.time: |
937 endtime = time.time() |
939 endtime = time.time() |
938 times.append((test, endtime - starttime)) |
940 times.append((test, endtime - starttime)) |
939 vlog("# Ret was:", ret) |
941 vlog("# Ret was:", ret) |
940 |
942 |