~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

remove all trailing whitespace from bzr source

Show diffs side-by-side

added added

removed removed

Lines of Context:
126
126
    """
127
127
 
128
128
    stop_early = False
129
 
    
 
129
 
130
130
    def __init__(self, stream, descriptions, verbosity,
131
131
                 bench_history=None,
132
132
                 num_tests=None,
162
162
        self.unsupported = {}
163
163
        self.count = 0
164
164
        self._overall_start_time = time.time()
165
 
    
 
165
 
166
166
    def _extractBenchmarkTime(self, testCase):
167
167
        """Add a benchmark time for the current test case."""
168
168
        return getattr(testCase, "_benchtime", None)
169
 
    
 
169
 
170
170
    def _elapsedTestTimeString(self):
171
171
        """Return a time string for the overall time the current test has taken."""
172
172
        return self._formatTime(time.time() - self._start_time)
276
276
        """The test will not be run because of a missing feature.
277
277
        """
278
278
        # this can be called in two different ways: it may be that the
279
 
        # test started running, and then raised (through addError) 
 
279
        # test started running, and then raised (through addError)
280
280
        # UnavailableFeature.  Alternatively this method can be called
281
281
        # while probing for features before running the tests; in that
282
282
        # case we will see startTest and stopTest, but the test will never
393
393
        self.count += 1
394
394
        self.pb.update(
395
395
                self._progress_prefix_text()
396
 
                + ' ' 
 
396
                + ' '
397
397
                + self._shortened_test_description(test))
398
398
 
399
399
    def _test_description(self, test):
400
400
        return self._shortened_test_description(test)
401
401
 
402
402
    def report_error(self, test, err):
403
 
        self.pb.note('ERROR: %s\n    %s\n', 
 
403
        self.pb.note('ERROR: %s\n    %s\n',
404
404
            self._test_description(test),
405
405
            err[1],
406
406
            )
407
407
 
408
408
    def report_failure(self, test, err):
409
 
        self.pb.note('FAIL: %s\n    %s\n', 
 
409
        self.pb.note('FAIL: %s\n    %s\n',
410
410
            self._test_description(test),
411
411
            err[1],
412
412
            )
423
423
 
424
424
    def report_unsupported(self, test, feature):
425
425
        """test cannot be run because feature is missing."""
426
 
                  
 
426
 
427
427
    def report_cleaning_up(self):
428
428
        self.pb.update('cleaning up...')
429
429
 
539
539
                self.stream.writeln("%s" % (t.id()))
540
540
                run += 1
541
541
            actionTaken = "Listed"
542
 
        else: 
 
542
        else:
543
543
            test.run(result)
544
544
            run = result.testsRun
545
545
            actionTaken = "Ran"
601
601
class TestNotApplicable(TestSkipped):
602
602
    """A test is not applicable to the situation where it was run.
603
603
 
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 
 
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
606
606
    of its interface.
607
607
    """
608
608
 
630
630
 
631
631
class StringIOWrapper(object):
632
632
    """A wrapper around cStringIO which just adds an encoding attribute.
633
 
    
 
633
 
634
634
    Internally we can check sys.stdout to see what the output encoding
635
635
    should be. However, cStringIO has no encoding attribute that we can
636
636
    set. So we wrap it instead.
727
727
 
728
728
class TestCase(unittest.TestCase):
729
729
    """Base class for bzr unit tests.
730
 
    
731
 
    Tests that need access to disk resources should subclass 
 
730
 
 
731
    Tests that need access to disk resources should subclass
732
732
    TestCaseInTempDir not TestCase.
733
733
 
734
734
    Error and debug log messages are redirected from their usual
736
736
    retrieved by _get_log().  We use a real OS file, not an in-memory object,
737
737
    so that it can also capture file IO.  When the test completes this file
738
738
    is read into memory and removed from disk.
739
 
       
 
739
 
740
740
    There are also convenience functions to invoke bzr's command-line
741
741
    routine, and to build and check bzr trees.
742
 
   
 
742
 
743
743
    In addition to the usual method of overriding tearDown(), this class also
744
744
    allows subclasses to register functions into the _cleanups list, which is
745
745
    run in order as the object is torn down.  It's less likely this will be
826
826
 
827
827
    def _ndiff_strings(self, a, b):
828
828
        """Return ndiff between two strings containing lines.
829
 
        
 
829
 
830
830
        A trailing newline is added if missing to make the strings
831
831
        print properly."""
832
832
        if b and b[-1] != '\n':
857
857
 
858
858
    def assertEqualDiff(self, a, b, message=None):
859
859
        """Assert two texts are equal, if not raise an exception.
860
 
        
861
 
        This is intended for use with multi-line strings where it can 
 
860
 
 
861
        This is intended for use with multi-line strings where it can
862
862
        be hard to find the differences by eye.
863
863
        """
864
864
        # TODO: perhaps override assertEquals to call this for strings?
872
872
            message = 'second string is missing a final newline.\n'
873
873
        raise AssertionError(message +
874
874
                             self._ndiff_strings(a, b))
875
 
        
 
875
 
876
876
    def assertEqualMode(self, mode, mode_test):
877
877
        self.assertEqual(mode, mode_test,
878
878
                         'mode mismatch %o != %o' % (mode, mode_test))
936
936
 
937
937
    def assertListRaises(self, excClass, func, *args, **kwargs):
938
938
        """Fail unless excClass is raised when the iterator from func is used.
939
 
        
 
939
 
940
940
        Many functions can return generators this makes sure
941
941
        to wrap them in a list() call to make sure the whole generator
942
942
        is run, and that the proper exception is raised.
990
990
 
991
991
    def assertTransportMode(self, transport, path, mode):
992
992
        """Fail if a path does not have mode mode.
993
 
        
 
993
 
994
994
        If modes are not supported on this transport, the assertion is ignored.
995
995
        """
996
996
        if not transport._can_roundtrip_unix_modebits():
1167
1167
    def callDeprecated(self, expected, callable, *args, **kwargs):
1168
1168
        """Assert that a callable is deprecated in a particular way.
1169
1169
 
1170
 
        This is a very precise test for unusual requirements. The 
 
1170
        This is a very precise test for unusual requirements. The
1171
1171
        applyDeprecated helper function is probably more suited for most tests
1172
1172
        as it allows you to simply specify the deprecation format being used
1173
1173
        and will ensure that that is issued for the function being called.
1218
1218
    def addCleanup(self, callable, *args, **kwargs):
1219
1219
        """Arrange to run a callable when this case is torn down.
1220
1220
 
1221
 
        Callables are run in the reverse of the order they are registered, 
 
1221
        Callables are run in the reverse of the order they are registered,
1222
1222
        ie last-in first-out.
1223
1223
        """
1224
1224
        self._cleanups.append((callable, args, kwargs))
1307
1307
 
1308
1308
    def time(self, callable, *args, **kwargs):
1309
1309
        """Run callable and accrue the time it takes to the benchmark time.
1310
 
        
 
1310
 
1311
1311
        If lsprofiling is enabled (i.e. by --lsprof-time to bzr selftest) then
1312
1312
        this will cause lsprofile statistics to be gathered and stored in
1313
1313
        self._benchcalls.
1328
1328
            self._benchtime += time.time() - start
1329
1329
 
1330
1330
    def _runCleanups(self):
1331
 
        """Run registered cleanup functions. 
 
1331
        """Run registered cleanup functions.
1332
1332
 
1333
1333
        This should only be called from TestCase.tearDown.
1334
1334
        """
1335
 
        # TODO: Perhaps this should keep running cleanups even if 
 
1335
        # TODO: Perhaps this should keep running cleanups even if
1336
1336
        # one of them fails?
1337
1337
 
1338
1338
        # Actually pop the cleanups from the list so tearDown running
1454
1454
        passed in three ways:
1455
1455
 
1456
1456
        1- A list of strings, eg ["commit", "a"].  This is recommended
1457
 
        when the command contains whitespace or metacharacters, or 
 
1457
        when the command contains whitespace or metacharacters, or
1458
1458
        is built up at run time.
1459
1459
 
1460
 
        2- A single string, eg "add a".  This is the most convenient 
 
1460
        2- A single string, eg "add a".  This is the most convenient
1461
1461
        for hardcoded commands.
1462
1462
 
1463
1463
        This runs bzr through the interface that catches and reports
1522
1522
    def run_bzr_subprocess(self, *args, **kwargs):
1523
1523
        """Run bzr in a subprocess for testing.
1524
1524
 
1525
 
        This starts a new Python interpreter and runs bzr in there. 
 
1525
        This starts a new Python interpreter and runs bzr in there.
1526
1526
        This should only be used for tests that have a justifiable need for
1527
1527
        this isolation: e.g. they are testing startup time, or signal
1528
 
        handling, or early startup code, etc.  Subprocess code can't be 
 
1528
        handling, or early startup code, etc.  Subprocess code can't be
1529
1529
        profiled or debugged so easily.
1530
1530
 
1531
1531
        :keyword retcode: The status code that is expected.  Defaults to 0.  If
1771
1771
 
1772
1772
    def __init__(self, methodName='runTest'):
1773
1773
        # allow test parameterization after test construction and before test
1774
 
        # execution. Variables that the parameterizer sets need to be 
 
1774
        # execution. Variables that the parameterizer sets need to be
1775
1775
        # ones that are not set by setUp, or setUp will trash them.
1776
1776
        super(TestCaseWithMemoryTransport, self).__init__(methodName)
1777
1777
        self.vfs_transport_factory = default_transport
1784
1784
 
1785
1785
        This transport is for the test scratch space relative to
1786
1786
        "self._test_root"
1787
 
        
 
1787
 
1788
1788
        :param relpath: a path relative to the base url.
1789
1789
        """
1790
1790
        t = get_transport(self.get_url(relpath))
1793
1793
 
1794
1794
    def get_readonly_transport(self, relpath=None):
1795
1795
        """Return a readonly transport for the test scratch space
1796
 
        
 
1796
 
1797
1797
        This can be used to test that operations which should only need
1798
1798
        readonly access in fact do not try to write.
1799
1799
 
1830
1830
    def get_readonly_url(self, relpath=None):
1831
1831
        """Get a URL for the readonly transport.
1832
1832
 
1833
 
        This will either be backed by '.' or a decorator to the transport 
 
1833
        This will either be backed by '.' or a decorator to the transport
1834
1834
        used by self.get_url()
1835
1835
        relpath provides for clients to get a path relative to the base url.
1836
1836
        These should only be downwards relative, not upwards.
1969
1969
 
1970
1970
    def makeAndChdirToTestDir(self):
1971
1971
        """Create a temporary directories for this one test.
1972
 
        
 
1972
 
1973
1973
        This must set self.test_home_dir and self.test_dir and chdir to
1974
1974
        self.test_dir.
1975
 
        
 
1975
 
1976
1976
        For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1977
1977
        """
1978
1978
        os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1979
1979
        self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1980
1980
        self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1981
 
        
 
1981
 
1982
1982
    def make_branch(self, relpath, format=None):
1983
1983
        """Create a branch on the transport at relpath."""
1984
1984
        repo = self.make_repository(relpath, format=format)
2002
2002
 
2003
2003
    def make_repository(self, relpath, shared=False, format=None):
2004
2004
        """Create a repository on our default transport at relpath.
2005
 
        
 
2005
 
2006
2006
        Note that relpath must be a relative path, not a full url.
2007
2007
        """
2008
2008
        # FIXME: If you create a remoterepository this returns the underlying
2009
 
        # real format, which is incorrect.  Actually we should make sure that 
 
2009
        # real format, which is incorrect.  Actually we should make sure that
2010
2010
        # RemoteBzrDir returns a RemoteRepository.
2011
2011
        # maybe  mbp 20070410
2012
2012
        made_control = self.make_bzrdir(relpath, format=format)
2025
2025
    def overrideEnvironmentForTesting(self):
2026
2026
        os.environ['HOME'] = self.test_home_dir
2027
2027
        os.environ['BZR_HOME'] = self.test_home_dir
2028
 
        
 
2028
 
2029
2029
    def setUp(self):
2030
2030
        super(TestCaseWithMemoryTransport, self).setUp()
2031
2031
        self._make_test_root()
2039
2039
        self.__server = None
2040
2040
        self.reduceLockdirTimeout()
2041
2041
 
2042
 
     
 
2042
 
2043
2043
class TestCaseInTempDir(TestCaseWithMemoryTransport):
2044
2044
    """Derived class that runs a test within a temporary directory.
2045
2045
 
2050
2050
    All test cases create their own directory within that.  If the
2051
2051
    tests complete successfully, the directory is removed.
2052
2052
 
2053
 
    :ivar test_base_dir: The path of the top-level directory for this 
 
2053
    :ivar test_base_dir: The path of the top-level directory for this
2054
2054
    test, which contains a home directory and a work directory.
2055
2055
 
2056
2056
    :ivar test_home_dir: An initially empty directory under test_base_dir
2082
2082
 
2083
2083
    def makeAndChdirToTestDir(self):
2084
2084
        """See TestCaseWithMemoryTransport.makeAndChdirToTestDir().
2085
 
        
 
2085
 
2086
2086
        For TestCaseInTempDir we create a temporary directory based on the test
2087
2087
        name and then create two subdirs - test and home under it.
2088
2088
        """
2187
2187
    ReadonlyTransportDecorator is used instead which allows the use of non disk
2188
2188
    based read write transports.
2189
2189
 
2190
 
    If an explicit class is provided for readonly access, that server and the 
 
2190
    If an explicit class is provided for readonly access, that server and the
2191
2191
    readwrite one must both define get_url() as resolving to os.getcwd().
2192
2192
    """
2193
2193
 
2279
2279
    for readonly urls.
2280
2280
 
2281
2281
    TODO RBC 20060127: make this an option to TestCaseWithTransport so it can
2282
 
                       be used without needed to redo it when a different 
 
2282
                       be used without needed to redo it when a different
2283
2283
                       subclass is in use ?
2284
2284
    """
2285
2285
 
2291
2291
 
2292
2292
def condition_id_re(pattern):
2293
2293
    """Create a condition filter which performs a re check on a test's id.
2294
 
    
 
2294
 
2295
2295
    :param pattern: A regular expression string.
2296
2296
    :return: A callable that returns True if the re matches.
2297
2297
    """
2304
2304
 
2305
2305
def condition_isinstance(klass_or_klass_list):
2306
2306
    """Create a condition filter which returns isinstance(param, klass).
2307
 
    
 
2307
 
2308
2308
    :return: A callable which when called with one parameter obj return the
2309
2309
        result of isinstance(obj, klass_or_klass_list).
2310
2310
    """
2315
2315
 
2316
2316
def condition_id_in_list(id_list):
2317
2317
    """Create a condition filter which verify that test's id in a list.
2318
 
    
 
2318
 
2319
2319
    :param id_list: A TestIdList object.
2320
2320
    :return: A callable that returns True if the test's id appears in the list.
2321
2321
    """
2326
2326
 
2327
2327
def condition_id_startswith(starts):
2328
2328
    """Create a condition filter verifying that test's id starts with a string.
2329
 
    
 
2329
 
2330
2330
    :param starts: A list of string.
2331
 
    :return: A callable that returns True if the test's id starts with one of 
 
2331
    :return: A callable that returns True if the test's id starts with one of
2332
2332
        the given strings.
2333
2333
    """
2334
2334
    def condition(test):
2357
2357
 
2358
2358
def filter_suite_by_condition(suite, condition):
2359
2359
    """Create a test suite by filtering another one.
2360
 
    
 
2360
 
2361
2361
    :param suite: The source suite.
2362
2362
    :param condition: A callable whose result evaluates True when called with a
2363
2363
        test case which should be included in the result.
2373
2373
 
2374
2374
def filter_suite_by_re(suite, pattern):
2375
2375
    """Create a test suite by filtering another one.
2376
 
    
 
2376
 
2377
2377
    :param suite:           the source suite
2378
2378
    :param pattern:         pattern that names must match
2379
2379
    :returns: the newly created suite
2431
2431
 
2432
2432
def randomize_suite(suite):
2433
2433
    """Return a new TestSuite with suite's tests in random order.
2434
 
    
 
2434
 
2435
2435
    The tests in the input suite are flattened into a single suite in order to
2436
2436
    accomplish this. Any nested TestSuites are removed to provide global
2437
2437
    randomness.
2443
2443
 
2444
2444
def split_suite_by_condition(suite, condition):
2445
2445
    """Split a test suite into two by a condition.
2446
 
    
 
2446
 
2447
2447
    :param suite: The suite to split.
2448
2448
    :param condition: The condition to match on. Tests that match this
2449
2449
        condition are returned in the first test suite, ones that do not match
2465
2465
 
2466
2466
def split_suite_by_re(suite, pattern):
2467
2467
    """Split a test suite into two by a regular expression.
2468
 
    
 
2468
 
2469
2469
    :param suite: The suite to split.
2470
2470
    :param pattern: A regular expression string. Test ids that match this
2471
2471
        pattern will be in the first test suite returned, and the others in the
2621
2621
    """Warns about tests not appearing or appearing more than once.
2622
2622
 
2623
2623
    :param test_suite: A TestSuite object.
2624
 
    :param test_id_list: The list of test ids that should be found in 
 
2624
    :param test_id_list: The list of test ids that should be found in
2625
2625
         test_suite.
2626
2626
 
2627
2627
    :return: (absents, duplicates) absents is a list containing the test found
2975
2975
            # No tests to keep here, move along
2976
2976
            continue
2977
2977
        try:
2978
 
            # note that this really does mean "report only" -- doctest 
 
2978
            # note that this really does mean "report only" -- doctest
2979
2979
            # still runs the rest of the examples
2980
2980
            doc_suite = doctest.DocTestSuite(mod,
2981
2981
                optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
3035
3035
 
3036
3036
    This is the recommended public interface for test parameterization.
3037
3037
    Typically the test_suite() method for a per-implementation test
3038
 
    suite will call multiply_tests_from_modules and return the 
 
3038
    suite will call multiply_tests_from_modules and return the
3039
3039
    result.
3040
3040
 
3041
3041
    :param module_name_list: List of fully-qualified names of test
3042
3042
        modules.
3043
 
    :param scenario_iter: Iterable of pairs of (scenario_name, 
 
3043
    :param scenario_iter: Iterable of pairs of (scenario_name,
3044
3044
        scenario_param_dict).
3045
 
    :param loader: If provided, will be used instead of a new 
 
3045
    :param loader: If provided, will be used instead of a new
3046
3046
        bzrlib.tests.TestLoader() instance.
3047
3047
 
3048
3048
    This returns a new TestSuite containing the cross product of
3049
3049
    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 
 
3050
    Each test is adapted by adding the scenario name at the end
3051
3051
    of its name, and updating the test object's __dict__ with the
3052
3052
    scenario_param_dict.
3053
3053
 
3054
3054
    >>> r = multiply_tests_from_modules(
3055
3055
    ...     ['bzrlib.tests.test_sampler'],
3056
 
    ...     [('one', dict(param=1)), 
 
3056
    ...     [('one', dict(param=1)),
3057
3057
    ...      ('two', dict(param=2))])
3058
3058
    >>> tests = list(iter_suite_tests(r))
3059
3059
    >>> len(tests)