Rev 5355: Implement config.reload and make sure we have a file name when using it. in file:///home/vila/src/bzr/bugs/525571-lock-bazaar-conf-files/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu Jul 22 08:56:13 BST 2010


At file:///home/vila/src/bzr/bugs/525571-lock-bazaar-conf-files/

------------------------------------------------------------
revno: 5355
revision-id: v.ladeuil+lp at free.fr-20100722075613-k91bj10ina8ybqne
parent: v.ladeuil+lp at free.fr-20100721163514-e491wd00fdgbp8sp
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: lockable-config-files
timestamp: Thu 2010-07-22 09:56:13 +0200
message:
  Implement config.reload and make sure we have a file name when using it.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2010-07-21 16:35:14 +0000
+++ b/bzrlib/config.py	2010-07-22 07:56:13 +0000
@@ -396,10 +396,17 @@
             self._parser = ConfigObj(co_input, encoding='utf-8')
         except configobj.ConfigObjError, e:
             raise errors.ParseConfigError(e.errors, e.config.filename)
-        # Make sure self._parser.reload() will use the right file name
+        # Make sure self.reload() will use the right file name
         self._parser.filename = self.file_name
         return self._parser
 
+    def reload(self):
+        """Reload the config file from disk."""
+        if self.file_name is None:
+            raise AssertionError('We need a file name to reload the config')
+        if self._parser is not None:
+            self._parser.reload()
+
     def _get_matching_sections(self):
         """Return an ordered list of (section_name, extra_path) pairs.
 
@@ -553,10 +560,8 @@
         self._write_config_file()
 
     def _set_option(self, option, value, section):
-        # FIXME: RBC 20051029 This should refresh the parser and also take a
-        # file lock on bazaar.conf.
-        if self._parser is not None:
-            self._parser.reload()
+        # FIXME: RBC 20051029 This should take a file lock on bazaar.conf.
+        self.reload()
         self._get_parser().setdefault(section, {})[option] = value
         self._write_config_file()
 
@@ -675,10 +680,8 @@
                          STORE_LOCATION_APPENDPATH]:
             raise ValueError('bad storage policy %r for %r' %
                 (store, option))
-        if self._parser is not None:
-            self._parser.reload()
-        # FIXME: RBC 20051029 This should refresh the parser and also take a
-        # file lock on locations.conf.
+        self.reload()
+        # FIXME: RBC 20051029 This should take a file lock on locations.conf.
         location = self.location
         if location.endswith('/'):
             location = location[:-1]

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2010-07-21 16:35:14 +0000
+++ b/bzrlib/tests/test_config.py	2010-07-22 07:56:13 +0000
@@ -407,6 +407,26 @@
         self.assertRaises(AssertionError, conf._write_config_file)
 
 
+class TestIniBaseConfigOnDisk(tests.TestCaseInTempDir):
+
+    def test_cannot_reload_without_name(self):
+        conf = config.IniBasedConfig(_content=sample_config_text)
+        self.assertRaises(AssertionError, conf.reload)
+
+    def test_reload_see_new_value(self):
+        c1 = config.IniBasedConfig(file_name='./test/conf',
+                                   _content='editor=vim\n')
+        c1._write_config_file()
+        c2 = config.IniBasedConfig(file_name='./test/conf',
+                                   _content='editor=emacs\n')
+        c2._write_config_file()
+        self.assertEqual('vim', c1.get_user_option('editor'))
+        self.assertEqual('emacs', c2.get_user_option('editor'))
+        # Make sure we get the Right value
+        c1.reload()
+        self.assertEqual('emacs', c1.get_user_option('editor'))
+
+
 class TestLockableConfig(tests.TestCaseInTempDir):
 
     config_class = config.GlobalConfig



More information about the bazaar-commits mailing list