robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 5local mkdir =t . mkdir 6local clear =n . clear 7local eq =t . eq 8local fn =n . fn 9local api =n . api 10local exec_lua =n . exec_lua 11local read_file =t . read_file 12local write_file =t . write_file 13local pcall_err =t . pcall_err 14local command =n . command 15 16local fname ='Xtest-functional-eval-writefile' 17local dname =fname .. '.d' 18local dfname_tail ='1' 19local dfname =dname .. '/' .. dfname_tail 20local ddname_tail ='2' 21local ddname =dname .. '/' .. ddname_tail 22 23before_each ( function () 24mkdir ( dname ) 25mkdir ( ddname ) 26clear () 27end ) 28 29after_each ( function () 30os . remove ( fname ) 31os . remove ( dfname ) 32vim . uv . fs_rmdir ( ddname ) 33vim . uv . fs_rmdir ( dname ) 34end ) 35 36describe ( 'writefile()' , function () 37it ( 'writes empty list to a file' , function () 38eq ( nil , read_file ( fname )) 39eq ( 0 , fn . writefile ( {} , fname )) 40eq ( '' , read_file ( fname )) 41os . remove ( fname ) 42eq ( nil , read_file ( fname )) 43eq ( 0 , fn . writefile ( {} , fname , 'b' )) 44eq ( '' , read_file ( fname )) 45os . remove ( fname ) 46eq ( nil , read_file ( fname )) 47eq ( 0 , fn . writefile ( {} , fname , 'ab' )) 48eq ( '' , read_file ( fname )) 49os . remove ( fname ) 50eq ( nil , read_file ( fname )) 51eq ( 0 , fn . writefile ( {} , fname , 'a' )) 52eq ( '' , read_file ( fname )) 53end ) 54 55it ( 'writes list with an empty string to a file' , function () 56command (( 'call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s", "b")' ): format ( fname )) 57eq ( '' , read_file ( fname )) 58command (( 'call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s")' ): format ( fname )) 59eq ( '\n' , read_file ( fname )) 60end ) 61 62it ( 'writes list with a null string to a file' , function () 63command (( 'call writefile([v:_null_string], "%s", "b")' ): format ( fname )) 64eq ( '' , read_file ( fname )) 65command (( 'call writefile([v:_null_string], "%s")' ): format ( fname )) 66eq ( '\n' , read_file ( fname )) 67end ) 68 69it ( 'writes a null blob to a file' , function () 70eq ( 0 , fn . writefile ( { 'line1' } , fname , 'b' )) 71eq ( 'line1' , read_file ( fname )) 72command (( 'call writefile(v:_null_blob, "%s")' ): format ( fname )) 73eq ( '' , read_file ( fname )) 74end ) 75 76it ( 'appends to a file' , function () 77eq ( nil , read_file ( fname )) 78eq ( 0 , fn . writefile ( { 'abc' , 'def' , 'ghi' } , fname )) 79eq ( 'abc\ndef\nghi\n' , read_file ( fname )) 80eq ( 0 , fn . writefile ( { 'jkl' } , fname , 'a' )) 81eq ( 'abc\ndef\nghi\njkl\n' , read_file ( fname )) 82os . remove ( fname ) 83eq ( nil , read_file ( fname )) 84eq ( 0 , fn . writefile ( { 'abc' , 'def' , 'ghi' } , fname , 'b' )) 85eq ( 'abc\ndef\nghi' , read_file ( fname )) 86eq ( 0 , fn . writefile ( { 'jkl' } , fname , 'ab' )) 87eq ( 'abc\ndef\nghijkl' , read_file ( fname )) 88end ) 89 90it ( 'correctly treats NLs' , function () 91eq ( 0 , fn . writefile ( { '\na\nb\n' } , fname , 'b' )) 92eq ( '\0a\0b\0' , read_file ( fname )) 93eq ( 0 , fn . writefile ( { 'a\n\n\nb' } , fname , 'b' )) 94eq ( 'a\0\0\0b' , read_file ( fname )) 95end ) 96 97it ( 'writes with s and S' , function () 98eq ( 0 , fn . writefile ( { '\na\nb\n' } , fname , 'bs' )) 99eq ( '\0a\0b\0' , read_file ( fname )) 100eq ( 0 , fn . writefile ( { 'a\n\n\nb' } , fname , 'bS' )) 101eq ( 'a\0\0\0b' , read_file ( fname )) 102end ) 103 104it ( 'correctly overwrites file' , function () 105eq ( 0 , fn . writefile ( { '\na\nb\n' } , fname , 'b' )) 106eq ( '\0a\0b\0' , read_file ( fname )) 107eq ( 0 , fn . writefile ( { 'a\n' } , fname , 'b' )) 108eq ( 'a\0' , read_file ( fname )) 109end ) 110 111it ( 'writes Lua (binary) strings' , function () 112eq ( 0 , exec_lua ( [[return vim.fn.writefile('foo\0bar', ..., 'b')]] , fname )) 113eq ( 'foo\0bar' , read_file ( fname )) 114end ) 115 116it ( 'writes RPC-API (binary) String' , function () 117eq ( 0 , api . nvim_call_function ( 'writefile' , { 'foobar' , fname } )) 118eq ( 'foobar' , read_file ( fname )) 119eq ( 0 , api . nvim_call_function ( 'writefile' , { 'foo\0bar' , fname } )) 120eq ( 'foo\0bar' , read_file ( fname )) 121end ) 122 123it ( 'shows correct file name when supplied numbers' , function () 124api . nvim_set_current_dir ( dname ) 125eq ( 126"Vim(call):E482: Can't open file 2 for writing: illegal operation on a directory" , 127pcall_err ( command , ( 'call writefile([42], %s)' ): format ( ddname_tail )) 128) 129end ) 130 131it ( 'writefile(..., "p") creates missing parent directories' , function () 132os . remove ( dname ) 133eq ( nil , read_file ( dfname )) 134eq ( 0 , fn . writefile ( { 'abc' , 'def' , 'ghi' } , dfname , 'p' )) 135eq ( 'abc\ndef\nghi\n' , read_file ( dfname )) 136os . remove ( dfname ) 137os . remove ( dname ) 138eq ( nil , read_file ( dfname )) 139eq ( 0 , fn . writefile ( { '\na\nb\n' } , dfname , 'pb' )) 140eq ( '\0a\0b\0' , read_file ( dfname )) 141os . remove ( dfname ) 142os . remove ( dname ) 143eq ( 144'Vim(call):E32: No file name' , 145pcall_err ( command , ( 'call writefile([], "%s", "p")' ): format ( dfname .. '.d/' )) 146) 147eq ( 148"Vim(call):E482: Can't open file ./ for writing: illegal operation on a directory" , 149pcall_err ( command , 'call writefile([], "./", "p")' ) 150) 151eq ( 152"Vim(call):E482: Can't open file . for writing: illegal operation on a directory" , 153pcall_err ( command , 'call writefile([], ".", "p")' ) 154) 155end ) 156 157it ( 'errors out with invalid arguments' , function () 158write_file ( fname , 'TEST' ) 159eq ( 160'Vim(call):E119: Not enough arguments for function: writefile' , 161pcall_err ( command , 'call writefile()' ) 162) 163eq ( 164'Vim(call):E119: Not enough arguments for function: writefile' , 165pcall_err ( command , 'call writefile([])' ) 166) 167eq ( 168'Vim(call):E118: Too many arguments for function: writefile' , 169pcall_err ( command , ( 'call writefile([], "%s", "b", 1)' ): format ( fname )) 170) 171for _ , arg in ipairs ( { '0' , '0.0' , 'function("tr")' , '{}' , '"test"' } ) do 172eq ( 173'Vim(call):E475: Invalid argument: writefile() first argument must be a List or a Blob' , 174pcall_err ( command , ( 'call writefile(%s, "%s", "b")' ): format ( arg , fname )) 175) 176end 177for _ , args in ipairs ( { '[], %s, "b"' , '[], "' .. fname .. '", %s' } ) do 178eq ( 179'Vim(call):E730: Using a List as a String' , 180pcall_err ( command , ( 'call writefile(%s)' ): format ( args : format ( '[]' ))) 181) 182eq ( 183'Vim(call):E731: Using a Dictionary as a String' , 184pcall_err ( command , ( 'call writefile(%s)' ): format ( args : format ( '{}' ))) 185) 186eq ( 187'Vim(call):E729: Using a Funcref as a String' , 188pcall_err ( command , ( 'call writefile(%s)' ): format ( args : format ( 'function("tr")' ))) 189) 190end 191eq ( 192'Vim(call):E5060: Unknown flag: «»' , 193pcall_err ( command , ( 'call writefile([], "%s", "bs«»")' ): format ( fname )) 194) 195eq ( 'TEST' , read_file ( fname )) 196end ) 197 198it ( 'does not write to file if error in list' , function () 199local args ='["tset"] + repeat([%s], 3), "' .. fname .. '"' 200eq ( 201'Vim(call):E805: Expected a Number or a String, Float found' , 202pcall_err ( command , ( 'call writefile(%s)' ): format ( args : format ( '0.0' ))) 203) 204eq ( nil , read_file ( fname )) 205write_file ( fname , 'TEST' ) 206eq ( 207'Vim(call):E745: Expected a Number or a String, List found' , 208pcall_err ( command , ( 'call writefile(%s)' ): format ( args : format ( '[]' ))) 209) 210eq ( 'TEST' , read_file ( fname )) 211eq ( 212'Vim(call):E728: Expected a Number or a String, Dictionary found' , 213pcall_err ( command , ( 'call writefile(%s)' ): format ( args : format ( '{}' ))) 214) 215eq ( 'TEST' , read_file ( fname )) 216eq ( 217'Vim(call):E703: Expected a Number or a String, Funcref found' , 218pcall_err ( command , ( 'call writefile(%s)' ): format ( args : format ( 'function("tr")' ))) 219) 220eq ( 'TEST' , read_file ( fname )) 221end ) 222end )