@@ -895,7 +895,18 @@ describe("Path2", function()
895895 end )
896896 end )
897897
898+ local function diff_str (a , b )
899+ a = a :gsub (" \n " , " \\ n" ):gsub (" \r " , " \\ r" ):gsub (" \t " , " \\ t" )
900+ b = b :gsub (" \n " , " \\ n" ):gsub (" \r " , " \\ r" ):gsub (" \t " , " \\ t" )
901+ --- @diagnostic disable-next-line : missing-fields
902+ return vim .diff (a , b , {})
903+ end
904+
898905 describe (" head" , function ()
906+ after_each (function ()
907+ uv .fs_unlink " foobar.txt"
908+ end )
909+
899910 it_cross_plat (" should read head of file" , function ()
900911 local p = Path :new " LICENSE"
901912 local data = p :head ()
@@ -920,7 +931,7 @@ furnished to do so, subject to the following conditions:]]
920931 assert .are .same (should , data )
921932 end )
922933
923- it_cross_plat (" head should max read whole file" , function ()
934+ it_cross_plat (" should max read whole file" , function ()
924935 local p = Path :new " LICENSE"
925936 local data = p :head (1000 )
926937 local should = [[ MIT License
@@ -946,9 +957,43 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
946957SOFTWARE.]]
947958 assert .are .same (should , data )
948959 end )
960+
961+ it_cross_plat (" handles unix lf line endings" , function ()
962+ local p = Path :new " foobar.txt"
963+ p :touch ()
964+
965+ local txt = " foo\n bar\n baz"
966+ p :write (txt , " w" )
967+ local data = p :head ()
968+ assert .are .same (txt , data , diff_str (txt , data ))
969+ end )
970+
971+ it_cross_plat (" handles windows crlf line endings" , function ()
972+ local p = Path :new " foobar.txt"
973+ p :touch ()
974+
975+ local txt = " foo\r\n bar\r\n baz"
976+ p :write (txt , " w" )
977+ local data = p :head ()
978+ assert .are .same (txt , data , diff_str (txt , data ))
979+ end )
980+
981+ it_cross_plat (" handles mac cr line endings" , function ()
982+ local p = Path :new " foobar.txt"
983+ p :touch ()
984+
985+ local txt = " foo\r bar\r baz"
986+ p :write (txt , " w" )
987+ local data = p :head ()
988+ assert .are .same (txt , data , diff_str (txt , data ))
989+ end )
949990 end )
950991
951992 describe (" tail" , function ()
993+ after_each (function ()
994+ uv .fs_unlink " foobar.txt"
995+ end )
996+
952997 it_cross_plat (" should read tail of file" , function ()
953998 local p = Path :new " LICENSE"
954999 local data = p :tail ()
@@ -972,7 +1017,7 @@ SOFTWARE.]]
9721017 assert .are .same (should , data )
9731018 end )
9741019
975- it_cross_plat (" tail should max read whole file" , function ()
1020+ it_cross_plat (" should max read whole file" , function ()
9761021 local p = Path :new " LICENSE"
9771022 local data = p :tail (1000 )
9781023 local should = [[ MIT License
@@ -998,5 +1043,46 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
9981043SOFTWARE.]]
9991044 assert .are .same (should , data )
10001045 end )
1046+
1047+ it_cross_plat (" handles unix lf line endings" , function ()
1048+ local p = Path :new " foobar.txt"
1049+ p :touch ()
1050+
1051+ local txt = " foo\n bar\n baz"
1052+ p :write (txt , " w" )
1053+ local data = p :tail ()
1054+ assert .are .same (txt , data , diff_str (txt , data ))
1055+ end )
1056+
1057+ it_cross_plat (" handles windows crlf line endings" , function ()
1058+ local p = Path :new " foobar.txt"
1059+ p :touch ()
1060+
1061+ local txt = " foo\r\n bar\r\n baz"
1062+ p :write (txt , " w" )
1063+ local data = p :tail ()
1064+ assert .are .same (txt , data , diff_str (txt , data ))
1065+ end )
1066+
1067+ it_cross_plat (" handles mac cr line endings" , function ()
1068+ local p = Path :new " foobar.txt"
1069+ p :touch ()
1070+
1071+ local txt = " foo\r bar\r baz"
1072+ p :write (txt , " w" )
1073+ local data = p :tail ()
1074+ assert .are .same (txt , data , diff_str (txt , data ))
1075+ end )
1076+
1077+ it_cross_plat (" handles extra newlines" , function ()
1078+ local p = Path :new " foobar.txt"
1079+ p :touch ()
1080+
1081+ local txt = " foo\n bar\n baz\n\n\n "
1082+ local expect = " foo\n bar\n baz\n\n "
1083+ p :write (txt , " w" )
1084+ local data = p :tail ()
1085+ assert .are .same (expect , data , diff_str (expect , data ))
1086+ end )
10011087 end )
10021088end )
0 commit comments