2010-10-11 06:14:32 +08:00
|
|
|
""" submit failure or test session information to a pastebin service. """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
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",
|
2017-07-17 07:25:07 +08:00
|
|
|
action='store', dest="pastebin", default=None,
|
|
|
|
choices=['failed', 'all'],
|
|
|
|
help="send failed|all info to bpaste.net pastebin service.")
|
2009-08-03 17:56:56 +08:00
|
|
|
|
2016-11-21 04:59:15 +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):
|
2015-12-04 06:07:18 +08:00
|
|
|
import py
|
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:
|
2015-12-04 06:07:18 +08:00
|
|
|
# pastebin file will be utf-8 encoded binary file
|
|
|
|
config._pastebinfile = tempfile.TemporaryFile('w+b')
|
2015-01-20 05:20:01 +08:00
|
|
|
oldwrite = tr._tw.write
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-01-20 05:20:01 +08:00
|
|
|
def tee_write(s, **kwargs):
|
|
|
|
oldwrite(s, **kwargs)
|
2015-12-04 06:07:18 +08:00
|
|
|
if py.builtin._istext(s):
|
|
|
|
s = s.encode('utf-8')
|
|
|
|
config._pastebinfile.write(s)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-01-20 05:20:01 +08:00
|
|
|
tr._tw.write = tee_write
|
2009-08-03 17:56:56 +08:00
|
|
|
|
2016-11-21 04:59:15 +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
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
def create_new_paste(contents):
|
|
|
|
"""
|
|
|
|
Creates a new paste using bpaste.net service.
|
|
|
|
|
2015-12-04 06:07:18 +08:00
|
|
|
:contents: paste contents as utf-8 encoded bytes
|
2014-10-23 07:52:40 +08:00
|
|
|
: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'
|
2015-12-04 06:07:18 +08:00
|
|
|
response = urlopen(url, data=urlencode(params).encode('ascii')).read()
|
|
|
|
m = re.search(r'href="/raw/(\w+)"', response.decode('utf-8'))
|
2014-10-23 07:52:40 +08:00
|
|
|
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
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
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)
|
2017-07-17 07:25:08 +08:00
|
|
|
tr.write_line("%s --> %s" % (msg, pastebinurl))
|