| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import email.message |
| 5 |
import io |
| 6 |
from unittest import mock |
| 7 |
|
| 8 |
from _pytest.config import ExitCode |
| 9 |
from _pytest.monkeypatch import MonkeyPatch |
| 10 |
from _pytest.pytester import Pytester |
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
class TestPasteCapture: |
| 15 |
@pytest.fixture |
| 16 |
def pastebinlist(self, monkeypatch, request) -> list[str | bytes]: |
| 17 |
pastebinlist: list[str | bytes] = [] |
| 18 |
plugin = request.config.pluginmanager.getplugin("pastebin") |
| 19 |
monkeypatch.setattr(plugin, "create_new_paste", pastebinlist.append) |
| 20 |
return pastebinlist |
| 21 |
|
| 22 |
def test_failed(self, pytester: Pytester, pastebinlist) -> None: |
| 23 |
testpath = pytester.makepyfile( |
| 24 |
""" |
| 25 |
import pytest |
| 26 |
def test_pass() -> None: |
| 27 |
pass |
| 28 |
def test_fail(): |
| 29 |
assert 0 |
| 30 |
def test_skip(): |
| 31 |
pytest.skip("") |
| 32 |
""" |
| 33 |
) |
| 34 |
reprec = pytester.inline_run(testpath, "--pastebin=failed") |
| 35 |
assert len(pastebinlist) == 1 |
| 36 |
s = pastebinlist[0] |
| 37 |
assert s.find("def test_fail") != -1 |
| 38 |
assert reprec.countoutcomes() == [1, 1, 1] |
| 39 |
|
| 40 |
def test_all(self, pytester: Pytester, pastebinlist) -> None: |
| 41 |
from _pytest.pytester import LineMatcher |
| 42 |
|
| 43 |
testpath = pytester.makepyfile( |
| 44 |
""" |
| 45 |
import pytest |
| 46 |
def test_pass(): |
| 47 |
pass |
| 48 |
def test_fail(): |
| 49 |
assert 0 |
| 50 |
def test_skip(): |
| 51 |
pytest.skip("") |
| 52 |
""" |
| 53 |
) |
| 54 |
reprec = pytester.inline_run( |
| 55 |
testpath, |
| 56 |
"--pastebin=all", |
| 57 |
"-v", |
| 58 |
"-W", |
| 59 |
"ignore:The --pastebin:DeprecationWarning", |
| 60 |
) |
| 61 |
assert reprec.countoutcomes() == [1, 1, 1] |
| 62 |
assert len(pastebinlist) == 1 |
| 63 |
contents = pastebinlist[0].decode("utf-8") |
| 64 |
matcher = LineMatcher(contents.splitlines()) |
| 65 |
matcher.fnmatch_lines( |
| 66 |
[ |
| 67 |
"*test_pass PASSED*", |
| 68 |
"*test_fail FAILED*", |
| 69 |
"*test_skip SKIPPED*", |
| 70 |
"*== 1 failed, 1 passed, 1 skipped in *", |
| 71 |
] |
| 72 |
) |
| 73 |
|
| 74 |
def test_non_ascii_paste_text(self, pytester: Pytester, pastebinlist) -> None: |
| 75 |
"""Make sure that text which contains non-ascii characters is pasted |
| 76 |
correctly. See #1219. |
| 77 |
""" |
| 78 |
pytester.makepyfile( |
| 79 |
test_unicode="""\ |
| 80 |
def test(): |
| 81 |
assert '☺' == 1 |
| 82 |
""" |
| 83 |
) |
| 84 |
result = pytester.runpytest( |
| 85 |
"--pastebin=all", |
| 86 |
"-W", |
| 87 |
"ignore:The --pastebin:DeprecationWarning", |
| 88 |
) |
| 89 |
expected_msg = "*assert '☺' == 1*" |
| 90 |
result.stdout.fnmatch_lines( |
| 91 |
[ |
| 92 |
expected_msg, |
| 93 |
"*== 1 failed in *", |
| 94 |
"*Sending information to Paste Service*", |
| 95 |
] |
| 96 |
) |
| 97 |
assert len(pastebinlist) == 1 |
| 98 |
|
| 99 |
def test_deprecated(self, pytester: Pytester, pastebinlist) -> None: |
| 100 |
result = pytester.runpytest("--pastebin=failed") |
| 101 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 102 |
result.assert_outcomes() |
| 103 |
result.stdout.fnmatch_lines(["*The --pastebin option is deprecated*"]) |
| 104 |
|
| 105 |
|
| 106 |
class TestPaste: |
| 107 |
@pytest.fixture |
| 108 |
def pastebin(self, request): |
| 109 |
return request.config.pluginmanager.getplugin("pastebin") |
| 110 |
|
| 111 |
@pytest.fixture |
| 112 |
def mocked_urlopen_invalid(self, monkeypatch: MonkeyPatch): |
| 113 |
"""Monkeypatch the actual urlopen calls done by the internal plugin |
| 114 |
function that connects to bpaste service, but return a url in an |
| 115 |
unexpected format.""" |
| 116 |
calls = [] |
| 117 |
|
| 118 |
def mocked(url, data): |
| 119 |
calls.append((url, data)) |
| 120 |
|
| 121 |
class DummyFile: |
| 122 |
def read(self): |
| 123 |
# part of html of a normal response |
| 124 |
return b'View <a href="/invalid/3c0c6750bd">raw</a>.' |
| 125 |
|
| 126 |
return DummyFile() |
| 127 |
|
| 128 |
import urllib.request |
| 129 |
|
| 130 |
monkeypatch.setattr(urllib.request, "urlopen", mocked) |
| 131 |
return calls |
| 132 |
|
| 133 |
@pytest.fixture |
| 134 |
def mocked_urlopen(self, monkeypatch: MonkeyPatch): |
| 135 |
"""Monkeypatch the actual urlopen calls done by the internal plugin |
| 136 |
function that connects to bpaste service.""" |
| 137 |
calls = [] |
| 138 |
|
| 139 |
def mocked(url, data): |
| 140 |
calls.append((url, data)) |
| 141 |
|
| 142 |
class DummyFile: |
| 143 |
def read(self): |
| 144 |
# part of html of a normal response |
| 145 |
return b'View <a href="/raw/3c0c6750bd">raw</a>.' |
| 146 |
|
| 147 |
return DummyFile() |
| 148 |
|
| 149 |
import urllib.request |
| 150 |
|
| 151 |
monkeypatch.setattr(urllib.request, "urlopen", mocked) |
| 152 |
return calls |
| 153 |
|
| 154 |
def test_pastebin_invalid_url(self, pastebin, mocked_urlopen_invalid) -> None: |
| 155 |
result = pastebin.create_new_paste(b"full-paste-contents") |
| 156 |
assert ( |
| 157 |
result |
| 158 |
== "bad response: invalid format ('View <a href=\"/invalid/3c0c6750bd\">raw</a>.')" |
| 159 |
) |
| 160 |
assert len(mocked_urlopen_invalid) == 1 |
| 161 |
|
| 162 |
def test_pastebin_http_error(self, pastebin) -> None: |
| 163 |
import urllib.error |
| 164 |
|
| 165 |
with mock.patch( |
| 166 |
"urllib.request.urlopen", |
| 167 |
side_effect=urllib.error.HTTPError( |
| 168 |
url="https://bpa.st", |
| 169 |
code=400, |
| 170 |
msg="Bad request", |
| 171 |
hdrs=email.message.Message(), |
| 172 |
fp=io.BytesIO(), |
| 173 |
), |
| 174 |
) as mock_urlopen: |
| 175 |
result = pastebin.create_new_paste(b"full-paste-contents") |
| 176 |
assert result == "bad response: HTTP Error 400: Bad request" |
| 177 |
assert len(mock_urlopen.mock_calls) == 1 |
| 178 |
|
| 179 |
def test_pastebin_url_error(self, pastebin) -> None: |
| 180 |
import urllib.error |
| 181 |
|
| 182 |
with mock.patch( |
| 183 |
"urllib.request.urlopen", |
| 184 |
side_effect=urllib.error.URLError("the url was bad"), |
| 185 |
) as mock_urlopen: |
| 186 |
result = pastebin.create_new_paste(b"full-paste-contents") |
| 187 |
assert result == "bad response: <urlopen error the url was bad>" |
| 188 |
assert len(mock_urlopen.mock_calls) == 1 |
| 189 |
|
| 190 |
def test_create_new_paste(self, pastebin, mocked_urlopen) -> None: |
| 191 |
result = pastebin.create_new_paste(b"full-paste-contents") |
| 192 |
assert result == "https://bpa.st/show/3c0c6750bd" |
| 193 |
assert len(mocked_urlopen) == 1 |
| 194 |
url, data = mocked_urlopen[0] |
| 195 |
assert type(data) is bytes |
| 196 |
lexer = "text" |
| 197 |
assert url == "https://bpa.st" |
| 198 |
assert f"lexer={lexer}" in data.decode() |
| 199 |
assert "code=full-paste-contents" in data.decode() |
| 200 |
assert "expiry=1week" in data.decode() |
| 201 |
|
| 202 |
def test_create_new_paste_failure(self, pastebin, monkeypatch: MonkeyPatch) -> None: |
| 203 |
import io |
| 204 |
import urllib.request |
| 205 |
|
| 206 |
def response(url, data): |
| 207 |
stream = io.BytesIO(b"something bad occurred") |
| 208 |
return stream |
| 209 |
|
| 210 |
monkeypatch.setattr(urllib.request, "urlopen", response) |
| 211 |
result = pastebin.create_new_paste(b"full-paste-contents") |
| 212 |
assert result == "bad response: invalid format ('something bad occurred')" |