Skip to content

Commit

Permalink
use unix line ending \n as default if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 5, 2024
1 parent c7ec2de commit ae125c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/monty/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import os
import subprocess
import time
import warnings
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -80,7 +81,8 @@ def _get_line_ending(
raise TypeError(f"Unknown file type {type(file).__name__}")

if not first_line:
raise ValueError("empty file.")
warnings.warn("File empty, use default line ending \n.", stacklevel=2)
return "\n"

if first_line.endswith(b"\r\n"):
return "\r\n"
Expand Down
16 changes: 9 additions & 7 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def test_empty_file(self):
test_file = "empty_file.txt"
open(test_file, "w").close()

with pytest.raises(ValueError, match="empty file"):
_get_line_ending(test_file)
with pytest.warns(match="File empty, use default line ending \n"):
assert _get_line_ending(test_file) == "\n"

def test_unknown_line_ending(self):
with ScratchDir("."):
Expand Down Expand Up @@ -119,9 +119,10 @@ def test_empty_file(self):
Make sure an empty file does not throw an error when reverse_readline
is called, which was a problem with an earlier implementation.
"""
with open(os.path.join(TEST_DIR, "empty_file.txt")) as f:
for _line in reverse_readline(f):
raise ValueError("an empty file is being read!")
with pytest.warns(match="File empty, use default line ending \n."):
with open(os.path.join(TEST_DIR, "empty_file.txt")) as f:
for _line in reverse_readline(f):
raise ValueError("an empty file is being read!")

@pytest.fixture()
def test_line_ending(self):
Expand Down Expand Up @@ -189,8 +190,9 @@ def test_empty_file(self):
Make sure an empty file does not throw an error when reverse_readline
is called, which was a problem with an earlier implementation.
"""
for _line in reverse_readfile(os.path.join(TEST_DIR, "empty_file.txt")):
raise ValueError("an empty file is being read!")
with pytest.warns(match="File empty, use default line ending \n."):
for _line in reverse_readfile(os.path.join(TEST_DIR, "empty_file.txt")):
raise ValueError("an empty file is being read!")

@pytest.fixture
def test_line_ending(self):
Expand Down

0 comments on commit ae125c3

Please sign in to comment.