Skip to content

Commit 1d636b5

Browse files
committed
fix: keep original line endings while writing and reading
1 parent b4553c2 commit 1d636b5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/matcher/plugin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def _maybe_store_pattern(self, text: str) -> None:
101101
self._pattern_filename.parent.mkdir(parents=True)
102102

103103
# Store!
104-
self._pattern_filename.write_text(text)
104+
with self._pattern_filename.open('w', newline='') as f:
105+
f.write(text)
105106

106107
# Also mark the test as skipped!
107108
pytest.skip(f'Pattern file has been saved `{self._pattern_filename}`')
@@ -111,7 +112,8 @@ def _read_expected_file_content(self) -> None:
111112
pytest.skip(f'Pattern file not found `{self._pattern_filename}`')
112113

113114
if self._expected_file_content is None:
114-
self._expected_file_content = self._pattern_filename.read_text()
115+
with self._pattern_filename.open(newline='') as f:
116+
self._expected_file_content = f.read()
115117

116118
def __eq__(self, text: object) -> bool:
117119
if not isinstance(text, str):

test/test_matcher.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,21 @@ def test_sfx(capfd, expected_out):
433433
# Run all tests with pytest
434434
result = pytester.runpytest()
435435
result.assert_outcomes(passed=1)
436+
437+
438+
def crlf_test(ourtestdir, expectdir) -> None:
439+
# Write a sample expectations file
440+
expectdir.makepatternfile('.out', test_sample_out='Hello Africa!\r\n')
441+
# Write a sample test
442+
ourtestdir.makepyfile(r"""
443+
def test_sample_out(capfd, expected_out):
444+
print('Hello Africa!\r\n', end='')
445+
stdout, stderr = capfd.readouterr()
446+
assert expected_out == stdout
447+
assert stderr == ''
448+
"""
449+
)
450+
451+
# Run all tests with pytest
452+
result = ourtestdir.runpytest()
453+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)