Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclass StringIO for _WarningStream. #886

Merged
merged 1 commit into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 4 additions & 11 deletions tests/test_check.py
Expand Up @@ -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("<string>: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("<string>: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):
Expand Down
26 changes: 9 additions & 17 deletions twine/commands/check.py
Expand Up @@ -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(
Expand Down