~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
661
661
                excName = str(excClass)
662
662
            raise self.failureException, "%s not raised" % excName
663
663
 
664
 
    def assertIs(self, left, right):
 
664
    def assertIs(self, left, right, message=None):
665
665
        if not (left is right):
666
 
            raise AssertionError("%r is not %r." % (left, right))
 
666
            if message is not None:
 
667
                raise AssertionError(message)
 
668
            else:
 
669
                raise AssertionError("%r is not %r." % (left, right))
 
670
 
 
671
    def assertIsNot(self, left, right, message=None):
 
672
        if (left is right):
 
673
            if message is not None:
 
674
                raise AssertionError(message)
 
675
            else:
 
676
                raise AssertionError("%r is %r." % (left, right))
667
677
 
668
678
    def assertTransportMode(self, transport, path, mode):
669
679
        """Fail if a path does not have mode mode.
1262
1272
        self.transport_server = default_transport
1263
1273
        self.transport_readonly_server = None
1264
1274
 
1265
 
    def failUnlessExists(self, path):
1266
 
        """Fail unless path, which may be abs or relative, exists."""
1267
 
        self.failUnless(osutils.lexists(path))
1268
 
 
1269
 
    def failIfExists(self, path):
1270
 
        """Fail if path, which may be abs or relative, exists."""
1271
 
        self.failIf(osutils.lexists(path))
1272
 
        
1273
1275
    def get_transport(self):
1274
1276
        """Return a writeable transport for the test scratch space"""
1275
1277
        t = get_transport(self.get_url())
1531
1533
                elif line_endings == 'native':
1532
1534
                    end = os.linesep
1533
1535
                else:
1534
 
                    raise errors.BzrError('Invalid line ending request %r' % (line_endings,))
 
1536
                    raise errors.BzrError(
 
1537
                        'Invalid line ending request %r' % line_endings)
1535
1538
                content = "contents of %s%s" % (name.encode('utf-8'), end)
1536
 
                # Technically 'put()' is the right command. However, put
1537
 
                # uses an AtomicFile, which requires an extra rename into place
1538
 
                # As long as the files didn't exist in the past, append() will
1539
 
                # do the same thing as put()
1540
 
                # On jam's machine, make_kernel_like_tree is:
1541
 
                #   put:    4.5-7.5s (averaging 6s)
1542
 
                #   append: 2.9-4.5s
1543
 
                #   put_non_atomic: 2.9-4.5s
1544
1539
                transport.put_bytes_non_atomic(urlutils.escape(name), content)
1545
1540
 
1546
1541
    def build_tree_contents(self, shape):
1548
1543
 
1549
1544
    def assertFileEqual(self, content, path):
1550
1545
        """Fail if path does not contain 'content'."""
1551
 
        self.failUnless(osutils.lexists(path))
 
1546
        self.failUnlessExists(path)
1552
1547
        # TODO: jam 20060427 Shouldn't this be 'rb'?
1553
1548
        self.assertEqualDiff(content, open(path, 'r').read())
1554
1549
 
 
1550
    def failUnlessExists(self, path):
 
1551
        """Fail unless path, which may be abs or relative, exists."""
 
1552
        self.failUnless(osutils.lexists(path),path+" does not exist")
 
1553
 
 
1554
    def failIfExists(self, path):
 
1555
        """Fail if path, which may be abs or relative, exists."""
 
1556
        self.failIf(osutils.lexists(path),path+" exists")
 
1557
 
1555
1558
 
1556
1559
class TestCaseWithTransport(TestCaseInTempDir):
1557
1560
    """A test case that provides get_url and get_readonly_url facilities.
1665
1668
    return result
1666
1669
 
1667
1670
 
 
1671
def sort_suite_by_re(suite, pattern):
 
1672
    first = []
 
1673
    second = []
 
1674
    filter_re = re.compile(pattern)
 
1675
    for test in iter_suite_tests(suite):
 
1676
        if filter_re.search(test.id()):
 
1677
            first.append(test)
 
1678
        else:
 
1679
            second.append(test)
 
1680
    return TestUtil.TestSuite(first + second)
 
1681
 
 
1682
 
1668
1683
def run_suite(suite, name='test', verbose=False, pattern=".*",
1669
1684
              stop_on_failure=False, keep_output=False,
1670
 
              transport=None, lsprof_timed=None, bench_history=None):
 
1685
              transport=None, lsprof_timed=None, bench_history=None,
 
1686
              matching_tests_first=None):
1671
1687
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
1672
1688
    if verbose:
1673
1689
        verbosity = 2
1680
1696
                            bench_history=bench_history)
1681
1697
    runner.stop_on_failure=stop_on_failure
1682
1698
    if pattern != '.*':
1683
 
        suite = filter_suite_by_re(suite, pattern)
 
1699
        if matching_tests_first:
 
1700
            suite = sort_suite_by_re(suite, pattern)
 
1701
        else:
 
1702
            suite = filter_suite_by_re(suite, pattern)
1684
1703
    result = runner.run(suite)
1685
1704
    return result.wasSuccessful()
1686
1705
 
1690
1709
             transport=None,
1691
1710
             test_suite_factory=None,
1692
1711
             lsprof_timed=None,
1693
 
             bench_history=None):
 
1712
             bench_history=None,
 
1713
             matching_tests_first=None):
1694
1714
    """Run the whole test suite under the enhanced runner"""
1695
1715
    # XXX: Very ugly way to do this...
1696
1716
    # Disable warning about old formats because we don't want it to disturb
1712
1732
                     stop_on_failure=stop_on_failure, keep_output=keep_output,
1713
1733
                     transport=transport,
1714
1734
                     lsprof_timed=lsprof_timed,
1715
 
                     bench_history=bench_history)
 
1735
                     bench_history=bench_history,
 
1736
                     matching_tests_first=matching_tests_first)
1716
1737
    finally:
1717
1738
        default_transport = old_transport
1718
1739