Rev 4299: Reasonable unit test coverage for initialize_on_transport_ex. in http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

Robert Collins robertc at robertcollins.net
Thu Apr 23 07:07:51 BST 2009


At http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

------------------------------------------------------------
revno: 4299
revision-id: robertc at robertcollins.net-20090423060744-ile6rixv4jqyqpts
parent: robertc at robertcollins.net-20090422233959-0xbh2uc4kzc5uuss
committer: Robert Collins <robertc at robertcollins.net>
branch nick: push.roundtrips
timestamp: Thu 2009-04-23 16:07:44 +1000
message:
  Reasonable unit test coverage for initialize_on_transport_ex.
=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py	2009-04-22 23:39:59 +0000
+++ b/bzrlib/bzrdir.py	2009-04-23 06:07:44 +0000
@@ -44,6 +44,7 @@
     lockdir,
     osutils,
     remote,
+    repository,
     revision as _mod_revision,
     ui,
     urlutils,
@@ -1824,6 +1825,10 @@
     def initialize(self, url, possible_transports=None):
         """Create a bzr control dir at this url and return an opened copy.
 
+        While not deprecated, this method is very specific and its use will
+        lead to many round trips to setup a working environment. See
+        initialize_on_transport_ex for a [nearly] all-in-one method.
+
         Subclasses should typically override initialize_on_transport
         instead of this method.
         """
@@ -1850,7 +1855,7 @@
 
     def initialize_on_transport_ex(self, transport, use_existing_dir=False,
         create_prefix=False, force_new_repo=False, stacked_on=None,
-        stack_on_pwd=None, repo_format_name=None, make_working_trees=False,
+        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
         shared_repo=False):
         """Create this format on transport.
 
@@ -1868,13 +1873,14 @@
             the repo_format_name is used to select the format of repository to
             create.
         :param make_working_trees: Control the setting of make_working_trees
-            for a new shared repository when one is made.
+            for a new shared repository when one is made. None to use whatever
+            default the format has.
         :param shared_repo: Control whether made repositories are shared or
             not.
-        :return: repo, bzrdir, require_stacking. repo is None if none was
-            created or found, bzrdir is always valid. require_stacking is the
-            result of examining the stacked_on parameter and any stacking
-            policy found for the target.
+        :return: repo, bzrdir, require_stacking, repository_policy. repo is
+            None if none was created or found, bzrdir is always valid.
+            require_stacking is the result of examining the stacked_on
+            parameter and any stacking policy found for the target.
         """
         # XXX: Refactor the create_prefix/no_create_prefix code into a
         #      common helper function
@@ -1900,8 +1906,16 @@
         # Now the target directory exists, but doesn't have a .bzr
         # directory. So we need to create it, along with any work to create
         # all of the dependent branches, etc.
+
         result = self.initialize_on_transport(transport)
         if repo_format_name:
+            try:
+                # use a custom format
+                result._format.repository_format = \
+                    repository.network_format_registry.get(repo_format_name)
+            except AttributeError:
+                # The format didn't permit it to be set.
+                pass
             # A repository is desired, either in-place or shared.
             repository_policy = result.determine_repository_policy(
                 force_new_repo, stacked_on, stack_on_pwd,
@@ -2133,7 +2147,32 @@
     repository_format = property(__return_repository_format)
 
 
-class BzrDirFormat5(BzrDirFormat):
+class BzrDirFormatAllInOne(BzrDirFormat):
+    """Common class for formats before meta-dirs."""
+
+    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
+        create_prefix=False, force_new_repo=False, stacked_on=None,
+        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
+        shared_repo=False):
+        """See BzrDirFormat.initialize_on_transport_ex."""
+        require_stacking = (stacked_on is not None)
+        # Format 5 cannot stack, but we've been asked do - actually init
+        # a Meta1Dir
+        if require_stacking:
+            format = BzrDirMetaFormat1()
+            return format.initialize_on_transport_ex(transport,
+                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
+                force_new_repo=force_new_repo, stacked_on=stacked_on,
+                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
+                make_working_trees=make_working_trees, shared_repo=shared_repo)
+        return BzrDirFormat.initialize_on_transport_ex(self, transport,
+            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
+            force_new_repo=force_new_repo, stacked_on=stacked_on,
+            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
+            make_working_trees=make_working_trees, shared_repo=shared_repo)
+
+
+class BzrDirFormat5(BzrDirFormatAllInOne):
     """Bzr control format 5.
 
     This format is a combined format for working tree, branch and repository.
@@ -2194,7 +2233,7 @@
     repository_format = property(__return_repository_format)
 
 
-class BzrDirFormat6(BzrDirFormat):
+class BzrDirFormat6(BzrDirFormatAllInOne):
     """Bzr control format 6.
 
     This format is a combined format for working tree, branch and repository.

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2009-04-15 04:45:06 +0000
+++ b/bzrlib/remote.py	2009-04-23 06:07:44 +0000
@@ -491,6 +491,8 @@
         # 1) get the network name to use.
         if self._custom_format:
             network_name = self._custom_format.network_name()
+        elif self._network_name:
+            network_name = self._network_name
         else:
             # Select the current bzrlib default and ask for that.
             reference_bzrdir_format = bzrdir.format_registry.get('default')()

=== modified file 'bzrlib/tests/bzrdir_implementations/test_bzrdir.py'
--- a/bzrlib/tests/bzrdir_implementations/test_bzrdir.py	2009-04-15 02:05:27 +0000
+++ b/bzrlib/tests/bzrdir_implementations/test_bzrdir.py	2009-04-23 06:07:44 +0000
@@ -1163,6 +1163,133 @@
                          opened_dir._format)
         self.failUnless(isinstance(opened_dir, bzrdir.BzrDir))
 
+    def test_format_initialize_on_transport_ex(self):
+        t = self.get_transport('dir')
+        self.assertInitializeEx(t)
+
+    def test_format_initialize_on_transport_ex_use_existing_dir_True(self):
+        t = self.get_transport('dir')
+        t.ensure_base()
+        self.assertInitializeEx(t, use_existing_dir=True)
+
+    def test_format_initialize_on_transport_ex_use_existing_dir_False(self):
+        if not self.bzrdir_format.is_supported():
+            # Not initializable - not a failure either.
+            return
+        t = self.get_transport('dir')
+        t.ensure_base()
+        self.assertRaises(errors.FileExists, self.assertInitializeEx, t,
+            use_existing_dir=False)
+
+    def test_format_initialize_on_transport_ex_create_prefix_True(self):
+        t = self.get_transport('missing/dir')
+        self.assertInitializeEx(t, create_prefix=True)
+
+    def test_format_initialize_on_transport_ex_create_prefix_False(self):
+        if not self.bzrdir_format.is_supported():
+            # Not initializable - not a failure either.
+            return
+        t = self.get_transport('missing/dir')
+        self.assertRaises(errors.NoSuchFile, self.assertInitializeEx, t,
+            create_prefix=False)
+
+    def test_format_initialize_on_transport_ex_force_new_repo_True(self):
+        t = self.get_transport('repo')
+        repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
+        repo_name = repo_fmt.repository_format.network_name()
+        repo = repo_fmt.initialize_on_transport_ex(t,
+            repo_format_name=repo_name, shared_repo=True)[0]
+        made_repo, control = self.assertInitializeEx(t.clone('branch'),
+            force_new_repo=True, repo_format_name=repo_name)
+        if control is None:
+            # uninitialisable format
+            return
+        self.assertNotEqual(repo.bzrdir.root_transport.base,
+            made_repo.bzrdir.root_transport.base)
+
+    def test_format_initialize_on_transport_ex_force_new_repo_False(self):
+        t = self.get_transport('repo')
+        repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
+        repo_name = repo_fmt.repository_format.network_name()
+        repo = repo_fmt.initialize_on_transport_ex(t,
+            repo_format_name=repo_name, shared_repo=True)[0]
+        made_repo, control = self.assertInitializeEx(t.clone('branch'),
+            force_new_repo=False, repo_format_name=repo_name)
+        if control is None:
+            # uninitialisable format
+            return
+        if not isinstance(control._format, (bzrdir.BzrDirFormat5,
+            bzrdir.BzrDirFormat6,)):
+            self.assertEqual(repo.bzrdir.root_transport.base,
+                made_repo.bzrdir.root_transport.base)
+
+    def test_format_initialize_on_transport_ex_stacked_on(self):
+        # trunk is a stackable format
+        trunk = self.make_branch('trunk', format='1.9')
+        t = self.get_transport('stacked')
+        old_fmt = bzrdir.format_registry.make_bzrdir('pack-0.92')
+        repo_name = old_fmt.repository_format.network_name()
+        # Should end up with a 1.9 format (stackable)
+        repo, control = self.assertInitializeEx(t, need_meta=True,
+            repo_format_name=repo_name, stacked_on='../trunk', stack_on_pwd=t.base)
+        if control is None:
+            # uninitialisable format
+            return
+        self.assertLength(1, repo._fallback_repositories)
+
+    def test_format_initialize_on_transport_ex_repo_fmt_name_None(self):
+        t = self.get_transport('dir')
+        repo, control = self.assertInitializeEx(t)
+        self.assertEqual(None, repo)
+
+    def test_format_initialize_on_transport_ex_repo_fmt_name_followed(self):
+        t = self.get_transport('dir')
+        # 1.6 is likely to neve be default
+        fmt = bzrdir.format_registry.make_bzrdir('1.6')
+        repo_name = fmt.repository_format.network_name()
+        repo, control = self.assertInitializeEx(t, repo_format_name=repo_name)
+        if control is None:
+            # uninitialisable format
+            return
+        if isinstance(self.bzrdir_format, (bzrdir.BzrDirFormat5,
+            bzrdir.BzrDirFormat6)):
+            # must stay with the all-in-one-format.
+            repo_name = self.bzrdir_format.network_name()
+        self.assertEqual(repo_name, repo._format.network_name())
+
+    def assertInitializeEx(self, t, need_meta=False, **kwargs):
+        """Execute initialize_on_transport_ex and check it succeeded correctly.
+
+        :param t: The transport to initialize on.
+        :param **kwargs: Additional arguments to pass to
+            initialize_on_transport_ex.
+        :return: the resulting repo, control dir tuple.
+        """
+        if not self.bzrdir_format.is_supported():
+            # Not initializable - not a failure either.
+            return None, None
+        repo, control, require_stacking, repo_policy = \
+            self.bzrdir_format.initialize_on_transport_ex(t, **kwargs)
+        self.assertIsInstance(control, bzrdir.BzrDir)
+        opened = bzrdir.BzrDir.open(t.base)
+        expected_format = self.bzrdir_format
+        if isinstance(expected_format, bzrdir.RemoteBzrDirFormat):
+            self.assertIsInstance(control, RemoteBzrDir)
+        else:
+            if need_meta and isinstance(expected_format, (bzrdir.BzrDirFormat5,
+                bzrdir.BzrDirFormat6)):
+                # Pre-metadir formats change when we are making something that
+                # needs a metaformat, because clone is used for push.
+                expected_format = bzrdir.BzrDirMetaFormat1()
+            self.assertEqual(control._format.network_name(),
+                expected_format.network_name())
+            # Current RemoteBzrDirFormat's do not reliably get network_name
+            # set, so only check in the off-case.
+            self.assertEqual(control._format.network_name(),
+                opened._format.network_name())
+        self.assertEqual(control.__class__, opened.__class__)
+        return repo, control
+
     def test_format_network_name(self):
         # All control formats must have a network name.
         dir = self.make_bzrdir('.')




More information about the bazaar-commits mailing list