/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: Martin Pool
  • Date: 2007-04-24 05:02:04 UTC
  • mfrom: (2449 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2450.
  • Revision ID: mbp@sourcefrog.net-20070424050204-bfkc1qiq0axt5f14
Merge trunk & fix NEWS conflict

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import logging
35
35
import os
36
36
from pprint import pformat
 
37
import random
37
38
import re
38
39
import shlex
39
40
import stat
89
90
from bzrlib.tests.treeshape import build_tree_contents
90
91
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
91
92
 
 
93
# Mark this python module as being part of the implementation
 
94
# of unittest: this gives us better tracebacks where the last
 
95
# shown frame is the test code, not our assertXYZ.
 
96
__unittest = 1
 
97
 
92
98
default_transport = LocalURLServer
93
99
 
94
100
MODULES_TO_TEST = []
496
502
                 keep_output=False,
497
503
                 bench_history=None,
498
504
                 use_numbered_dirs=False,
 
505
                 list_only=False
499
506
                 ):
500
507
        self.stream = unittest._WritelnDecorator(stream)
501
508
        self.descriptions = descriptions
503
510
        self.keep_output = keep_output
504
511
        self._bench_history = bench_history
505
512
        self.use_numbered_dirs = use_numbered_dirs
 
513
        self.list_only = list_only
506
514
 
507
515
    def run(self, test):
508
516
        "Run the given test case or test suite."
520
528
                              )
521
529
        result.stop_early = self.stop_on_failure
522
530
        result.report_starting()
523
 
        test.run(result)
 
531
        if self.list_only:
 
532
            if self.verbosity >= 2:
 
533
                self.stream.writeln("Listing tests only ...\n")
 
534
            run = 0
 
535
            for t in iter_suite_tests(test):
 
536
                self.stream.writeln("%s" % (t.id()))
 
537
                run += 1
 
538
            actionTaken = "Listed"
 
539
        else: 
 
540
            test.run(result)
 
541
            run = result.testsRun
 
542
            actionTaken = "Ran"
524
543
        stopTime = time.time()
525
544
        timeTaken = stopTime - startTime
526
545
        result.printErrors()
527
546
        self.stream.writeln(result.separator2)
528
 
        run = result.testsRun
529
 
        self.stream.writeln("Ran %d test%s in %.3fs" %
530
 
                            (run, run != 1 and "s" or "", timeTaken))
 
547
        self.stream.writeln("%s %d test%s in %.3fs" % (actionTaken,
 
548
                            run, run != 1 and "s" or "", timeTaken))
531
549
        self.stream.writeln()
532
550
        if not result.wasSuccessful():
533
551
            self.stream.write("FAILED (")
765
783
        # this list of hooks must be kept in sync with the defaults
766
784
        # in branch.py
767
785
        bzrlib.branch.Branch.hooks = bzrlib.branch.BranchHooks()
 
786
        bzrlib.smart.server.SmartTCPServer.hooks = \
 
787
            bzrlib.smart.server.SmartServerHooks()
768
788
 
769
789
    def _silenceUI(self):
770
790
        """Turn off UI for duration of test"""
2046
2066
            self.transport_readonly_server = HttpServer
2047
2067
 
2048
2068
 
2049
 
def filter_suite_by_re(suite, pattern):
2050
 
    result = TestUtil.TestSuite()
2051
 
    filter_re = re.compile(pattern)
2052
 
    for test in iter_suite_tests(suite):
2053
 
        if filter_re.search(test.id()):
2054
 
            result.addTest(test)
2055
 
    return result
2056
 
 
2057
 
 
2058
 
def sort_suite_by_re(suite, pattern):
 
2069
def filter_suite_by_re(suite, pattern, exclude_pattern=None,
 
2070
                       random_order=False):
 
2071
    """Create a test suite by filtering another one.
 
2072
    
 
2073
    :param suite:           the source suite
 
2074
    :param pattern:         pattern that names must match
 
2075
    :param exclude_pattern: pattern that names must not match, if any
 
2076
    :param random_order:    if True, tests in the new suite will be put in
 
2077
                            random order
 
2078
    :returns: the newly created suite
 
2079
    """ 
 
2080
    return sort_suite_by_re(suite, pattern, exclude_pattern,
 
2081
        random_order, False)
 
2082
 
 
2083
 
 
2084
def sort_suite_by_re(suite, pattern, exclude_pattern=None,
 
2085
                     random_order=False, append_rest=True):
 
2086
    """Create a test suite by sorting another one.
 
2087
    
 
2088
    :param suite:           the source suite
 
2089
    :param pattern:         pattern that names must match in order to go
 
2090
                            first in the new suite
 
2091
    :param exclude_pattern: pattern that names must not match, if any
 
2092
    :param random_order:    if True, tests in the new suite will be put in
 
2093
                            random order
 
2094
    :param append_rest:     if False, pattern is a strict filter and not
 
2095
                            just an ordering directive
 
2096
    :returns: the newly created suite
 
2097
    """ 
2059
2098
    first = []
2060
2099
    second = []
2061
2100
    filter_re = re.compile(pattern)
 
2101
    if exclude_pattern is not None:
 
2102
        exclude_re = re.compile(exclude_pattern)
2062
2103
    for test in iter_suite_tests(suite):
2063
 
        if filter_re.search(test.id()):
2064
 
            first.append(test)
2065
 
        else:
2066
 
            second.append(test)
 
2104
        test_id = test.id()
 
2105
        if exclude_pattern is None or not exclude_re.search(test_id):
 
2106
            if filter_re.search(test_id):
 
2107
                first.append(test)
 
2108
            elif append_rest:
 
2109
                second.append(test)
 
2110
    if random_order:
 
2111
        random.shuffle(first)
 
2112
        random.shuffle(second)
2067
2113
    return TestUtil.TestSuite(first + second)
2068
2114
 
2069
2115
 
2071
2117
              stop_on_failure=False, keep_output=False,
2072
2118
              transport=None, lsprof_timed=None, bench_history=None,
2073
2119
              matching_tests_first=None,
2074
 
              numbered_dirs=None):
 
2120
              numbered_dirs=None,
 
2121
              list_only=False,
 
2122
              random_seed=None,
 
2123
              exclude_pattern=None,
 
2124
              ):
2075
2125
    use_numbered_dirs = bool(numbered_dirs)
2076
2126
 
2077
2127
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
2087
2137
                            keep_output=keep_output,
2088
2138
                            bench_history=bench_history,
2089
2139
                            use_numbered_dirs=use_numbered_dirs,
 
2140
                            list_only=list_only,
2090
2141
                            )
2091
2142
    runner.stop_on_failure=stop_on_failure
2092
 
    if pattern != '.*':
 
2143
    # Initialise the random number generator and display the seed used.
 
2144
    # We convert the seed to a long to make it reuseable across invocations.
 
2145
    random_order = False
 
2146
    if random_seed is not None:
 
2147
        random_order = True
 
2148
        if random_seed == "now":
 
2149
            random_seed = long(time.time())
 
2150
        else:
 
2151
            # Convert the seed to a long if we can
 
2152
            try:
 
2153
                random_seed = long(random_seed)
 
2154
            except:
 
2155
                pass
 
2156
        runner.stream.writeln("Randomizing test order using seed %s\n" %
 
2157
            (random_seed))
 
2158
        random.seed(random_seed)
 
2159
    # Customise the list of tests if requested
 
2160
    if pattern != '.*' or exclude_pattern is not None or random_order:
2093
2161
        if matching_tests_first:
2094
 
            suite = sort_suite_by_re(suite, pattern)
 
2162
            suite = sort_suite_by_re(suite, pattern, exclude_pattern,
 
2163
                random_order)
2095
2164
        else:
2096
 
            suite = filter_suite_by_re(suite, pattern)
 
2165
            suite = filter_suite_by_re(suite, pattern, exclude_pattern,
 
2166
                random_order)
2097
2167
    result = runner.run(suite)
2098
2168
    return result.wasSuccessful()
2099
2169
 
2105
2175
             lsprof_timed=None,
2106
2176
             bench_history=None,
2107
2177
             matching_tests_first=None,
2108
 
             numbered_dirs=None):
 
2178
             numbered_dirs=None,
 
2179
             list_only=False,
 
2180
             random_seed=None,
 
2181
             exclude_pattern=None):
2109
2182
    """Run the whole test suite under the enhanced runner"""
2110
2183
    # XXX: Very ugly way to do this...
2111
2184
    # Disable warning about old formats because we don't want it to disturb
2129
2202
                     lsprof_timed=lsprof_timed,
2130
2203
                     bench_history=bench_history,
2131
2204
                     matching_tests_first=matching_tests_first,
2132
 
                     numbered_dirs=numbered_dirs)
 
2205
                     numbered_dirs=numbered_dirs,
 
2206
                     list_only=list_only,
 
2207
                     random_seed=random_seed,
 
2208
                     exclude_pattern=exclude_pattern)
2133
2209
    finally:
2134
2210
        default_transport = old_transport
2135
2211
 
2147
2223
                   'bzrlib.tests.test_atomicfile',
2148
2224
                   'bzrlib.tests.test_bad_files',
2149
2225
                   'bzrlib.tests.test_branch',
 
2226
                   'bzrlib.tests.test_bugtracker',
2150
2227
                   'bzrlib.tests.test_bundle',
2151
2228
                   'bzrlib.tests.test_bzrdir',
2152
2229
                   'bzrlib.tests.test_cache_utf8',
2171
2248
                   'bzrlib.tests.test_gpg',
2172
2249
                   'bzrlib.tests.test_graph',
2173
2250
                   'bzrlib.tests.test_hashcache',
 
2251
                   'bzrlib.tests.test_help',
2174
2252
                   'bzrlib.tests.test_http',
2175
2253
                   'bzrlib.tests.test_http_response',
2176
2254
                   'bzrlib.tests.test_https_ca_bundle',
2201
2279
                   'bzrlib.tests.test_progress',
2202
2280
                   'bzrlib.tests.test_reconcile',
2203
2281
                   'bzrlib.tests.test_registry',
 
2282
                   'bzrlib.tests.test_remote',
2204
2283
                   'bzrlib.tests.test_repository',
2205
2284
                   'bzrlib.tests.test_revert',
2206
2285
                   'bzrlib.tests.test_revision',
2211
2290
                   'bzrlib.tests.test_selftest',
2212
2291
                   'bzrlib.tests.test_setup',
2213
2292
                   'bzrlib.tests.test_sftp_transport',
 
2293
                   'bzrlib.tests.test_smart',
2214
2294
                   'bzrlib.tests.test_smart_add',
2215
2295
                   'bzrlib.tests.test_smart_transport',
2216
2296
                   'bzrlib.tests.test_source',
2311
2391
    :param  quiet:  suppress report about deleting directories
2312
2392
    """
2313
2393
    import re
2314
 
    import shutil
2315
 
 
2316
2394
    re_dir = re.compile(r'''test\d\d\d\d\.tmp''')
2317
2395
    if root is None:
2318
2396
        root = u'.'