@@ -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