Rev 6089: If conversion fails, the default value still needs to be expanded (if applicable). in file:///home/vila/src/bzr/experimental/expand-in-stack/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed Aug 24 19:38:19 UTC 2011


At file:///home/vila/src/bzr/experimental/expand-in-stack/

------------------------------------------------------------
revno: 6089
revision-id: v.ladeuil+lp at free.fr-20110824193819-h090pa0rodntz2gd
parent: v.ladeuil+lp at free.fr-20110824185639-cdg7rrldkpswea4y
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: expand-in-stack
timestamp: Wed 2011-08-24 21:38:19 +0200
message:
  If conversion fails, the default value still needs to be expanded (if applicable).
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-08-24 18:56:39 +0000
+++ b/bzrlib/config.py	2011-08-24 19:38:19 +0000
@@ -3014,29 +3014,35 @@
         except KeyError:
             # Not registered
             opt = None
-        if value is None:
+        if opt is not None and value is None:
             # If the option is registered, it may provide a default value
-            if opt is not None:
-                value = opt.get_default()
+            value = opt.get_default()
         if expand:
-            if isinstance(value, list):
-                value = self._expand_options_in_list(value)
-            elif isinstance(value, dict):
-                trace.warning('Cannot expand "%s":'
-                              ' Dicts do not support option expansion'
-                              % (name,))
-            elif isinstance(value, (str, unicode)):
-                value = self._expand_options_in_string(value)
-        if opt is not None:
+            value = self._expand_option_value(value)
+        if opt is not None and value is not None:
             value = opt.convert_from_unicode(value)
             if value is None:
-                # The conversion failed or there was no value to convert,
-                # fallback to the default value
-                value = opt.convert_from_unicode(opt.get_default())
+                # The conversion failed fallback to the default value
+                value = opt.get_default()
+                if expand:
+                    value = self._expand_option_value(value)
+                value = opt.convert_from_unicode(value)
         for hook in ConfigHooks['get']:
             hook(self, name, value)
         return value
 
+    def _expand_option_value(self, value):
+        """Expand the option value depending on its type."""
+        if isinstance(value, list):
+            value = self._expand_options_in_list(value)
+        elif isinstance(value, dict):
+            trace.warning('Cannot expand "%s":'
+                          ' Dicts do not support option expansion'
+                          % (name,))
+        elif isinstance(value, (str, unicode)):
+            value = self._expand_options_in_string(value)
+        return value
+
     def expand_options(self, string, env=None):
         """Expand option references in the string in the configuration context.
 

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-08-24 18:49:35 +0000
+++ b/bzrlib/tests/test_config.py	2011-08-24 19:38:19 +0000
@@ -3229,6 +3229,8 @@
 
     def setUp(self):
         super(TestStackExpandOptions, self).setUp()
+        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
+        self.registry = config.option_registry
         self.conf = build_branch_stack(self)
 
     def assertExpansion(self, expected, string, env=None):
@@ -3237,6 +3239,24 @@
     def test_no_expansion(self):
         self.assertExpansion('foo', 'foo')
 
+    def test_expand_default_value(self):
+        self.conf.store._load_from_string('bar=baz')
+        self.registry.register(config.Option('foo', default=u'{bar}'))
+        self.assertEquals('baz', self.conf.get('foo', expand=True))
+
+    def test_expand_default_from_env(self):
+        self.conf.store._load_from_string('bar=baz')
+        self.registry.register(config.Option('foo', default_from_env=['FOO']))
+        self.overrideEnv('FOO', '{bar}')
+        self.assertEquals('baz', self.conf.get('foo', expand=True))
+
+    def test_expand_default_on_failed_conversion(self):
+        self.conf.store._load_from_string('baz=bogus\nbar=42\nfoo={baz}')
+        self.registry.register(
+            config.Option('foo', default=u'{bar}',
+                          from_unicode=config.int_from_store))
+        self.assertEquals(42, self.conf.get('foo', expand=True))
+
     def test_env_adding_options(self):
         self.assertExpansion('bar', '{foo}', {'foo': 'bar'})
 



More information about the bazaar-commits mailing list