comparison tests/run-tests.py @ 25031:0adc22a0b6b3

python3: update killdaemons and run-tests print and exception syntax test-run-tests.t still passes fine on Python 2.6. run-tests.py --local no longer fails with syntax errors, and now fails looking for xrange. Most changes done with 2to3 -w -f numliterals -f except -f print tests/run-tests.py tests/killdaemons.py after which one import was fixed in run-tests and a __future__ import was added.
author Augie Fackler <augie@google.com>
date Sat, 11 Apr 2015 18:20:44 -0400
parents 5195322b9f80
children 2bdd9e442bcc
comparison
equal deleted inserted replaced
25030:33298a8b472f 25031:0adc22a0b6b3
39 # (You could use any subset of the tests: test-s* happens to match 39 # (You could use any subset of the tests: test-s* happens to match
40 # enough that it's worth doing parallel runs, few enough that it 40 # enough that it's worth doing parallel runs, few enough that it
41 # completes fairly quickly, includes both shell and Python scripts, and 41 # completes fairly quickly, includes both shell and Python scripts, and
42 # includes some scripts that run daemon processes.) 42 # includes some scripts that run daemon processes.)
43 43
44 from __future__ import print_function
45
44 from distutils import version 46 from distutils import version
45 import difflib 47 import difflib
46 import errno 48 import errno
47 import optparse 49 import optparse
48 import os 50 import os
55 import time 57 import time
56 import random 58 import random
57 import re 59 import re
58 import threading 60 import threading
59 import killdaemons as killmod 61 import killdaemons as killmod
60 import Queue as queue 62 try:
63 import Queue as queue
64 except ImportError:
65 import queue
61 from xml.dom import minidom 66 from xml.dom import minidom
62 import unittest 67 import unittest
63 68
64 try: 69 try:
65 import json 70 import json
84 try: 89 try:
85 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 90 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
86 s.bind(('localhost', port)) 91 s.bind(('localhost', port))
87 s.close() 92 s.close()
88 return True 93 return True
89 except socket.error, exc: 94 except socket.error as exc:
90 if not exc.errno == errno.EADDRINUSE: 95 if not exc.errno == errno.EADDRINUSE:
91 raise 96 raise
92 return False 97 return False
93 98
94 closefds = os.name == 'posix' 99 closefds = os.name == 'posix'
133 entries = dict() 138 entries = dict()
134 for filename in files: 139 for filename in files:
135 try: 140 try:
136 path = os.path.expanduser(os.path.expandvars(filename)) 141 path = os.path.expanduser(os.path.expandvars(filename))
137 f = open(path, "rb") 142 f = open(path, "rb")
138 except IOError, err: 143 except IOError as err:
139 if err.errno != errno.ENOENT: 144 if err.errno != errno.ENOENT:
140 raise 145 raise
141 if warn: 146 if warn:
142 print "warning: no such %s file: %s" % (listtype, filename) 147 print("warning: no such %s file: %s" % (listtype, filename))
143 continue 148 continue
144 149
145 for line in f.readlines(): 150 for line in f.readlines():
146 line = line.split('#', 1)[0].strip() 151 line = line.split('#', 1)[0].strip()
147 if line: 152 if line:
356 361
357 Arguments are strings to print. 362 Arguments are strings to print.
358 """ 363 """
359 iolock.acquire() 364 iolock.acquire()
360 if verbose: 365 if verbose:
361 print verbose, 366 print(verbose, end=' ')
362 for m in msg: 367 for m in msg:
363 print m, 368 print(m, end=' ')
364 print 369 print()
365 sys.stdout.flush() 370 sys.stdout.flush()
366 iolock.release() 371 iolock.release()
367 372
368 def terminate(proc): 373 def terminate(proc):
369 """Terminate subprocess (with fallback for Python versions < 2.6)""" 374 """Terminate subprocess (with fallback for Python versions < 2.6)"""
473 self._out = None 478 self._out = None
474 self._skipped = None 479 self._skipped = None
475 480
476 try: 481 try:
477 os.mkdir(self._threadtmp) 482 os.mkdir(self._threadtmp)
478 except OSError, e: 483 except OSError as e:
479 if e.errno != errno.EEXIST: 484 if e.errno != errno.EEXIST:
480 raise 485 raise
481 486
482 self._testtmp = os.path.join(self._threadtmp, 487 self._testtmp = os.path.join(self._threadtmp,
483 os.path.basename(self.path)) 488 os.path.basename(self.path))
485 490
486 # Remove any previous output files. 491 # Remove any previous output files.
487 if os.path.exists(self.errpath): 492 if os.path.exists(self.errpath):
488 try: 493 try:
489 os.remove(self.errpath) 494 os.remove(self.errpath)
490 except OSError, e: 495 except OSError as e:
491 # We might have raced another test to clean up a .err 496 # We might have raced another test to clean up a .err
492 # file, so ignore ENOENT when removing a previous .err 497 # file, so ignore ENOENT when removing a previous .err
493 # file. 498 # file.
494 if e.errno != errno.ENOENT: 499 if e.errno != errno.ENOENT:
495 raise 500 raise
515 try: 520 try:
516 self.runTest() 521 self.runTest()
517 except KeyboardInterrupt: 522 except KeyboardInterrupt:
518 self._aborted = True 523 self._aborted = True
519 raise 524 raise
520 except SkipTest, e: 525 except SkipTest as e:
521 result.addSkip(self, str(e)) 526 result.addSkip(self, str(e))
522 # The base class will have already counted this as a 527 # The base class will have already counted this as a
523 # test we "ran", but we want to exclude skipped tests 528 # test we "ran", but we want to exclude skipped tests
524 # from those we count towards those run. 529 # from those we count towards those run.
525 result.testsRun -= 1 530 result.testsRun -= 1
526 except IgnoreTest, e: 531 except IgnoreTest as e:
527 result.addIgnore(self, str(e)) 532 result.addIgnore(self, str(e))
528 # As with skips, ignores also should be excluded from 533 # As with skips, ignores also should be excluded from
529 # the number of tests executed. 534 # the number of tests executed.
530 result.testsRun -= 1 535 result.testsRun -= 1
531 except WarnTest, e: 536 except WarnTest as e:
532 result.addWarn(self, str(e)) 537 result.addWarn(self, str(e))
533 except self.failureException, e: 538 except self.failureException as e:
534 # This differs from unittest in that we don't capture 539 # This differs from unittest in that we don't capture
535 # the stack trace. This is for historical reasons and 540 # the stack trace. This is for historical reasons and
536 # this decision could be revisited in the future, 541 # this decision could be revisited in the future,
537 # especially for PythonTest instances. 542 # especially for PythonTest instances.
538 if result.addFailure(self, str(e)): 543 if result.addFailure(self, str(e)):
871 stdout, stderr = proc.communicate() 876 stdout, stderr = proc.communicate()
872 ret = proc.wait() 877 ret = proc.wait()
873 if wifexited(ret): 878 if wifexited(ret):
874 ret = os.WEXITSTATUS(ret) 879 ret = os.WEXITSTATUS(ret)
875 if ret == 2: 880 if ret == 2:
876 print stdout 881 print(stdout)
877 sys.exit(1) 882 sys.exit(1)
878 883
879 return ret == 0 884 return ret == 0
880 885
881 def _parsetest(self, lines): 886 def _parsetest(self, lines):
1619 self._portoffset = 0 1624 self._portoffset = 0
1620 self._ports = {} 1625 self._ports = {}
1621 1626
1622 def run(self, args, parser=None): 1627 def run(self, args, parser=None):
1623 """Run the test suite.""" 1628 """Run the test suite."""
1624 oldmask = os.umask(022) 1629 oldmask = os.umask(0o22)
1625 try: 1630 try:
1626 parser = parser or getparser() 1631 parser = parser or getparser()
1627 options, args = parseargs(args, parser) 1632 options, args = parseargs(args, parser)
1628 self.options = options 1633 self.options = options
1629 1634
1641 slow = 'svn gendoc check-code-hg'.split() 1646 slow = 'svn gendoc check-code-hg'.split()
1642 def sortkey(f): 1647 def sortkey(f):
1643 # run largest tests first, as they tend to take the longest 1648 # run largest tests first, as they tend to take the longest
1644 try: 1649 try:
1645 val = -os.stat(f).st_size 1650 val = -os.stat(f).st_size
1646 except OSError, e: 1651 except OSError as e:
1647 if e.errno != errno.ENOENT: 1652 if e.errno != errno.ENOENT:
1648 raise 1653 raise
1649 return -1e9 # file does not exist, tell early 1654 return -1e9 # file does not exist, tell early
1650 for kw in slow: 1655 for kw in slow:
1651 if kw in f: 1656 if kw in f:
1665 tmpdir = self.options.tmpdir 1670 tmpdir = self.options.tmpdir
1666 if os.path.exists(tmpdir): 1671 if os.path.exists(tmpdir):
1667 # Meaning of tmpdir has changed since 1.3: we used to create 1672 # Meaning of tmpdir has changed since 1.3: we used to create
1668 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if 1673 # HGTMP inside tmpdir; now HGTMP is tmpdir. So fail if
1669 # tmpdir already exists. 1674 # tmpdir already exists.
1670 print "error: temp dir %r already exists" % tmpdir 1675 print("error: temp dir %r already exists" % tmpdir)
1671 return 1 1676 return 1
1672 1677
1673 # Automatically removing tmpdir sounds convenient, but could 1678 # Automatically removing tmpdir sounds convenient, but could
1674 # really annoy anyone in the habit of using "--tmpdir=/tmp" 1679 # really annoy anyone in the habit of using "--tmpdir=/tmp"
1675 # or "--tmpdir=$HOME". 1680 # or "--tmpdir=$HOME".
1781 while tests: 1786 while tests:
1782 if os.path.exists(tests[0] + ".err"): 1787 if os.path.exists(tests[0] + ".err"):
1783 break 1788 break
1784 tests.pop(0) 1789 tests.pop(0)
1785 if not tests: 1790 if not tests:
1786 print "running all tests" 1791 print("running all tests")
1787 tests = orig 1792 tests = orig
1788 1793
1789 tests = [self._gettest(t, i) for i, t in enumerate(tests)] 1794 tests = [self._gettest(t, i) for i, t in enumerate(tests)]
1790 1795
1791 failed = False 1796 failed = False
1813 1818
1814 if self.options.anycoverage: 1819 if self.options.anycoverage:
1815 self._outputcoverage() 1820 self._outputcoverage()
1816 except KeyboardInterrupt: 1821 except KeyboardInterrupt:
1817 failed = True 1822 failed = True
1818 print "\ninterrupted!" 1823 print("\ninterrupted!")
1819 1824
1820 if failed: 1825 if failed:
1821 return 1 1826 return 1
1822 if warned: 1827 if warned:
1823 return 80 1828 return 80
1892 mypython = os.path.join(self._tmpbindir, pyexename) 1897 mypython = os.path.join(self._tmpbindir, pyexename)
1893 try: 1898 try:
1894 if os.readlink(mypython) == sys.executable: 1899 if os.readlink(mypython) == sys.executable:
1895 return 1900 return
1896 os.unlink(mypython) 1901 os.unlink(mypython)
1897 except OSError, err: 1902 except OSError as err:
1898 if err.errno != errno.ENOENT: 1903 if err.errno != errno.ENOENT:
1899 raise 1904 raise
1900 if self._findprogram(pyexename) != sys.executable: 1905 if self._findprogram(pyexename) != sys.executable:
1901 try: 1906 try:
1902 os.symlink(sys.executable, mypython) 1907 os.symlink(sys.executable, mypython)
1903 self._createdfiles.append(mypython) 1908 self._createdfiles.append(mypython)
1904 except OSError, err: 1909 except OSError as err:
1905 # child processes may race, which is harmless 1910 # child processes may race, which is harmless
1906 if err.errno != errno.EEXIST: 1911 if err.errno != errno.EEXIST:
1907 raise 1912 raise
1908 else: 1913 else:
1909 exedir, exename = os.path.split(sys.executable) 1914 exedir, exename = os.path.split(sys.executable)
1912 path = os.environ['PATH'].split(os.pathsep) 1917 path = os.environ['PATH'].split(os.pathsep)
1913 while exedir in path: 1918 while exedir in path:
1914 path.remove(exedir) 1919 path.remove(exedir)
1915 os.environ['PATH'] = os.pathsep.join([exedir] + path) 1920 os.environ['PATH'] = os.pathsep.join([exedir] + path)
1916 if not self._findprogram(pyexename): 1921 if not self._findprogram(pyexename):
1917 print "WARNING: Cannot find %s in search path" % pyexename 1922 print("WARNING: Cannot find %s in search path" % pyexename)
1918 1923
1919 def _installhg(self): 1924 def _installhg(self):
1920 """Install hg into the test environment. 1925 """Install hg into the test environment.
1921 1926
1922 This will also configure hg with the appropriate testing settings. 1927 This will also configure hg with the appropriate testing settings.
1960 1965
1961 # setuptools requires install directories to exist. 1966 # setuptools requires install directories to exist.
1962 def makedirs(p): 1967 def makedirs(p):
1963 try: 1968 try:
1964 os.makedirs(p) 1969 os.makedirs(p)
1965 except OSError, e: 1970 except OSError as e:
1966 if e.errno != errno.EEXIST: 1971 if e.errno != errno.EEXIST:
1967 raise 1972 raise
1968 makedirs(self._pythondir) 1973 makedirs(self._pythondir)
1969 makedirs(self._bindir) 1974 makedirs(self._bindir)
1970 1975
2005 '"%~dp0python" "%~dp0hg" %*') 2010 '"%~dp0python" "%~dp0hg" %*')
2006 f = open(hgbat, 'wb') 2011 f = open(hgbat, 'wb')
2007 f.write(data) 2012 f.write(data)
2008 f.close() 2013 f.close()
2009 else: 2014 else:
2010 print 'WARNING: cannot fix hg.bat reference to python.exe' 2015 print('WARNING: cannot fix hg.bat reference to python.exe')
2011 2016
2012 if self.options.anycoverage: 2017 if self.options.anycoverage:
2013 custom = os.path.join(self._testdir, 'sitecustomize.py') 2018 custom = os.path.join(self._testdir, 'sitecustomize.py')
2014 target = os.path.join(self._pythondir, 'sitecustomize.py') 2019 target = os.path.join(self._pythondir, 'sitecustomize.py')
2015 vlog('# Installing coverage trigger to %s' % target) 2020 vlog('# Installing coverage trigger to %s' % target)
2018 vlog('# Installing coverage rc to %s' % rc) 2023 vlog('# Installing coverage rc to %s' % rc)
2019 os.environ['COVERAGE_PROCESS_START'] = rc 2024 os.environ['COVERAGE_PROCESS_START'] = rc
2020 covdir = os.path.join(self._installdir, '..', 'coverage') 2025 covdir = os.path.join(self._installdir, '..', 'coverage')
2021 try: 2026 try:
2022 os.mkdir(covdir) 2027 os.mkdir(covdir)
2023 except OSError, e: 2028 except OSError as e:
2024 if e.errno != errno.EEXIST: 2029 if e.errno != errno.EEXIST:
2025 raise 2030 raise
2026 2031
2027 os.environ['COVERAGE_DIR'] = covdir 2032 os.environ['COVERAGE_DIR'] = covdir
2028 2033
2098 p += '.exe' 2103 p += '.exe'
2099 found = self._findprogram(p) 2104 found = self._findprogram(p)
2100 if found: 2105 if found:
2101 vlog("# Found prerequisite", p, "at", found) 2106 vlog("# Found prerequisite", p, "at", found)
2102 else: 2107 else:
2103 print "WARNING: Did not find prerequisite tool: %s " % p 2108 print("WARNING: Did not find prerequisite tool: %s " % p)
2104 2109
2105 if __name__ == '__main__': 2110 if __name__ == '__main__':
2106 runner = TestRunner() 2111 runner = TestRunner()
2107 2112
2108 try: 2113 try: