Mercurial > hgsubversion
view tests/test_hooks.py @ 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 | 20850abfb91a |
children | 4f1461428334 |
line wrap: on
line source
import sys import test_util import unittest from mercurial import hg from mercurial import commands class TestHooks(test_util.TestBase): def setUp(self): super(TestHooks, self).setUp() def _loadupdate(self, fixture_name, *args, **kwargs): kwargs = kwargs.copy() kwargs.update(noupdate=False) repo, repo_path = self.load_and_fetch(fixture_name, *args, **kwargs) return repo, repo_path def test_updatemetahook(self): repo, repo_path = self._loadupdate('single_rev.svndump') state = repo.parents() self.add_svn_rev(repo_path, {'trunk/alpha': 'Changed'}) commands.pull(self.repo.ui, self.repo) # Clone to a new repository and add a hook new_wc_path = "%s-2" % self.wc_path commands.clone(self.repo.ui, self.wc_path, new_wc_path) newrepo = hg.repository(test_util.testui(), new_wc_path) newrepo.ui.setconfig('hooks', 'changegroup.meta', 'python:hgsubversion.hooks.updatemeta.hook') # Commit a rev that should trigger svn meta update self.add_svn_rev(repo_path, {'trunk/alpha': 'Changed Again'}) commands.pull(self.repo.ui, self.repo) self.called = False import hgsubversion.svncommands oldupdatemeta = hgsubversion.svncommands.updatemeta def _updatemeta(ui, repo, args=[]): self.called = True hgsubversion.svncommands.updatemeta = _updatemeta # Pull and make sure our updatemeta function gets called commands.pull(newrepo.ui, newrepo) hgsubversion.svncommands.updatemeta = oldupdatemeta self.assertTrue(self.called)