robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each , pending = 5t . describe , t . it , t . before_each , t . after_each , t . pending 6local eq , eval , clear , write_file , source , insert = 7t . eq , n . eval , n . clear , t . write_file , n . source , n . insert 8local pcall_err =t . pcall_err 9local command =n . command 10local feed_command =n . feed_command 11local fn =n . fn 12local api =n . api 13local skip =t . skip 14local is_os =t . is_os 15local read_file =t . read_file 16 17local fname ='Xtest-functional-ex_cmds-write' 18local fname_bak =fname .. '~' 19local fname_broken =fname_bak .. 'broken' 20 21describe ( ':write' , function () 22local function cleanup () 23n . rmdir ( 'Xtest_write' ) 24os . remove ( 'Xtest_bkc_file.txt' ) 25os . remove ( 'Xtest_bkc_link.txt' ) 26os . remove ( 'Xtest_fifo' ) 27os . remove ( fname ) 28os . remove ( fname_bak ) 29os . remove ( fname_broken ) 30end 31before_each ( function () 32clear () 33cleanup () 34end ) 35after_each ( function () 36cleanup () 37end ) 38 39it ( '&backupcopy=auto preserves symlinks' , function () 40command ( 'set backupcopy=auto' ) 41write_file ( 'Xtest_bkc_file.txt' , 'content0' ) 42if is_os ( 'win' ) then 43command ( 'silent !mklink Xtest_bkc_link.txt Xtest_bkc_file.txt' ) 44else 45command ( 'silent !ln -s Xtest_bkc_file.txt Xtest_bkc_link.txt' ) 46end 47if eval ( 'v:shell_error' ) ~= 0 then 48pending ( 'Cannot create symlink' ) 49end 50source ( [[ 51edit Xtest_bkc_link.txt 52call setline(1, ['content1']) 53write 54]] ) 55eq ( eval ( "['content1']" ), eval ( "readfile('Xtest_bkc_file.txt')" )) 56eq ( eval ( "['content1']" ), eval ( "readfile('Xtest_bkc_link.txt')" )) 57end ) 58 59it ( '&backupcopy=no replaces symlink with new file' , function () 60command ( 'set backupcopy=no' ) 61write_file ( 'Xtest_bkc_file.txt' , 'content0' ) 62if is_os ( 'win' ) then 63command ( 'silent !mklink Xtest_bkc_link.txt Xtest_bkc_file.txt' ) 64else 65command ( 'silent !ln -s Xtest_bkc_file.txt Xtest_bkc_link.txt' ) 66end 67if eval ( 'v:shell_error' ) ~= 0 then 68pending ( 'Cannot create symlink' ) 69end 70source ( [[ 71edit Xtest_bkc_link.txt 72call setline(1, ['content1']) 73write 74]] ) 75eq ( eval ( "['content0']" ), eval ( "readfile('Xtest_bkc_file.txt')" )) 76eq ( eval ( "['content1']" ), eval ( "readfile('Xtest_bkc_link.txt')" )) 77end ) 78 79it ( 'appends FIFO file' , function () 80-- mkfifo creates read-only .lnk files on Windows 81if is_os ( 'win' ) or eval ( "executable('mkfifo')" ) == 0 then 82pending ( 'N/A: missing "mkfifo" command' ) 83end 84 85local text ='some fifo text from write_spec' 86assert ( os . execute ( 'mkfifo Xtest_fifo' )) 87insert ( text ) 88 89-- Blocks until a consumer reads the FIFO. 90feed_command ( 'write >> Xtest_fifo' ) 91 92-- Read the FIFO, this will unblock the :write above. 93local fifo =assert ( io . open ( 'Xtest_fifo' )) 94eq ( text .. '\n' , fifo : read ( '*all' )) 95fifo : close () 96end ) 97 98it ( '++p creates missing parent directories' , function () 99eq ( 0 , eval ( "filereadable('p_opt.txt')" )) 100command ( 'write ++p p_opt.txt' ) 101eq ( 1 , eval ( "filereadable('p_opt.txt')" )) 102os . remove ( 'p_opt.txt' ) 103 104eq ( 0 , eval ( "filereadable('p_opt.txt')" )) 105command ( 'write ++p ./p_opt.txt' ) 106eq ( 1 , eval ( "filereadable('p_opt.txt')" )) 107os . remove ( 'p_opt.txt' ) 108 109eq ( 0 , eval ( "filereadable('Xtest_write/write/p_opt.txt')" )) 110command ( 'write ++p Xtest_write/write/p_opt.txt' ) 111eq ( 1 , eval ( "filereadable('Xtest_write/write/p_opt.txt')" )) 112 113eq ( 0 , eval ( "filereadable('Xtest_write/write2/p_opt.txt')" )) 114eq ( 0 , eval ( "filereadable('Xtest_write/write2/p_opt2.txt')" )) 115eq ( 0 , eval ( "filereadable('Xtest_write/write3/p_opt3.txt')" )) 116command ( 'file Xtest_write/write2/p_opt.txt' ) 117command ( 'set modified' ) 118command ( 'sp Xtest_write/write2/p_opt2.txt' ) 119command ( 'set modified' ) 120command ( 'sp Xtest_write/write3/p_opt3.txt' ) 121-- don't set p_opt3.txt modified - assert it isn't written 122-- and that write3/ isn't created 123command ( 'wall ++p' ) 124eq ( 1 , eval ( "filereadable('Xtest_write/write2/p_opt.txt')" )) 125eq ( 1 , eval ( "filereadable('Xtest_write/write2/p_opt2.txt')" )) 126eq ( 0 , eval ( "filereadable('Xtest_write/write3/p_opt3.txt')" )) 127t . matches ( 128'E474: Invalid argument' , 129pcall_err ( command , 'read ++edits Xtest_write/write/p_opt.txt' ) 130) 131 132eq ( 'Vim(write):E32: No file name' , pcall_err ( command , 'write ++p Xotherdir/' )) 133eq ( 134( 'Vim(write):E17: "' .. fn . fnamemodify ( '.' , ':p:h' ) .. '" is a directory' ), 135pcall_err ( command , 'write ++p .' ) 136) 137eq ( 138( 'Vim(write):E17: "' .. fn . fnamemodify ( '.' , ':p:h' ) .. '" is a directory' ), 139pcall_err ( command , 'write ++p ./' ) 140) 141 142t . matches ( 143'E474: Invalid argument' , 144pcall_err ( command , 'write ++patate Xtest_write/garbage.txt' ) 145) 146end ) 147 148it ( 'errors out correctly' , function () 149command ( 'let $HOME=""' ) 150eq ( fn . fnamemodify ( '.' , ':p:h' ), fn . fnamemodify ( '.' , ':p:h:~' )) 151-- Message from check_overwrite 152eq ( 153( 'Vim(write):E17: "' .. fn . fnamemodify ( '.' , ':p:h' ) .. '" is a directory' ), 154pcall_err ( command , 'write .' ) 155) 156api . nvim_set_option_value ( 'writeany' , true , {} ) 157-- Message from buf_write 158eq ( 'Vim(write):E502: "." is a directory' , pcall_err ( command , 'write .' )) 159fn . mkdir ( fname_bak ) 160api . nvim_set_option_value ( 'backupdir' , '.' , {} ) 161api . nvim_set_option_value ( 'backup' , true , {} ) 162write_file ( fname , 'content0' ) 163command ( 'edit ' .. fname ) 164fn . setline ( 1 , 'TTY' ) 165eq ( "Vim(write):E510: Can't make backup file (add ! to override)" , pcall_err ( command , 'write' )) 166api . nvim_set_option_value ( 'backup' , false , {} ) 167fn . setfperm ( fname , 'r--------' ) 168eq ( 169'Vim(write):E505: "Xtest-functional-ex_cmds-write" is read-only (add ! to override)' , 170pcall_err ( command , 'write' ) 171) 172if is_os ( 'win' ) then 173eq ( 0 , os . execute ( 'del /q/f ' .. fname )) 174eq ( 0 , os . execute ( 'rd /q/s ' .. fname_bak )) 175else 176eq ( true , os . remove ( fname )) 177eq ( true , os . remove ( fname_bak )) 178end 179write_file ( fname_bak , 'TTYX' ) 180skip ( is_os ( 'win' ), [[FIXME: pcall_err(command, 'write!') outputs 0 in Windows]] ) 181vim . uv . fs_symlink ( fname_bak .. ( '/xxxxx' ): rep ( 20 ), fname ) 182eq ( "Vim(write):E166: Can't open linked file for writing" , pcall_err ( command , 'write!' )) 183end ) 184 185it ( 'fails converting a trailing incomplete sequence' , function () 186-- From https://github.com/neovim/neovim/issues/36990, an invalid UTF-8 sequence at the end of 187-- the file during conversion testing can overwrite the rest of the file during the real 188-- conversion. 189 190api . nvim_buf_set_lines ( 0 , 0 , 1 , true , { 'line 1' , 'line 2' , 'aaabbb\235\128' } ) 191command ( 'set noendofline nofixendofline' ) 192 193eq ( 194"Vim(write):E513: Write error, conversion failed in line 3 (make 'fenc' empty to override)" , 195pcall_err ( command , 'write ++enc=latin1 ' .. fname ) 196) 197end ) 198 199it ( 'converts to latin1 with an invalid sequence at buffer boundary' , function () 200-- From https://github.com/neovim/neovim/issues/36990, an invalid UTF-8 sequence that falls 201-- right at the end of the 8 KiB buffer used for encoding conversions causes subsequent data to 202-- be overwritten. 203 204local content =string . rep ( 'a' , 1024 * 8 - 1 ) .. '\251' .. string . rep ( 'b' , 20 ) 205api . nvim_buf_set_lines ( 0 , 0 , 1 , true , { content } ) 206command ( 'set noendofline nofixendofline fenc=latin1' ) 207command ( 'write ' .. fname ) 208 209local tail =string . sub ( read_file ( fname ) or '' , - 10 ) 210eq ( 'bbbbbbbbbb' , tail ) 211end ) 212 213it ( 'converts to CP1251 with iconv' , function () 214api . nvim_buf_set_lines ( 2150 , 2160 , 2171 , 218true , 219{ 'Привет, мир!' , 'Это простой тест.' } 220) 221command ( 'write ++enc=cp1251 ++ff=unix ' .. fname ) 222 223eq ( 224'\207\240\232\226\229\242, \236\232\240!\n' 225.. '\221\242\238 \239\240\238\241\242\238\233 \242\229\241\242.\n' , 226read_file ( fname ) 227) 228end ) 229 230it ( 'converts to GB18030 with iconv' , function () 231api . nvim_buf_set_lines ( 0 , 0 , 1 , true , { '你好,世界!' , '这是一个测试。' } ) 232command ( 'write ++enc=gb18030 ++ff=unix ' .. fname ) 233 234eq ( 235'\196\227\186\195\163\172\202\192\189\231\163\161\n' 236.. '\213\226\202\199\210\187\184\246\178\226\202\212\161\163\n' , 237read_file ( fname ) 238) 239end ) 240 241it ( 'converts to Shift_JIS with iconv' , function () 242api . nvim_buf_set_lines ( 2430 , 2440 , 2451 , 246true , 247{ 'こんにちは、世界!' , 'これはテストです。' } 248) 249command ( 'write ++enc=sjis ++ff=unix ' .. fname ) 250 251eq ( 252'\130\177\130\241\130\201\130\191\130\205\129A\144\162\138E\129I\n' 253.. '\130\177\130\234\130\205\131e\131X\131g\130\197\130\183\129B\n' , 254read_file ( fname ) 255) 256end ) 257 258it ( 'fails converting an illegal sequence with iconv' , function () 259api . nvim_buf_set_lines ( 0 , 0 , 1 , true , { 'line 1' , 'aaa\128bbb' } ) 260 261eq ( 262"Vim(write):E513: Write error, conversion failed (make 'fenc' empty to override)" , 263pcall_err ( command , 'write ++enc=cp1251 ' .. fname ) 264) 265end ) 266 267it ( 'unaffected if a BufWritePre autocmd changes CWD away from the file #41838' , function () 268-- Leaving the file's directory clears its short name; the write must fall back to the full one. 269local dir =vim . fs . normalize ( t . tmpname ( false )) 270t . mkdir ( dir ) 271t . mkdir ( dir .. '/other' ) 272local file =dir .. '/f.txt' 273command ( 'edit ' .. file ) 274command ( 'lcd ' .. dir ) 275eq ( 'f.txt' , fn . bufname ( '%' )) 276command (( 'autocmd BufWritePre <buffer> lcd %s/other' ): format ( dir )) 277api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'hello' } ) 278command ( 'write' ) 279 280eq ( file , fn . bufname ( '%' )) 281eq ( { 'hello' } , fn . readfile ( file )) 282end ) 283 284it ( 'unaffected if a Progress handler changes CWD #41417' , function () 285-- ASAN catches the read of the freed name. Also assert the written file name/contents. 286local dir =vim . fs . normalize ( t . tmpname ( false )) 287t . mkdir ( dir ) 288local file =dir .. '/f.txt' 289write_file ( file , 'one\n' ) 290command ( 'edit ' .. file ) 291command ( 'lcd ' .. dir ) 292eq ( 'f.txt' , fn . bufname ( '%' )) 293-- Frees every buffer's short name, twice; the net CWD is unchanged. 294command (( 'autocmd Progress * lcd %s | lcd %s' ): format ( vim . fs . dirname ( dir ), dir )) 295api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'one' , 'two' } ) 296command ( 'write' ) 297 298eq ( 'f.txt' , fn . bufname ( '%' )) 299eq ( { 'one' , 'two' } , fn . readfile ( file )) 300eq ( { 'f.txt' } , fn . readdir ( dir )) 301end ) 302 303it ( 'handles a multi-byte sequence crossing the buffer boundary converting with iconv' , function () 304local content =string . rep ( 'a' , 1024 * 8 - 1 ) .. 'Дbbbbb' 305api . nvim_buf_set_lines ( 0 , 0 , 1 , true , { content } ) 306-- Skip the backup so we're testing the "checking" phase also. 307command ( 'set nowritebackup' ) 308command ( 'write ++enc=cp1251 ++ff=unix ' .. fname ) 309 310local expected =string . rep ( 'a' , 1024 * 8 - 1 ) .. '\196bbbbb\n' 311eq ( expected , read_file ( fname )) 312end ) 313end ) 314 315describe ( ':update' , function () 316before_each ( function () 317clear () 318fn . mkdir ( 'Xtest_update' , 'p' ) 319end ) 320 321after_each ( function () 322fn . delete ( 'Xtest_update' , 'rf' ) 323end ) 324 325it ( 'works for a new buffer' , function () 326command ( 'edit Xtest_update/foo/bar/nonexist.taz | update ++p' ) 327eq ( 1 , eval ( "filereadable('Xtest_update/foo/bar/nonexist.taz')" )) 328end ) 329 330it ( 'writes modified buffer' , function () 331command ( 'edit Xtest_update/modified.txt' ) 332command ( 'call setline(1, "hello world")' ) 333command ( 'update' ) 334eq ( { 'hello world' } , fn . readfile ( 'Xtest_update/modified.txt' )) 335end ) 336 337it ( 'does not write unmodified existing file' , function () 338local filename ='Xtest_update/existing.txt' 339local fd =io . open ( filename , 'w' ) 340fd : write ( 'content' ) 341fd : close () 342local mtime_before =fn . getftime ( filename ) 343command ( 'edit ' .. filename ) 344command ( 'update' ) 345eq ( mtime_before , fn . getftime ( filename )) 346end ) 347 348it ( 'creates parent directories with ++p' , function () 349command ( 'edit Xtest_update/deep/nested/path/file.txt' ) 350command ( 'call setline(1, "content")' ) 351command ( 'update ++p' ) 352eq ( 1 , eval ( "filereadable('Xtest_update/deep/nested/path/file.txt')" )) 353end ) 354 355it ( 'fails gracefully for unnamed buffer' , function () 356command ( 'enew' ) 357command ( 'call setline(1, "some content")' ) 358eq ( 'Vim(update):E32: No file name' , pcall_err ( command , 'update' )) 359end ) 360 361it ( 'respects readonly files' , function () 362local filename ='Xtest_update/readonly.txt' 363local fd =io . open ( filename , 'w' ) 364fd : write ( 'readonly content' ) 365fd : close () 366command ( 'edit ' .. filename ) 367command ( 'set readonly' ) 368command ( 'call setline(1, "modified")' ) 369eq ( 370"Vim(update):E45: 'readonly' option is set (add ! to override)" , 371pcall_err ( command , 'update' ) 372) 373 374command ( 'update!' ) 375eq ( { 'modified' } , fn . readfile ( 'Xtest_update/readonly.txt' )) 376end ) 377 378it ( 'can write line ranges' , function () 379command ( 'edit Xtest_update/range.txt' ) 380command ( 'call setline(1, ["line1", "line2", "line3", "line4"])' ) 381command ( '2,3update!' ) 382eq ( { 'line2' , 'line3' } , fn . readfile ( 'Xtest_update/range.txt' )) 383end ) 384 385it ( 'can append to existing file' , function () 386local filename ='Xtest_update/append.txt' 387local fd =io . open ( filename , 'w' ) 388fd : write ( 'existing\n' ) 389fd : close () 390command ( 'edit Xtest_update/new_content.txt' ) 391command ( 'call setline(1, "new content")' ) 392command ( 'update >> ' .. filename ) 393 394eq ( { 'existing' , 'new content' } , fn . readfile ( 'Xtest_update/append.txt' )) 395end ) 396 397it ( 'triggers autocmds properly' , function () 398command ( 'autocmd BufWritePre * let g:write_pre = 1' ) 399command ( 'autocmd BufWritePost * let g:write_post = 1' ) 400 401command ( 'edit Xtest_update/autocmd.txt' ) 402command ( 'call setline(1, "trigger autocmds")' ) 403command ( 'update' ) 404 405eq ( 1 , eval ( 'g:write_pre' )) 406eq ( 1 , eval ( 'g:write_post' )) 407end ) 408 409it ( 'does not write acwrite buffer when unchanged' , function () 410command ( 'file remote://test' ) 411command ( 'setlocal buftype=acwrite' ) 412command ( 'let g:triggered = 0 | autocmd BufWriteCmd remote://* let g:triggered = 1' ) 413command ( 'update' ) 414eq ( 0 , eval ( 'g:triggered' )) 415command ( 'call setline(1, ["hello"])' ) 416command ( 'update' ) 417eq ( 1 , eval ( 'g:triggered' )) 418end ) 419end )