1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
# -*- coding: utf-8 -*-
# Copyright (c) 2011, Sebastian Wiesner <lunaryorn@googlemail.com>
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import (print_function, division, unicode_literals,
absolute_import)
import pytest
from sphinxcontrib.issuetracker import Issue, TrackerConfig
try:
import debianbts
except ImportError:
debianbts = None
try:
import launchpadlib
except ImportError:
launchpadlib = None
def pytest_generate_tests(metafunc):
"""
Generate tests.
Generate tests for all test functions with an ``issue`` argument by adding
calls for each tests in testname in the ``issues`` attribute of the test
class, the function is defined in. The ``issues`` attribute is expected to
be a mapping from test names to Issue objects which this test is expected
to resolve to, or with issue ids as string, if the test is expected to be
unable to resolve the issue.
"""
if 'issue' in metafunc.funcargnames:
for testname in sorted(metafunc.cls.issues):
metafunc.addcall(id=testname, param=testname)
def pytest_funcarg__testname(request):
"""
The testname as string, or ``None``, if no testname is known.
This is the parameter added by the test generation hook, or ``None`` if no
parameter was set, because test generation didn't add a call for this test.
"""
return getattr(request, 'param', None)
def pytest_funcarg__tracker(request):
"""
The tracker name as string, or ``None``, if no tracker is known.
The tracker name is taken from the ``name`` attribute of the class this
test is defined in. If the test isn't defined in a class, ``None`` is
returned.
"""
if not request.cls:
return None
return request.cls.name
def pytest_funcarg__tracker_config(request):
"""
The tracker configuration as ``TrackerConfig`` object, or ``None``, if
there is no tracker configuration.
Tracker configuration is taken from the class this test is defined in. If
there is a ``testname`` for this test, the tracker config is taken from the
``tracker_config`` map defined in the class, falling back to the
``default_tracker_config`` defined in the class. If there is no
``testname``, the ``default_tracker_config`` is used right away. If the
test isn't defined in a class, ``None`` is returned.
"""
cls = request.cls
if cls is None:
return None
testname = getattr(request, 'param', None)
if testname is None:
return cls.default_tracker_config
else:
return cls.tracker_config.get(testname, cls.default_tracker_config)
def pytest_funcarg__confoverrides(request):
"""
Confoverrides for this test as dictionary.
Provides confoverrides for this test that include the tracker name and
tracker configuration (as returned by the ``tracker`` and
``tracker_config`` funcargs). The ``expandtitle`` setting is enabled. The
global ``confoverrides`` are included, and overwrite any configuration key
set in this funcarg.
"""
# configure tracker and enable title expansion to test the title retrieval
# of builtin trackers, too
tracker = request.getfuncargvalue('tracker')
confoverrides = dict(issuetracker=tracker, issuetracker_expandtitle=True)
tracker_config = request.getfuncargvalue('tracker_config')
if tracker_config:
# bring tracker configuration in
confoverrides.update(issuetracker_project=tracker_config.project,
issuetracker_url=tracker_config.url)
# bring test-class specific overrides in
if request.cls:
confoverrides.update(request.cls.confoverrides)
# add overrides from the test itself
confoverrides.update(request.getfuncargvalue('confoverrides'))
return confoverrides
def pytest_funcarg__issue_id(request):
"""
The issue id of this test as string, or ``None``, if this test doesn't have
a ``testname``.
The issue id is taken from the issue defined in the ``issues`` attribute of
the class this test is defined in.
"""
testname = request.getfuncargvalue('testname')
if not testname:
return None
issue = request.cls.issues[testname]
if isinstance(issue, basestring):
return issue
else:
return issue.id
def pytest_funcarg__issue(request):
"""
The issue object for this test, or ``None``, if the test is expected to be
unable to resolve the issue.
The issue id is taken from the issue defined in the ``issues`` attribute of
the class this test is defined in.
"""
testname = request.getfuncargvalue('testname')
issue = request.cls.issues[testname]
if isinstance(issue, basestring):
return None
else:
return issue
class TrackerTest(object):
"""
Base class for tests for builtin issue trackers.
This class defines a single test which tests issue lookup.
"""
#: the name of the issuetracker to use in this test
name = None
#: the default tracker configuration
default_tracker_config = None
#: test-specific tracker configuration
tracker_config = {}
#: issues to test. the key is a descriptive name for the issue
#: (e.g. resolved, invalid or something the like of), the value is either a
#: plain string, in which case the test is expected to not resolve the
#: issue, or an ``Issue`` object, in which case the test is expected to
#: resolve the issue id to excately this issue object
issues = {}
#: confoverrides to use for tests defined in this class
confoverrides = {}
def test_lookup(self, app, doctree, issue_id, issue):
"""
Test that this tracker correctly looks up an issue.
"""
assert app.env.issuetracker_cache == {issue_id: issue}
if not issue:
assert not doctree.is_('reference')
else:
pytest.assert_issue_reference(doctree, issue, title=True)
class ScopedProjectTrackerTest(TrackerTest):
"""
Base class for tests for issue trackers which use scoped project names
including the user name.
Defines an additional tests which tests for exceptions raised if the
username is missing.
"""
@pytest.mark.with_content('#10')
@pytest.mark.confoverrides(issuetracker_project='eggs')
def test_project_missing_username(self, app):
"""
Test that a project name without an username fails with a ValueError.
"""
with pytest.raises(ValueError) as excinfo:
app.build()
assert str(excinfo.value) == \
'username missing in project name: eggs'
class TestBitBucket(ScopedProjectTrackerTest):
name = 'bitbucket'
default_tracker_config = TrackerConfig('birkenfeld/sphinx')
tracker_config = {'no project': TrackerConfig('lunar/foobar')}
SPHINX_URL = 'https://bitbucket.org/birkenfeld/sphinx/issue/{0}/'
issues = {
'resolved': Issue(id='478', closed=True, url=SPHINX_URL.format('478'),
title='Adapt py:decorator from Python docs'),
'invalid': Issue(id='327', closed=True, url=SPHINX_URL.format('327'),
title='Spaces at the end of console messages'),
'duplicate': Issue(id='733', closed=True, url=SPHINX_URL.format('733'),
title='byte/str conversion fails on Python 3.2'),
'no project': '10',
'no issue': '10000'
}
class TestGitHub(ScopedProjectTrackerTest):
name = 'github'
default_tracker_config = TrackerConfig('lunaryorn/pyudev')
tracker_config = {'no project': TrackerConfig('lunaryorn/foobar')}
issues = {
'closed': Issue(id='2', title=u'python 3 support', closed=True,
url='https://github.com/lunaryorn/pyudev/issues/2'),
'no project': '10',
'no issue': '1000',
}
class TestGoogleCode(TrackerTest):
name = 'google code'
default_tracker_config = TrackerConfig('pytox')
tracker_config = {'no project': TrackerConfig('foobar')}
PYTOX_URL = 'http://code.google.com/p/pytox/issues/detail?id={0}'
issues = {
'fixed': Issue(id='2', closed=True, url=PYTOX_URL.format('2'),
title='Hudson exists with SUCCESS status even if tox '
'failed with ERROR'),
'invalid': Issue(id='5', title='0.7: "error: File exists"',
closed=True, url=PYTOX_URL.format('5')),
'wontfix': Issue(id='6', title='Copy modules from site packages',
closed=True, url=PYTOX_URL.format('6')),
'no issue': '1000',
'no project': '1',
}
class TestDebian(TrackerTest):
pytestmark = pytest.mark.skipif(b'debianbts is None')
name = 'debian'
tracker_config = {'fixed': TrackerConfig('ldb-tools'),
'no project': TrackerConfig('release.debian.org')}
DEBIAN_URL = 'http://bugs.debian.org/cgi-bin/bugreport.cgi?bug={0}'
issues = {
'fixed': Issue(id='584227', title='ldb-tools: missing ldb(7) manpage',
closed=True, url=DEBIAN_URL.format('584227')),
'no project': '1',
}
class TestLaunchpad(TrackerTest):
pytestmark = pytest.mark.skipif(b'launchpadlib is None')
name = 'launchpad'
default_tracker_config = TrackerConfig('inkscape')
issues = {
'closed': Issue('647789', title='tries to install file(s) outside of '
'./configure\'s --prefix', closed=True,
url='https://bugs.launchpad.net/bugs/647789')
}
class TestJira(TrackerTest):
name = 'jira'
issues = {
'resolved': Issue('SHERPA-15', closed=True, title='Breadcrumbs and '
'page title missing from admin screens',
url='https://studio.atlassian.com/browse/SHERPA-15'),
'open': Issue('PYO-84', closed=False,
title='Implement LLSD login in pyogp',
url='https://jira.secondlife.com/browse/PYO-84'),
}
tracker_config = {
'resolved': TrackerConfig('Sherpa', 'https://studio.atlassian.com'),
'open': TrackerConfig('Pyogp', 'https://jira.secondlife.com'),
}
confoverrides = dict(issuetracker_issue_pattern=r'#([A-Z]+-\d+)')
@pytest.mark.with_content('#FOO-15')
def test_no_url(self, app):
"""
Test that the jira tracker fails with a ValueError, if no URL was
configured.
"""
with pytest.raises(ValueError) as excinfo:
app.build()
assert str(excinfo.value) == 'URL required'
|