2 new commits in pytest: https://bitbucket.org/hpk42/pytest/commits/5f75f34c3ef7/ Changeset: 5f75f34c3ef7 Branch: fix-pastebin User: nicoddemus Date: 2014-10-22 23:52:40+00:00 Summary: Fixing --pastebin option by using a POST request instead of a XMLRPC call
fixes #614 Affected #: 3 files diff -r 7f1df35743c6fcd112d839efaf705d5f34d78dc2 -r 5f75f34c3ef75674994c6111fe4b8728e2709ce8 CHANGELOG --- a/CHANGELOG +++ b/CHANGELOG @@ -19,6 +19,9 @@ - "python_classes" and "python_functions" options now support glob-patterns for test discovery, as discussed in issue600. Thanks Ldiary Translations. +- fix issue614: fixed pastebin support. + + 2.6.4.dev ---------- diff -r 7f1df35743c6fcd112d839efaf705d5f34d78dc2 -r 5f75f34c3ef75674994c6111fe4b8728e2709ce8 _pytest/pastebin.py --- a/_pytest/pastebin.py +++ b/_pytest/pastebin.py @@ -3,10 +3,6 @@ import py, sys import tempfile -class url: - base = "http://bpaste.net" - xmlrpc = base + "/xmlrpc/" - show = base + "/show/" def pytest_addoption(parser): group = parser.getgroup("terminal reporting") @@ -28,22 +24,45 @@ def pytest_unconfigure(config): if hasattr(config, '_pastebinfile'): + # get terminal contents and delete file config._pastebinfile.seek(0) sessionlog = config._pastebinfile.read() config._pastebinfile.close() del config._pastebinfile - proxyid = getproxy().newPaste("python", sessionlog) - pastebinurl = "%s%s" % (url.show, proxyid) - sys.stderr.write("pastebin session-log: %s\n" % pastebinurl) + # undo our patching in the terminal reporter tr = config.pluginmanager.getplugin('terminalreporter') 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): - from xmlrpclib import ServerProxy + from urllib import urlopen, urlencode else: - from xmlrpc.client import ServerProxy - return ServerProxy(url.xmlrpc).pastes + 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)) + else: + return 'bad response: ' + response def pytest_terminal_summary(terminalreporter): if terminalreporter.config.option.pastebin != "failed": @@ -51,9 +70,6 @@ tr = terminalreporter if 'failed' in tr.stats: 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'): try: msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc @@ -63,6 +79,5 @@ rep.toterminal(tw) s = tw.stringio.getvalue() assert len(s) - proxyid = serverproxy.newPaste("python", s) - pastebinurl = "%s%s" % (url.show, proxyid) + pastebinurl = create_new_paste(s) tr.write_line("%s --> %s" %(msg, pastebinurl)) diff -r 7f1df35743c6fcd112d839efaf705d5f34d78dc2 -r 5f75f34c3ef75674994c6111fe4b8728e2709ce8 testing/test_pastebin.py --- a/testing/test_pastebin.py +++ b/testing/test_pastebin.py @@ -1,13 +1,13 @@ +import sys +import pytest class TestPasting: - def pytest_funcarg__pastebinlist(self, request): - mp = request.getfuncargvalue("monkeypatch") + + @pytest.fixture + def pastebinlist(self, monkeypatch, request): pastebinlist = [] - class MockProxy: - def newPaste(self, language, code): - pastebinlist.append((language, code)) plugin = request.config.pluginmanager.getplugin('pastebin') - mp.setattr(plugin, 'getproxy', MockProxy) + monkeypatch.setattr(plugin, 'create_new_paste', pastebinlist.append) return pastebinlist def test_failed(self, testdir, pastebinlist): @@ -22,8 +22,7 @@ """) reprec = testdir.inline_run(testpath, "--paste=failed") assert len(pastebinlist) == 1 - assert pastebinlist[0][0] == "python" - s = pastebinlist[0][1] + s = pastebinlist[0] assert s.find("def test_fail") != -1 assert reprec.countoutcomes() == [1,1,1] @@ -37,22 +36,52 @@ def test_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 len(pastebinlist) == 1 - assert pastebinlist[0][0] == "python" - s = pastebinlist[0][1] - for x in 'test_fail test_skip skipped'.split(): - assert s.find(x), (s, x) + s = pastebinlist[0] + for x in 'test_fail test_skip test_pass'.split(): + assert x in s -class TestRPCClient: - def pytest_funcarg__pastebin(self, request): +class TestPaste: + + @pytest.fixture + def pastebin(self, request): return request.config.pluginmanager.getplugin('pastebin') - def test_getproxy(self, pastebin): - proxy = pastebin.getproxy() - assert proxy is not None - assert proxy.__class__.__module__.startswith('xmlrpc') + @pytest.fixture + def mocked_urlopen(self, monkeypatch): + """ + 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 + + https://bitbucket.org/hpk42/pytest/commits/d6a644c4399a/ Changeset: d6a644c4399a User: hpk42 Date: 2014-10-23 07:08:30+00:00 Summary: Merged in nicoddemus/pytest/fix-pastebin (pull request #228) Fix --pastebin option Affected #: 3 files diff -r 3b0a3f1767860c9efd2d4e3467187f85985bbf49 -r d6a644c4399ad5fee77a5d0ce1e482b3b7a2bcbc CHANGELOG --- a/CHANGELOG +++ b/CHANGELOG @@ -23,6 +23,9 @@ - "python_classes" and "python_functions" options now support glob-patterns for test discovery, as discussed in issue600. Thanks Ldiary Translations. +- fix issue614: fixed pastebin support. + + 2.6.4.dev ---------- diff -r 3b0a3f1767860c9efd2d4e3467187f85985bbf49 -r d6a644c4399ad5fee77a5d0ce1e482b3b7a2bcbc _pytest/pastebin.py --- a/_pytest/pastebin.py +++ b/_pytest/pastebin.py @@ -3,10 +3,6 @@ import py, sys import tempfile -class url: - base = "http://bpaste.net" - xmlrpc = base + "/xmlrpc/" - show = base + "/show/" def pytest_addoption(parser): group = parser.getgroup("terminal reporting") @@ -28,22 +24,45 @@ def pytest_unconfigure(config): if hasattr(config, '_pastebinfile'): + # get terminal contents and delete file config._pastebinfile.seek(0) sessionlog = config._pastebinfile.read() config._pastebinfile.close() del config._pastebinfile - proxyid = getproxy().newPaste("python", sessionlog) - pastebinurl = "%s%s" % (url.show, proxyid) - sys.stderr.write("pastebin session-log: %s\n" % pastebinurl) + # undo our patching in the terminal reporter tr = config.pluginmanager.getplugin('terminalreporter') 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): - from xmlrpclib import ServerProxy + from urllib import urlopen, urlencode else: - from xmlrpc.client import ServerProxy - return ServerProxy(url.xmlrpc).pastes + 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)) + else: + return 'bad response: ' + response def pytest_terminal_summary(terminalreporter): if terminalreporter.config.option.pastebin != "failed": @@ -51,9 +70,6 @@ tr = terminalreporter if 'failed' in tr.stats: 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'): try: msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc @@ -63,6 +79,5 @@ rep.toterminal(tw) s = tw.stringio.getvalue() assert len(s) - proxyid = serverproxy.newPaste("python", s) - pastebinurl = "%s%s" % (url.show, proxyid) + pastebinurl = create_new_paste(s) tr.write_line("%s --> %s" %(msg, pastebinurl)) diff -r 3b0a3f1767860c9efd2d4e3467187f85985bbf49 -r d6a644c4399ad5fee77a5d0ce1e482b3b7a2bcbc testing/test_pastebin.py --- a/testing/test_pastebin.py +++ b/testing/test_pastebin.py @@ -1,13 +1,13 @@ +import sys +import pytest class TestPasting: - def pytest_funcarg__pastebinlist(self, request): - mp = request.getfuncargvalue("monkeypatch") + + @pytest.fixture + def pastebinlist(self, monkeypatch, request): pastebinlist = [] - class MockProxy: - def newPaste(self, language, code): - pastebinlist.append((language, code)) plugin = request.config.pluginmanager.getplugin('pastebin') - mp.setattr(plugin, 'getproxy', MockProxy) + monkeypatch.setattr(plugin, 'create_new_paste', pastebinlist.append) return pastebinlist def test_failed(self, testdir, pastebinlist): @@ -22,8 +22,7 @@ """) reprec = testdir.inline_run(testpath, "--paste=failed") assert len(pastebinlist) == 1 - assert pastebinlist[0][0] == "python" - s = pastebinlist[0][1] + s = pastebinlist[0] assert s.find("def test_fail") != -1 assert reprec.countoutcomes() == [1,1,1] @@ -37,22 +36,52 @@ def test_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 len(pastebinlist) == 1 - assert pastebinlist[0][0] == "python" - s = pastebinlist[0][1] - for x in 'test_fail test_skip skipped'.split(): - assert s.find(x), (s, x) + s = pastebinlist[0] + for x in 'test_fail test_skip test_pass'.split(): + assert x in s -class TestRPCClient: - def pytest_funcarg__pastebin(self, request): +class TestPaste: + + @pytest.fixture + def pastebin(self, request): return request.config.pluginmanager.getplugin('pastebin') - def test_getproxy(self, pastebin): - proxy = pastebin.getproxy() - assert proxy is not None - assert proxy.__class__.__module__.startswith('xmlrpc') + @pytest.fixture + def mocked_urlopen(self, monkeypatch): + """ + 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 + + Repository URL: https://bitbucket.org/hpk42/pytest/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit