Skip to content

Commit 7da186d

Browse files
authored
Merge pull request #3 from community-of-python/skip-binary-files
skip binary files
2 parents 390fa0a + 2be11c6 commit 7da186d

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

end_of_file_fixer/main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@
77
import pathspec
88

99

10-
def _fix_file(file_obj: IO[bytes], check: bool) -> int: # noqa: C901
10+
def _is_binary(file_obj: IO[bytes]) -> bool:
11+
current_pos = file_obj.tell()
12+
file_obj.seek(0)
13+
sample = file_obj.read(1024)
14+
file_obj.seek(current_pos)
15+
return b"\x00" in sample
16+
17+
18+
def _fix_file(file_obj: IO[bytes], check: bool) -> int: # noqa: C901, PLR0911
19+
# Skip binary files
20+
if _is_binary(file_obj):
21+
return 0
22+
1123
# Test for newline at end of file
1224
# Empty files will throw IOError here
1325
try:

tests/test_end_of_file_fixer.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,58 @@ def test_end_of_file_fixer_with_gitignore() -> None:
188188
# The .tmp file should be unchanged since it's ignored
189189
temp_file_content = (temp_path / "temp_file.tmp").read_text()
190190
assert temp_file_content == "This is a temporary file that should be ignored"
191+
192+
193+
def test_end_of_file_fixer_skips_binary_files() -> None:
194+
# Create a temporary directory with test files including a binary file
195+
with tempfile.TemporaryDirectory() as temp_dir:
196+
temp_path = Path(temp_dir)
197+
198+
# Copy all fixture files to temp directory
199+
fixtures_dir = Path(__file__).parent / "fixtures"
200+
for fixture_file in fixtures_dir.glob("*"):
201+
assert shutil.copy(fixture_file, temp_path / fixture_file.name)
202+
203+
# Create a binary file with null bytes
204+
binary_file = temp_path / "binary_file.bin"
205+
binary_file.write_bytes(b"\x00\x01\x02\x03\x04\x05")
206+
207+
# Also create a text file without newline that should be fixed
208+
text_file = temp_path / "text_no_newline.txt"
209+
text_file.write_text("This is a text file without newline")
210+
211+
# Change to the temp directory and capture stdout
212+
original_cwd = Path.cwd()
213+
original_argv = sys.argv
214+
original_stdout = sys.stdout
215+
captured_output = StringIO()
216+
217+
try:
218+
os.chdir(temp_dir)
219+
sys.argv = ["end-of-file-fixer", "."]
220+
sys.stdout = captured_output
221+
222+
# Run end-of-file-fixer . command
223+
result = main()
224+
225+
# Should exit with code 1 (since the text file needed fixing)
226+
assert result == 1
227+
228+
# Should output that the text file is being fixed
229+
output = captured_output.getvalue()
230+
assert "text_no_newline.txt" in output
231+
# Should NOT mention the binary file
232+
assert "binary_file.bin" not in output
233+
234+
finally:
235+
os.chdir(original_cwd)
236+
sys.argv = original_argv
237+
sys.stdout = original_stdout
238+
239+
# Check that text file was fixed
240+
text_content = (temp_path / "text_no_newline.txt").read_text()
241+
assert text_content == "This is a text file without newline\n"
242+
243+
# Check that binary file was not modified
244+
binary_content = (temp_path / "binary_file.bin").read_bytes()
245+
assert binary_content == b"\x00\x01\x02\x03\x04\x05"

0 commit comments

Comments
 (0)