Commits

Sebastian Wiesner committed 2f72c95

Added tests for issuetracker and use tox for running them

  • Participants
  • Parent commits f2bb3c9

Comments (0)

Files changed (3)

issuetracker/MANIFEST.in

 include README
 include LICENSE
 include CHANGES.*
+include test_issuetracker.py
 recursive-include sphinxcontrib *.css
 recursive-include doc *.rst *.py Makefile
 prune doc/_build

issuetracker/test_issuetracker.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import re
+
+import py.test
+from mock import Mock
+from docutils import nodes
+from sphinx.addnodes import pending_xref
+
+from sphinxcontrib import issuetracker
+
+
+def pytest_funcarg__issue_info(request):
+    return dict(uri='http://example.com/issues/10', closed=False)
+
+
+def pytest_funcarg__get_issue_information(request):
+    get_issue_information = Mock()
+    info = request.getfuncargvalue('issue_info')
+    get_issue_information.return_value = info
+    return get_issue_information
+
+
+def pytest_funcarg__resolver(request):
+    return issuetracker.make_issue_reference_resolver(
+        request.getfuncargvalue('get_issue_information'))
+
+
+def pytest_funcarg__config(request):
+    config = Mock()
+    config.project = 'issuetracker'
+    config.issuetracker = 'spamtracker'
+    config.issuetracker_user = 'foobar'
+    config.issuetracker_project = None
+    config.issuetracker_issue_pattern = re.compile(r'#(\d+)')
+    return config
+
+def pytest_funcarg__app(request):
+    app = Mock()
+    app.config = request.getfuncargvalue('config')
+    return app
+
+
+def pytest_funcarg__env(request):
+    env = Mock()
+    env.config = request.getfuncargvalue('config')
+    return env
+
+
+def pytest_funcarg__node(request):
+    node = pending_xref()
+    node['reftype'] = 'issue'
+    node['reftarget'] = '10'
+    node.append(nodes.Text('#10'))
+    return node
+
+
+def pytest_funcarg__doc(request):
+    doc = nodes.paragraph()
+    doc.append(nodes.Text('foo #1 bar'))
+    em = nodes.emphasis()
+    em.append(nodes.Text('see #2 if you are #abc brave'))
+    doc.append(em)
+    doc.settings = Mock()
+    doc.settings.language_code = ''
+    doc.settings.env = request.getfuncargvalue('env')
+    return doc
+
+
+def test_make_isssue_reference_resolver_invalid_reftype(
+    app, env, resolver, node):
+    node['reftype'] = 'spam'
+    assert resolver(app, env, node, node[0]) is None
+
+
+def test_make_issue_reference_resolver_no_issue(
+    app, env, resolver, node, get_issue_information):
+    get_issue_information.return_value = None
+    assert resolver(app, env, node, node[0]) is None
+
+
+def test_make_issue_reference_resolver_no_uri(
+    app, env, resolver, node, issue_info):
+    del issue_info['uri']
+    assert resolver(app, env, node, node[0]) is None
+
+
+def test_make_issue_reference_resolver_open_issue(
+    app, env, resolver, node, issue_info):
+    contnode = node[0]
+    node = resolver(app, env, node, node[0])
+    assert isinstance(node, nodes.reference)
+    assert node[0] is contnode
+    assert node['refuri'] == issue_info['uri']
+    assert 'reference-issue' in node['classes']
+    assert 'issue-closed' not in node['classes']
+
+
+def test_make_issue_reference_resolver_closed_issue(
+    app, env, resolver, node, issue_info):
+    issue_info['closed'] = True
+    contnode = node[0]
+    node = resolver(app, env, node, node[0])
+    assert isinstance(node, nodes.reference)
+    assert node[0] is contnode
+    assert node['refuri'] == issue_info['uri']
+    assert 'reference-issue' in node['classes']
+    assert 'issue-closed' in node['classes']
+
+
+def test_get_issue_information_called(
+    app, env, resolver, node, get_issue_information):
+    resolver(app, env, node, node[0])
+    get_issue_information.assert_called_with(
+        'issuetracker', 'foobar', '10', env)
+    app.config.issuetracker_project = 'spam with eggs'
+    resolver(app, env, node, node[0])
+    get_issue_information.assert_called_with(
+        'spam with eggs', 'foobar', '10', env)
+
+
+def test_builtin_issue_trackers():
+    for tracker in ('github', 'bitbucket',
+                    'launchpad', 'google code'):
+        assert tracker in issuetracker.BUILTIN_ISSUE_TRACKERS
+
+
+def _assert_text(node, text):
+    assert isinstance(node, nodes.Text)
+    assert node.astext() == text
+
+
+def _assert_xref(node, target):
+    assert isinstance(node, pending_xref)
+    _assert_text(node[0], '#%s' % target)
+    assert node['reftype'] == 'issue'
+    assert node['reftarget'] == target
+
+
+def test_issues_references(doc):
+    transformer = issuetracker.IssuesReferences(doc)
+    transformer.apply()
+    assert isinstance(doc, nodes.paragraph)
+    assert doc.astext() == 'foo #1 barsee #2 if you are #abc brave'
+    _assert_text(doc[0], 'foo ')
+    _assert_xref(doc[1], '1')
+    _assert_text(doc[2], ' bar')
+    em = doc[3]
+    assert isinstance(em, nodes.emphasis)
+    _assert_text(em[0], 'see ')
+    _assert_xref(em[1], '2')
+    _assert_text(em[2], ' if you are #abc brave')
+
+
+def test_auto_connect_builtin_issue_resolvers_known_tracker(app):
+    app.config.issuetracker = 'bitbucket'
+    issuetracker.auto_connect_builtin_issue_resolvers(app)
+    assert app.connect.called
+
+
+def test_auto_connect_builtin_issue_resolvers_unknown_tracker(app):
+    app.config.issuetracker = 'spamtracker'
+    with py.test.raises(KeyError):
+        issuetracker.auto_connect_builtin_issue_resolvers(app)
+
+
+def test_auto_connect_builtin_issue_resolvers_no_tracker(app):
+    app.config.issuetracker = None
+    issuetracker.auto_connect_builtin_issue_resolvers(app)
+    assert not app.connect.called
+
+
+def test_add_stylesheet(app):
+    issuetracker.add_stylesheet(app)
+    app.add_stylesheet.assert_called_with('issuetracker.css')
+
+
+def test_setup(app):
+    issuetracker.setup(app)
+    app.require_sphinx.assert_called_with('1.0')
+    app.add_transform.assert_called_with(issuetracker.IssuesReferences)
+    app.connect.call_args_list = [
+        (('builder-inited',
+          issuetracker.auto_connect_builtin_issue_resolvers), {}),
+        (('builder-inited', issuetracker.add_stylesheet), {}),
+        (('build-finished', issuetracker.copy_stylesheet), {})]
+    app.add_config_value.call_args_list = [
+        (('issuetracker_issue_pattern', re.compile(r'#(\d+)'), 'env'), {}),
+        (('issuetracker_user', None, 'env'), {}),
+        (('issuetracker_project', None, 'env'), {}),
+        (('issuetracker', None, 'env'), {})]
+
+
+def main():
+    import py
+    py.cmdline.pytest()
+
+
+if __name__ == '__main__':
+    main()

issuetracker/tox.ini

+[tox]
+envlist=python,doc
+
+[testenv:python]
+deps=
+    mock
+    py
+commands=
+    py.test --junitxml={envlogdir}/tests.xml []
+
+[testenv:doc]
+deps=
+    sphinx
+    lxml
+    sphinxcontrib-issuetracker
+commands=
+    sphinx-build -W -b linkcheck -d {envtmpdir}/doctrees doc {envtmpdir}/linkcheck
+    sphinx-build -W -b html -d {envtmpdir}/doctrees doc {envtmpdir}/html