2019-05-15 06:56:31 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-10-25 15:01:29 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
import sys
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPasteCapture(object):
|
2014-10-23 07:52:40 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def pastebinlist(self, monkeypatch, request):
|
2009-09-06 22:59:39 +08:00
|
|
|
pastebinlist = []
|
2018-05-23 22:48:46 +08:00
|
|
|
plugin = request.config.pluginmanager.getplugin("pastebin")
|
|
|
|
monkeypatch.setattr(plugin, "create_new_paste", pastebinlist.append)
|
2010-07-27 03:15:15 +08:00
|
|
|
return pastebinlist
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_failed(self, testdir, pastebinlist):
|
2018-05-23 22:48:46 +08:00
|
|
|
testpath = testdir.makepyfile(
|
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2009-09-06 22:59:39 +08:00
|
|
|
reprec = testdir.inline_run(testpath, "--paste=failed")
|
|
|
|
assert len(pastebinlist) == 1
|
2014-10-23 07:52:40 +08:00
|
|
|
s = pastebinlist[0]
|
2009-09-06 22:59:39 +08:00
|
|
|
assert s.find("def test_fail") != -1
|
2017-07-17 07:25:08 +08:00
|
|
|
assert reprec.countoutcomes() == [1, 1, 1]
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_all(self, testdir, pastebinlist):
|
2015-12-04 06:07:18 +08:00
|
|
|
from _pytest.pytester import LineMatcher
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
testpath = testdir.makepyfile(
|
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
reprec = testdir.inline_run(testpath, "--pastebin=all", "-v")
|
2017-07-17 07:25:08 +08:00
|
|
|
assert reprec.countoutcomes() == [1, 1, 1]
|
2009-09-06 22:59:39 +08:00
|
|
|
assert len(pastebinlist) == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
contents = pastebinlist[0].decode("utf-8")
|
2015-12-04 06:07:18 +08:00
|
|
|
matcher = LineMatcher(contents.splitlines())
|
2018-05-23 22:48:46 +08:00
|
|
|
matcher.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_pass PASSED*",
|
|
|
|
"*test_fail FAILED*",
|
|
|
|
"*test_skip SKIPPED*",
|
|
|
|
"*== 1 failed, 1 passed, 1 skipped in *",
|
|
|
|
]
|
|
|
|
)
|
2015-12-04 06:07:18 +08:00
|
|
|
|
|
|
|
def test_non_ascii_paste_text(self, testdir):
|
|
|
|
"""Make sure that text which contains non-ascii characters is pasted
|
|
|
|
correctly. See #1219.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_unicode="""
|
2019-05-15 06:56:31 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-12-04 06:07:18 +08:00
|
|
|
def test():
|
|
|
|
assert '☺' == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("--pastebin=all")
|
2015-12-04 06:07:18 +08:00
|
|
|
if sys.version_info[0] == 3:
|
|
|
|
expected_msg = "*assert '☺' == 1*"
|
|
|
|
else:
|
|
|
|
expected_msg = "*assert '\\xe2\\x98\\xba' == 1*"
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
expected_msg,
|
|
|
|
"*== 1 failed in *",
|
|
|
|
"*Sending information to Paste Service*",
|
|
|
|
]
|
|
|
|
)
|
2014-10-23 07:52:40 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPaste(object):
|
2014-10-23 07:52:40 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def pastebin(self, request):
|
2018-05-23 22:48:46 +08:00
|
|
|
return request.config.pluginmanager.getplugin("pastebin")
|
2011-11-15 00:51:12 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def mocked_urlopen(self, monkeypatch):
|
|
|
|
"""
|
|
|
|
monkeypatch the actual urlopen calls done by the internal plugin
|
|
|
|
function that connects to bpaste service.
|
|
|
|
"""
|
|
|
|
calls = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
def mocked(url, data):
|
|
|
|
calls.append((url, data))
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class DummyFile(object):
|
2014-10-23 07:52:40 +08:00
|
|
|
def read(self):
|
|
|
|
# part of html of a normal response
|
2015-12-02 09:30:05 +08:00
|
|
|
return b'View <a href="/raw/3c0c6750bd">raw</a>.'
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
return DummyFile()
|
|
|
|
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
import urllib
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
monkeypatch.setattr(urllib, "urlopen", mocked)
|
2014-10-23 07:52:40 +08:00
|
|
|
else:
|
|
|
|
import urllib.request
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", mocked)
|
2014-10-23 07:52:40 +08:00
|
|
|
return calls
|
|
|
|
|
|
|
|
def test_create_new_paste(self, pastebin, mocked_urlopen):
|
2018-05-23 22:48:46 +08:00
|
|
|
result = pastebin.create_new_paste(b"full-paste-contents")
|
|
|
|
assert result == "https://bpaste.net/show/3c0c6750bd"
|
2014-10-23 07:52:40 +08:00
|
|
|
assert len(mocked_urlopen) == 1
|
|
|
|
url, data = mocked_urlopen[0]
|
2015-12-02 09:30:05 +08:00
|
|
|
assert type(data) is bytes
|
2018-05-23 22:48:46 +08:00
|
|
|
lexer = "python3" if sys.version_info[0] == 3 else "python"
|
|
|
|
assert url == "https://bpaste.net"
|
|
|
|
assert "lexer=%s" % lexer in data.decode()
|
|
|
|
assert "code=full-paste-contents" in data.decode()
|
|
|
|
assert "expiry=1week" in data.decode()
|