Skip to content

Commit 10307b9

Browse files
chmphukkinpre-commit-ci[bot]
authored
fix: read UTF-8 from standard input on all systems (#515)
* Use Utf-8 when reading stdin * Document the use of Utf-8 encoding * Update changelog * Fix tests using stdin * Reformat conftest.py * Update docs/users/changelog.md * Update docs/users/style.md * Update test_cli.py * Update test_config_file.py * Trigger pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Taneli Hukkinen <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f51432e commit 10307b9

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

docs/users/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
This log documents all Python API or CLI breaking backwards incompatible changes.
44
Note that there is currently no guarantee for a stable Markdown formatting style across versions.
55

6+
## **unreleased**
7+
8+
- Fixed
9+
- Read UTF-8 from standard input on all systems.
10+
Thank you, [Christopher Prohm](https://github.com/chmp), for the PR.
11+
612
## 0.7.22
713

814
- Performance

src/mdformat/_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def run(cli_args: Sequence[str], cache_toml: bool = True) -> int: # noqa: C901
124124
original_str = path.read_bytes().decode()
125125
else:
126126
path_str = "-"
127-
original_str = sys.stdin.read()
127+
original_str = sys.stdin.buffer.read().decode()
128128

129129
# Lazy import to improve module import time
130130
from mdformat.renderer import LOGGER as RENDERER_LOGGER

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import io
2+
import sys
3+
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def patch_stdin(monkeypatch):
9+
"""Fixture to patch sys.stdin for the running test."""
10+
11+
def set_stdin(text):
12+
buffer = io.BytesIO(text.encode())
13+
fobj = io.TextIOWrapper(buffer)
14+
monkeypatch.setattr(sys, "stdin", fobj)
15+
16+
return set_stdin

tests/test_cli.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from io import StringIO
21
import os
32
import sys
43
from unittest.mock import patch
@@ -143,8 +142,8 @@ def test_formatter_plugin(tmp_path, monkeypatch):
143142
assert file_path.read_text() == "```lang\ndummy\n```\n"
144143

145144

146-
def test_dash_stdin(capfd, monkeypatch):
147-
monkeypatch.setattr(sys, "stdin", StringIO(UNFORMATTED_MARKDOWN))
145+
def test_dash_stdin(capfd, patch_stdin):
146+
patch_stdin(UNFORMATTED_MARKDOWN)
148147
assert run(("-",)) == 0
149148
captured = capfd.readouterr()
150149
assert captured.out == FORMATTED_MARKDOWN
@@ -298,8 +297,8 @@ def test_eol__keep_crlf(tmp_path):
298297
assert file_path.read_bytes() == b"Oi\r\n"
299298

300299

301-
def test_eol__crlf_stdin(capfd, monkeypatch):
302-
monkeypatch.setattr(sys, "stdin", StringIO("Oi\n"))
300+
def test_eol__crlf_stdin(capfd, patch_stdin):
301+
patch_stdin("Oi\n")
303302
assert run(["-", "--end-of-line=crlf"]) == 0
304303
captured = capfd.readouterr()
305304
assert captured.out == "Oi\r\n"

tests/test_config_file.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from io import StringIO
21
import sys
32
from unittest import mock
43

@@ -90,11 +89,11 @@ def test_invalid_conf_value(bad_conf, conf_key, tmp_path, capsys):
9089
assert f"Invalid '{conf_key}' value" in captured.err
9190

9291

93-
def test_conf_with_stdin(tmp_path, capfd, monkeypatch):
92+
def test_conf_with_stdin(tmp_path, capfd, patch_stdin):
9493
config_path = tmp_path / ".mdformat.toml"
9594
config_path.write_text("number = true")
9695

97-
monkeypatch.setattr(sys, "stdin", StringIO("1. one\n1. two\n1. three"))
96+
patch_stdin("1. one\n1. two\n1. three")
9897

9998
with mock.patch("mdformat._cli.Path.cwd", return_value=tmp_path):
10099
assert run(("-",), cache_toml=False) == 0

0 commit comments

Comments
 (0)