~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

Parametrize the generic tests against the concrete stores.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    mergetools,
37
37
    ui,
38
38
    urlutils,
 
39
    registry,
39
40
    tests,
40
41
    trace,
41
42
    transport,
62
63
 
63
64
load_tests = scenarios.load_tests_apply_scenarios
64
65
 
 
66
# We need adpaters that can build a config store in a test context. Test
 
67
# classes, based on TestCaseWithTransport, can use the registry to parametrize
 
68
# themselves. The builder will receive a test instance and should return a
 
69
# ready-to-use store.  Plugins that defines new stores can also register
 
70
# themselves here to be tested against the tests defined below.
 
71
test_store_builder_registry = registry.Registry()
 
72
test_store_builder_registry.register(
 
73
    'configobj', lambda test: config.ConfigObjStore(test.get_transport(),
 
74
                                                    'configobj.conf'))
 
75
test_store_builder_registry.register(
 
76
    'bazaar', lambda test: config.GlobalStore())
 
77
test_store_builder_registry.register(
 
78
    'location', lambda test: config.LocationStore())
 
79
test_store_builder_registry.register(
 
80
    'branch', lambda test: config.BranchStore(test.branch))
 
81
 
 
82
 
65
83
 
66
84
sample_long_alias="log -r-15..-1 --line"
67
85
sample_config_text = u"""
1921
1939
 
1922
1940
class TestReadonlyStore(TestStore):
1923
1941
 
1924
 
    scenarios = [('configobj', {'_get_store': get_ConfigObjStore})]
1925
 
 
1926
 
    def get_store(self, file_name, content=None):
1927
 
        return self._get_store(
1928
 
            self.get_readonly_transport(), file_name, content=content)
1929
 
 
1930
 
    def test_delayed_load(self):
1931
 
        self.build_tree_contents([('foo.conf', '')])
1932
 
        store = self.get_store('foo.conf')
 
1942
    scenarios = [(key, {'get_store': builder})
 
1943
                 for key, builder in test_store_builder_registry.iteritems()]
 
1944
 
 
1945
    def setUp(self):
 
1946
        super(TestReadonlyStore, self).setUp()
 
1947
        self.branch = self.make_branch('branch')
 
1948
 
 
1949
    def test_building_delays_load(self):
 
1950
        store = self.get_store(self)
1933
1951
        self.assertEquals(False, store.loaded)
1934
 
        store.load()
 
1952
        store._load_from_string('')
1935
1953
        self.assertEquals(True, store.loaded)
1936
1954
 
1937
1955
    def test_get_no_sections_for_empty(self):
1938
 
        store = self.get_store('foo.conf', '')
1939
 
        store.load()
 
1956
        store = self.get_store(self)
 
1957
        store._load_from_string('')
1940
1958
        self.assertEquals([], list(store.get_sections()))
1941
1959
 
1942
1960
    def test_get_default_section(self):
1943
 
        store = self.get_store('foo.conf', 'foo=bar')
 
1961
        store = self.get_store(self)
 
1962
        store._load_from_string('foo=bar')
1944
1963
        sections = list(store.get_sections())
1945
1964
        self.assertLength(1, sections)
1946
1965
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
1947
1966
 
1948
1967
    def test_get_named_section(self):
1949
 
        store = self.get_store('foo.conf', '[baz]\nfoo=bar')
 
1968
        store = self.get_store(self)
 
1969
        store._load_from_string('[baz]\nfoo=bar')
1950
1970
        sections = list(store.get_sections())
1951
1971
        self.assertLength(1, sections)
1952
1972
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
1953
1973
 
1954
1974
    def test_load_from_string_fails_for_non_empty_store(self):
1955
 
        store = self.get_store('foo.conf', 'foo=bar')
 
1975
        store = self.get_store(self)
 
1976
        store._load_from_string('foo=bar')
1956
1977
        self.assertRaises(AssertionError, store._load_from_string, 'bar=baz')
1957
1978
 
1958
1979
 
1959
1980
class TestMutableStore(TestStore):
1960
1981
 
1961
 
    scenarios = [('configobj', {'_get_store': get_ConfigObjStore})]
1962
 
 
1963
 
    def get_store(self, file_name, content=None):
1964
 
        return self._get_store(
1965
 
            self.get_transport(), file_name, content=content)
 
1982
    scenarios = [(key, {'store_id': key, 'get_store': builder})
 
1983
                 for key, builder in test_store_builder_registry.iteritems()]
 
1984
 
 
1985
    def setUp(self):
 
1986
        super(TestMutableStore, self).setUp()
 
1987
        self.transport = self.get_transport()
 
1988
        self.branch = self.make_branch('branch')
 
1989
 
 
1990
    def has_store(self, store):
 
1991
        store_basename = urlutils.relative_url(self.transport.external_url(),
 
1992
                                               store.external_url())
 
1993
        return self.transport.has(store_basename)
1966
1994
 
1967
1995
    def test_save_empty_creates_no_file(self):
1968
 
        store = self.get_store('foo.conf')
 
1996
        if self.store_id == 'branch':
 
1997
            raise tests.TestNotApplicable(
 
1998
                'branch.conf is *always* created when a branch is initialized')
 
1999
        store = self.get_store(self)
1969
2000
        store.save()
1970
 
        self.assertEquals(False, self.get_transport().has('foo.conf'))
 
2001
        self.assertEquals(False, self.has_store(store))
1971
2002
 
1972
2003
    def test_save_emptied_succeeds(self):
1973
 
        store = self.get_store('foo.conf', 'foo=bar\n')
 
2004
        store = self.get_store(self)
 
2005
        store._load_from_string('foo=bar\n')
1974
2006
        section = store.get_mutable_section(None)
1975
2007
        section.remove('foo')
1976
2008
        store.save()
1977
 
        self.assertEquals(True, self.get_transport().has('foo.conf'))
1978
 
        modified_store = self.get_store('foo.conf')
 
2009
        self.assertEquals(True, self.has_store(store))
 
2010
        modified_store = self.get_store(self)
1979
2011
        sections = list(modified_store.get_sections())
1980
2012
        self.assertLength(0, sections)
1981
2013
 
1982
2014
    def test_save_with_content_succeeds(self):
1983
 
        store = self.get_store('foo.conf', 'foo=bar\n')
1984
 
        store.load()
1985
 
        self.assertEquals(False, self.get_transport().has('foo.conf'))
 
2015
        if self.store_id == 'branch':
 
2016
            raise tests.TestNotApplicable(
 
2017
                'branch.conf is *always* created when a branch is initialized')
 
2018
        store = self.get_store(self)
 
2019
        store._load_from_string('foo=bar\n')
 
2020
        self.assertEquals(False, self.has_store(store))
1986
2021
        store.save()
1987
 
        self.assertEquals(True, self.get_transport().has('foo.conf'))
1988
 
        modified_store = self.get_store('foo.conf')
 
2022
        self.assertEquals(True, self.has_store(store))
 
2023
        modified_store = self.get_store(self)
1989
2024
        sections = list(modified_store.get_sections())
1990
2025
        self.assertLength(1, sections)
1991
2026
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
1992
2027
 
1993
2028
    def test_set_option_in_empty_store(self):
1994
 
        store = self.get_store('foo.conf')
 
2029
        store = self.get_store(self)
1995
2030
        section = store.get_mutable_section(None)
1996
2031
        section.set('foo', 'bar')
1997
2032
        store.save()
1998
 
        modified_store = self.get_store('foo.conf')
 
2033
        modified_store = self.get_store(self)
1999
2034
        sections = list(modified_store.get_sections())
2000
2035
        self.assertLength(1, sections)
2001
2036
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2002
2037
 
2003
2038
    def test_set_option_in_default_section(self):
2004
 
        store = self.get_store('foo.conf', '')
 
2039
        store = self.get_store(self)
 
2040
        store._load_from_string('')
2005
2041
        section = store.get_mutable_section(None)
2006
2042
        section.set('foo', 'bar')
2007
2043
        store.save()
2008
 
        modified_store = self.get_store('foo.conf')
 
2044
        modified_store = self.get_store(self)
2009
2045
        sections = list(modified_store.get_sections())
2010
2046
        self.assertLength(1, sections)
2011
2047
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2012
2048
 
2013
2049
    def test_set_option_in_named_section(self):
2014
 
        store = self.get_store('foo.conf', '')
 
2050
        store = self.get_store(self)
 
2051
        store._load_from_string('')
2015
2052
        section = store.get_mutable_section('baz')
2016
2053
        section.set('foo', 'bar')
2017
2054
        store.save()
2018
 
        modified_store = self.get_store('foo.conf')
 
2055
        modified_store = self.get_store(self)
2019
2056
        sections = list(modified_store.get_sections())
2020
2057
        self.assertLength(1, sections)
2021
2058
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])