comparison tests/run-tests.py @ 21536:92a6b16c9186

run-tests: add docstrings
author Gregory Szorc <gregory.szorc@gmail.com>
date Fri, 09 May 2014 16:13:14 -0700
parents ab7e224bc089
children 05925bb5f95a
comparison
equal deleted inserted replaced
21535:ab7e224bc089 21536:92a6b16c9186
431 # Remove any previous output files. 431 # Remove any previous output files.
432 if os.path.exists(self.errpath): 432 if os.path.exists(self.errpath):
433 os.remove(self.errpath) 433 os.remove(self.errpath)
434 434
435 def run(self, result): 435 def run(self, result):
436 """Run this test and report results against a TestResult instance."""
437 # This function is extremely similar to unittest.TestCase.run(). Once
438 # we require Python 2.7 (or at least its version of unittest), this
439 # function can largely go away.
436 self._result = result 440 self._result = result
437 result.startTest(self) 441 result.startTest(self)
438 try: 442 try:
439 try: 443 try:
440 self.setUp() 444 self.setUp()
571 def abort(self): 575 def abort(self):
572 """Terminate execution of this test.""" 576 """Terminate execution of this test."""
573 self._aborted = True 577 self._aborted = True
574 578
575 def _getreplacements(self): 579 def _getreplacements(self):
580 """Obtain a mapping of text replacements to apply to test output.
581
582 Test output needs to be normalized so it can be compared to expected
583 output. This function defines how some of that normalization will
584 occur.
585 """
576 r = [ 586 r = [
577 (r':%s\b' % self._startport, ':$HGPORT'), 587 (r':%s\b' % self._startport, ':$HGPORT'),
578 (r':%s\b' % (self._startport + 1), ':$HGPORT1'), 588 (r':%s\b' % (self._startport + 1), ':$HGPORT1'),
579 (r':%s\b' % (self._startport + 2), ':$HGPORT2'), 589 (r':%s\b' % (self._startport + 2), ':$HGPORT2'),
580 ] 590 ]
588 r.append((re.escape(self._testtmp), '$TESTTMP')) 598 r.append((re.escape(self._testtmp), '$TESTTMP'))
589 599
590 return r 600 return r
591 601
592 def _getenv(self): 602 def _getenv(self):
603 """Obtain environment variables to use during test execution."""
593 env = os.environ.copy() 604 env = os.environ.copy()
594 env['TESTTMP'] = self._testtmp 605 env['TESTTMP'] = self._testtmp
595 env['HOME'] = self._testtmp 606 env['HOME'] = self._testtmp
596 env["HGPORT"] = str(self._startport) 607 env["HGPORT"] = str(self._startport)
597 env["HGPORT1"] = str(self._startport + 1) 608 env["HGPORT1"] = str(self._startport + 1)
623 del env[k] 634 del env[k]
624 635
625 return env 636 return env
626 637
627 def _createhgrc(self, path): 638 def _createhgrc(self, path):
628 # create a fresh hgrc 639 """Create an hgrc file for this test."""
629 hgrc = open(path, 'w') 640 hgrc = open(path, 'w')
630 hgrc.write('[ui]\n') 641 hgrc.write('[ui]\n')
631 hgrc.write('slash = True\n') 642 hgrc.write('slash = True\n')
632 hgrc.write('interactive = False\n') 643 hgrc.write('interactive = False\n')
633 hgrc.write('[defaults]\n') 644 hgrc.write('[defaults]\n')
1319 """Holds context for executing tests. 1330 """Holds context for executing tests.
1320 1331
1321 Tests rely on a lot of state. This object holds it for them. 1332 Tests rely on a lot of state. This object holds it for them.
1322 """ 1333 """
1323 1334
1335 # Programs required to run tests.
1324 REQUIREDTOOLS = [ 1336 REQUIREDTOOLS = [
1325 os.path.basename(sys.executable), 1337 os.path.basename(sys.executable),
1326 'diff', 1338 'diff',
1327 'grep', 1339 'grep',
1328 'unzip', 1340 'unzip',
1329 'gunzip', 1341 'gunzip',
1330 'bunzip2', 1342 'bunzip2',
1331 'sed', 1343 'sed',
1332 ] 1344 ]
1333 1345
1346 # Maps file extensions to test class.
1334 TESTTYPES = [ 1347 TESTTYPES = [
1335 ('.py', PythonTest), 1348 ('.py', PythonTest),
1336 ('.t', TTest), 1349 ('.t', TTest),
1337 ] 1350 ]
1338 1351
1576 os.remove(f) 1589 os.remove(f)
1577 except OSError: 1590 except OSError:
1578 pass 1591 pass
1579 1592
1580 def _usecorrectpython(self): 1593 def _usecorrectpython(self):
1581 # Some tests run the Python interpreter. They must use the 1594 """Configure the environment to use the appropriate Python in tests."""
1582 # same interpreter or bad things will happen. 1595 # Tests must use the same interpreter as us or bad things will happen.
1583 pyexename = sys.platform == 'win32' and 'python.exe' or 'python' 1596 pyexename = sys.platform == 'win32' and 'python.exe' or 'python'
1584 if getattr(os, 'symlink', None): 1597 if getattr(os, 'symlink', None):
1585 vlog("# Making python executable in test path a symlink to '%s'" % 1598 vlog("# Making python executable in test path a symlink to '%s'" %
1586 sys.executable) 1599 sys.executable)
1587 mypython = os.path.join(self._tmpbindir, pyexename) 1600 mypython = os.path.join(self._tmpbindir, pyexename)
1610 os.environ['PATH'] = os.pathsep.join([exedir] + path) 1623 os.environ['PATH'] = os.pathsep.join([exedir] + path)
1611 if not self._findprogram(pyexename): 1624 if not self._findprogram(pyexename):
1612 print "WARNING: Cannot find %s in search path" % pyexename 1625 print "WARNING: Cannot find %s in search path" % pyexename
1613 1626
1614 def _installhg(self): 1627 def _installhg(self):
1628 """Install hg into the test environment.
1629
1630 This will also configure hg with the appropriate testing settings.
1631 """
1615 vlog("# Performing temporary installation of HG") 1632 vlog("# Performing temporary installation of HG")
1616 installerrs = os.path.join("tests", "install.err") 1633 installerrs = os.path.join("tests", "install.err")
1617 compiler = '' 1634 compiler = ''
1618 if self.options.compiler: 1635 if self.options.compiler:
1619 compiler = '--compiler ' + self.options.compiler 1636 compiler = '--compiler ' + self.options.compiler
1719 pipe.close() 1736 pipe.close()
1720 1737
1721 return self._hgpath 1738 return self._hgpath
1722 1739
1723 def _outputcoverage(self): 1740 def _outputcoverage(self):
1741 """Produce code coverage output."""
1724 vlog('# Producing coverage report') 1742 vlog('# Producing coverage report')
1725 os.chdir(self._pythondir) 1743 os.chdir(self._pythondir)
1726 1744
1727 def covrun(*args): 1745 def covrun(*args):
1728 cmd = 'coverage %s' % ' '.join(args) 1746 cmd = 'coverage %s' % ' '.join(args)
1750 if os.name == 'nt' or os.access(name, os.X_OK): 1768 if os.name == 'nt' or os.access(name, os.X_OK):
1751 return name 1769 return name
1752 return None 1770 return None
1753 1771
1754 def _checktools(self): 1772 def _checktools(self):
1755 # Before we go any further, check for pre-requisite tools 1773 """Ensure tools required to run tests are present."""
1756 # stuff from coreutils (cat, rm, etc) are not tested
1757 for p in self.REQUIREDTOOLS: 1774 for p in self.REQUIREDTOOLS:
1758 if os.name == 'nt' and not p.endswith('.exe'): 1775 if os.name == 'nt' and not p.endswith('.exe'):
1759 p += '.exe' 1776 p += '.exe'
1760 found = self._findprogram(p) 1777 found = self._findprogram(p)
1761 if found: 1778 if found: