From 18d1c2974b536d26d694abefacda8d6ac658ddc3 Mon Sep 17 00:00:00 2001 From: Brian Rutledge Date: Sat, 26 Mar 2022 05:59:25 -0400 Subject: [PATCH] Subclass StringIO for _WarningStream. This is responding to a change in readme_renderer.rst.render that expects an `IO[str]` instance. See discussion at https://github.com/pypa/readme_renderer/pull/231#pullrequestreview-915151083 --- tests/test_check.py | 15 ++++----------- twine/commands/check.py | 26 +++++++++----------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/tests/test_check.py b/tests/test_check.py index 66809ccf..18984e2d 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -24,25 +24,18 @@ class TestWarningStream: def setup(self): self.stream = check._WarningStream() - self.stream.output = pretend.stub( - write=pretend.call_recorder(lambda a: None), - getvalue=lambda: "result", - ) def test_write_match(self): self.stream.write(":2: (WARNING/2) Title underline too short.") - - assert self.stream.output.write.calls == [ - pretend.call("line 2: Warning: Title underline too short.\n") - ] + assert self.stream.getvalue() == "line 2: Warning: Title underline too short.\n" def test_write_nomatch(self): self.stream.write("this does not match") - - assert self.stream.output.write.calls == [pretend.call("this does not match")] + assert self.stream.getvalue() == "this does not match" def test_str_representation(self): - assert str(self.stream) == "result" + self.stream.write(":2: (WARNING/2) Title underline too short.") + assert str(self.stream) == "line 2: Warning: Title underline too short." def test_check_no_distributions(monkeypatch, caplog): diff --git a/twine/commands/check.py b/twine/commands/check.py index d4f0f25f..e30fddc8 100644 --- a/twine/commands/check.py +++ b/twine/commands/check.py @@ -48,27 +48,19 @@ ) -class _WarningStream: - def __init__(self) -> None: - self.output = io.StringIO() - - def write(self, text: str) -> None: +class _WarningStream(io.StringIO): + def write(self, text: str) -> int: matched = _REPORT_RE.search(text) + if matched: + line = matched.group("line") + level_text = matched.group("level").capitalize() + message = matched.group("message").rstrip("\r\n") + text = f"line {line}: {level_text}: {message}\n" - if not matched: - self.output.write(text) - return - - self.output.write( - "line {line}: {level_text}: {message}\n".format( - level_text=matched.group("level").capitalize(), - line=matched.group("line"), - message=matched.group("message").rstrip("\r\n"), - ) - ) + return super().write(text) def __str__(self) -> str: - return self.output.getvalue().strip() + return self.getvalue().strip() def _check_file(