~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-06-16 06:10:29 UTC
  • mfrom: (5975.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20110616061029-e58iq1gyvk3pl4yz
(vila) Implement config hooks (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
    ui,
38
38
    urlutils,
39
39
    registry,
 
40
    remote,
40
41
    tests,
41
42
    trace,
42
43
    transport,
45
46
    deprecated_in,
46
47
    deprecated_method,
47
48
    )
48
 
from bzrlib.transport import remote
 
49
from bzrlib.transport import remote as transport_remote
49
50
from bzrlib.tests import (
50
51
    features,
51
 
    TestSkipped,
52
52
    scenarios,
 
53
    test_server,
53
54
    )
54
55
from bzrlib.util.configobj import configobj
55
56
 
118
119
def build_remote_branch_store(test):
119
120
    # There is only one permutation (but we won't be able to handle more with
120
121
    # this design anyway)
121
 
    (transport_class, server_class) = remote.get_test_permutations()[0]
 
122
    (transport_class,
 
123
     server_class) = transport_remote.get_test_permutations()[0]
122
124
    build_backing_branch(test, 'branch', transport_class, server_class)
123
125
    b = branch.Branch.open(test.get_url('branch'))
124
126
    return config.BranchStore(b)
142
144
def build_remote_branch_stack(test):
143
145
    # There is only one permutation (but we won't be able to handle more with
144
146
    # this design anyway)
145
 
    (transport_class, server_class) = remote.get_test_permutations()[0]
 
147
    (transport_class,
 
148
     server_class) = transport_remote.get_test_permutations()[0]
146
149
    build_backing_branch(test, 'branch', transport_class, server_class)
147
150
    b = branch.Branch.open(test.get_url('branch'))
148
151
    return config.BranchStack(b)
1906
1909
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1907
1910
 
1908
1911
 
 
1912
class TestOldConfigHooks(tests.TestCaseWithTransport):
 
1913
 
 
1914
    def setUp(self):
 
1915
        super(TestOldConfigHooks, self).setUp()
 
1916
        create_configs_with_file_option(self)
 
1917
 
 
1918
    def assertGetHook(self, conf, name, value):
 
1919
        calls = []
 
1920
        def hook(*args):
 
1921
            calls.append(args)
 
1922
        config.OldConfigHooks.install_named_hook('get', hook, None)
 
1923
        self.addCleanup(
 
1924
            config.OldConfigHooks.uninstall_named_hook, 'get', None)
 
1925
        self.assertLength(0, calls)
 
1926
        actual_value = conf.get_user_option(name)
 
1927
        self.assertEquals(value, actual_value)
 
1928
        self.assertLength(1, calls)
 
1929
        self.assertEquals((conf, name, value), calls[0])
 
1930
 
 
1931
    def test_get_hook_bazaar(self):
 
1932
        self.assertGetHook(self.bazaar_config, 'file', 'bazaar')
 
1933
 
 
1934
    def test_get_hook_locations(self):
 
1935
        self.assertGetHook(self.locations_config, 'file', 'locations')
 
1936
 
 
1937
    def test_get_hook_branch(self):
 
1938
        # Since locations masks branch, we define a different option
 
1939
        self.branch_config.set_user_option('file2', 'branch')
 
1940
        self.assertGetHook(self.branch_config, 'file2', 'branch')
 
1941
 
 
1942
    def assertSetHook(self, conf, name, value):
 
1943
        calls = []
 
1944
        def hook(*args):
 
1945
            calls.append(args)
 
1946
        config.OldConfigHooks.install_named_hook('set', hook, None)
 
1947
        self.addCleanup(
 
1948
            config.OldConfigHooks.uninstall_named_hook, 'set', None)
 
1949
        self.assertLength(0, calls)
 
1950
        conf.set_user_option(name, value)
 
1951
        self.assertLength(1, calls)
 
1952
        # We can't assert the conf object below as different configs use
 
1953
        # different means to implement set_user_option and we care only about
 
1954
        # coverage here.
 
1955
        self.assertEquals((name, value), calls[0][1:])
 
1956
 
 
1957
    def test_set_hook_bazaar(self):
 
1958
        self.assertSetHook(self.bazaar_config, 'foo', 'bazaar')
 
1959
 
 
1960
    def test_set_hook_locations(self):
 
1961
        self.assertSetHook(self.locations_config, 'foo', 'locations')
 
1962
 
 
1963
    def test_set_hook_branch(self):
 
1964
        self.assertSetHook(self.branch_config, 'foo', 'branch')
 
1965
 
 
1966
    def assertRemoveHook(self, conf, name, section_name=None):
 
1967
        calls = []
 
1968
        def hook(*args):
 
1969
            calls.append(args)
 
1970
        config.OldConfigHooks.install_named_hook('remove', hook, None)
 
1971
        self.addCleanup(
 
1972
            config.OldConfigHooks.uninstall_named_hook, 'remove', None)
 
1973
        self.assertLength(0, calls)
 
1974
        conf.remove_user_option(name, section_name)
 
1975
        self.assertLength(1, calls)
 
1976
        # We can't assert the conf object below as different configs use
 
1977
        # different means to implement remove_user_option and we care only about
 
1978
        # coverage here.
 
1979
        self.assertEquals((name,), calls[0][1:])
 
1980
 
 
1981
    def test_remove_hook_bazaar(self):
 
1982
        self.assertRemoveHook(self.bazaar_config, 'file')
 
1983
 
 
1984
    def test_remove_hook_locations(self):
 
1985
        self.assertRemoveHook(self.locations_config, 'file',
 
1986
                              self.locations_config.location)
 
1987
 
 
1988
    def test_remove_hook_branch(self):
 
1989
        self.assertRemoveHook(self.branch_config, 'file')
 
1990
 
 
1991
    def assertLoadHook(self, name, conf_class, *conf_args):
 
1992
        calls = []
 
1993
        def hook(*args):
 
1994
            calls.append(args)
 
1995
        config.OldConfigHooks.install_named_hook('load', hook, None)
 
1996
        self.addCleanup(
 
1997
            config.OldConfigHooks.uninstall_named_hook, 'load', None)
 
1998
        self.assertLength(0, calls)
 
1999
        # Build a config
 
2000
        conf = conf_class(*conf_args)
 
2001
        # Access an option to trigger a load
 
2002
        conf.get_user_option(name)
 
2003
        self.assertLength(1, calls)
 
2004
        # Since we can't assert about conf, we just use the number of calls ;-/
 
2005
 
 
2006
    def test_load_hook_bazaar(self):
 
2007
        self.assertLoadHook('file', config.GlobalConfig)
 
2008
 
 
2009
    def test_load_hook_locations(self):
 
2010
        self.assertLoadHook('file', config.LocationConfig, self.tree.basedir)
 
2011
 
 
2012
    def test_load_hook_branch(self):
 
2013
        self.assertLoadHook('file', config.BranchConfig, self.tree.branch)
 
2014
 
 
2015
    def assertSaveHook(self, conf):
 
2016
        calls = []
 
2017
        def hook(*args):
 
2018
            calls.append(args)
 
2019
        config.OldConfigHooks.install_named_hook('save', hook, None)
 
2020
        self.addCleanup(
 
2021
            config.OldConfigHooks.uninstall_named_hook, 'save', None)
 
2022
        self.assertLength(0, calls)
 
2023
        # Setting an option triggers a save
 
2024
        conf.set_user_option('foo', 'bar')
 
2025
        self.assertLength(1, calls)
 
2026
        # Since we can't assert about conf, we just use the number of calls ;-/
 
2027
 
 
2028
    def test_save_hook_bazaar(self):
 
2029
        self.assertSaveHook(self.bazaar_config)
 
2030
 
 
2031
    def test_save_hook_locations(self):
 
2032
        self.assertSaveHook(self.locations_config)
 
2033
 
 
2034
    def test_save_hook_branch(self):
 
2035
        self.assertSaveHook(self.branch_config)
 
2036
 
 
2037
 
 
2038
class TestOldConfigHooksForRemote(tests.TestCaseWithTransport):
 
2039
    """Tests config hooks for remote configs.
 
2040
 
 
2041
    No tests for the remove hook as this is not implemented there.
 
2042
    """
 
2043
 
 
2044
    def setUp(self):
 
2045
        super(TestOldConfigHooksForRemote, self).setUp()
 
2046
        self.transport_server = test_server.SmartTCPServer_for_testing
 
2047
        create_configs_with_file_option(self)
 
2048
 
 
2049
    def assertGetHook(self, conf, name, value):
 
2050
        calls = []
 
2051
        def hook(*args):
 
2052
            calls.append(args)
 
2053
        config.OldConfigHooks.install_named_hook('get', hook, None)
 
2054
        self.addCleanup(
 
2055
            config.OldConfigHooks.uninstall_named_hook, 'get', None)
 
2056
        self.assertLength(0, calls)
 
2057
        actual_value = conf.get_option(name)
 
2058
        self.assertEquals(value, actual_value)
 
2059
        self.assertLength(1, calls)
 
2060
        self.assertEquals((conf, name, value), calls[0])
 
2061
 
 
2062
    def test_get_hook_remote_branch(self):
 
2063
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
2064
        self.assertGetHook(remote_branch._get_config(), 'file', 'branch')
 
2065
 
 
2066
    def test_get_hook_remote_bzrdir(self):
 
2067
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2068
        conf = remote_bzrdir._get_config()
 
2069
        conf.set_option('remotedir', 'file')
 
2070
        self.assertGetHook(conf, 'file', 'remotedir')
 
2071
 
 
2072
    def assertSetHook(self, conf, name, value):
 
2073
        calls = []
 
2074
        def hook(*args):
 
2075
            calls.append(args)
 
2076
        config.OldConfigHooks.install_named_hook('set', hook, None)
 
2077
        self.addCleanup(
 
2078
            config.OldConfigHooks.uninstall_named_hook, 'set', None)
 
2079
        self.assertLength(0, calls)
 
2080
        conf.set_option(value, name)
 
2081
        self.assertLength(1, calls)
 
2082
        # We can't assert the conf object below as different configs use
 
2083
        # different means to implement set_user_option and we care only about
 
2084
        # coverage here.
 
2085
        self.assertEquals((name, value), calls[0][1:])
 
2086
 
 
2087
    def test_set_hook_remote_branch(self):
 
2088
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
2089
        self.addCleanup(remote_branch.lock_write().unlock)
 
2090
        self.assertSetHook(remote_branch._get_config(), 'file', 'remote')
 
2091
 
 
2092
    def test_set_hook_remote_bzrdir(self):
 
2093
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
2094
        self.addCleanup(remote_branch.lock_write().unlock)
 
2095
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2096
        self.assertSetHook(remote_bzrdir._get_config(), 'file', 'remotedir')
 
2097
 
 
2098
    def assertLoadHook(self, expected_nb_calls, name, conf_class, *conf_args):
 
2099
        calls = []
 
2100
        def hook(*args):
 
2101
            calls.append(args)
 
2102
        config.OldConfigHooks.install_named_hook('load', hook, None)
 
2103
        self.addCleanup(
 
2104
            config.OldConfigHooks.uninstall_named_hook, 'load', None)
 
2105
        self.assertLength(0, calls)
 
2106
        # Build a config
 
2107
        conf = conf_class(*conf_args)
 
2108
        # Access an option to trigger a load
 
2109
        conf.get_option(name)
 
2110
        self.assertLength(expected_nb_calls, calls)
 
2111
        # Since we can't assert about conf, we just use the number of calls ;-/
 
2112
 
 
2113
    def test_load_hook_remote_branch(self):
 
2114
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
2115
        self.assertLoadHook(1, 'file', remote.RemoteBranchConfig, remote_branch)
 
2116
 
 
2117
    def test_load_hook_remote_bzrdir(self):
 
2118
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2119
        # The config file doesn't exist, set an option to force its creation
 
2120
        conf = remote_bzrdir._get_config()
 
2121
        conf.set_option('remotedir', 'file')
 
2122
        # We get one call for the server and one call for the client, this is
 
2123
        # caused by the differences in implementations betwen
 
2124
        # SmartServerBzrDirRequestConfigFile (in smart/bzrdir.py) and
 
2125
        # SmartServerBranchGetConfigFile (in smart/branch.py)
 
2126
        self.assertLoadHook(2 ,'file', remote.RemoteBzrDirConfig, remote_bzrdir)
 
2127
 
 
2128
    def assertSaveHook(self, conf):
 
2129
        calls = []
 
2130
        def hook(*args):
 
2131
            calls.append(args)
 
2132
        config.OldConfigHooks.install_named_hook('save', hook, None)
 
2133
        self.addCleanup(
 
2134
            config.OldConfigHooks.uninstall_named_hook, 'save', None)
 
2135
        self.assertLength(0, calls)
 
2136
        # Setting an option triggers a save
 
2137
        conf.set_option('foo', 'bar')
 
2138
        self.assertLength(1, calls)
 
2139
        # Since we can't assert about conf, we just use the number of calls ;-/
 
2140
 
 
2141
    def test_save_hook_remote_branch(self):
 
2142
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
2143
        self.addCleanup(remote_branch.lock_write().unlock)
 
2144
        self.assertSaveHook(remote_branch._get_config())
 
2145
 
 
2146
    def test_save_hook_remote_bzrdir(self):
 
2147
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
2148
        self.addCleanup(remote_branch.lock_write().unlock)
 
2149
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2150
        self.assertSaveHook(remote_bzrdir._get_config())
 
2151
 
 
2152
 
1909
2153
class TestOption(tests.TestCase):
1910
2154
 
1911
2155
    def test_default_value(self):
2167
2411
        self.assertLength(1, sections)
2168
2412
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2169
2413
 
 
2414
    def test_load_hook(self):
 
2415
        # We first needs to ensure that the store exists
 
2416
        store = self.get_store(self)
 
2417
        section = store.get_mutable_section('baz')
 
2418
        section.set('foo', 'bar')
 
2419
        store.save()
 
2420
        # Now we can try to load it
 
2421
        store = self.get_store(self)
 
2422
        calls = []
 
2423
        def hook(*args):
 
2424
            calls.append(args)
 
2425
        config.ConfigHooks.install_named_hook('load', hook, None)
 
2426
        self.assertLength(0, calls)
 
2427
        store.load()
 
2428
        self.assertLength(1, calls)
 
2429
        self.assertEquals((store,), calls[0])
 
2430
 
 
2431
    def test_save_hook(self):
 
2432
        calls = []
 
2433
        def hook(*args):
 
2434
            calls.append(args)
 
2435
        config.ConfigHooks.install_named_hook('save', hook, None)
 
2436
        self.assertLength(0, calls)
 
2437
        store = self.get_store(self)
 
2438
        section = store.get_mutable_section('baz')
 
2439
        section.set('foo', 'bar')
 
2440
        store.save()
 
2441
        self.assertLength(1, calls)
 
2442
        self.assertEquals((store,), calls[0])
 
2443
 
2170
2444
 
2171
2445
class TestIniFileStore(TestStore):
2172
2446
 
2494
2768
        conf_stack = config.Stack([conf1, conf2])
2495
2769
        self.assertEquals('baz', conf_stack.get('foo'))
2496
2770
 
2497
 
    def test_get_for_empty_stack(self):
2498
 
        conf_stack = config.Stack([])
2499
 
        self.assertEquals(None, conf_stack.get('foo'))
2500
 
 
2501
2771
    def test_get_for_empty_section_callable(self):
2502
2772
        conf_stack = config.Stack([lambda : []])
2503
2773
        self.assertEquals(None, conf_stack.get('foo'))
2521
2791
        stack = self.get_stack(self)
2522
2792
 
2523
2793
 
 
2794
class TestStackGet(TestStackWithTransport):
 
2795
 
 
2796
    def test_get_for_empty_stack(self):
 
2797
        conf = self.get_stack(self)
 
2798
        self.assertEquals(None, conf.get('foo'))
 
2799
 
 
2800
    def test_get_hook(self):
 
2801
        conf = self.get_stack(self)
 
2802
        conf.store._load_from_string('foo=bar')
 
2803
        calls = []
 
2804
        def hook(*args):
 
2805
            calls.append(args)
 
2806
        config.ConfigHooks.install_named_hook('get', hook, None)
 
2807
        self.assertLength(0, calls)
 
2808
        value = conf.get('foo')
 
2809
        self.assertEquals('bar', value)
 
2810
        self.assertLength(1, calls)
 
2811
        self.assertEquals((conf, 'foo', 'bar'), calls[0])
 
2812
 
 
2813
 
2524
2814
class TestStackSet(TestStackWithTransport):
2525
2815
 
2526
2816
    def test_simple_set(self):
2536
2826
        conf.set('foo', 'baz')
2537
2827
        self.assertEquals, 'baz', conf.get('foo')
2538
2828
 
 
2829
    def test_set_hook(self):
 
2830
        calls = []
 
2831
        def hook(*args):
 
2832
            calls.append(args)
 
2833
        config.ConfigHooks.install_named_hook('set', hook, None)
 
2834
        self.assertLength(0, calls)
 
2835
        conf = self.get_stack(self)
 
2836
        conf.set('foo', 'bar')
 
2837
        self.assertLength(1, calls)
 
2838
        self.assertEquals((conf, 'foo', 'bar'), calls[0])
 
2839
 
2539
2840
 
2540
2841
class TestStackRemove(TestStackWithTransport):
2541
2842
 
2551
2852
        conf = self.get_stack(self)
2552
2853
        self.assertRaises(KeyError, conf.remove, 'I_do_not_exist')
2553
2854
 
 
2855
    def test_remove_hook(self):
 
2856
        calls = []
 
2857
        def hook(*args):
 
2858
            calls.append(args)
 
2859
        config.ConfigHooks.install_named_hook('remove', hook, None)
 
2860
        self.assertLength(0, calls)
 
2861
        conf = self.get_stack(self)
 
2862
        conf.store._load_from_string('foo=bar')
 
2863
        conf.remove('foo')
 
2864
        self.assertLength(1, calls)
 
2865
        self.assertEquals((conf, 'foo'), calls[0])
 
2866
 
2554
2867
 
2555
2868
class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
2556
2869
 
3216
3529
        to be able to choose a user name with no configuration.
3217
3530
        """
3218
3531
        if sys.platform == 'win32':
3219
 
            raise TestSkipped("User name inference not implemented on win32")
 
3532
            raise tests.TestSkipped(
 
3533
                "User name inference not implemented on win32")
3220
3534
        realname, address = config._auto_user_id()
3221
3535
        if os.path.exists('/etc/mailname'):
3222
3536
            self.assertIsNot(None, realname)