~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2009-02-23 20:55:58 UTC
  • mfrom: (4034 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4053.
  • Revision ID: jelmer@samba.org-20090223205558-1cx2k4w1zgs8r5qa
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
from bzrlib.merge import merge_inner
77
77
import bzrlib.merge3
78
78
import bzrlib.plugin
 
79
from bzrlib.smart import client, server
79
80
import bzrlib.store
80
81
from bzrlib import symbol_versioning
81
82
from bzrlib.symbol_versioning import (
126
127
    """
127
128
 
128
129
    stop_early = False
129
 
    
 
130
 
130
131
    def __init__(self, stream, descriptions, verbosity,
131
132
                 bench_history=None,
132
133
                 num_tests=None,
162
163
        self.unsupported = {}
163
164
        self.count = 0
164
165
        self._overall_start_time = time.time()
165
 
    
 
166
 
166
167
    def _extractBenchmarkTime(self, testCase):
167
168
        """Add a benchmark time for the current test case."""
168
169
        return getattr(testCase, "_benchtime", None)
169
 
    
 
170
 
170
171
    def _elapsedTestTimeString(self):
171
172
        """Return a time string for the overall time the current test has taken."""
172
173
        return self._formatTime(time.time() - self._start_time)
276
277
        """The test will not be run because of a missing feature.
277
278
        """
278
279
        # this can be called in two different ways: it may be that the
279
 
        # test started running, and then raised (through addError) 
 
280
        # test started running, and then raised (through addError)
280
281
        # UnavailableFeature.  Alternatively this method can be called
281
282
        # while probing for features before running the tests; in that
282
283
        # case we will see startTest and stopTest, but the test will never
393
394
        self.count += 1
394
395
        self.pb.update(
395
396
                self._progress_prefix_text()
396
 
                + ' ' 
 
397
                + ' '
397
398
                + self._shortened_test_description(test))
398
399
 
399
400
    def _test_description(self, test):
400
401
        return self._shortened_test_description(test)
401
402
 
402
403
    def report_error(self, test, err):
403
 
        self.pb.note('ERROR: %s\n    %s\n', 
 
404
        self.pb.note('ERROR: %s\n    %s\n',
404
405
            self._test_description(test),
405
406
            err[1],
406
407
            )
407
408
 
408
409
    def report_failure(self, test, err):
409
 
        self.pb.note('FAIL: %s\n    %s\n', 
 
410
        self.pb.note('FAIL: %s\n    %s\n',
410
411
            self._test_description(test),
411
412
            err[1],
412
413
            )
423
424
 
424
425
    def report_unsupported(self, test, feature):
425
426
        """test cannot be run because feature is missing."""
426
 
                  
 
427
 
427
428
    def report_cleaning_up(self):
428
429
        self.pb.update('cleaning up...')
429
430
 
539
540
                self.stream.writeln("%s" % (t.id()))
540
541
                run += 1
541
542
            actionTaken = "Listed"
542
 
        else: 
 
543
        else:
543
544
            test.run(result)
544
545
            run = result.testsRun
545
546
            actionTaken = "Ran"
601
602
class TestNotApplicable(TestSkipped):
602
603
    """A test is not applicable to the situation where it was run.
603
604
 
604
 
    This is only normally raised by parameterized tests, if they find that 
605
 
    the instance they're constructed upon does not support one aspect 
 
605
    This is only normally raised by parameterized tests, if they find that
 
606
    the instance they're constructed upon does not support one aspect
606
607
    of its interface.
607
608
    """
608
609
 
630
631
 
631
632
class StringIOWrapper(object):
632
633
    """A wrapper around cStringIO which just adds an encoding attribute.
633
 
    
 
634
 
634
635
    Internally we can check sys.stdout to see what the output encoding
635
636
    should be. However, cStringIO has no encoding attribute that we can
636
637
    set. So we wrap it instead.
727
728
 
728
729
class TestCase(unittest.TestCase):
729
730
    """Base class for bzr unit tests.
730
 
    
731
 
    Tests that need access to disk resources should subclass 
 
731
 
 
732
    Tests that need access to disk resources should subclass
732
733
    TestCaseInTempDir not TestCase.
733
734
 
734
735
    Error and debug log messages are redirected from their usual
736
737
    retrieved by _get_log().  We use a real OS file, not an in-memory object,
737
738
    so that it can also capture file IO.  When the test completes this file
738
739
    is read into memory and removed from disk.
739
 
       
 
740
 
740
741
    There are also convenience functions to invoke bzr's command-line
741
742
    routine, and to build and check bzr trees.
742
 
   
 
743
 
743
744
    In addition to the usual method of overriding tearDown(), this class also
744
745
    allows subclasses to register functions into the _cleanups list, which is
745
746
    run in order as the object is torn down.  It's less likely this will be
808
809
            bzrlib.mutabletree.MutableTree: bzrlib.mutabletree.MutableTree.hooks,
809
810
            bzrlib.smart.client._SmartClient: bzrlib.smart.client._SmartClient.hooks,
810
811
            bzrlib.smart.server.SmartTCPServer: bzrlib.smart.server.SmartTCPServer.hooks,
 
812
            bzrlib.commands.Command: bzrlib.commands.Command.hooks,
811
813
            }
812
814
        self.addCleanup(self._restoreHooks)
813
815
        # reset all hooks to an empty instance of the appropriate type
814
816
        bzrlib.branch.Branch.hooks = bzrlib.branch.BranchHooks()
815
817
        bzrlib.smart.client._SmartClient.hooks = bzrlib.smart.client.SmartClientHooks()
816
818
        bzrlib.smart.server.SmartTCPServer.hooks = bzrlib.smart.server.SmartServerHooks()
 
819
        bzrlib.commands.Command.hooks = bzrlib.commands.CommandHooks()
817
820
 
818
821
    def _silenceUI(self):
819
822
        """Turn off UI for duration of test"""
826
829
 
827
830
    def _ndiff_strings(self, a, b):
828
831
        """Return ndiff between two strings containing lines.
829
 
        
 
832
 
830
833
        A trailing newline is added if missing to make the strings
831
834
        print properly."""
832
835
        if b and b[-1] != '\n':
857
860
 
858
861
    def assertEqualDiff(self, a, b, message=None):
859
862
        """Assert two texts are equal, if not raise an exception.
860
 
        
861
 
        This is intended for use with multi-line strings where it can 
 
863
 
 
864
        This is intended for use with multi-line strings where it can
862
865
        be hard to find the differences by eye.
863
866
        """
864
867
        # TODO: perhaps override assertEquals to call this for strings?
872
875
            message = 'second string is missing a final newline.\n'
873
876
        raise AssertionError(message +
874
877
                             self._ndiff_strings(a, b))
875
 
        
 
878
 
876
879
    def assertEqualMode(self, mode, mode_test):
877
880
        self.assertEqual(mode, mode_test,
878
881
                         'mode mismatch %o != %o' % (mode, mode_test))
936
939
 
937
940
    def assertListRaises(self, excClass, func, *args, **kwargs):
938
941
        """Fail unless excClass is raised when the iterator from func is used.
939
 
        
 
942
 
940
943
        Many functions can return generators this makes sure
941
944
        to wrap them in a list() call to make sure the whole generator
942
945
        is run, and that the proper exception is raised.
990
993
 
991
994
    def assertTransportMode(self, transport, path, mode):
992
995
        """Fail if a path does not have mode mode.
993
 
        
 
996
 
994
997
        If modes are not supported on this transport, the assertion is ignored.
995
998
        """
996
999
        if not transport._can_roundtrip_unix_modebits():
1167
1170
    def callDeprecated(self, expected, callable, *args, **kwargs):
1168
1171
        """Assert that a callable is deprecated in a particular way.
1169
1172
 
1170
 
        This is a very precise test for unusual requirements. The 
 
1173
        This is a very precise test for unusual requirements. The
1171
1174
        applyDeprecated helper function is probably more suited for most tests
1172
1175
        as it allows you to simply specify the deprecation format being used
1173
1176
        and will ensure that that is issued for the function being called.
1218
1221
    def addCleanup(self, callable, *args, **kwargs):
1219
1222
        """Arrange to run a callable when this case is torn down.
1220
1223
 
1221
 
        Callables are run in the reverse of the order they are registered, 
 
1224
        Callables are run in the reverse of the order they are registered,
1222
1225
        ie last-in first-out.
1223
1226
        """
1224
1227
        self._cleanups.append((callable, args, kwargs))
1307
1310
 
1308
1311
    def time(self, callable, *args, **kwargs):
1309
1312
        """Run callable and accrue the time it takes to the benchmark time.
1310
 
        
 
1313
 
1311
1314
        If lsprofiling is enabled (i.e. by --lsprof-time to bzr selftest) then
1312
1315
        this will cause lsprofile statistics to be gathered and stored in
1313
1316
        self._benchcalls.
1328
1331
            self._benchtime += time.time() - start
1329
1332
 
1330
1333
    def _runCleanups(self):
1331
 
        """Run registered cleanup functions. 
 
1334
        """Run registered cleanup functions.
1332
1335
 
1333
1336
        This should only be called from TestCase.tearDown.
1334
1337
        """
1335
 
        # TODO: Perhaps this should keep running cleanups even if 
 
1338
        # TODO: Perhaps this should keep running cleanups even if
1336
1339
        # one of them fails?
1337
1340
 
1338
1341
        # Actually pop the cleanups from the list so tearDown running
1454
1457
        passed in three ways:
1455
1458
 
1456
1459
        1- A list of strings, eg ["commit", "a"].  This is recommended
1457
 
        when the command contains whitespace or metacharacters, or 
 
1460
        when the command contains whitespace or metacharacters, or
1458
1461
        is built up at run time.
1459
1462
 
1460
 
        2- A single string, eg "add a".  This is the most convenient 
 
1463
        2- A single string, eg "add a".  This is the most convenient
1461
1464
        for hardcoded commands.
1462
1465
 
1463
1466
        This runs bzr through the interface that catches and reports
1522
1525
    def run_bzr_subprocess(self, *args, **kwargs):
1523
1526
        """Run bzr in a subprocess for testing.
1524
1527
 
1525
 
        This starts a new Python interpreter and runs bzr in there. 
 
1528
        This starts a new Python interpreter and runs bzr in there.
1526
1529
        This should only be used for tests that have a justifiable need for
1527
1530
        this isolation: e.g. they are testing startup time, or signal
1528
 
        handling, or early startup code, etc.  Subprocess code can't be 
 
1531
        handling, or early startup code, etc.  Subprocess code can't be
1529
1532
        profiled or debugged so easily.
1530
1533
 
1531
1534
        :keyword retcode: The status code that is expected.  Defaults to 0.  If
1771
1774
 
1772
1775
    def __init__(self, methodName='runTest'):
1773
1776
        # allow test parameterization after test construction and before test
1774
 
        # execution. Variables that the parameterizer sets need to be 
 
1777
        # execution. Variables that the parameterizer sets need to be
1775
1778
        # ones that are not set by setUp, or setUp will trash them.
1776
1779
        super(TestCaseWithMemoryTransport, self).__init__(methodName)
1777
1780
        self.vfs_transport_factory = default_transport
1784
1787
 
1785
1788
        This transport is for the test scratch space relative to
1786
1789
        "self._test_root"
1787
 
        
 
1790
 
1788
1791
        :param relpath: a path relative to the base url.
1789
1792
        """
1790
1793
        t = get_transport(self.get_url(relpath))
1793
1796
 
1794
1797
    def get_readonly_transport(self, relpath=None):
1795
1798
        """Return a readonly transport for the test scratch space
1796
 
        
 
1799
 
1797
1800
        This can be used to test that operations which should only need
1798
1801
        readonly access in fact do not try to write.
1799
1802
 
1830
1833
    def get_readonly_url(self, relpath=None):
1831
1834
        """Get a URL for the readonly transport.
1832
1835
 
1833
 
        This will either be backed by '.' or a decorator to the transport 
 
1836
        This will either be backed by '.' or a decorator to the transport
1834
1837
        used by self.get_url()
1835
1838
        relpath provides for clients to get a path relative to the base url.
1836
1839
        These should only be downwards relative, not upwards.
1969
1972
 
1970
1973
    def makeAndChdirToTestDir(self):
1971
1974
        """Create a temporary directories for this one test.
1972
 
        
 
1975
 
1973
1976
        This must set self.test_home_dir and self.test_dir and chdir to
1974
1977
        self.test_dir.
1975
 
        
 
1978
 
1976
1979
        For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1977
1980
        """
1978
1981
        os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1979
1982
        self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1980
1983
        self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1981
 
        
 
1984
 
1982
1985
    def make_branch(self, relpath, format=None):
1983
1986
        """Create a branch on the transport at relpath."""
1984
1987
        repo = self.make_repository(relpath, format=format)
2002
2005
 
2003
2006
    def make_repository(self, relpath, shared=False, format=None):
2004
2007
        """Create a repository on our default transport at relpath.
2005
 
        
 
2008
 
2006
2009
        Note that relpath must be a relative path, not a full url.
2007
2010
        """
2008
2011
        # FIXME: If you create a remoterepository this returns the underlying
2009
 
        # real format, which is incorrect.  Actually we should make sure that 
 
2012
        # real format, which is incorrect.  Actually we should make sure that
2010
2013
        # RemoteBzrDir returns a RemoteRepository.
2011
2014
        # maybe  mbp 20070410
2012
2015
        made_control = self.make_bzrdir(relpath, format=format)
2025
2028
    def overrideEnvironmentForTesting(self):
2026
2029
        os.environ['HOME'] = self.test_home_dir
2027
2030
        os.environ['BZR_HOME'] = self.test_home_dir
2028
 
        
 
2031
 
2029
2032
    def setUp(self):
2030
2033
        super(TestCaseWithMemoryTransport, self).setUp()
2031
2034
        self._make_test_root()
2039
2042
        self.__server = None
2040
2043
        self.reduceLockdirTimeout()
2041
2044
 
2042
 
     
 
2045
    def setup_smart_server_with_call_log(self):
 
2046
        """Sets up a smart server as the transport server with a call log."""
 
2047
        self.transport_server = server.SmartTCPServer_for_testing
 
2048
        self.hpss_calls = []
 
2049
        def capture_hpss_call(params):
 
2050
            import traceback
 
2051
            self.hpss_calls.append((params, traceback.format_stack()))
 
2052
        client._SmartClient.hooks.install_named_hook(
 
2053
            'call', capture_hpss_call, None)
 
2054
 
 
2055
    def reset_smart_call_log(self):
 
2056
        self.hpss_calls = []
 
2057
 
 
2058
 
2043
2059
class TestCaseInTempDir(TestCaseWithMemoryTransport):
2044
2060
    """Derived class that runs a test within a temporary directory.
2045
2061
 
2050
2066
    All test cases create their own directory within that.  If the
2051
2067
    tests complete successfully, the directory is removed.
2052
2068
 
2053
 
    :ivar test_base_dir: The path of the top-level directory for this 
 
2069
    :ivar test_base_dir: The path of the top-level directory for this
2054
2070
    test, which contains a home directory and a work directory.
2055
2071
 
2056
2072
    :ivar test_home_dir: An initially empty directory under test_base_dir
2082
2098
 
2083
2099
    def makeAndChdirToTestDir(self):
2084
2100
        """See TestCaseWithMemoryTransport.makeAndChdirToTestDir().
2085
 
        
 
2101
 
2086
2102
        For TestCaseInTempDir we create a temporary directory based on the test
2087
2103
        name and then create two subdirs - test and home under it.
2088
2104
        """
2187
2203
    ReadonlyTransportDecorator is used instead which allows the use of non disk
2188
2204
    based read write transports.
2189
2205
 
2190
 
    If an explicit class is provided for readonly access, that server and the 
 
2206
    If an explicit class is provided for readonly access, that server and the
2191
2207
    readwrite one must both define get_url() as resolving to os.getcwd().
2192
2208
    """
2193
2209
 
2279
2295
    for readonly urls.
2280
2296
 
2281
2297
    TODO RBC 20060127: make this an option to TestCaseWithTransport so it can
2282
 
                       be used without needed to redo it when a different 
 
2298
                       be used without needed to redo it when a different
2283
2299
                       subclass is in use ?
2284
2300
    """
2285
2301
 
2291
2307
 
2292
2308
def condition_id_re(pattern):
2293
2309
    """Create a condition filter which performs a re check on a test's id.
2294
 
    
 
2310
 
2295
2311
    :param pattern: A regular expression string.
2296
2312
    :return: A callable that returns True if the re matches.
2297
2313
    """
2304
2320
 
2305
2321
def condition_isinstance(klass_or_klass_list):
2306
2322
    """Create a condition filter which returns isinstance(param, klass).
2307
 
    
 
2323
 
2308
2324
    :return: A callable which when called with one parameter obj return the
2309
2325
        result of isinstance(obj, klass_or_klass_list).
2310
2326
    """
2315
2331
 
2316
2332
def condition_id_in_list(id_list):
2317
2333
    """Create a condition filter which verify that test's id in a list.
2318
 
    
 
2334
 
2319
2335
    :param id_list: A TestIdList object.
2320
2336
    :return: A callable that returns True if the test's id appears in the list.
2321
2337
    """
2326
2342
 
2327
2343
def condition_id_startswith(starts):
2328
2344
    """Create a condition filter verifying that test's id starts with a string.
2329
 
    
 
2345
 
2330
2346
    :param starts: A list of string.
2331
 
    :return: A callable that returns True if the test's id starts with one of 
 
2347
    :return: A callable that returns True if the test's id starts with one of
2332
2348
        the given strings.
2333
2349
    """
2334
2350
    def condition(test):
2357
2373
 
2358
2374
def filter_suite_by_condition(suite, condition):
2359
2375
    """Create a test suite by filtering another one.
2360
 
    
 
2376
 
2361
2377
    :param suite: The source suite.
2362
2378
    :param condition: A callable whose result evaluates True when called with a
2363
2379
        test case which should be included in the result.
2373
2389
 
2374
2390
def filter_suite_by_re(suite, pattern):
2375
2391
    """Create a test suite by filtering another one.
2376
 
    
 
2392
 
2377
2393
    :param suite:           the source suite
2378
2394
    :param pattern:         pattern that names must match
2379
2395
    :returns: the newly created suite
2431
2447
 
2432
2448
def randomize_suite(suite):
2433
2449
    """Return a new TestSuite with suite's tests in random order.
2434
 
    
 
2450
 
2435
2451
    The tests in the input suite are flattened into a single suite in order to
2436
2452
    accomplish this. Any nested TestSuites are removed to provide global
2437
2453
    randomness.
2443
2459
 
2444
2460
def split_suite_by_condition(suite, condition):
2445
2461
    """Split a test suite into two by a condition.
2446
 
    
 
2462
 
2447
2463
    :param suite: The suite to split.
2448
2464
    :param condition: The condition to match on. Tests that match this
2449
2465
        condition are returned in the first test suite, ones that do not match
2465
2481
 
2466
2482
def split_suite_by_re(suite, pattern):
2467
2483
    """Split a test suite into two by a regular expression.
2468
 
    
 
2484
 
2469
2485
    :param suite: The suite to split.
2470
2486
    :param pattern: A regular expression string. Test ids that match this
2471
2487
        pattern will be in the first test suite returned, and the others in the
2485
2501
              list_only=False,
2486
2502
              random_seed=None,
2487
2503
              exclude_pattern=None,
2488
 
              strict=False):
 
2504
              strict=False,
 
2505
              runner_class=None):
 
2506
    """Run a test suite for bzr selftest.
 
2507
 
 
2508
    :param runner_class: The class of runner to use. Must support the
 
2509
        constructor arguments passed by run_suite which are more than standard
 
2510
        python uses.
 
2511
    :return: A boolean indicating success.
 
2512
    """
2489
2513
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
2490
2514
    if verbose:
2491
2515
        verbosity = 2
2492
2516
    else:
2493
2517
        verbosity = 1
2494
 
    runner = TextTestRunner(stream=sys.stdout,
 
2518
    if runner_class is None:
 
2519
        runner_class = TextTestRunner
 
2520
    runner = runner_class(stream=sys.stdout,
2495
2521
                            descriptions=0,
2496
2522
                            verbosity=verbosity,
2497
2523
                            bench_history=bench_history,
2553
2579
             load_list=None,
2554
2580
             debug_flags=None,
2555
2581
             starting_with=None,
 
2582
             runner_class=None,
2556
2583
             ):
2557
2584
    """Run the whole test suite under the enhanced runner"""
2558
2585
    # XXX: Very ugly way to do this...
2588
2615
                     list_only=list_only,
2589
2616
                     random_seed=random_seed,
2590
2617
                     exclude_pattern=exclude_pattern,
2591
 
                     strict=strict)
 
2618
                     strict=strict,
 
2619
                     runner_class=runner_class,
 
2620
                     )
2592
2621
    finally:
2593
2622
        default_transport = old_transport
2594
2623
        selftest_debug_flags = old_debug_flags
2621
2650
    """Warns about tests not appearing or appearing more than once.
2622
2651
 
2623
2652
    :param test_suite: A TestSuite object.
2624
 
    :param test_id_list: The list of test ids that should be found in 
 
2653
    :param test_id_list: The list of test ids that should be found in
2625
2654
         test_suite.
2626
2655
 
2627
2656
    :return: (absents, duplicates) absents is a list containing the test found
2874
2903
                   'bzrlib.tests.test_shelf_ui',
2875
2904
                   'bzrlib.tests.test_smart',
2876
2905
                   'bzrlib.tests.test_smart_add',
 
2906
                   'bzrlib.tests.test_smart_request',
2877
2907
                   'bzrlib.tests.test_smart_transport',
2878
2908
                   'bzrlib.tests.test_smtp_connection',
2879
2909
                   'bzrlib.tests.test_source',
2975
3005
            # No tests to keep here, move along
2976
3006
            continue
2977
3007
        try:
2978
 
            # note that this really does mean "report only" -- doctest 
 
3008
            # note that this really does mean "report only" -- doctest
2979
3009
            # still runs the rest of the examples
2980
3010
            doc_suite = doctest.DocTestSuite(mod,
2981
3011
                optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
3035
3065
 
3036
3066
    This is the recommended public interface for test parameterization.
3037
3067
    Typically the test_suite() method for a per-implementation test
3038
 
    suite will call multiply_tests_from_modules and return the 
 
3068
    suite will call multiply_tests_from_modules and return the
3039
3069
    result.
3040
3070
 
3041
3071
    :param module_name_list: List of fully-qualified names of test
3042
3072
        modules.
3043
 
    :param scenario_iter: Iterable of pairs of (scenario_name, 
 
3073
    :param scenario_iter: Iterable of pairs of (scenario_name,
3044
3074
        scenario_param_dict).
3045
 
    :param loader: If provided, will be used instead of a new 
 
3075
    :param loader: If provided, will be used instead of a new
3046
3076
        bzrlib.tests.TestLoader() instance.
3047
3077
 
3048
3078
    This returns a new TestSuite containing the cross product of
3049
3079
    all the tests in all the modules, each repeated for each scenario.
3050
 
    Each test is adapted by adding the scenario name at the end 
 
3080
    Each test is adapted by adding the scenario name at the end
3051
3081
    of its name, and updating the test object's __dict__ with the
3052
3082
    scenario_param_dict.
3053
3083
 
3054
3084
    >>> r = multiply_tests_from_modules(
3055
3085
    ...     ['bzrlib.tests.test_sampler'],
3056
 
    ...     [('one', dict(param=1)), 
 
3086
    ...     [('one', dict(param=1)),
3057
3087
    ...      ('two', dict(param=2))])
3058
3088
    >>> tests = list(iter_suite_tests(r))
3059
3089
    >>> len(tests)