1+ from patchwork .common .utils .utils import open_with_chardet
2+
3+ def test_empty_file ():
4+ # Test empty file
5+ with open ('test_empty.txt' , 'wb' ) as f :
6+ f .write (b'' )
7+
8+ with open_with_chardet ('test_empty.txt' , 'r' ) as f :
9+ content = f .read ()
10+
11+ with open ('test_empty_out.txt' , 'w' ) as f :
12+ f .write (content )
13+
14+ with open ('test_empty.txt' , 'rb' ) as f1 , open ('test_empty_out.txt' , 'rb' ) as f2 :
15+ assert f1 .read () == f2 .read (), "Empty file test failed"
16+ print ("Empty file test passed" )
17+
18+ def test_mixed_endings ():
19+ # Test file with mixed line endings
20+ with open ('test_mixed.txt' , 'wb' ) as f :
21+ f .write (b'line1\n ' ) # LF
22+ f .write (b'line2\r \n ' ) # CRLF
23+ f .write (b'line3\n ' ) # LF
24+
25+ with open_with_chardet ('test_mixed.txt' , 'r' ) as f :
26+ content = f .read ()
27+
28+ with open ('test_mixed_out.txt' , 'w' ) as f :
29+ f .write (content )
30+
31+ with open ('test_mixed.txt' , 'rb' ) as f1 , open ('test_mixed_out.txt' , 'rb' ) as f2 :
32+ orig = f1 .read ()
33+ mod = f2 .read ()
34+ assert orig == mod , f"Mixed endings test failed:\n Original: { orig } \n Modified: { mod } "
35+ print ("Mixed endings test passed" )
36+
37+ def test_no_final_newline ():
38+ # Test file without final newline
39+ with open ('test_no_final.txt' , 'wb' ) as f :
40+ f .write (b'line1\r \n line2\r \n line3' ) # No final newline
41+
42+ with open_with_chardet ('test_no_final.txt' , 'r' ) as f :
43+ content = f .read ()
44+
45+ with open ('test_no_final_out.txt' , 'w' ) as f :
46+ f .write (content )
47+
48+ with open ('test_no_final.txt' , 'rb' ) as f1 , open ('test_no_final_out.txt' , 'rb' ) as f2 :
49+ orig = f1 .read ()
50+ mod = f2 .read ()
51+ assert orig == mod , f"No final newline test failed:\n Original: { orig } \n Modified: { mod } "
52+ print ("No final newline test passed" )
53+
54+ if __name__ == '__main__' :
55+ test_empty_file ()
56+ test_mixed_endings ()
57+ test_no_final_newline ()
58+ print ("All edge case tests passed!" )
0 commit comments