Skip to content
Snippets Groups Projects
Commit 3fba0bcde67a authored by Georges Racinet's avatar Georges Racinet
Browse files

wsgi: separated a tested method to load repository

This is about as much we can test without using a WSGI request
object (fake or real)
parent ffb7ae8c62e3
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -2,6 +2,9 @@
from mercurial import (
error,
)
from mercurial.hgweb.common import (
ErrorResponse,
)
from ..wsgi import HgServe
from ..testhelpers import (
......@@ -5,6 +8,7 @@
from ..wsgi import HgServe
from ..testhelpers import (
LocalRepoWrapper,
make_ui,
)
......@@ -58,3 +62,27 @@
env = {'HTTP_X_HEPTAPOD_PROJECT_ID': '18'}
hgs.apply_heptapod_headers(env)
assert env.get('HEPTAPOD_PROJECT_ID') == '18'
def test_load_repo(tmpdir):
wrapper = LocalRepoWrapper.init(
tmpdir.join('group').ensure(dir=True)
.join('proj.hg'))
wrapper.write_commit('foo', message='Foo')
hgs = make_hgserve(tmpdir)
hgs.ui.setconfig('heptapod', 'canary', 'yellow')
repo = hgs.load_repo('group/proj.hg')
assert repo[0].description() == 'Foo'
repo.ui.setconfig('heptapod', 'canary', 'red')
assert repo.ui.config('heptapod', 'canary') == 'red'
assert hgs.ui.config('heptapod', 'canary') == 'yellow'
def test_load_repo_not_found(tmpdir):
hgs = make_hgserve(tmpdir)
with pytest.raises(ErrorResponse) as exc_info:
hgs.load_repo('does/not/exist')
assert exc_info.value.args[0] == 'Not Found'
......@@ -128,6 +128,17 @@
# instances instead of every request.
gc.collect()
def load_repo(self, uri_path):
repo_path = os.path.join(self.repos_root, uri_path)
if not os.path.isdir(os.path.join(repo_path, '.hg')):
# hg.repository() would raise a RepoError which is
# not qualified enough to distinguish it cleanly (just
# the message)
raise ErrorResponse(HTTP_NOT_FOUND, "Not Found")
logger.info("loading repo at %r", repo_path)
# ensure caller gets private copy of ui
return hg.repository(self.ui.copy(), repo_path)
def _runwsgi(self, req, res):
try:
csp = cspvalues(self.ui)[0]
......@@ -135,12 +146,6 @@
res.headers['Content-Security-Policy'] = csp
uri_path = req.dispatchpath.strip('/')
repo_path = os.path.join(self.repos_root, uri_path)
if not os.path.isdir(os.path.join(repo_path, '.hg')):
# hg.repository() would raise a RepoError which is
# not qualified enough to distinguish it cleanly (just
# the message)
raise ErrorResponse(HTTP_NOT_FOUND, "Not Found")
# Re-parse the WSGI environment to take into account our
# repository path component.
uenv = req.rawenv
......@@ -154,9 +159,7 @@
# tracking can get confused.
bodyfh=req.bodyfh)
try:
logger.info("loading repo at %r", repo_path)
# ensure caller gets private copy of ui
repo = hg.repository(self.ui.copy(), repo_path)
repo = self.load_repo(uri_path)
return hgweb_mod.hgweb(repo).run_wsgi(req, res)
except IOError as inst:
msg = encoding.strtolocal(inst.strerror)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment