Rev 5744: Basic tests and implementations for read-only and mutable sections. in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Apr 1 09:59:27 UTC 2011


At file:///home/vila/src/bzr/experimental/config/

------------------------------------------------------------
revno: 5744
revision-id: v.ladeuil+lp at free.fr-20110401095926-okln8han4c4l7prr
parent: pqm at pqm.ubuntu.com-20110328161032-1a2n8l4z60xsvo50
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-section
timestamp: Fri 2011-04-01 11:59:26 +0200
message:
  Basic tests and implementations for read-only and mutable sections.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-02-25 12:12:39 +0000
+++ b/bzrlib/config.py	2011-04-01 09:59:26 +0000
@@ -1991,6 +1991,39 @@
         self._transport.put_file(self._filename, out_file)
 
 
+class ReadOnlySection(object):
+    """A section defines a dict of options.
+
+    This is merely a read-only dict which can add some knowledge about the
+    options.
+    """
+
+    def __init__(self, section_id, options):
+        self.id = section_id
+        # We re-use the dict-like object received
+        self.options = options
+
+    def get(self, name, default=None):
+        return self.options.get(name, default)
+
+
+class MutableSection(ReadOnlySection):
+
+    def __init__(self, section_id, options):
+        super(MutableSection, self).__init__(section_id, options)
+        self.orig = {}
+
+    def set(self, name, value):
+        if name not in self.orig:
+            self.orig[name] = self.get(name, None)
+        self.options[name] = value
+
+    def remove(self, name):
+        if name not in self.orig:
+            self.orig[name] = self.get(name, None)
+        del self.options[name]
+
+
 class cmd_config(commands.Command):
     __doc__ = """Display, set or remove a configuration option.
 

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-02-25 12:12:39 +0000
+++ b/bzrlib/tests/test_config.py	2011-04-01 09:59:26 +0000
@@ -1817,6 +1817,48 @@
         self.assertIs(None, bzrdir_config.get_default_stack_on())
 
 
+class TestConfigReadOnlySection(tests.TestCase):
+
+    # FIXME: Parametrize so that all sections produced by Stores run these
+    # tests.
+
+    def test_get_a_value(self):
+        a_dict = dict(foo='bar')
+        section = config.ReadOnlySection('myID', a_dict)
+        self.assertEquals('bar', section.get('foo'))
+
+    def test_options_is_shared(self):
+        a_dict = dict()
+        section = config.ReadOnlySection('myID', a_dict)
+        self.assertIs(a_dict, section.options)
+
+
+class TestConfigMutableSection(tests.TestCase):
+
+    # FIXME: Parametrize so that all sections produced by Stores run these
+    # tests.
+
+    def test_set(self):
+        a_dict = dict(foo='bar')
+        section = config.MutableSection('myID', a_dict)
+        section.set('foo', 'new_value')
+        self.assertEquals('new_value', section.get('foo'))
+        # The change appears in the shared section
+        self.assertEquals('new_value', a_dict.get('foo'))
+        # We keep track of the change
+        self.assertTrue('foo' in section.orig)
+        self.assertEquals('bar', section.orig.get('foo'))
+
+    def test_set_preserve_original_once(self):
+        a_dict = dict(foo='bar')
+        section = config.MutableSection('myID', a_dict)
+        section.set('foo', 'first_value')
+        section.set('foo', 'second_value')
+        # We keep track of the original value
+        self.assertTrue('foo' in section.orig)
+        self.assertEquals('bar', section.orig.get('foo'))
+
+
 class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
 
     def setUp(self):



More information about the bazaar-commits mailing list