/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2010-09-10 09:46:15 UTC
  • mfrom: (5417 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5418.
  • Revision ID: jelmer@samba.org-20100910094615-7soefxkmqroylqhj
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
114
114
    TestUtil,
115
115
    treeshape,
116
116
    )
117
 
from bzrlib.tests.http_server import HttpServer
118
 
from bzrlib.tests.TestUtil import (
119
 
                          TestSuite,
120
 
                          TestLoader,
121
 
                          )
122
117
from bzrlib.ui import NullProgressView
123
118
from bzrlib.ui.text import TextUIFactory
124
119
import bzrlib.version_info_formats.format_custom
140
135
SUBUNIT_SEEK_SET = 0
141
136
SUBUNIT_SEEK_CUR = 1
142
137
 
 
138
# These are intentionally brought into this namespace. That way plugins, etc
 
139
# can just "from bzrlib.tests import TestCase, TestLoader, etc"
 
140
TestSuite = TestUtil.TestSuite
 
141
TestLoader = TestUtil.TestLoader
143
142
 
144
143
class ExtendedTestResult(testtools.TextTestResult):
145
144
    """Accepts, reports and accumulates the results of running tests.
799
798
    _active_threads = None
800
799
    _leaking_threads_tests = 0
801
800
    _first_thread_leaker_id = None
802
 
    _log_file_name = None
 
801
    _log_file = None
803
802
    # record lsprof data when performing benchmark calls.
804
803
    _gather_lsprof_in_benchmarks = False
805
804
 
848
847
        # going away but leak one) but it seems less likely than the actual
849
848
        # false positives (the test see threads going away and does not leak).
850
849
        if leaked_threads > 0:
 
850
            if 'threads' in selftest_debug_flags:
 
851
                print '%s is leaking, active is now %d' % (self.id(), active)
851
852
            TestCase._leaking_threads_tests += 1
852
853
            if TestCase._first_thread_leaker_id is None:
853
854
                TestCase._first_thread_leaker_id = self.id()
1457
1458
 
1458
1459
        The file is removed as the test is torn down.
1459
1460
        """
1460
 
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
1461
 
        self._log_file = os.fdopen(fileno, 'w+')
 
1461
        self._log_file = StringIO()
1462
1462
        self._log_memento = bzrlib.trace.push_log_file(self._log_file)
1463
 
        self._log_file_name = name
1464
1463
        self.addCleanup(self._finishLogFile)
1465
1464
 
1466
1465
    def _finishLogFile(self):
1668
1667
                unicodestr = self._log_contents.decode('utf8', 'replace')
1669
1668
                self._log_contents = unicodestr.encode('utf8')
1670
1669
            return self._log_contents
1671
 
        import bzrlib.trace
1672
 
        if bzrlib.trace._trace_file:
1673
 
            # flush the log file, to get all content
1674
 
            bzrlib.trace._trace_file.flush()
1675
 
        if self._log_file_name is not None:
1676
 
            logfile = open(self._log_file_name)
1677
 
            try:
1678
 
                log_contents = logfile.read()
1679
 
            finally:
1680
 
                logfile.close()
 
1670
        if self._log_file is not None:
 
1671
            log_contents = self._log_file.getvalue()
1681
1672
            try:
1682
1673
                log_contents.decode('utf8')
1683
1674
            except UnicodeDecodeError:
1684
1675
                unicodestr = log_contents.decode('utf8', 'replace')
1685
1676
                log_contents = unicodestr.encode('utf8')
1686
1677
            if not keep_log_file:
1687
 
                close_attempts = 0
1688
 
                max_close_attempts = 100
1689
 
                first_close_error = None
1690
 
                while close_attempts < max_close_attempts:
1691
 
                    close_attempts += 1
1692
 
                    try:
1693
 
                        self._log_file.close()
1694
 
                    except IOError, ioe:
1695
 
                        if ioe.errno is None:
1696
 
                            # No errno implies 'close() called during
1697
 
                            # concurrent operation on the same file object', so
1698
 
                            # retry.  Probably a thread is trying to write to
1699
 
                            # the log file.
1700
 
                            if first_close_error is None:
1701
 
                                first_close_error = ioe
1702
 
                            continue
1703
 
                        raise
1704
 
                    else:
1705
 
                        break
1706
 
                if close_attempts > 1:
1707
 
                    sys.stderr.write(
1708
 
                        'Unable to close log file on first attempt, '
1709
 
                        'will retry: %s\n' % (first_close_error,))
1710
 
                    if close_attempts == max_close_attempts:
1711
 
                        sys.stderr.write(
1712
 
                            'Unable to close log file after %d attempts.\n'
1713
 
                            % (max_close_attempts,))
1714
1678
                self._log_file = None
1715
1679
                # Permit multiple calls to get_log until we clean it up in
1716
1680
                # finishLogFile
1717
1681
                self._log_contents = log_contents
1718
 
                try:
1719
 
                    os.remove(self._log_file_name)
1720
 
                except OSError, e:
1721
 
                    if sys.platform == 'win32' and e.errno == errno.EACCES:
1722
 
                        sys.stderr.write(('Unable to delete log file '
1723
 
                                             ' %r\n' % self._log_file_name))
1724
 
                    else:
1725
 
                        raise
1726
 
                self._log_file_name = None
1727
1682
            return log_contents
1728
1683
        else:
1729
 
            return "No log file content and no log file name."
 
1684
            return "No log file content."
1730
1685
 
1731
1686
    def get_log(self):
1732
1687
        """Get a unicode string containing the log from bzrlib.trace.
1947
1902
            variables. A value of None will unset the env variable.
1948
1903
            The values must be strings. The change will only occur in the
1949
1904
            child, so you don't need to fix the environment after running.
1950
 
        :param skip_if_plan_to_signal: raise TestSkipped when true and os.kill
1951
 
            is not available.
 
1905
        :param skip_if_plan_to_signal: raise TestSkipped when true and system
 
1906
            doesn't support signalling subprocesses.
1952
1907
        :param allow_plugins: If False (default) pass --no-plugins to bzr.
1953
1908
 
1954
1909
        :returns: Popen object for the started process.
1955
1910
        """
1956
1911
        if skip_if_plan_to_signal:
1957
 
            if not getattr(os, 'kill', None):
1958
 
                raise TestSkipped("os.kill not available.")
 
1912
            if os.name != "posix":
 
1913
                raise TestSkipped("Sending signals not supported")
1959
1914
 
1960
1915
        if env_changes is None:
1961
1916
            env_changes = {}
2439
2394
 
2440
2395
    def setUp(self):
2441
2396
        super(TestCaseWithMemoryTransport, self).setUp()
 
2397
        # Ensure that ConnectedTransport doesn't leak sockets
 
2398
        def get_transport_with_cleanup(*args, **kwargs):
 
2399
            t = orig_get_transport(*args, **kwargs)
 
2400
            if isinstance(t, _mod_transport.ConnectedTransport):
 
2401
                self.addCleanup(t.disconnect)
 
2402
            return t
 
2403
 
 
2404
        orig_get_transport = self.overrideAttr(_mod_transport, 'get_transport',
 
2405
                                               get_transport_with_cleanup)
2442
2406
        self._make_test_root()
2443
2407
        self.addCleanup(os.chdir, os.getcwdu())
2444
2408
        self.makeAndChdirToTestDir()
2736
2700
    """
2737
2701
 
2738
2702
    def setUp(self):
 
2703
        from bzrlib.tests import http_server
2739
2704
        super(ChrootedTestCase, self).setUp()
2740
2705
        if not self.vfs_transport_factory == memory.MemoryServer:
2741
 
            self.transport_readonly_server = HttpServer
 
2706
            self.transport_readonly_server = http_server.HttpServer
2742
2707
 
2743
2708
 
2744
2709
def condition_id_re(pattern):
3065
3030
    return suite
3066
3031
 
3067
3032
 
3068
 
class TestDecorator(TestSuite):
 
3033
class TestDecorator(TestUtil.TestSuite):
3069
3034
    """A decorator for TestCase/TestSuite objects.
3070
3035
    
3071
3036
    Usually, subclasses should override __iter__(used when flattening test
3074
3039
    """
3075
3040
 
3076
3041
    def __init__(self, suite):
3077
 
        TestSuite.__init__(self)
 
3042
        TestUtil.TestSuite.__init__(self)
3078
3043
        self.addTest(suite)
3079
3044
 
3080
3045
    def countTestCases(self):
3248
3213
 
3249
3214
    test_blocks = partition_tests(suite, concurrency)
3250
3215
    for process_tests in test_blocks:
3251
 
        process_suite = TestSuite()
 
3216
        process_suite = TestUtil.TestSuite()
3252
3217
        process_suite.addTests(process_tests)
3253
3218
        c2pread, c2pwrite = os.pipe()
3254
3219
        pid = os.fork()
3409
3374
#                           rather than failing tests. And no longer raise
3410
3375
#                           LockContention when fctnl locks are not being used
3411
3376
#                           with proper exclusion rules.
 
3377
#   -Ethreads               Will display thread ident at creation/join time to
 
3378
#                           help track thread leaks
3412
3379
selftest_debug_flags = set()
3413
3380
 
3414
3381
 
3809
3776
        'bzrlib.tests.test_switch',
3810
3777
        'bzrlib.tests.test_symbol_versioning',
3811
3778
        'bzrlib.tests.test_tag',
 
3779
        'bzrlib.tests.test_test_server',
3812
3780
        'bzrlib.tests.test_testament',
3813
3781
        'bzrlib.tests.test_textfile',
3814
3782
        'bzrlib.tests.test_textmerge',
4005
3973
    ...     bzrlib.tests.test_sampler.DemoTest('test_nothing'),
4006
3974
    ...     [('one', dict(param=1)),
4007
3975
    ...      ('two', dict(param=2))],
4008
 
    ...     TestSuite())
 
3976
    ...     TestUtil.TestSuite())
4009
3977
    >>> tests = list(iter_suite_tests(r))
4010
3978
    >>> len(tests)
4011
3979
    2