~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Ross Lagerwall
  • Date: 2012-08-07 06:32:51 UTC
  • mto: (6437.63.5 2.5)
  • mto: This revision was merged to the branch mainline in revision 6558.
  • Revision ID: rosslagerwall@gmail.com-20120807063251-x9p03ghg2ws8oqjc
Add bzrlib/locale to .bzrignore

bzrlib/locale is generated with ./setup.py build_mo which is in turn called
by ./setup.py build

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2013, 2015, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
32
32
import errno
33
33
import itertools
34
34
import logging
35
 
import math
36
35
import os
37
36
import platform
38
37
import pprint
61
60
import bzrlib
62
61
from bzrlib import (
63
62
    branchbuilder,
64
 
    controldir,
 
63
    bzrdir,
65
64
    chk_map,
66
65
    commands as _mod_commands,
67
66
    config,
249
248
    different types of display.
250
249
 
251
250
    When a test finishes, in whatever way, it calls one of the addSuccess,
252
 
    addFailure or addError methods.  These in turn may redirect to a more
 
251
    addFailure or addError classes.  These in turn may redirect to a more
253
252
    specific case for the special test results supported by our extended
254
253
    tests.
255
254
 
360
359
            return float(''.join(details['benchtime'].iter_bytes()))
361
360
        return getattr(testCase, "_benchtime", None)
362
361
 
363
 
    def _delta_to_float(self, a_timedelta, precision):
364
 
        # This calls ceiling to ensure that the most pessimistic view of time
365
 
        # taken is shown (rather than leaving it to the Python %f operator
366
 
        # to decide whether to round/floor/ceiling. This was added when we
367
 
        # had pyp3 test failures that suggest a floor was happening.
368
 
        shift = 10 ** precision
369
 
        return math.ceil((a_timedelta.days * 86400.0 + a_timedelta.seconds +
370
 
            a_timedelta.microseconds / 1000000.0) * shift) / shift
371
 
 
372
362
    def _elapsedTestTimeString(self):
373
363
        """Return a time string for the overall time the current test has taken."""
374
364
        return self._formatTime(self._delta_to_float(
375
 
            self._now() - self._start_datetime, 3))
 
365
            self._now() - self._start_datetime))
376
366
 
377
367
    def _testTimeString(self, testCase):
378
368
        benchmark_time = self._extractBenchmarkTime(testCase)
1011
1001
    def setUp(self):
1012
1002
        super(TestCase, self).setUp()
1013
1003
 
1014
 
        # At this point we're still accessing the config files in $BZR_HOME (as
1015
 
        # set by the user running selftest).
1016
1004
        timeout = config.GlobalStack().get('selftest.timeout')
1017
1005
        if timeout:
1018
1006
            timeout_fixture = fixtures.TimeoutFixture(timeout)
1039
1027
        # between tests.  We should get rid of this altogether: bug 656694. --
1040
1028
        # mbp 20101008
1041
1029
        self.overrideAttr(bzrlib.trace, '_verbosity_level', 0)
 
1030
        # Isolate config option expansion until its default value for bzrlib is
 
1031
        # settled on or a the FIXME associated with _get_expand_default_value
 
1032
        # is addressed -- vila 20110219
 
1033
        self.overrideAttr(config, '_expand_default_value', None)
1042
1034
        self._log_files = set()
1043
1035
        # Each key in the ``_counters`` dict holds a value for a different
1044
1036
        # counter. When the test ends, addDetail() should be used to output the
1337
1329
        # hook into bzr dir opening. This leaves a small window of error for
1338
1330
        # transport tests, but they are well known, and we can improve on this
1339
1331
        # step.
1340
 
        controldir.ControlDir.hooks.install_named_hook("pre_open",
 
1332
        bzrdir.BzrDir.hooks.install_named_hook("pre_open",
1341
1333
            self._preopen_isolate_transport, "Check bzr directories are safe.")
1342
1334
 
1343
1335
    def _ndiff_strings(self, a, b):
1369
1361
            % (message,
1370
1362
               pprint.pformat(a), pprint.pformat(b)))
1371
1363
 
1372
 
    # FIXME: This is deprecated in unittest2 but plugins may still use it so we
1373
 
    # need a deprecation period for them -- vila 2016-02-01
1374
1364
    assertEquals = assertEqual
1375
1365
 
1376
1366
    def assertEqualDiff(self, a, b, message=None):
1379
1369
        This is intended for use with multi-line strings where it can
1380
1370
        be hard to find the differences by eye.
1381
1371
        """
1382
 
        # TODO: perhaps override assertEqual to call this for strings?
 
1372
        # TODO: perhaps override assertEquals to call this for strings?
1383
1373
        if a == b:
1384
1374
            return
1385
1375
        if message is None:
1792
1782
 
1793
1783
        :returns: The actual attr value.
1794
1784
        """
 
1785
        value = getattr(obj, attr_name)
1795
1786
        # The actual value is captured by the call below
1796
 
        value = getattr(obj, attr_name, _unitialized_attr)
1797
 
        if value is _unitialized_attr:
1798
 
            # When the test completes, the attribute should not exist, but if
1799
 
            # we aren't setting a value, we don't need to do anything.
1800
 
            if new is not _unitialized_attr:
1801
 
                self.addCleanup(delattr, obj, attr_name)
1802
 
        else:
1803
 
            self.addCleanup(setattr, obj, attr_name, value)
 
1787
        self.addCleanup(setattr, obj, attr_name, value)
1804
1788
        if new is not _unitialized_attr:
1805
1789
            setattr(obj, attr_name, new)
1806
1790
        return value
2052
2036
        if err:
2053
2037
            self.log('errors:\n%r', err)
2054
2038
        if retcode is not None:
2055
 
            self.assertEqual(retcode, result,
 
2039
            self.assertEquals(retcode, result,
2056
2040
                              message='Unexpected return code')
2057
2041
        return result, out, err
2058
2042
 
2467
2451
        self.transport_readonly_server = None
2468
2452
        self.__vfs_server = None
2469
2453
 
2470
 
    def setUp(self):
2471
 
        super(TestCaseWithMemoryTransport, self).setUp()
2472
 
 
2473
 
        def _add_disconnect_cleanup(transport):
2474
 
            """Schedule disconnection of given transport at test cleanup
2475
 
 
2476
 
            This needs to happen for all connected transports or leaks occur.
2477
 
 
2478
 
            Note reconnections may mean we call disconnect multiple times per
2479
 
            transport which is suboptimal but seems harmless.
2480
 
            """
2481
 
            self.addCleanup(transport.disconnect)
2482
 
 
2483
 
        _mod_transport.Transport.hooks.install_named_hook('post_connect',
2484
 
            _add_disconnect_cleanup, None)
2485
 
 
2486
 
        self._make_test_root()
2487
 
        self.addCleanup(os.chdir, os.getcwdu())
2488
 
        self.makeAndChdirToTestDir()
2489
 
        self.overrideEnvironmentForTesting()
2490
 
        self.__readonly_server = None
2491
 
        self.__server = None
2492
 
        self.reduceLockdirTimeout()
2493
 
        # Each test may use its own config files even if the local config files
2494
 
        # don't actually exist. They'll rightly fail if they try to create them
2495
 
        # though.
2496
 
        self.overrideAttr(config, '_shared_stores', {})
2497
 
 
2498
2454
    def get_transport(self, relpath=None):
2499
2455
        """Return a writeable transport.
2500
2456
 
2643
2599
        real branch.
2644
2600
        """
2645
2601
        root = TestCaseWithMemoryTransport.TEST_ROOT
2646
 
        try:
2647
 
            # Make sure we get a readable and accessible home for .bzr.log
2648
 
            # and/or config files, and not fallback to weird defaults (see
2649
 
            # http://pad.lv/825027).
2650
 
            self.assertIs(None, os.environ.get('BZR_HOME', None))
2651
 
            os.environ['BZR_HOME'] = root
2652
 
            wt = controldir.ControlDir.create_standalone_workingtree(root)
2653
 
            del os.environ['BZR_HOME']
2654
 
        except Exception, e:
2655
 
            self.fail("Fail to initialize the safety net: %r\n" % (e,))
 
2602
        # Make sure we get a readable and accessible home for .bzr.log
 
2603
        # and/or config files, and not fallback to weird defaults (see
 
2604
        # http://pad.lv/825027).
 
2605
        self.assertIs(None, os.environ.get('BZR_HOME', None))
 
2606
        os.environ['BZR_HOME'] = root
 
2607
        wt = bzrdir.BzrDir.create_standalone_workingtree(root)
 
2608
        del os.environ['BZR_HOME']
2656
2609
        # Hack for speed: remember the raw bytes of the dirstate file so that
2657
2610
        # we don't need to re-open the wt to check it hasn't changed.
2658
2611
        TestCaseWithMemoryTransport._SAFETY_NET_PRISTINE_DIRSTATE = (
2728
2681
        if format is None:
2729
2682
            format = self.get_default_format()
2730
2683
        if isinstance(format, basestring):
2731
 
            format = controldir.format_registry.make_bzrdir(format)
 
2684
            format = bzrdir.format_registry.make_bzrdir(format)
2732
2685
        return format
2733
2686
 
2734
2687
    def make_bzrdir(self, relpath, format=None):
2781
2734
        self.overrideEnv('HOME', test_home_dir)
2782
2735
        self.overrideEnv('BZR_HOME', test_home_dir)
2783
2736
 
 
2737
    def setUp(self):
 
2738
        super(TestCaseWithMemoryTransport, self).setUp()
 
2739
 
 
2740
        def _add_disconnect_cleanup(transport):
 
2741
            """Schedule disconnection of given transport at test cleanup
 
2742
 
 
2743
            This needs to happen for all connected transports or leaks occur.
 
2744
 
 
2745
            Note reconnections may mean we call disconnect multiple times per
 
2746
            transport which is suboptimal but seems harmless.
 
2747
            """
 
2748
            self.addCleanup(transport.disconnect)
 
2749
 
 
2750
        _mod_transport.Transport.hooks.install_named_hook('post_connect',
 
2751
            _add_disconnect_cleanup, None)
 
2752
 
 
2753
        self._make_test_root()
 
2754
        self.addCleanup(os.chdir, os.getcwdu())
 
2755
        self.makeAndChdirToTestDir()
 
2756
        self.overrideEnvironmentForTesting()
 
2757
        self.__readonly_server = None
 
2758
        self.__server = None
 
2759
        self.reduceLockdirTimeout()
 
2760
 
2784
2761
    def setup_smart_server_with_call_log(self):
2785
2762
        """Sets up a smart server as the transport server with a call log."""
2786
2763
        self.transport_server = test_server.SmartTCPServer_for_testing
2878
2855
        # stacking policy to honour; create a bzr dir with an unshared
2879
2856
        # repository (but not a branch - our code would be trying to escape
2880
2857
        # then!) to stop them, and permit it to be read.
2881
 
        # control = controldir.ControlDir.create(self.test_base_dir)
 
2858
        # control = bzrdir.BzrDir.create(self.test_base_dir)
2882
2859
        # control.create_repository()
2883
2860
        self.test_home_dir = self.test_base_dir + '/home'
2884
2861
        os.mkdir(self.test_home_dir)
2973
2950
    readwrite one must both define get_url() as resolving to os.getcwd().
2974
2951
    """
2975
2952
 
2976
 
    def setUp(self):
2977
 
        super(TestCaseWithTransport, self).setUp()
2978
 
        self.__vfs_server = None
2979
 
 
2980
2953
    def get_vfs_only_server(self):
2981
2954
        """See TestCaseWithMemoryTransport.
2982
2955
 
3028
3001
            if self.vfs_transport_factory is test_server.LocalURLServer:
3029
3002
                # the branch is colocated on disk, we cannot create a checkout.
3030
3003
                # hopefully callers will expect this.
3031
 
                local_controldir = controldir.ControlDir.open(
3032
 
                    self.get_vfs_only_url(relpath))
 
3004
                local_controldir= bzrdir.BzrDir.open(self.get_vfs_only_url(relpath))
3033
3005
                wt = local_controldir.create_workingtree()
3034
3006
                if wt.branch._format != b._format:
3035
3007
                    wt._branch = b
3065
3037
        self.assertFalse(differences.has_changed(),
3066
3038
            "Trees %r and %r are different: %r" % (left, right, differences))
3067
3039
 
 
3040
    def setUp(self):
 
3041
        super(TestCaseWithTransport, self).setUp()
 
3042
        self.__vfs_server = None
 
3043
 
3068
3044
    def disable_missing_extensions_warning(self):
3069
3045
        """Some tests expect a precise stderr content.
3070
3046
 
3071
3047
        There is no point in forcing them to duplicate the extension related
3072
3048
        warning.
3073
3049
        """
3074
 
        config.GlobalStack().set('ignore_missing_extensions', True)
 
3050
        config.GlobalConfig().set_user_option('ignore_missing_extensions', True)
3075
3051
 
3076
3052
 
3077
3053
class ChrootedTestCase(TestCaseWithTransport):
3804
3780
 
3805
3781
    :return: (absents, duplicates) absents is a list containing the test found
3806
3782
        in id_list but not in test_suite, duplicates is a list containing the
3807
 
        tests found multiple times in test_suite.
 
3783
        test found multiple times in test_suite.
3808
3784
 
3809
3785
    When using a prefined test id list, it may occurs that some tests do not
3810
3786
    exist anymore or that some tests use the same id. This function warns the
4353
4329
    """Copy test and apply scenario to it.
4354
4330
 
4355
4331
    :param test: A test to adapt.
4356
 
    :param scenario: A tuple describing the scenario.
 
4332
    :param scenario: A tuple describing the scenarion.
4357
4333
        The first element of the tuple is the new test id.
4358
4334
        The second element is a dict containing attributes to set on the
4359
4335
        test.