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
|
2017-08-03 01:33:52 +08:00
|
|
|
import six
|
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")
|
2018-05-23 22:48:46 +08:00
|
|
|
group._addoption(
|
|
|
|
"--pastebin",
|
|
|
|
metavar="mode",
|
|
|
|
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):
|
2009-08-03 17:56:56 +08:00
|
|
|
if config.option.pastebin == "all":
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
config._pastebinfile = tempfile.TemporaryFile("w+b")
|
2017-11-24 05:26:57 +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)
|
2017-08-03 01:33:52 +08:00
|
|
|
if isinstance(s, six.text_type):
|
2018-05-23 22:48:46 +08:00
|
|
|
s = s.encode("utf-8")
|
2015-12-04 06:07:18 +08:00
|
|
|
config._pastebinfile.write(s)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-11-24 05:26:57 +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):
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
tr = config.pluginmanager.getplugin("terminalreporter")
|
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
|
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 = {
|
2018-05-23 22:48:46 +08:00
|
|
|
"code": contents,
|
|
|
|
"lexer": "python3" if sys.version_info[0] == 3 else "python",
|
|
|
|
"expiry": "1week",
|
2014-10-23 07:52:40 +08:00
|
|
|
}
|
2018-05-23 22:48:46 +08:00
|
|
|
url = "https://bpaste.net"
|
|
|
|
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:
|
2018-05-23 22:48:46 +08:00
|
|
|
return "%s/show/%s" % (url, m.group(1))
|
2011-11-15 00:51:12 +08:00
|
|
|
else:
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-08-03 17:56:56 +08:00
|
|
|
if terminalreporter.config.option.pastebin != "failed":
|
|
|
|
return
|
|
|
|
tr = terminalreporter
|
2018-05-23 22:48:46 +08:00
|
|
|
if "failed" in tr.stats:
|
2009-08-03 17:56:56 +08:00
|
|
|
terminalreporter.write_sep("=", "Sending information to Paste Service")
|
2018-05-23 22:48:46 +08:00
|
|
|
for rep in terminalreporter.stats.get("failed"):
|
2009-08-03 17:56:56 +08:00
|
|
|
try:
|
|
|
|
msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc
|
|
|
|
except AttributeError:
|
|
|
|
msg = tr._getfailureheadline(rep)
|
2018-05-23 22:48:46 +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))
|