Rev 5352: Simplify test config building. in file:///home/vila/src/bzr/bugs/525571-lock-bazaar-conf-files/

Vincent Ladeuil v.ladeuil+lp at free.fr
Mon Jul 19 10:31:44 BST 2010


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

------------------------------------------------------------
revno: 5352
revision-id: v.ladeuil+lp at free.fr-20100719093144-xh55czw06o6sdncd
parent: v.ladeuil+lp at free.fr-20100717064857-lo1gzqx8b4r9xbuy
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: simplify-test-config-building
timestamp: Mon 2010-07-19 11:31:44 +0200
message:
        Simplify test config building.
        
        * bzrlib/tests/test_config.py:
        Simplify tests that use _content to build a config in memory.
        
        * bzrlib/tests/test_commands.py:
        (TestGetAlias._get_config): Simplified.
        
        * bzrlib/config.py:
        (IniBasedConfig.__init__): _content is a basestring that is
        transformed before calling ConfigObj.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2010-07-16 14:57:08 +0000
+++ b/bzrlib/config.py	2010-07-19 09:31:44 +0000
@@ -369,6 +369,9 @@
                 self.file_name = get_filename()
         else:
             self.file_name = file_name
+        if _content is not None:
+            # wrap the content as a file-like object
+            _content = StringIO(_content.encode('utf-8'))
         self._content = _content
         self._parser = None
 

=== modified file 'bzrlib/tests/test_commands.py'
--- a/bzrlib/tests/test_commands.py	2010-07-17 06:48:57 +0000
+++ b/bzrlib/tests/test_commands.py	2010-07-19 09:31:44 +0000
@@ -95,8 +95,7 @@
 class TestGetAlias(tests.TestCase):
 
     def _get_config(self, config_text):
-        config_file = StringIO(config_text.encode('utf-8'))
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig(_content=config_text)
         return my_config
 
     def test_simple(self):

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2010-07-16 14:57:08 +0000
+++ b/bzrlib/tests/test_config.py	2010-07-19 09:31:44 +0000
@@ -366,7 +366,7 @@
 class TestIniConfig(tests.TestCase):
 
     def make_config_parser(self, s):
-        conf = config.IniBasedConfig(_content=StringIO(s.encode('utf-8')))
+        conf = config.IniBasedConfig(_content=s)
         return conf, conf._get_parser()
 
 
@@ -376,13 +376,11 @@
         my_config = config.IniBasedConfig()
 
     def test_from_fp(self):
-        config_file = StringIO(sample_config_text.encode('utf-8'))
-        my_config = config.IniBasedConfig(_content=config_file)
+        my_config = config.IniBasedConfig(_content=sample_config_text)
         self.assertIsInstance(my_config._get_parser(), configobj.ConfigObj)
 
     def test_cached(self):
-        config_file = StringIO(sample_config_text.encode('utf-8'))
-        my_config = config.IniBasedConfig(_content=config_file)
+        my_config = config.IniBasedConfig(_content=sample_config_text)
         parser = my_config._get_parser()
         self.failUnless(my_config._get_parser() is parser)
 
@@ -394,7 +392,7 @@
 
     def test_get_parser_file_parameter_is_deprecated_(self):
         config_file = StringIO(sample_config_text.encode('utf-8'))
-        conf = config.IniBasedConfig(_content=config_file)
+        conf = config.IniBasedConfig(_content=sample_config_text)
         conf = self.callDeprecated([
             'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
             ' Use IniBasedConfig(_content=xxx) instead.'],
@@ -592,24 +590,20 @@
 class TestGlobalConfigItems(tests.TestCase):
 
     def test_user_id(self):
-        config_file = StringIO(sample_config_text.encode('utf-8'))
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig(_content=sample_config_text)
         self.assertEqual(u"Erik B\u00e5gfors <erik at bagfors.nu>",
                          my_config._get_user_id())
 
     def test_absent_user_id(self):
-        config_file = StringIO("")
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig()
         self.assertEqual(None, my_config._get_user_id())
 
     def test_configured_editor(self):
-        config_file = StringIO(sample_config_text.encode('utf-8'))
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig(_content=sample_config_text)
         self.assertEqual("vim", my_config.get_editor())
 
     def test_signatures_always(self):
-        config_file = StringIO(sample_always_signatures)
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig(_content=sample_always_signatures)
         self.assertEqual(config.CHECK_NEVER,
                          my_config.signature_checking())
         self.assertEqual(config.SIGN_ALWAYS,
@@ -617,8 +611,7 @@
         self.assertEqual(True, my_config.signature_needed())
 
     def test_signatures_if_possible(self):
-        config_file = StringIO(sample_maybe_signatures)
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig(_content=sample_maybe_signatures)
         self.assertEqual(config.CHECK_NEVER,
                          my_config.signature_checking())
         self.assertEqual(config.SIGN_WHEN_REQUIRED,
@@ -626,8 +619,7 @@
         self.assertEqual(False, my_config.signature_needed())
 
     def test_signatures_ignore(self):
-        config_file = StringIO(sample_ignore_signatures)
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig(_content=sample_ignore_signatures)
         self.assertEqual(config.CHECK_ALWAYS,
                          my_config.signature_checking())
         self.assertEqual(config.SIGN_NEVER,
@@ -635,8 +627,7 @@
         self.assertEqual(False, my_config.signature_needed())
 
     def _get_sample_config(self):
-        config_file = StringIO(sample_config_text.encode('utf-8'))
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig(_content=sample_config_text)
         return my_config
 
     def test_gpg_signing_command(self):
@@ -645,8 +636,7 @@
         self.assertEqual(False, my_config.signature_needed())
 
     def _get_empty_config(self):
-        config_file = StringIO("")
-        my_config = config.GlobalConfig(_content=config_file)
+        my_config = config.GlobalConfig()
         return my_config
 
     def test_gpg_signing_command_unset(self):
@@ -983,16 +973,13 @@
     def get_branch_config(self, location, global_config=None):
         my_branch = FakeBranch(location)
         if global_config is None:
-            global_file = StringIO(sample_config_text.encode('utf-8'))
-        else:
-            global_file = StringIO(global_config.encode('utf-8'))
-        branches_file = StringIO(sample_branches_text.encode('utf-8'))
+            global_config = sample_config_text
 
         config.ensure_config_dir_exists()
-        my_global_config = config.GlobalConfig(_content=global_file)
+        my_global_config = config.GlobalConfig(_content=global_config)
         my_global_config._write_config_file()
-        my_location_config = config.LocationConfig(my_branch.base,
-                                                   _content=branches_file)
+        my_location_config = config.LocationConfig(
+            my_branch.base, _content=sample_branches_text)
         my_location_config._write_config_file()
 
         my_config = config.BranchConfig(my_branch)
@@ -1064,14 +1051,12 @@
                           location_config=None, branch_data_config=None):
         my_branch = FakeBranch(location)
         if global_config is not None:
-            my_global_config = config.GlobalConfig(
-                _content=StringIO(global_config.encode('utf-8')))
+            my_global_config = config.GlobalConfig(_content=global_config)
             config.ensure_config_dir_exists()
             my_global_config._write_config_file()
         if location_config is not None:
-            my_location_config = config.LocationConfig(
-                my_branch.base,
-                _content=StringIO(location_config.encode('utf-8')))
+            my_location_config = config.LocationConfig(my_branch.base,
+                                                       _content=location_config)
             config.ensure_config_dir_exists()
             my_location_config._write_config_file()
         my_config = config.BranchConfig(my_branch)



More information about the bazaar-commits mailing list