Skip to content

Commit 6ecb8f5

Browse files
authored
Merge pull request #7521 from asottile/newlines_unaffected
Preserve newlines when captured with capfd
2 parents 2294f10 + 8616a5f commit 6ecb8f5

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

changelog/7517.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Preserve line endings when captured via ``capfd``.

src/_pytest/capture.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ def __init__(self, targetfd: int) -> None:
388388
TemporaryFile(buffering=0), # type: ignore[arg-type]
389389
encoding="utf-8",
390390
errors="replace",
391+
newline="",
391392
write_through=True,
392393
)
393394
if targetfd in patchsysdict:

testing/test_capture.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ def test_hello(capfd):
514514
)
515515
reprec.assertoutcome(passed=1)
516516

517+
@pytest.mark.parametrize("nl", ("\n", "\r\n", "\r"))
518+
def test_cafd_preserves_newlines(self, capfd, nl):
519+
print("test", end=nl)
520+
out, err = capfd.readouterr()
521+
assert out.endswith(nl)
522+
517523
def test_capfdbinary(self, testdir):
518524
reprec = testdir.inline_runsource(
519525
"""\

0 commit comments

Comments
 (0)