changeset 1101:4a92eb1484ba

layouts: fix crash when importing hgsubversion itself (issue #402) Mercurial extensions are a bit weird: they aren't normally in sys.path, so you can't assume that "import hgsubversion" works. Luckily, Mercurial sneaks a little treat into sys.modules so that "import hgext_hgsubversion" does work. In fact, to get things working *as a Mercurial extension*, all that's needed is that trivial change to two import lines, in layouts/detect.py and layouts/standard.py. Unfortunately, hgsubversion is also imported as a Python module, in its own test suite. In that context, there is no "hgext_" trick -- unless we do it in ourselves, which I've done in TestBase.setUp(). That would work fine ... except that test_util imports from hgsubversion, which ends up importing hgsubversion.layouts.{detect,standard}, which want the "hgext_" trick to work. But it hasn't been done yet when we're still importing; it doesn't happen until setUp() runs. So make those two imports happen late, in the functions that need them. Incidentally, this is only necessary to support Mercurial <= 2.7. Mercurial got a bit smarter in 2.8: http://selenic.com/repo/hg/rev/621a26eb3a99
author Greg Ward <greg@gerg.ca>
date Thu, 02 Jan 2014 21:53:39 -0500
parents 91cf81a36236
children 7635d30effa7
files hgsubversion/layouts/detect.py hgsubversion/layouts/standard.py tests/test_util.py
diffstat 3 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/layouts/detect.py
+++ b/hgsubversion/layouts/detect.py
@@ -10,8 +10,6 @@ import os.path
 
 from mercurial import util as hgutil
 
-from hgsubversion import svnwrap
-
 import __init__ as layouts
 
 def layout_from_subversion(svn, revision=None, ui=None):
@@ -26,6 +24,8 @@ def layout_from_subversion(svn, revision
     passed, if any.
 
     """
+    # import late to avoid trouble when running the test suite
+    from hgext_hgsubversion import svnwrap
 
     try:
         rootlist = svn.list_dir('', revision=revision)
--- a/hgsubversion/layouts/standard.py
+++ b/hgsubversion/layouts/standard.py
@@ -2,7 +2,6 @@ import os.path
 import pickle
 
 import base
-import hgsubversion.util as util
 
 class StandardLayout(base.BaseLayout):
     """The standard trunk, branches, tags layout"""
@@ -57,6 +56,9 @@ class StandardLayout(base.BaseLayout):
         return '%s/%s' % (subdir or '', branchpath)
 
     def taglocations(self, meta_data_dir):
+        # import late to avoid trouble when running the test suite
+        from hgext_hgsubversion import util
+
         if self._tag_locations is None:
 
             tag_locations_file = os.path.join(meta_data_dir, 'tag_locations')
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -434,6 +434,8 @@ class TestBase(unittest.TestCase):
 
     def setUp(self):
         _verify_our_modules()
+        if 'hgsubversion' in sys.modules:
+            sys.modules['hgext_hgsubversion'] = sys.modules['hgsubversion']
 
         # the Python 2.7 default of 640 is obnoxiously low
         self.maxDiff = 4096