Skip to content

Commit 8eafce9

Browse files
committed
Made clean_writer output a single newline in cases where the input is empty.
1 parent 35d58ab commit 8eafce9

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

domdf_python_tools/paths.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ def clean_writer(string: str, fp: IO) -> None:
264264
while buffer[-1:] == ['']:
265265
buffer = buffer[:-1]
266266

267+
if not buffer:
268+
fp.write('\n')
269+
267270
for line in buffer:
268271
fp.write(line)
269272
fp.write('\n')

tests/test_paths.py

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -261,45 +261,31 @@ def test_clean_writer():
261261
"""
262262

263263

264-
def test_pathplus_write_clean():
264+
@pytest.mark.parametrize(
265+
"input_string, output_string",
266+
[(["Top line", " ", "Line with whitespace ", "Line with tabs ", "No newline at end of file"
267+
], ["Top line", '', "Line with whitespace", "Line with tabs", "No newline at end of file", ""]),
268+
([
269+
"Top line",
270+
" ",
271+
"Line with whitespace ",
272+
"Line with tabs ",
273+
"Too many newlines\n\n\n\n\n\n\n"
274+
], [
275+
"Top line",
276+
"",
277+
"Line with whitespace",
278+
"Line with tabs",
279+
"Too many newlines",
280+
"",
281+
]), ([], ["\n"])]
282+
)
283+
def test_pathplus_write_clean(input_string, output_string):
265284
with TemporaryDirectory() as tmpdir:
266285
tempfile = PathPlus(tmpdir) / "tmpfile.txt"
267286

268-
test_string = "\n".join([
269-
"Top line",
270-
" ",
271-
"Line with whitespace ",
272-
"Line with tabs ",
273-
"No newline at end of file",
274-
])
275-
276-
with tempfile.open("w") as fp:
277-
tempfile.write_clean(test_string)
278-
279-
assert tempfile.read_text() == """Top line
280-
281-
Line with whitespace
282-
Line with tabs
283-
No newline at end of file
284-
"""
285-
# Again with lots of newlines
286-
test_string = "\n".join([
287-
"Top line",
288-
" ",
289-
"Line with whitespace ",
290-
"Line with tabs ",
291-
"Too many newlines\n\n\n\n\n\n\n",
292-
])
293-
294-
with tempfile.open("w") as fp:
295-
tempfile.write_clean(test_string)
296-
297-
assert tempfile.read_text() == """Top line
298-
299-
Line with whitespace
300-
Line with tabs
301-
Too many newlines
302-
"""
287+
tempfile.write_clean("\n".join(input_string))
288+
assert tempfile.read_text() == "\n".join(output_string)
303289

304290

305291
@pytest.mark.xfail(reason="Unsupported on PyPy3 <7.2", condition=(platform.python_implementation() == "PyPy"))

0 commit comments

Comments
 (0)