Patchwork [1,of,9] bugzilla: do not load style file if template is specified (BC)

login
register
mail settings
Submitter Yuya Nishihara
Date April 15, 2016, 1:15 p.m.
Message ID <f57c8f4f61459d76e62c.1460726140@mimosa>
Download mbox | patch
Permalink /patch/14640/
State Accepted
Headers show

Comments

Yuya Nishihara - April 15, 2016, 1:15 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1459777714 -32400
#      Mon Apr 04 22:48:34 2016 +0900
# Node ID f57c8f4f61459d76e62c72412e4a5c0eee28c947
# Parent  ecfb768ecf5b62f8a40291dfe8e688a55bb09d55
bugzilla: do not load style file if template is specified (BC)

This prepares for the API change to support template aliases. I'm going to
extract a factory function of templater that reads a map file:

  # original
  templater(mapfile, ..., cache, ...)
  # new
  templater.frommapfile(mapfile, ...)  # read mapfile to build cache/map
  templater(..., cache, ...)           # use specified cache (= map elements)

This will make it clear to isolate stock styles (i.e. map files) from user
aliases. Template aliases should be applied to command arguments and templates
in hgrc, but not to map files. Otherwise, our stock styles and web templates
could be modified unintentionally.

This patch makes sure that either "tmpl" or "mapfile" is exclusively set. It's
theoretically a behavior change, since you could put new keywords in template
by defining them in a map file before:

  # mapfile
  foo = "{rev}"
  # hgrc
  [bugzilla]
  style = mapfile
  template = {foo}

But the old behavior would be a bug because bugzilla.template is documented
as "overrides style if specified". Also, common log-like templates and
formatter doesn't allow using mapfile-keywords in a separate template. So
I decided to make a BC.

Since there was no test for the bugzilla extension, this adds new test that
covers style/template output.

Patch

diff --git a/hgext/bugzilla.py b/hgext/bugzilla.py
--- a/hgext/bugzilla.py
+++ b/hgext/bugzilla.py
@@ -886,8 +886,10 @@  class bugzilla(object):
                 count -= 1
             return root
 
-        mapfile = self.ui.config('bugzilla', 'style')
+        mapfile = None
         tmpl = self.ui.config('bugzilla', 'template')
+        if not tmpl:
+            mapfile = self.ui.config('bugzilla', 'style')
         if not mapfile and not tmpl:
             tmpl = _('changeset {node|short} in repo {root} refers '
                      'to bug {bug}.\ndetails:\n\t{desc|tabindent}')
diff --git a/tests/test-bugzilla.t b/tests/test-bugzilla.t
new file mode 100644
--- /dev/null
+++ b/tests/test-bugzilla.t
@@ -0,0 +1,97 @@ 
+mock bugzilla driver for testing template output:
+
+  $ cat <<EOF > bzmock.py
+  > from __future__ import absolute_import
+  > from mercurial import extensions
+  > 
+  > def extsetup(ui):
+  >     bugzilla = extensions.find('bugzilla')
+  >     class bzmock(bugzilla.bzaccess):
+  >         def __init__(self, ui):
+  >             super(bzmock, self).__init__(ui)
+  >             self._logfile = ui.config('bugzilla', 'mocklog')
+  >         def updatebug(self, bugid, newstate, text, committer):
+  >             with open(self._logfile, 'a') as f:
+  >                 f.write('update bugid=%r, newstate=%r, committer=%r\n'
+  >                         % (bugid, newstate, committer))
+  >                 f.write('----\n' + text + '\n----\n')
+  >         def notify(self, bugs, committer):
+  >             with open(self._logfile, 'a') as f:
+  >                 f.write('notify bugs=%r, committer=%r\n'
+  >                         % (bugs, committer))
+  >     bugzilla.bugzilla._versions['mock'] = bzmock
+  > EOF
+
+set up mock repository:
+
+  $ hg init mockremote
+  $ cat <<EOF > mockremote/.hg/hgrc
+  > [extensions]
+  > bugzilla =
+  > bzmock = $TESTTMP/bzmock.py
+  > 
+  > [bugzilla]
+  > version = mock
+  > mocklog = $TESTTMP/bzmock.log
+  > 
+  > [hooks]
+  > incoming.bugzilla = python:hgext.bugzilla.hook
+  > 
+  > [web]
+  > baseurl=http://example.org/hg
+  > 
+  > %include $TESTTMP/bzstyle.hgrc
+  > EOF
+
+  $ hg clone -q mockremote mocklocal
+
+push with default template:
+
+  $ echo '[bugzilla]' > bzstyle.hgrc
+  $ echo foo > mocklocal/foo
+  $ hg ci -R mocklocal -Aqm 'Fixes bug 123'
+  $ hg -R mocklocal push -q
+  $ cat bzmock.log && rm bzmock.log
+  update bugid=123, newstate={}, committer='test'
+  ----
+  changeset 7875a8342c6f in repo $TESTTMP/mockremote refers to bug 123.
+  details:
+  	Fixes bug 123
+  ----
+  notify bugs={123: {}}, committer='test'
+
+push with style:
+
+  $ cat <<EOF > bzstyle.map
+  > changeset = "{node|short} refers to bug {bug}."
+  > EOF
+  $ echo "style = $TESTTMP/bzstyle.map" >> bzstyle.hgrc
+  $ echo foo >> mocklocal/foo
+  $ hg ci -R mocklocal -qm 'Fixes bug 456'
+  $ hg -R mocklocal push -q
+  $ cat bzmock.log && rm bzmock.log
+  update bugid=456, newstate={}, committer='test'
+  ----
+  2808b172464b refers to bug 456.
+  ----
+  notify bugs={456: {}}, committer='test'
+
+push with template (overrides style):
+
+  $ cat <<EOF >> bzstyle.hgrc
+  > template = Changeset {node|short} in {root|basename}.
+  >            {hgweb}/rev/{node|short}\n
+  >            {desc}
+  > EOF
+  $ echo foo >> mocklocal/foo
+  $ hg ci -R mocklocal -qm 'Fixes bug 789'
+  $ hg -R mocklocal push -q
+  $ cat bzmock.log && rm bzmock.log
+  update bugid=789, newstate={}, committer='test'
+  ----
+  Changeset a770f3e409f2 in mockremote.
+  http://example.org/hg/rev/a770f3e409f2
+  
+  Fixes bug 789
+  ----
+  notify bugs={789: {}}, committer='test'