2010-10-11 06:14:32 +08:00
|
|
|
""" submit failure or test session information to a pastebin service. """
|
2014-10-09 02:23:40 +08:00
|
|
|
import pytest
|
2015-07-22 06:55:01 +08:00
|
|
|
import sys
|
2014-10-09 02:23:40 +08:00
|
|
|
import tempfile
|
2009-08-03 17:56:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2010-01-03 19:41:29 +08:00
|
|
|
group = parser.getgroup("terminal reporting")
|
2009-08-03 17:56:56 +08:00
|
|
|
group._addoption('--pastebin', metavar="mode",
|
2010-07-27 03:15:15 +08:00
|
|
|
action='store', dest="pastebin", default=None,
|
2013-07-25 21:33:43 +08:00
|
|
|
choices=['failed', 'all'],
|
2012-05-19 16:54:12 +08:00
|
|
|
help="send failed|all info to bpaste.net pastebin service.")
|
2009-08-03 17:56:56 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(trylast=True)
|
2014-10-09 02:23:40 +08:00
|
|
|
def pytest_configure(config):
|
2009-08-03 17:56:56 +08:00
|
|
|
if config.option.pastebin == "all":
|
2009-10-17 18:56:59 +08:00
|
|
|
tr = config.pluginmanager.getplugin('terminalreporter')
|
2015-01-20 05:20:01 +08:00
|
|
|
# if no terminal reporter plugin is present, nothing we can do here;
|
|
|
|
# this can happen when this function executes in a slave node
|
|
|
|
# when using pytest-xdist, for example
|
|
|
|
if tr is not None:
|
|
|
|
config._pastebinfile = tempfile.TemporaryFile('w+')
|
|
|
|
oldwrite = tr._tw.write
|
|
|
|
def tee_write(s, **kwargs):
|
|
|
|
oldwrite(s, **kwargs)
|
|
|
|
config._pastebinfile.write(str(s))
|
|
|
|
tr._tw.write = tee_write
|
2009-08-03 17:56:56 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def pytest_unconfigure(config):
|
2009-08-03 17:56:56 +08:00
|
|
|
if hasattr(config, '_pastebinfile'):
|
2014-10-23 07:52:40 +08:00
|
|
|
# get terminal contents and delete file
|
2009-08-03 17:56:56 +08:00
|
|
|
config._pastebinfile.seek(0)
|
|
|
|
sessionlog = config._pastebinfile.read()
|
|
|
|
config._pastebinfile.close()
|
|
|
|
del config._pastebinfile
|
2014-10-23 07:52:40 +08:00
|
|
|
# undo our patching in the terminal reporter
|
2009-10-17 18:56:59 +08:00
|
|
|
tr = config.pluginmanager.getplugin('terminalreporter')
|
2009-08-03 17:56:56 +08:00
|
|
|
del tr._tw.__dict__['write']
|
2014-10-23 07:52:40 +08:00
|
|
|
# write summary
|
|
|
|
tr.write_sep("=", "Sending information to Paste Service")
|
|
|
|
pastebinurl = create_new_paste(sessionlog)
|
|
|
|
tr.write_line("pastebin session-log: %s\n" % pastebinurl)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
def create_new_paste(contents):
|
|
|
|
"""
|
|
|
|
Creates a new paste using bpaste.net service.
|
|
|
|
|
|
|
|
:contents: paste contents
|
|
|
|
:returns: url to the pasted contents
|
|
|
|
"""
|
|
|
|
import re
|
2011-11-15 00:51:12 +08:00
|
|
|
if sys.version_info < (3, 0):
|
2014-10-23 07:52:40 +08:00
|
|
|
from urllib import urlopen, urlencode
|
|
|
|
else:
|
|
|
|
from urllib.request import urlopen
|
|
|
|
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))
|
2011-11-15 00:51:12 +08:00
|
|
|
else:
|
2014-10-23 07:52:40 +08:00
|
|
|
return 'bad response: ' + response
|
2009-08-03 17:56:56 +08:00
|
|
|
|
|
|
|
def pytest_terminal_summary(terminalreporter):
|
2015-07-21 23:55:18 +08:00
|
|
|
import _pytest.config
|
2009-08-03 17:56:56 +08:00
|
|
|
if terminalreporter.config.option.pastebin != "failed":
|
|
|
|
return
|
|
|
|
tr = terminalreporter
|
|
|
|
if 'failed' in tr.stats:
|
|
|
|
terminalreporter.write_sep("=", "Sending information to Paste Service")
|
|
|
|
for rep in terminalreporter.stats.get('failed'):
|
|
|
|
try:
|
|
|
|
msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc
|
|
|
|
except AttributeError:
|
|
|
|
msg = tr._getfailureheadline(rep)
|
2015-07-21 23:55:18 +08:00
|
|
|
tw = _pytest.config.create_terminal_writer(terminalreporter.config, stringio=True)
|
2009-08-03 17:56:56 +08:00
|
|
|
rep.toterminal(tw)
|
|
|
|
s = tw.stringio.getvalue()
|
|
|
|
assert len(s)
|
2014-10-23 07:52:40 +08:00
|
|
|
pastebinurl = create_new_paste(s)
|
2009-08-03 17:56:56 +08:00
|
|
|
tr.write_line("%s --> %s" %(msg, pastebinurl))
|