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(