~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

Merge config-concrete-stores into config-section-matchers resolving conflicts

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"""
1826
1844
 
1827
1845
    def test_get_unkown_option(self):
1828
1846
        a_dict = dict()
1829
 
        section = config.ReadOnlySection('myID', a_dict)
 
1847
        section = config.ReadOnlySection(None, a_dict)
1830
1848
        self.assertEquals('out of thin air',
1831
1849
                          section.get('foo', 'out of thin air'))
1832
1850
 
1833
1851
    def test_options_is_shared(self):
1834
1852
        a_dict = dict()
1835
 
        section = config.ReadOnlySection('myID', a_dict)
 
1853
        section = config.ReadOnlySection(None, a_dict)
1836
1854
        self.assertIs(a_dict, section.options)
1837
1855
 
1838
1856
 
1839
1857
class TestConfigMutableSection(tests.TestCase):
1840
1858
 
1841
 
    # FIXME: Parametrize so that all sections (includind os.envrion and the
 
1859
    # FIXME: Parametrize so that all sections (including os.environ and the
1842
1860
    # ones produced by Stores) run these tests -- vila 2011-04-01
1843
1861
 
1844
1862
    def test_set(self):
1898
1916
    If provided, the content is added to the store but without saving it on
1899
1917
    disk. It should be a string or a unicode string in the ConfigObj syntax.
1900
1918
    While this poses a constraint on other store implementations, it keeps a
1901
 
    simple syntax usable by test writers.
 
1919
    simple syntax usable by test writers. Note that the other store
 
1920
    implementations can rely on ConfigObj to parse the content and get the
 
1921
    option definitions and values from it.
1902
1922
    """
1903
 
    if content is None:
1904
 
        store = config.ConfigObjStore(transport, file_name)
1905
 
    else:
1906
 
        store = config.ConfigObjStore.from_string(content, transport, file_name)
 
1923
    store = config.ConfigObjStore(transport, file_name)
 
1924
    if content is not None:
 
1925
        store._load_from_string(content)
1907
1926
    return store
1908
1927
 
1909
1928
 
1920
1939
 
1921
1940
class TestReadonlyStore(TestStore):
1922
1941
 
1923
 
    scenarios = [('configobj', {'_get_store': get_ConfigObjStore})]
1924
 
 
1925
 
    def get_store(self, file_name, content=None):
1926
 
        return self._get_store(
1927
 
            self.get_readonly_transport(), file_name, content=content)
1928
 
 
1929
 
    def test_delayed_load(self):
1930
 
        self.build_tree_contents([('foo.conf', '')])
1931
 
        store = self.get_store('foo.conf')
1932
 
        self.assertEquals(False, store.loaded)
1933
 
        store.load()
1934
 
        self.assertEquals(True, store.loaded)
1935
 
 
1936
 
    def test_from_string_delayed_load(self):
1937
 
        store = self.get_store('foo.conf', '')
1938
 
        self.assertEquals(False, store.loaded)
1939
 
        store.load()
1940
 
        # We loaded the store from the provided content
 
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)
 
1951
        self.assertEquals(False, store.loaded)
 
1952
        store._load_from_string('')
1941
1953
        self.assertEquals(True, store.loaded)
1942
1954
 
1943
1955
    def test_get_no_sections_for_empty(self):
1944
 
        store = self.get_store('foo.conf', '')
1945
 
        store.load()
 
1956
        store = self.get_store(self)
 
1957
        store._load_from_string('')
1946
1958
        self.assertEquals([], list(store.get_sections()))
1947
1959
 
1948
1960
    def test_get_default_section(self):
1949
 
        store = self.get_store('foo.conf', 'foo=bar')
 
1961
        store = self.get_store(self)
 
1962
        store._load_from_string('foo=bar')
1950
1963
        sections = list(store.get_sections())
1951
1964
        self.assertLength(1, sections)
1952
1965
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
1953
1966
 
1954
1967
    def test_get_named_section(self):
1955
 
        store = self.get_store('foo.conf', '[baz]\nfoo=bar')
 
1968
        store = self.get_store(self)
 
1969
        store._load_from_string('[baz]\nfoo=bar')
1956
1970
        sections = list(store.get_sections())
1957
1971
        self.assertLength(1, sections)
1958
1972
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
1959
1973
 
 
1974
    def test_load_from_string_fails_for_non_empty_store(self):
 
1975
        store = self.get_store(self)
 
1976
        store._load_from_string('foo=bar')
 
1977
        self.assertRaises(AssertionError, store._load_from_string, 'bar=baz')
 
1978
 
1960
1979
 
1961
1980
class TestMutableStore(TestStore):
1962
1981
 
1963
 
    scenarios = [('configobj', {'_get_store': get_ConfigObjStore})]
1964
 
 
1965
 
    def get_store(self, file_name, content=None):
1966
 
        # Overriden to get a writable transport
1967
 
        return self._get_store(
1968
 
            self.get_transport(), file_name, content=content)
1969
 
 
1970
 
    def test_save_empty_succeeds(self):
1971
 
        store = self.get_store('foo.conf', '')
1972
 
        store.load()
1973
 
        self.assertEquals(False, self.get_transport().has('foo.conf'))
1974
 
        store.save()
1975
 
        self.assertEquals(True, self.get_transport().has('foo.conf'))
 
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)
 
1994
 
 
1995
    def test_save_empty_creates_no_file(self):
 
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)
 
2000
        store.save()
 
2001
        self.assertEquals(False, self.has_store(store))
 
2002
 
 
2003
    def test_save_emptied_succeeds(self):
 
2004
        store = self.get_store(self)
 
2005
        store._load_from_string('foo=bar\n')
 
2006
        section = store.get_mutable_section(None)
 
2007
        section.remove('foo')
 
2008
        store.save()
 
2009
        self.assertEquals(True, self.has_store(store))
 
2010
        modified_store = self.get_store(self)
 
2011
        sections = list(modified_store.get_sections())
 
2012
        self.assertLength(0, sections)
1976
2013
 
1977
2014
    def test_save_with_content_succeeds(self):
1978
 
        store = self.get_store('foo.conf', 'foo=bar\n')
1979
 
        store.load()
1980
 
        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))
1981
2021
        store.save()
1982
 
        self.assertEquals(True, self.get_transport().has('foo.conf'))
1983
 
        modified_store = self.get_store('foo.conf')
 
2022
        self.assertEquals(True, self.has_store(store))
 
2023
        modified_store = self.get_store(self)
1984
2024
        sections = list(modified_store.get_sections())
1985
2025
        self.assertLength(1, sections)
1986
2026
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
1987
2027
 
1988
2028
    def test_set_option_in_empty_store(self):
1989
 
        store = self.get_store('foo.conf')
 
2029
        store = self.get_store(self)
1990
2030
        section = store.get_mutable_section(None)
1991
2031
        section.set('foo', 'bar')
1992
2032
        store.save()
1993
 
        modified_store = self.get_store('foo.conf')
 
2033
        modified_store = self.get_store(self)
1994
2034
        sections = list(modified_store.get_sections())
1995
2035
        self.assertLength(1, sections)
1996
2036
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
1997
2037
 
1998
2038
    def test_set_option_in_default_section(self):
1999
 
        store = self.get_store('foo.conf', '')
 
2039
        store = self.get_store(self)
 
2040
        store._load_from_string('')
2000
2041
        section = store.get_mutable_section(None)
2001
2042
        section.set('foo', 'bar')
2002
2043
        store.save()
2003
 
        modified_store = self.get_store('foo.conf')
 
2044
        modified_store = self.get_store(self)
2004
2045
        sections = list(modified_store.get_sections())
2005
2046
        self.assertLength(1, sections)
2006
2047
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2007
2048
 
2008
2049
    def test_set_option_in_named_section(self):
2009
 
        store = self.get_store('foo.conf', '')
 
2050
        store = self.get_store(self)
 
2051
        store._load_from_string('')
2010
2052
        section = store.get_mutable_section('baz')
2011
2053
        section.set('foo', 'bar')
2012
2054
        store.save()
2013
 
        modified_store = self.get_store('foo.conf')
 
2055
        modified_store = self.get_store(self)
2014
2056
        sections = list(modified_store.get_sections())
2015
2057
        self.assertLength(1, sections)
2016
2058
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2023
2065
        self.assertRaises(errors.NoSuchFile, store.load)
2024
2066
 
2025
2067
    def test_invalid_content(self):
2026
 
        store = config.ConfigObjStore.from_string(
2027
 
            'this is invalid !', self.get_transport(), 'foo.conf', )
 
2068
        store = config.ConfigObjStore(self.get_transport(), 'foo.conf', )
2028
2069
        self.assertEquals(False, store.loaded)
2029
 
        exc = self.assertRaises(errors.ParseConfigError, store.load)
 
2070
        exc = self.assertRaises(
 
2071
            errors.ParseConfigError, store._load_from_string,
 
2072
            'this is invalid !')
2030
2073
        self.assertEndsWith(exc.filename, 'foo.conf')
2031
2074
        # And the load failed
2032
2075
        self.assertEquals(False, store.loaded)
2036
2079
        # option names share the same name space...)
2037
2080
        # FIXME: This should be fixed by forbidding dicts as values ?
2038
2081
        # -- vila 2011-04-05
2039
 
        store = config.ConfigObjStore.from_string('''
 
2082
        store = config.ConfigObjStore(self.get_transport(), 'foo.conf', )
 
2083
        store._load_from_string('''
2040
2084
foo=bar
2041
2085
l=1,2
2042
2086
[DEFAULT]
2047
2091
foo_in_baz=barbaz
2048
2092
[[qux]]
2049
2093
foo_in_qux=quux
2050
 
''', self.get_transport(), 'foo.conf')
 
2094
''')
2051
2095
        sections = list(store.get_sections())
2052
2096
        self.assertLength(4, sections)
2053
2097
        # The default section has no name.