~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

merge hpss changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
                      bzrlib.store,
105
105
                      ]
106
106
 
107
 
NUMBERED_DIRS = False   # dirs kind for TestCaseInTempDir (numbered or named)
108
 
 
109
107
 
110
108
def packages_to_test():
111
109
    """Return a list of packages to test.
154
152
    def __init__(self, stream, descriptions, verbosity,
155
153
                 bench_history=None,
156
154
                 num_tests=None,
 
155
                 use_numbered_dirs=False,
157
156
                 ):
158
157
        """Construct new TestResult.
159
158
 
184
183
        self.skip_count = 0
185
184
        self.unsupported = {}
186
185
        self.count = 0
 
186
        self.use_numbered_dirs = use_numbered_dirs
187
187
        self._overall_start_time = time.time()
188
188
    
189
189
    def extractBenchmarkTime(self, testCase):
290
290
        for test, err in errors:
291
291
            self.stream.writeln(self.separator1)
292
292
            self.stream.write("%s: " % flavour)
293
 
            if NUMBERED_DIRS:
 
293
            if self.use_numbered_dirs:
294
294
                self.stream.write('#%d ' % test.number)
295
295
            self.stream.writeln(self.getDescription(test))
296
296
            if getattr(test, '_get_log', None) is not None:
320
320
                 bench_history=None,
321
321
                 num_tests=None,
322
322
                 pb=None,
 
323
                 use_numbered_dirs=False,
323
324
                 ):
324
325
        ExtendedTestResult.__init__(self, stream, descriptions, verbosity,
325
 
            bench_history, num_tests)
 
326
            bench_history, num_tests, use_numbered_dirs)
326
327
        if pb is None:
327
328
            self.pb = self.ui.nested_progress_bar()
328
329
            self._supplied_pb = False
364
365
                + self._shortened_test_description(test))
365
366
 
366
367
    def _test_description(self, test):
367
 
        if NUMBERED_DIRS:
 
368
        if self.use_numbered_dirs:
368
369
            return '#%d %s' % (self.count,
369
370
                               self._shortened_test_description(test))
370
371
        else:
433
434
        # width needs space for 6 char status, plus 1 for slash, plus 2 10-char
434
435
        # numbers, plus a trailing blank
435
436
        # when NUMBERED_DIRS: plus 5 chars on test number, plus 1 char on space
436
 
        if NUMBERED_DIRS:
 
437
        if self.use_numbered_dirs:
437
438
            self.stream.write('%5d ' % self.count)
438
439
            self.stream.write(self._ellipsize_to_right(name,
439
440
                                osutils.terminal_width()-36))
444
445
 
445
446
    def _error_summary(self, err):
446
447
        indent = ' ' * 4
447
 
        if NUMBERED_DIRS:
 
448
        if self.use_numbered_dirs:
448
449
            indent += ' ' * 6
449
450
        return '%s%s' % (indent, err[1])
450
451
 
493
494
                 descriptions=0,
494
495
                 verbosity=1,
495
496
                 keep_output=False,
496
 
                 bench_history=None):
 
497
                 bench_history=None,
 
498
                 use_numbered_dirs=False,
 
499
                 ):
497
500
        self.stream = unittest._WritelnDecorator(stream)
498
501
        self.descriptions = descriptions
499
502
        self.verbosity = verbosity
500
503
        self.keep_output = keep_output
501
504
        self._bench_history = bench_history
 
505
        self.use_numbered_dirs = use_numbered_dirs
502
506
 
503
507
    def run(self, test):
504
508
        "Run the given test case or test suite."
512
516
                              self.verbosity,
513
517
                              bench_history=self._bench_history,
514
518
                              num_tests=test.countTestCases(),
 
519
                              use_numbered_dirs=self.use_numbered_dirs,
515
520
                              )
516
521
        result.stop_early = self.stop_on_failure
517
522
        result.report_starting()
570
575
                else:
571
576
                    test_root = test_root.encode(
572
577
                        sys.getfilesystemencoding())
573
 
                try:
574
 
                    osutils.rmtree(test_root)
575
 
                except OSError, e:
576
 
                    if sys.platform == 'win32' and e.errno == errno.EACCES:
577
 
                        print >>sys.stderr, ('Permission denied: '
578
 
                                             'unable to remove testing dir '
579
 
                                             '%s' % os.path.basename(test_root))
580
 
                    else:
581
 
                        raise
 
578
                _rmtree_temp_dir(test_root)
582
579
        else:
583
580
            note("Failed tests working directories are in '%s'\n", test_root)
584
581
        TestCaseWithMemoryTransport.TEST_ROOT = None
755
752
        self._benchcalls = []
756
753
        self._benchtime = None
757
754
        # prevent hooks affecting tests
758
 
        self._preserved_hooks = bzrlib.branch.Branch.hooks
 
755
        self._preserved_hooks = {
 
756
            bzrlib.branch.Branch:bzrlib.branch.Branch.hooks,
 
757
            bzrlib.smart.server.SmartTCPServer:bzrlib.smart.server.SmartTCPServer.hooks,
 
758
            }
759
759
        self.addCleanup(self._restoreHooks)
760
760
        # this list of hooks must be kept in sync with the defaults
761
761
        # in branch.py
920
920
            self.fail("%r is an instance of %s rather than %s" % (
921
921
                obj, obj.__class__, kls))
922
922
 
 
923
    def expectFailure(self, reason, assertion, *args, **kwargs):
 
924
        """Invoke a test, expecting it to fail for the given reason.
 
925
 
 
926
        This is for assertions that ought to succeed, but currently fail.
 
927
        (The failure is *expected* but not *wanted*.)  Please be very precise
 
928
        about the failure you're expecting.  If a new bug is introduced,
 
929
        AssertionError should be raised, not KnownFailure.
 
930
 
 
931
        Frequently, expectFailure should be followed by an opposite assertion.
 
932
        See example below.
 
933
 
 
934
        Intended to be used with a callable that raises AssertionError as the
 
935
        'assertion' parameter.  args and kwargs are passed to the 'assertion'.
 
936
 
 
937
        Raises KnownFailure if the test fails.  Raises AssertionError if the
 
938
        test succeeds.
 
939
 
 
940
        example usage::
 
941
 
 
942
          self.expectFailure('Math is broken', self.assertNotEqual, 54,
 
943
                             dynamic_val)
 
944
          self.assertEqual(42, dynamic_val)
 
945
 
 
946
          This means that a dynamic_val of 54 will cause the test to raise
 
947
          a KnownFailure.  Once math is fixed and the expectFailure is removed,
 
948
          only a dynamic_val of 42 will allow the test to pass.  Anything other
 
949
          than 54 or 42 will cause an AssertionError.
 
950
        """
 
951
        try:
 
952
            assertion(*args, **kwargs)
 
953
        except AssertionError:
 
954
            raise KnownFailure(reason)
 
955
        else:
 
956
            self.fail('Unexpected success.  Should have failed: %s' % reason)
 
957
 
923
958
    def _capture_warnings(self, a_callable, *args, **kwargs):
924
959
        """A helper for callDeprecated and applyDeprecated.
925
960
 
1063
1098
            osutils.set_or_unset_env(name, value)
1064
1099
 
1065
1100
    def _restoreHooks(self):
1066
 
        bzrlib.branch.Branch.hooks = self._preserved_hooks
 
1101
        for klass, hooks in self._preserved_hooks.items():
 
1102
            setattr(klass, 'hooks', hooks)
1067
1103
 
1068
1104
    def knownFailure(self, reason):
1069
1105
        """This test has failed for some known reason."""
1798
1834
    """
1799
1835
 
1800
1836
    OVERRIDE_PYTHON = 'python'
 
1837
    use_numbered_dirs = False
1801
1838
 
1802
1839
    def check_file_contents(self, filename, expect):
1803
1840
        self.log("check contents of file %s" % filename)
1813
1850
        For TestCaseInTempDir we create a temporary directory based on the test
1814
1851
        name and then create two subdirs - test and home under it.
1815
1852
        """
1816
 
        if NUMBERED_DIRS:       # strongly recommended on Windows
1817
 
                                # due the path length limitation (260 chars)
 
1853
        if self.use_numbered_dirs:  # strongly recommended on Windows
 
1854
                                    # due the path length limitation (260 ch.)
1818
1855
            candidate_dir = '%s/%dK/%05d' % (self.TEST_ROOT,
1819
1856
                                             int(self.number/1000),
1820
1857
                                             self.number)
2043
2080
              transport=None, lsprof_timed=None, bench_history=None,
2044
2081
              matching_tests_first=None,
2045
2082
              numbered_dirs=None):
2046
 
    global NUMBERED_DIRS
 
2083
    use_numbered_dirs = bool(numbered_dirs)
 
2084
 
 
2085
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
2047
2086
    if numbered_dirs is not None:
2048
 
        NUMBERED_DIRS = bool(numbered_dirs)
2049
 
 
2050
 
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
 
2087
        TestCaseInTempDir.use_numbered_dirs = use_numbered_dirs
2051
2088
    if verbose:
2052
2089
        verbosity = 2
2053
2090
    else:
2056
2093
                            descriptions=0,
2057
2094
                            verbosity=verbosity,
2058
2095
                            keep_output=keep_output,
2059
 
                            bench_history=bench_history)
 
2096
                            bench_history=bench_history,
 
2097
                            use_numbered_dirs=use_numbered_dirs,
 
2098
                            )
2060
2099
    runner.stop_on_failure=stop_on_failure
2061
2100
    if pattern != '.*':
2062
2101
        if matching_tests_first:
2261
2300
        suite.addTests(adapter.adapt(test))
2262
2301
 
2263
2302
 
 
2303
def _rmtree_temp_dir(dirname):
 
2304
    try:
 
2305
        osutils.rmtree(dirname)
 
2306
    except OSError, e:
 
2307
        if sys.platform == 'win32' and e.errno == errno.EACCES:
 
2308
            print >>sys.stderr, ('Permission denied: '
 
2309
                                 'unable to remove testing dir '
 
2310
                                 '%s' % os.path.basename(test_root))
 
2311
        else:
 
2312
            raise
 
2313
 
 
2314
 
2264
2315
def clean_selftest_output(root=None, quiet=False):
2265
2316
    """Remove all selftest output directories from root directory.
2266
2317
 
2278
2329
        if os.path.isdir(i) and re_dir.match(i):
2279
2330
            if not quiet:
2280
2331
                print 'delete directory:', i
2281
 
            shutil.rmtree(i)
 
2332
            _rmtree_temp_dir(i)
2282
2333
 
2283
2334
 
2284
2335
class Feature(object):