robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 5local uv =vim . uv 6 7local assert_log =t . assert_log 8local assert_nolog =t . assert_nolog 9local clear =n . clear 10local command =n . command 11local eq =t . eq 12local neq =t . neq 13local ok =t . ok 14local feed =n . feed 15local fn =n . fn 16local nvim_prog =n . nvim_prog 17local request =n . request 18local retry =t . retry 19local rmdir =n . rmdir 20local matches =t . matches 21local api =n . api 22local mkdir =t . mkdir 23local sleep =vim . uv . sleep 24local read_file =t . read_file 25local trim =vim . trim 26local currentdir =n . fn . getcwd 27local assert_alive =n . assert_alive 28local check_close =n . check_close 29local expect_exit =n . expect_exit 30local write_file =t . write_file 31local feed_command =n . feed_command 32local skip =t . skip 33local is_os =t . is_os 34local set_session =n . set_session 35 36describe ( 'fileio' , function () 37before_each ( function () end ) 38after_each ( function () 39check_close () 40os . remove ( 'Xtest_startup_shada' ) 41os . remove ( 'Xtest_startup_file1' ) 42os . remove ( 'Xtest_startup_file1~' ) 43os . remove ( 'Xtest_startup_file2' ) 44os . remove ( 'Xtest_startup_file2~' ) 45os . remove ( 'Xtest_тест.md' ) 46os . remove ( 'Xtest-u8-int-max' ) 47os . remove ( 'Xtest-overwrite-forced' ) 48rmdir ( 'Xtest_startup_swapdir' ) 49rmdir ( 'Xtest_backupdir' ) 50rmdir ( 'Xtest_backupdir with spaces' ) 51rmdir ( 'Xtest_rename_dir' ) 52rmdir ( 'Xtest_rename_dest' ) 53end ) 54 55local args ={ '--clean' , '--cmd' , 'set nofsync directory=Xtest_startup_swapdir' } 56--- Starts a new nvim session and returns an attached screen. 57local function startup () 58local argv =vim . iter ( { args , '--embed' } ): flatten (): totable () 59local screen_nvim =n . new_session ( false , { args =argv , merge =false } ) 60set_session ( screen_nvim ) 61local screen =Screen . new ( 70 , 10 ) 62screen : set_default_attr_ids ( { 63[ 1 ] ={ foreground =Screen . colors . NvimDarkGrey4 } , 64[ 2 ] ={ background =Screen . colors . NvimDarkGrey1 , foreground =Screen . colors . NvimLightGrey3 } , 65[ 3 ] ={ foreground =Screen . colors . NvimLightCyan } , 66} ) 67return screen 68end 69 70it ( "fsync() with 'nofsync' #8304" , function () 71clear ( { args ={ '--cmd' , 'set nofsync directory=Xtest_startup_swapdir' } } ) 72 73-- These cases ALWAYS force fsync (regardless of 'fsync' option): 74 75-- 1. Idle (CursorHold) with modified buffers (+ 'swapfile'). 76command ( 'write Xtest_startup_file1' ) 77feed ( 'Afoo<esc>h' ) 78command ( 'write' ) 79eq ( 0 , request ( 'nvim__stats' ). fsync ) 80command ( 'set swapfile' ) 81command ( 'set updatetime=1' ) 82feed ( 'Azub<esc>h' ) -- File is 'modified'. 83sleep ( 3 ) -- Allow 'updatetime' to expire. 84retry ( 3 , nil , function () 85eq ( 1 , request ( 'nvim__stats' ). fsync ) 86end ) 87command ( 'set updatetime=100000 updatecount=100000' ) 88 89-- 2. Explicit :preserve command. 90command ( 'preserve' ) 91-- TODO: should be exactly 2; where is the extra fsync() is coming from? #26404 92ok ( request ( 'nvim__stats' ). fsync == 2 or request ( 'nvim__stats' ). fsync == 3 ) 93 94-- 3. Enable 'fsync' option, write file. 95command ( 'set fsync' ) 96feed ( 'Abaz<esc>h' ) 97command ( 'write' ) 98-- TODO: should be exactly 4; where is the extra fsync() is coming from? #26404 99ok ( request ( 'nvim__stats' ). fsync == 4 or request ( 'nvim__stats' ). fsync == 5 ) 100eq ( 'foozubbaz' , trim ( read_file ( 'Xtest_startup_file1' ))) 101 102-- 4. Exit caused by deadly signal (+ 'swapfile'). 103local j = 104fn . jobstart ( vim . iter ( { nvim_prog , args , '--embed' } ): flatten (): totable (), { rpc =true } ) 105fn . rpcrequest ( 106j , 107'nvim_exec2' , 108[[ 109set nofsync directory=Xtest_startup_swapdir 110edit Xtest_startup_file2 111write 112put ='fsyncd text' 113]] , 114{} 115) 116eq ( 'Xtest_startup_swapdir' , fn . rpcrequest ( j , 'nvim_eval' , '&directory' )) 117fn . jobstop ( j ) -- Send deadly signal. 118 119local screen =startup () 120feed ( ':recover Xtest_startup_file2<cr>' ) 121screen : expect ( { any =[[Using swap file "Xtest_startup_swapdir[/\]Xtest_startup_file2%.swp"]] } ) 122feed ( '<cr>' ) 123screen : expect ( { any ='fsyncd text' } ) 124 125-- 5. SIGPWR signal. 126-- oldtest: Test_signal_PWR() 127end ) 128 129it ( 'backup #9709' , function () 130clear ( { 131args ={ 132'-i' , 133'Xtest_startup_shada' , 134'--cmd' , 135'set directory=Xtest_startup_swapdir' , 136} , 137} ) 138 139command ( 'write Xtest_startup_file1' ) 140feed ( 'ifoo<esc>' ) 141command ( 'set backup' ) 142command ( 'set backupcopy=yes' ) 143command ( 'write' ) 144feed ( 'Abar<esc>' ) 145command ( 'write' ) 146 147local foobar_contents =trim ( read_file ( 'Xtest_startup_file1' )) 148local bar_contents =trim ( read_file ( 'Xtest_startup_file1~' )) 149 150eq ( 'foobar' , foobar_contents ) 151eq ( 'foo' , bar_contents ) 152end ) 153 154it ( 'backup with full path #11214' , function () 155clear () 156mkdir ( 'Xtest_backupdir' ) 157command ( 'set backup' ) 158command ( 'set backupdir=Xtest_backupdir//' ) 159command ( 'write Xtest_startup_file1' ) 160feed ( 'ifoo<esc>' ) 161command ( 'write' ) 162feed ( 'Abar<esc>' ) 163command ( 'write' ) 164 165-- Backup filename = fullpath, separators replaced with "%". 166local backup_file_name =string . gsub ( 167currentdir () .. '/Xtest_startup_file1' , 168is_os ( 'win' ) and '[:/\\]' or '/' , 169'%%' 170) .. '~' 171local foo_contents =trim ( read_file ( 'Xtest_backupdir/' .. backup_file_name )) 172local foobar_contents =trim ( read_file ( 'Xtest_startup_file1' )) 173 174eq ( 'foobar' , foobar_contents ) 175eq ( 'foo' , foo_contents ) 176end ) 177 178it ( 'backup with full path with spaces' , function () 179clear () 180mkdir ( 'Xtest_backupdir with spaces' ) 181command ( 'set backup' ) 182command ( 'set backupdir=Xtest_backupdir\\ with\\ spaces//' ) 183command ( 'write Xtest_startup_file1' ) 184feed ( 'ifoo<esc>' ) 185command ( 'write' ) 186feed ( 'Abar<esc>' ) 187command ( 'write' ) 188 189-- Backup filename = fullpath, separators replaced with "%". 190local backup_file_name =string . gsub ( 191currentdir () .. '/Xtest_startup_file1' , 192is_os ( 'win' ) and '[:/\\]' or '/' , 193'%%' 194) .. '~' 195local foo_contents =trim ( read_file ( 'Xtest_backupdir with spaces/' .. backup_file_name )) 196local foobar_contents =trim ( read_file ( 'Xtest_startup_file1' )) 197 198eq ( 'foobar' , foobar_contents ) 199eq ( 'foo' , foo_contents ) 200end ) 201 202it ( 'backup symlinked files #11349' , function () 203clear () 204 205local initial_content ='foo' 206local link_file_name ='Xtest_startup_file2' 207local backup_file_name =link_file_name .. '~' 208 209write_file ( 'Xtest_startup_file1' , initial_content , false ) 210uv . fs_symlink ( 'Xtest_startup_file1' , link_file_name ) 211command ( 'set backup' ) 212command ( 'set backupcopy=yes' ) 213command ( 'edit ' .. link_file_name ) 214feed ( 'Abar<esc>' ) 215command ( 'write' ) 216 217local backup_raw =read_file ( backup_file_name ) 218neq ( nil , backup_raw , 'Expected backup file ' .. backup_file_name .. 'to exist but did not' ) 219eq ( initial_content , trim ( backup_raw ), 'Expected backup to contain original contents' ) 220end ) 221 222it ( 'backup symlinked files in first available backupdir #11349' , function () 223clear () 224 225local initial_content ='foo' 226local backup_dir ='Xtest_backupdir' 227local sep =n . get_pathsep () 228local link_file_name ='Xtest_startup_file2' 229local backup_file_name =backup_dir .. sep .. link_file_name .. '~' 230 231write_file ( 'Xtest_startup_file1' , initial_content , false ) 232uv . fs_symlink ( 'Xtest_startup_file1' , link_file_name ) 233mkdir ( backup_dir ) 234command ( 'set backup' ) 235command ( 'set backupcopy=yes' ) 236command ( 'set backupdir=.__this_does_not_exist__,' .. backup_dir ) 237command ( 'edit ' .. link_file_name ) 238feed ( 'Abar<esc>' ) 239command ( 'write' ) 240 241local backup_raw =read_file ( backup_file_name ) 242neq ( nil , backup_raw , 'Expected backup file ' .. backup_file_name .. ' to exist but did not' ) 243eq ( initial_content , trim ( backup_raw ), 'Expected backup to contain original contents' ) 244end ) 245 246it ( 'readfile() on multibyte filename #10586' , function () 247clear () 248local text ={ 249'line1' , 250' ...line2... ' , 251'' , 252'line3!' , 253'тест yay тест.' , 254'' , 255} 256local fname ='Xtest_тест.md' 257fn . writefile ( text , fname , 's' ) 258table . insert ( text , '' ) 259eq ( text , fn . readfile ( fname , 'b' )) 260end ) 261it ( "read invalid u8 over INT_MAX doesn't segfault" , function () 262clear () 263command ( 'call writefile(0zFFFFFFFF, "Xtest-u8-int-max")' ) 264-- This should not segfault 265command ( 'edit ++enc=utf32 Xtest-u8-int-max' ) 266assert_alive () 267end ) 268 269it ( 'rename() replaces an existing file with a directory' , function () 270clear () 271mkdir ( 'Xtest_rename_dir' ) 272write_file ( 'Xtest_rename_dir/file' , 'dir_content' ) 273write_file ( 'Xtest_rename_dest' , 'old_content' ) 274 275eq ( 0 , fn . rename ( 'Xtest_rename_dir' , 'Xtest_rename_dest' )) 276eq ( nil , uv . fs_stat ( 'Xtest_rename_dir' )) 277eq ( 'directory' , uv . fs_stat ( 'Xtest_rename_dest' ). type ) 278eq ( 'dir_content' , read_file ( 'Xtest_rename_dest/file' )) 279end ) 280 281it ( ':w! does not show "file has been changed" warning' , function () 282clear () 283write_file ( 'Xtest-overwrite-forced' , 'foobar' ) 284command ( 'set nofixendofline' ) 285local screen =Screen . new ( 40 , 4 ) 286command ( 'set shortmess-=F' ) 287 288command ( 'e Xtest-overwrite-forced' ) 289screen : expect ( [[ 290^foobar | 291{1:~ }|*2 292"Xtest-overwrite-forced" [noeol] 1L, 6B | 293]] ) 294 295-- Get current unix time. 296local cur_unix_time =os . time ( os . date ( '!*t' )) 297local future_time =cur_unix_time + 999999 298-- Set the file's access/update time to be 299-- greater than the time at which it was created. 300uv . fs_utime ( 'Xtest-overwrite-forced' , future_time , future_time ) 301-- use async feed_command because nvim basically hangs on the prompt 302feed_command ( 'w' ) 303screen : expect ( [[ 304{9:WARNING: The file has been changed since}| 305{9: reading it!!!} | 306{6:Do you really want to write to it (y/n)?}| 307^ | 308]] ) 309 310feed ( 'n' ) 311feed ( '<cr>' ) 312screen : expect ( [[ 313^foobar | 314{1:~ }|*2 315| 316]] ) 317-- Use a screen test because the warning does not set v:errmsg. 318command ( 'w!' ) 319screen : expect ( [[ 320^foobar | 321{1:~ }|*2 322<erwrite-forced" [noeol] 1L, 6B written | 323]] ) 324end ) 325end ) 326 327describe ( 'tmpdir' , function () 328local tmproot_pat =[=[.*[/\\]nvim%.[^/\\]+]=] 329local testlog ='Xtest_tmpdir_log' 330local os_tmpdir ---@type string 331 332before_each ( function () 333-- Fake /tmp dir so that we can mess it up. 334os_tmpdir =assert ( vim . uv . fs_mkdtemp ( vim . fs . dirname ( t . tmpname ( false )) .. '/nvim_XXXXXXXXXX' )) 335end ) 336 337after_each ( function () 338check_close () 339os . remove ( testlog ) 340end ) 341 342local function get_tmproot () 343-- Tempfiles typically look like: "…/nvim.<user>/xxx/0". 344-- - "…/nvim.<user>/xxx/" is the per-process tmpdir, not shared with other Nvims. 345-- - "…/nvim.<user>/" is the tmpdir root, shared by all Nvims (normally). 346local tmproot =( fn . tempname ()): match ( tmproot_pat ) 347ok ( tmproot : len () > 4 , 'tmproot like "nvim.foo"' , tmproot ) 348return tmproot 349end 350 351it ( 'failure modes' , function () 352clear ( { env ={ NVIM_LOG_FILE =testlog , TMPDIR =os_tmpdir } } ) 353assert_nolog ( 'tempdir is not a directory' , testlog ) 354assert_nolog ( 'tempdir has invalid permissions' , testlog ) 355 356local tmproot =get_tmproot () 357 358-- Test how Nvim handles invalid tmpdir root (by hostile users or accidents). 359-- 360-- "…/nvim.<user>/" is not a directory: 361expect_exit ( command , ':qall!' ) 362rmdir ( tmproot ) 363write_file ( tmproot , '' ) -- Not a directory, vim_mktempdir() should skip it. 364clear ( { env ={ NVIM_LOG_FILE =testlog , TMPDIR =os_tmpdir } } ) 365matches ( tmproot_pat , fn . stdpath ( 'run' )) -- Tickle vim_mktempdir(). 366-- Assert that broken tmpdir root was handled. 367assert_log ( 'tempdir root not a directory' , testlog , 100 ) 368 369-- "…/nvim.<user>/" has wrong permissions: 370skip ( is_os ( 'win' ), 'TODO(justinmk): need setfperm/getfperm on Windows. #8244' ) 371os . remove ( testlog ) 372os . remove ( tmproot ) 373mkdir ( tmproot ) 374fn . setfperm ( tmproot , 'rwxr--r--' ) -- Invalid permissions, vim_mktempdir() should skip it. 375clear ( { env ={ NVIM_LOG_FILE =testlog , TMPDIR =os_tmpdir } } ) 376matches ( tmproot_pat , fn . stdpath ( 'run' )) -- Tickle vim_mktempdir(). 377-- Assert that broken tmpdir root was handled. 378assert_log ( 'tempdir root has invalid permissions' , testlog , 100 ) 379end ) 380 381it ( 'too long' , function () 382local bigname =( '%s/%s' ): format ( os_tmpdir , ( 'x' ): rep ( 666 )) 383mkdir ( bigname ) 384clear ( { env ={ NVIM_LOG_FILE =testlog , TMPDIR =bigname } } ) 385matches ( tmproot_pat , fn . stdpath ( 'run' )) -- Tickle vim_mktempdir(). 386local len =( fn . tempname ()): len () 387ok ( len > 4 and len < 256 , '4 < len < 256' , tostring ( len )) 388end ) 389 390it ( 'disappeared #1432' , function () 391clear ( { env ={ NVIM_LOG_FILE =testlog , TMPDIR =os_tmpdir } } ) 392assert_nolog ( 'tempdir disappeared' , testlog ) 393 394local function rm_tmpdir () 395local tmpname1 =fn . tempname () 396local tmpdir1 =fn . fnamemodify ( tmpname1 , ':h' ) 397eq ( fn . stdpath ( 'run' ), tmpdir1 ) 398 399rmdir ( tmpdir1 ) 400retry ( nil , 1000 , function () 401eq ( 0 , fn . isdirectory ( tmpdir1 )) 402end ) 403local tmpname2 =fn . tempname () 404local tmpdir2 =fn . fnamemodify ( tmpname2 , ':h' ) 405neq ( tmpdir1 , tmpdir2 ) 406end 407 408-- Your antivirus hates you... 409rm_tmpdir () 410assert_log ( 'tempdir disappeared' , testlog , 100 ) 411fn . tempname () 412fn . tempname () 413fn . tempname () 414eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 415rm_tmpdir () 416fn . tempname () 417fn . tempname () 418fn . tempname () 419eq ( 'E5431: tempdir disappeared (2 times)' , api . nvim_get_vvar ( 'errmsg' )) 420rm_tmpdir () 421eq ( 'E5431: tempdir disappeared (3 times)' , api . nvim_get_vvar ( 'errmsg' )) 422end ) 423end )