Rev 5774: Clarify that only Store.get_mutable_section() can accept an empty file. in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Apr 12 07:26:36 UTC 2011


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

------------------------------------------------------------
revno: 5774
revision-id: v.ladeuil+lp at free.fr-20110412072636-2ovjedcp9u7pg0vo
parent: v.ladeuil+lp at free.fr-20110412070658-mig3cbgqbf0yyt5j
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-abstract-store
timestamp: Tue 2011-04-12 09:26:36 +0200
message:
  Clarify that only Store.get_mutable_section() can accept an empty file.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-04-12 07:06:58 +0000
+++ b/bzrlib/config.py	2011-04-12 07:26:36 +0000
@@ -2181,21 +2181,11 @@
     def loaded(self):
         return self._config_obj != None
 
-    def load(self, allow_no_such_file=False):
-        """Load the store from the associated file.
-
-        :param allow_no_such_file: Swallow the NoSuchFile exception if True.
-            This allows delayed loading when creating the first option ever.
-        """
+    def load(self):
+        """Load the store from the associated file."""
         if self.loaded:
             return
-        try:
-            content = self.transport.get_bytes(self.file_name)
-        except errors.NoSuchFile:
-            if allow_no_such_file:
-                content = ''
-            else:
-                raise
+        content = self.transport.get_bytes(self.file_name)
         self._load_from_string(content)
 
     def _load_from_string(self, str_or_unicode):
@@ -2225,6 +2215,9 @@
             raise errors.ParseConfigError(e.errors, file_path)
 
     def save(self):
+        if not self.loaded:
+            # Nothing to save
+            return
         out = StringIO()
         self._config_obj.write(out)
         self.transport.put_bytes(self.file_name, out.getvalue())
@@ -2247,7 +2240,11 @@
 
     def get_mutable_section(self, section_name=None):
         # We need a loaded store
-        self.load(allow_no_such_file=True)
+        try:
+            self.load()
+        except errors.NoSuchFile:
+            # The file doesn't exist, let's pretend it was empty
+            self._load_from_string('')
         if section_name is None:
             section = self._config_obj
         else:

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-04-12 07:06:58 +0000
+++ b/bzrlib/tests/test_config.py	2011-04-12 07:26:36 +0000
@@ -1960,12 +1960,20 @@
         return self._get_store(
             self.get_transport(), file_name, content=content)
 
-    def test_save_empty_succeeds(self):
-        store = self.get_store('foo.conf', '')
-        store.load()
+    def test_save_empty_creates_no_file(self):
+        store = self.get_store('foo.conf')
+        store.save()
         self.assertEquals(False, self.get_transport().has('foo.conf'))
+
+    def test_save_emptied_succeeds(self):
+        store = self.get_store('foo.conf', 'foo=bar\n')
+        section = store.get_mutable_section(None)
+        section.remove('foo')
         store.save()
         self.assertEquals(True, self.get_transport().has('foo.conf'))
+        modified_store = self.get_store('foo.conf')
+        sections = list(modified_store.get_sections())
+        self.assertLength(0, sections)
 
     def test_save_with_content_succeeds(self):
         store = self.get_store('foo.conf', 'foo=bar\n')



More information about the bazaar-commits mailing list