Rev 5761: Parametrize Store tests and isolate the ConfigObjStore specific ones. in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed Apr 6 16:15:18 UTC 2011


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

------------------------------------------------------------
revno: 5761
revision-id: v.ladeuil+lp at free.fr-20110406161518-3gbcvxmg44m18n0u
parent: v.ladeuil+lp at free.fr-20110406125441-sldoscgyc5th45op
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-abstract-store
timestamp: Wed 2011-04-06 18:15:18 +0200
message:
  Parametrize Store tests and isolate the ConfigObjStore specific ones.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-04-06 09:12:48 +0000
+++ b/bzrlib/config.py	2011-04-06 16:15:18 +0000
@@ -2116,7 +2116,7 @@
             # parameter (bug #750169) :-/ -- vila 2011-04-04
             # The following will do in the interim but maybe we don't want to
             # expose a path here but rather a config ID and its associated
-            # object (hand wawing).
+            # object </hand wawe>.
             file_path = os.path.join(self.transport.external_url(),
                                      self.file_name)
             raise errors.ParseConfigError(e.errors, file_path)

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-04-06 12:54:41 +0000
+++ b/bzrlib/tests/test_config.py	2011-04-06 16:15:18 +0000
@@ -1889,19 +1889,32 @@
         self.assertEquals(config._Created, section.orig['foo'])
 
 
-class TestReadonlyStore(tests.TestCaseWithTransport):
-
-    # FIXME: parametrize against all valid (store, transport) combinations
-
-    def get_store(self, name=None, content=None):
-        if name is None:
-            name = 'foo.conf'
-        if content is None:
-            store = config.ConfigObjStore(self.get_readonly_transport(), name)
-        else:
-            store = config.ConfigObjStore.from_string(
-                content, self.get_transport(), name)
-        return store
+def get_ConfigObjStore(transport, file_name, content=None):
+    if content is None:
+        store = config.ConfigObjStore(transport, file_name)
+    else:
+        store = config.ConfigObjStore.from_string(content, transport, file_name)
+    return store
+
+
+class TestStore(tests.TestCaseWithTransport):
+
+    def assertSectionContent(self, expected, section):
+        """Assert that some options have the proper values in a section."""
+        expected_name, expected_options = expected
+        self.assertEquals(expected_name, section.id)
+        self.assertEquals(
+            expected_options,
+            dict([(k, section.get(k)) for k in expected_options.keys()]))
+
+
+class TestReadonlyStore(TestStore):
+
+    scenarios = [('configobj', {'_get_store': get_ConfigObjStore})]
+
+    def get_store(self, file_name, content=None):
+        return self._get_store(
+            self.get_readonly_transport(), file_name, content=content)
 
     def test_delayed_load(self):
         self.build_tree_contents([('foo.conf', '')])
@@ -1918,64 +1931,101 @@
         # We use from_string and don't save, so the file shouldn't be created
         self.failIfExists('foo.conf')
 
+    def test_get_no_sections_for_empty(self):
+        store = self.get_store('foo.conf', '')
+        store.load()
+        self.assertEquals([], list(store.get_sections()))
+
+    def test_get_default_section(self):
+        store = self.get_store('foo.conf', 'foo=bar')
+        sections = list(store.get_sections())
+        self.assertLength(1, sections)
+        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
+
+    def test_get_named_section(self):
+        store = self.get_store('foo.conf', '[baz]\nfoo=bar')
+        sections = list(store.get_sections())
+        self.assertLength(1, sections)
+        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
+
+
+class TestMutableStore(TestStore):
+
+    scenarios = [('configobj', {'_get_store': get_ConfigObjStore})]
+
+    def get_store(self, file_name, content=None):
+        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()
+        self.assertEquals(False, self.get_transport().has('foo.conf'))
+        store.save()
+        self.assertEquals(True, self.get_transport().has('foo.conf'))
+
+    def test_save_with_content_succeeds(self):
+        store = self.get_store('foo.conf', 'foo=bar\n')
+        store.load()
+        self.assertEquals(False, self.get_transport().has('foo.conf'))
+        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(1, sections)
+        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
+
+    def test_set_option_in_empty_store(self):
+        store = self.get_store('foo.conf')
+        section = store.get_mutable_section(None)
+        section.set('foo', 'bar')
+        store.save()
+        modified_store = self.get_store('foo.conf')
+        sections = list(modified_store.get_sections())
+        self.assertLength(1, sections)
+        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
+
+    def test_set_option_in_default_section(self):
+        store = self.get_store('foo.conf', '')
+        section = store.get_mutable_section(None)
+        section.set('foo', 'bar')
+        store.save()
+        modified_store = self.get_store('foo.conf')
+        sections = list(modified_store.get_sections())
+        self.assertLength(1, sections)
+        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
+
+    def test_set_option_in_named_section(self):
+        store = self.get_store('foo.conf', '')
+        section = store.get_mutable_section('baz')
+        section.set('foo', 'bar')
+        store.save()
+        modified_store = self.get_store('foo.conf')
+        sections = list(modified_store.get_sections())
+        self.assertLength(1, sections)
+        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
+
+
+class TestConfigObjStore(TestStore):
+
     def test_loading_unknown_file_fails(self):
         store = config.ConfigObjStore(self.get_transport(), 'I-do-not-exist')
         self.assertRaises(errors.NoSuchFile, store.load)
 
     def test_invalid_content(self):
-        store = self.get_store('foo.conf', 'this is invalid !')
+        store = config.ConfigObjStore.from_string(
+            'this is invalid !', self.get_transport(), 'foo.conf', )
         self.assertEquals(False, store.loaded)
         exc = self.assertRaises(errors.ParseConfigError, store.load)
         self.assertEndsWith(exc.filename, 'foo.conf')
         # And the load failed
         self.assertEquals(False, store.loaded)
 
-    def test_save_empty_succeeds(self):
-        store = self.get_store('foo.conf', '')
-        store.load()
-        self.failIfExists('foo.conf')
-        store.save()
-        self.failUnlessExists('foo.conf')
-
-    def test_save_with_content_succeeds(self):
-        store = self.get_store('foo.conf', 'foo=bar\n')
-        store.load()
-        self.failIfExists('foo.conf')
-        store.save()
-        self.failUnlessExists('foo.conf')
-        # FIXME: Far too ConfigObj specific
-        self.assertFileEqual('foo = bar\n', 'foo.conf')
-
-    def test_get_no_sections_for_empty(self):
-        store = self.get_store('foo.conf', '')
-        store.load()
-        self.assertEquals([], list(store.get_sections()))
-
-    def assertSectionContent(self, expected, section):
-        """Assert that some options have the proper values in a section."""
-        expected_name, expected_options = expected
-        self.assertEquals(expected_name, section.id)
-        self.assertEquals(
-            expected_options,
-            dict([(k, section.get(k)) for k in expected_options.keys()]))
-
-    def test_get_default_section(self):
-        store = self.get_store('foo.conf', 'foo=bar')
-        sections = list(store.get_sections())
-        self.assertLength(1, sections)
-        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
-
-    def test_get_named_section(self):
-        store = self.get_store('foo.conf', '[baz]\nfoo=bar')
-        sections = list(store.get_sections())
-        self.assertLength(1, sections)
-        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
-
     def test_get_embedded_sections(self):
         # A more complicated example (which also shows that section names and
         # option names share the same name space...)
         # FIXME: This is really specific to ConfigObjStore -- vila 2011-04-05
-        store = self.get_store('foo.conf', '''
+        store = config.ConfigObjStore.from_string('''
 foo=bar
 l=1,2
 [DEFAULT]
@@ -1986,7 +2036,7 @@
 foo_in_baz=barbaz
 [[qux]]
 foo_in_qux=quux
-''')
+''', self.get_transport(), 'foo.conf')
         sections = list(store.get_sections())
         self.assertLength(4, sections)
         # The default section has no name.
@@ -2003,42 +2053,6 @@
             sections[3])
 
 
-class TestMutableStore(tests.TestCaseWithTransport):
-
-    # FIXME: parametrize against all valid (store, transport) combinations
-
-    def get_store(self, name=None, content=None):
-        if name is None:
-            name = 'foo.conf'
-        if content is None:
-            store = config.ConfigObjStore(self.get_transport(), name)
-        else:
-            store = config.ConfigObjStore.from_string(
-                content, self.get_transport(), name)
-        return store
-
-    def test_set_option_in_empty_file(self):
-        store = self.get_store('foo.conf')
-        section = store.get_mutable_section(None)
-        section.set('foo', 'bar')
-        store.save()
-        self.assertFileEqual('foo = bar\n', 'foo.conf')
-
-    def test_set_option_in_default_section(self):
-        store = self.get_store('foo.conf', '')
-        section = store.get_mutable_section(None)
-        section.set('foo', 'bar')
-        store.save()
-        self.assertFileEqual('foo = bar\n', 'foo.conf')
-
-    def test_set_option_in_named_section(self):
-        store = self.get_store('foo.conf', '')
-        section = store.get_mutable_section('baz')
-        section.set('foo', 'bar')
-        store.save()
-        self.assertFileEqual('[baz]\nfoo = bar\n', 'foo.conf')
-
-
 class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
 
     def setUp(self):



More information about the bazaar-commits mailing list