backport pastebin fix

--HG--
branch : pytest-2.6
This commit is contained in:
holger krekel 2014-10-24 15:24:44 +02:00
parent e0251ecb41
commit 60cdb875ed
3 changed files with 82 additions and 36 deletions

View File

@ -1,4 +1,4 @@
2.6.4.dev 2.6.4
---------- ----------
- Improve assertion failure reporting on iterables, by using ndiff and pprint. - Improve assertion failure reporting on iterables, by using ndiff and pprint.
@ -18,6 +18,8 @@
- fix issue620: add explanation in the --genscript target about what - fix issue620: add explanation in the --genscript target about what
the binary blob means. Thanks Dinu Gherman. the binary blob means. Thanks Dinu Gherman.
- fix issue614: fixed pastebin support.
2.6.3 2.6.3
----------- -----------

View File

@ -1,10 +1,6 @@
""" submit failure or test session information to a pastebin service. """ """ submit failure or test session information to a pastebin service. """
import py, sys import py, sys
class url:
base = "http://bpaste.net"
xmlrpc = base + "/xmlrpc/"
show = base + "/show/"
def pytest_addoption(parser): def pytest_addoption(parser):
group = parser.getgroup("terminal reporting") group = parser.getgroup("terminal reporting")
@ -27,22 +23,45 @@ def pytest_configure(__multicall__, config):
def pytest_unconfigure(config): def pytest_unconfigure(config):
if hasattr(config, '_pastebinfile'): if hasattr(config, '_pastebinfile'):
# get terminal contents and delete file
config._pastebinfile.seek(0) config._pastebinfile.seek(0)
sessionlog = config._pastebinfile.read() sessionlog = config._pastebinfile.read()
config._pastebinfile.close() config._pastebinfile.close()
del config._pastebinfile del config._pastebinfile
proxyid = getproxy().newPaste("python", sessionlog) # undo our patching in the terminal reporter
pastebinurl = "%s%s" % (url.show, proxyid)
sys.stderr.write("pastebin session-log: %s\n" % pastebinurl)
tr = config.pluginmanager.getplugin('terminalreporter') tr = config.pluginmanager.getplugin('terminalreporter')
del tr._tw.__dict__['write'] del tr._tw.__dict__['write']
# write summary
tr.write_sep("=", "Sending information to Paste Service")
pastebinurl = create_new_paste(sessionlog)
tr.write_line("pastebin session-log: %s\n" % pastebinurl)
def getproxy(): def create_new_paste(contents):
"""
Creates a new paste using bpaste.net service.
:contents: paste contents
:returns: url to the pasted contents
"""
import re
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
from xmlrpclib import ServerProxy from urllib import urlopen, urlencode
else: else:
from xmlrpc.client import ServerProxy from urllib.request import urlopen
return ServerProxy(url.xmlrpc).pastes from urllib.parse import urlencode
params = {
'code': contents,
'lexer': 'python3' if sys.version_info[0] == 3 else 'python',
'expiry': '1week',
}
url = 'https://bpaste.net'
response = urlopen(url, data=urlencode(params)).read()
m = re.search(r'href="/raw/(\w+)"', response)
if m:
return '%s/show/%s' % (url, m.group(1))
else:
return 'bad response: ' + response
def pytest_terminal_summary(terminalreporter): def pytest_terminal_summary(terminalreporter):
if terminalreporter.config.option.pastebin != "failed": if terminalreporter.config.option.pastebin != "failed":
@ -50,9 +69,6 @@ def pytest_terminal_summary(terminalreporter):
tr = terminalreporter tr = terminalreporter
if 'failed' in tr.stats: if 'failed' in tr.stats:
terminalreporter.write_sep("=", "Sending information to Paste Service") terminalreporter.write_sep("=", "Sending information to Paste Service")
if tr.config.option.debug:
terminalreporter.write_line("xmlrpcurl: %s" %(url.xmlrpc,))
serverproxy = getproxy()
for rep in terminalreporter.stats.get('failed'): for rep in terminalreporter.stats.get('failed'):
try: try:
msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc
@ -62,6 +78,5 @@ def pytest_terminal_summary(terminalreporter):
rep.toterminal(tw) rep.toterminal(tw)
s = tw.stringio.getvalue() s = tw.stringio.getvalue()
assert len(s) assert len(s)
proxyid = serverproxy.newPaste("python", s) pastebinurl = create_new_paste(s)
pastebinurl = "%s%s" % (url.show, proxyid)
tr.write_line("%s --> %s" %(msg, pastebinurl)) tr.write_line("%s --> %s" %(msg, pastebinurl))

View File

@ -1,13 +1,13 @@
import sys
import pytest
class TestPasting: class TestPasting:
def pytest_funcarg__pastebinlist(self, request):
mp = request.getfuncargvalue("monkeypatch") @pytest.fixture
def pastebinlist(self, monkeypatch, request):
pastebinlist = [] pastebinlist = []
class MockProxy:
def newPaste(self, language, code):
pastebinlist.append((language, code))
plugin = request.config.pluginmanager.getplugin('pastebin') plugin = request.config.pluginmanager.getplugin('pastebin')
mp.setattr(plugin, 'getproxy', MockProxy) monkeypatch.setattr(plugin, 'create_new_paste', pastebinlist.append)
return pastebinlist return pastebinlist
def test_failed(self, testdir, pastebinlist): def test_failed(self, testdir, pastebinlist):
@ -22,8 +22,7 @@ class TestPasting:
""") """)
reprec = testdir.inline_run(testpath, "--paste=failed") reprec = testdir.inline_run(testpath, "--paste=failed")
assert len(pastebinlist) == 1 assert len(pastebinlist) == 1
assert pastebinlist[0][0] == "python" s = pastebinlist[0]
s = pastebinlist[0][1]
assert s.find("def test_fail") != -1 assert s.find("def test_fail") != -1
assert reprec.countoutcomes() == [1,1,1] assert reprec.countoutcomes() == [1,1,1]
@ -37,22 +36,52 @@ class TestPasting:
def test_skip(): def test_skip():
pytest.skip("") pytest.skip("")
""") """)
reprec = testdir.inline_run(testpath, "--pastebin=all") reprec = testdir.inline_run(testpath, "--pastebin=all", '-v')
assert reprec.countoutcomes() == [1,1,1] assert reprec.countoutcomes() == [1,1,1]
assert len(pastebinlist) == 1 assert len(pastebinlist) == 1
assert pastebinlist[0][0] == "python" s = pastebinlist[0]
s = pastebinlist[0][1] for x in 'test_fail test_skip test_pass'.split():
for x in 'test_fail test_skip skipped'.split(): assert x in s
assert s.find(x), (s, x)
class TestRPCClient: class TestPaste:
def pytest_funcarg__pastebin(self, request):
@pytest.fixture
def pastebin(self, request):
return request.config.pluginmanager.getplugin('pastebin') return request.config.pluginmanager.getplugin('pastebin')
def test_getproxy(self, pastebin): @pytest.fixture
proxy = pastebin.getproxy() def mocked_urlopen(self, monkeypatch):
assert proxy is not None """
assert proxy.__class__.__module__.startswith('xmlrpc') monkeypatch the actual urlopen calls done by the internal plugin
function that connects to bpaste service.
"""
calls = []
def mocked(url, data):
calls.append((url, data))
class DummyFile:
def read(self):
# part of html of a normal response
return 'View <a href="/raw/3c0c6750bd">raw</a>.'
return DummyFile()
if sys.version_info < (3, 0):
import urllib
monkeypatch.setattr(urllib, 'urlopen', mocked)
else:
import urllib.request
monkeypatch.setattr(urllib.request, 'urlopen', mocked)
return calls
def test_create_new_paste(self, pastebin, mocked_urlopen):
result = pastebin.create_new_paste('full-paste-contents')
assert result == 'https://bpaste.net/show/3c0c6750bd'
assert len(mocked_urlopen) == 1
url, data = mocked_urlopen[0]
lexer = 'python3' if sys.version_info[0] == 3 else 'python'
assert url == 'https://bpaste.net'
assert 'lexer=%s' % lexer in data
assert 'code=full-paste-contents' in data
assert 'expiry=1week' in data