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' ) 4 5local describe , it , before_each , after_each , finally = 6t . describe , t . it , t . before_each , t . after_each , t . finally 7local clear =n . clear 8local command =n . command 9local get_pathsep =n . get_pathsep 10local eq =t . eq 11local neq =t . neq 12local fn =n . fn 13local matches =t . matches 14local pesc =vim . pesc 15local rmdir =n . rmdir 16local sleep =vim . uv . sleep 17local api =n . api 18local skip =t . skip 19local is_os =t . is_os 20local mkdir =t . mkdir 21 22local file_prefix ='Xtest-functional-ex_cmds-mksession_spec' 23 24describe ( ':mksession' , function () 25local session_file =file_prefix .. '.vim' 26local tab_dir =file_prefix .. '.tab.d' 27local buf_dir =file_prefix .. '.buf.d' 28 29before_each ( function () 30clear () 31mkdir ( tab_dir ) 32mkdir ( buf_dir ) 33end ) 34 35after_each ( function () 36os . remove ( session_file ) 37rmdir ( tab_dir ) 38rmdir ( buf_dir ) 39end ) 40 41it ( 'restores same :terminal buf in splits' , function () 42-- If the same :terminal is displayed in multiple windows, :mksession 43-- should restore it as such. 44 45-- Create three windows: first two from top show same terminal, third - 46-- another one (created earlier). 47command ( 'terminal' ) 48command ( 'split' ) 49command ( 'terminal' ) 50command ( 'split' ) 51command ( 'mksession ' .. session_file ) 52command ( '%bwipeout!' ) 53 54-- Create a new test instance of Nvim. 55clear () 56-- Restore session. 57command ( 'source ' .. session_file ) 58 59eq ( fn . winbufnr ( 1 ), fn . winbufnr ( 2 )) 60neq ( fn . winbufnr ( 1 ), fn . winbufnr ( 3 )) 61end ) 62 63-- common testing procedure for testing "sessionoptions-=terminal" 64local function test_terminal_session_disabled ( expected_buf_count ) 65command ( 'set sessionoptions-=terminal' ) 66 67command ( 'mksession ' .. session_file ) 68 69-- Create a new test instance of Nvim. 70clear () 71 72-- Restore session. 73command ( 'source ' .. session_file ) 74 75eq ( expected_buf_count , # api . nvim_list_bufs ()) 76end 77 78it ( 'do not restore :terminal if not set in sessionoptions, terminal in curwin #13078' , function () 79local tmpfile_base =file_prefix .. '-tmpfile' 80command ( 'edit ' .. tmpfile_base ) 81command ( 'terminal' ) 82 83local buf_count =# api . nvim_list_bufs () 84eq ( 2 , buf_count ) 85 86eq ( 'terminal' , api . nvim_get_option_value ( 'buftype' , {} )) 87 88test_terminal_session_disabled ( 2 ) 89 90-- no terminal should be set. As a side effect we end up with a blank buffer 91eq ( '' , api . nvim_get_option_value ( 'buftype' , { buf =api . nvim_list_bufs ()[ 1 ] } )) 92eq ( '' , api . nvim_get_option_value ( 'buftype' , { buf =api . nvim_list_bufs ()[ 2 ] } )) 93end ) 94 95it ( 'do not restore :terminal if not set in sessionoptions, terminal hidden #13078' , function () 96command ( 'terminal' ) 97local terminal_bufnr =api . nvim_get_current_buf () 98 99local tmpfile_base =file_prefix .. '-tmpfile' 100-- make terminal hidden by opening a new file 101command ( 'edit ' .. tmpfile_base .. '1' ) 102 103local buf_count =# api . nvim_list_bufs () 104eq ( 2 , buf_count ) 105 106eq ( 1 , fn . getbufinfo ( terminal_bufnr )[ 1 ]. hidden ) 107 108test_terminal_session_disabled ( 1 ) 109 110-- no terminal should exist here 111neq ( '' , api . nvim_buf_get_name ( api . nvim_list_bufs ()[ 1 ])) 112end ) 113 114it ( 'do not restore :terminal if not set in sessionoptions, only buffer #13078' , function () 115command ( 'terminal' ) 116eq ( 'terminal' , api . nvim_get_option_value ( 'buftype' , {} )) 117 118local buf_count =# api . nvim_list_bufs () 119eq ( 1 , buf_count ) 120 121test_terminal_session_disabled ( 1 ) 122 123-- no terminal should be set 124eq ( '' , api . nvim_get_option_value ( 'buftype' , {} )) 125end ) 126 127it ( 'restores tab-local working directories' , function () 128local tmpfile_base =file_prefix .. '-tmpfile' 129local cwd_dir =fn . getcwd () 130 131-- :mksession does not save empty tabs, so create some buffers. 132command ( 'edit ' .. tmpfile_base .. '1' ) 133command ( 'tabnew' ) 134command ( 'edit ' .. tmpfile_base .. '2' ) 135command ( 'tcd ' .. tab_dir ) 136command ( 'tabfirst' ) 137command ( 'mksession ' .. session_file ) 138 139-- Create a new test instance of Nvim. 140clear () 141 142command ( 'source ' .. session_file ) 143-- First tab should have the original working directory. 144command ( 'tabnext 1' ) 145eq ( cwd_dir , fn . getcwd ()) 146-- Second tab should have the tab-local working directory. 147command ( 'tabnext 2' ) 148eq ( cwd_dir .. get_pathsep () .. tab_dir , fn . getcwd ()) 149end ) 150 151it ( 'restores buffers with tab-local CWD' , function () 152local tmpfile_base =file_prefix .. '-tmpfile' 153local cwd_dir =vim . fs . normalize ( fn . getcwd ()) 154local session_path =cwd_dir .. get_pathsep () .. session_file 155 156command ( 'edit ' .. tmpfile_base .. '1' ) 157command ( 'tcd ' .. tab_dir ) 158command ( 'tabnew' ) 159command ( 'edit ' .. cwd_dir .. get_pathsep () .. tmpfile_base .. '2' ) 160command ( 'tabfirst' ) 161command ( 'mksession ' .. session_path ) 162 163-- Create a new test instance of Nvim. 164clear () 165 166-- Use :silent to avoid hit-enter prompt due to long path 167command ( 'silent source ' .. session_path ) 168command ( 'tabnext 1' ) 169eq (( '%s/%s1' ): format ( cwd_dir , tmpfile_base ), vim . fs . normalize ( fn . expand ( '%:p' ))) 170command ( 'tabnext 2' ) 171eq (( '%s/%s2' ): format ( cwd_dir , tmpfile_base ), vim . fs . normalize ( fn . expand ( '%:p' ))) 172end ) 173 174it ( 'restores symlinked directory buffer names' , function () 175skip ( is_os ( 'win' ), 'N/A for Windows' ) 176 177local cwd_dir =t . fix_slashes ( fn . getcwd ()) 178local link_dir =file_prefix .. '-link' 179assert ( vim . uv . fs_symlink ( assert ( vim . uv . fs_realpath ( tab_dir )), link_dir , { dir =true } )) 180finally ( function () 181os . remove ( link_dir ) 182end ) 183 184command ( 'set sessionoptions=buffers' ) 185command ( 'edit ' .. link_dir ) 186local expected =cwd_dir .. '/' .. link_dir .. '/' 187eq ( expected , api . nvim_buf_get_name ( 0 )) 188command ( 'mksession ' .. session_file ) 189command ( '%bwipeout!' ) 190command ( 'source ' .. session_file ) 191eq ( expected , api . nvim_buf_get_name ( 0 )) 192end ) 193 194it ( 'restores a directory buffer for the CWD #40939' , function () 195local cwd_dir =t . fix_slashes ( fn . getcwd ()) 196local expected =cwd_dir .. '/' 197 198command ( 'set sessionoptions=buffers,curdir' ) 199command ( 'edit ' .. cwd_dir ) 200command ( 'cd ' .. cwd_dir ) 201neq ( '' , fn . bufname ( '%' )) 202eq ( expected , api . nvim_buf_get_name ( 0 )) 203 204command ( 'mksession ' .. session_file ) 205command ( '%bwipeout!' ) 206command ( 'source ' .. session_file ) 207eq ( expected , api . nvim_buf_get_name ( 0 )) 208end ) 209 210it ( 'restores CWD for :terminal buffers #11288' , function () 211local cwd_dir =fn . fnamemodify ( '.' , ':p:~' ): gsub ( [[/*$]] , '' ) 212local session_path =cwd_dir .. '/' .. session_file 213 214command ( 'cd ' .. tab_dir ) 215command ( 'terminal' ) 216command ( 'cd ' .. cwd_dir ) 217command ( 'mksession ' .. session_path ) 218command ( 'sleep 5m | %bwipeout!' ) 219if is_os ( 'win' ) then 220sleep ( 100 ) -- Make sure all child processes have exited. 221end 222 223-- Create a new test instance of Nvim. 224clear () 225command ( 'silent source ' .. session_path ) 226 227local expected_cwd =cwd_dir .. '/' .. tab_dir 228matches ( '^term://' .. pesc ( expected_cwd ) .. '//%d+:' , fn . expand ( '%' )) 229-- Wait some time for the PTY process to call setsid() and chdir(), otherwise 230-- SIGHUP may be sent to the parent, or after_each() may run before chdir(). 231command ( 'sleep 5m | %bwipeout!' ) 232if is_os ( 'win' ) then 233sleep ( 100 ) -- Make sure all child processes have exited. 234end 235end ) 236 237it ( 'restores CWD for :terminal buffer at root directory #16988' , function () 238skip ( is_os ( 'win' ), 'N/A for Windows' ) 239 240local screen 241local cwd_dir =fn . fnamemodify ( '.' , ':p:~' ): gsub ( [[[\/]*$]] , '' ) 242local session_path =cwd_dir .. '/' .. session_file 243 244screen =Screen . new ( 50 , 6 , { rgb =false } ) 245local expected_screen =[[ 246^/ | 247| 248[Process exited 0] | 249|*3 250]] 251 252command ( 'cd /' ) 253command ( 'terminal echo $PWD' ) 254 255-- Verify that the terminal's working directory is "/". 256screen : expect ( expected_screen ) 257 258command ( 'cd ' .. cwd_dir ) 259command ( 'mksession ' .. session_path ) 260command ( '%bwipeout!' ) 261 262-- Create a new test instance of Nvim. 263clear () 264screen =Screen . new ( 50 , 6 , { rgb =false } ) 265command ( 'silent source ' .. session_path ) 266 267-- Verify that the terminal's working directory is "/". 268screen : expect ( expected_screen ) 269end ) 270 271it ( 'restores a session when there is a float #18432' , function () 272local tmpfile =file_prefix .. '-tmpfile-float' 273 274command ( 'edit ' .. tmpfile ) 275eq ( 80 , fn . winwidth ( 1 )) 276command ( '30vsplit' ) 277eq ( 2 , # api . nvim_list_wins ()) 278eq ( 30 , fn . winwidth ( 1 )) 279eq ( 49 , fn . winwidth ( 2 )) 280local buf =api . nvim_create_buf ( false , true ) 281local config ={ 282relative ='editor' , 283focusable =false , 284width =10 , 285height =3 , 286row =0 , 287col =1 , 288style ='minimal' , 289} 290api . nvim_open_win ( buf , false , config ) 291eq ( 3 , # api . nvim_list_wins ()) 292local cmdheight =api . nvim_get_option_value ( 'cmdheight' , {} ) 293command ( 'mksession ' .. session_file ) 294 295-- Create a new test instance of Nvim. 296clear () 297 298command ( 'source ' .. session_file ) 299 300eq ( tmpfile , fn . expand ( '%' )) 301-- Check that there are only two windows, which indicates the floating 302-- window was not restored. 303-- Don't use winnr('$') as that doesn't count unfocusable floating windows. 304eq ( 2 , # api . nvim_list_wins ()) 305eq ( 30 , fn . winwidth ( 1 )) 306eq ( 49 , fn . winwidth ( 2 )) 307-- The command-line height should remain the same as it was. 308eq ( cmdheight , api . nvim_get_option_value ( 'cmdheight' , {} )) 309 310os . remove ( tmpfile ) 311end ) 312 313it ( 'fires session write autocmds only for :mksession' , function () 314command ( 'let g:session_write_events = []' ) 315command ( "autocmd SessionWritePre * call add(g:session_write_events, 'pre')" ) 316command ( "autocmd SessionWritePost * call add(g:session_write_events, 'post')" ) 317for _ , cmd in ipairs ( { 'mkview' , 'mkvimrc' , 'mkexrc' } ) do 318command ( cmd .. '! ' .. session_file ) 319eq ( {} , api . nvim_eval ( 'g:session_write_events' ), cmd ) 320end 321command ( 'mksession! ' .. session_file ) 322eq ( { 'pre' , 'post' } , api . nvim_eval ( 'g:session_write_events' )) 323end ) 324 325it ( 'SessionWritePre handles editing buffer contents' , function () 326local tmpfile =file_prefix .. '-tmpfile-float' 327command ( 'edit ' .. tmpfile ) 328command ( "autocmd SessionWritePre * call append(0, 'foo') | write" ) 329command ( 'mksession ' .. session_file ) 330 331clear () 332command ( 'source ' .. session_file ) 333eq ( { 'foo' , '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 334os . remove ( tmpfile ) 335end ) 336 337it ( 'SessionWritePre handles switching buffers' , function () 338command ( 'autocmd SessionWritePre * new' ) 339command ( 'mksession ' .. session_file ) 340 341clear () 342command ( 'source ' .. session_file ) 343-- both buffers are saved 344eq ( 2 , # api . nvim_list_bufs ()) 345end ) 346 347it ( 'SessionWritePre handles buffer removal' , function () 348-- :bdelete and :bwipeout differ for "live" Nvim but are equivalent 349-- from :mksession's perspective, so one test covers both. 350api . nvim_buf_set_name ( 0 , 'foo' ) 351command ( 'autocmd SessionWritePre * bwipeout!' ) 352command ( 'mksession ' .. session_file ) 353 354clear () 355command ( 'source ' .. session_file ) 356-- gone for good 357eq ( 358nil , 359vim . iter ( api . nvim_list_bufs ()): find ( function ( b ) 360return api . nvim_buf_get_name ( b ) == 'foo' 361end ) 362) 363end ) 364 365it ( 'SessionWritePre handles splits' , function () 366command ( 'autocmd SessionWritePre * split | vsplit' ) 367command ( 'mksession ' .. session_file ) 368 369clear () 370command ( 'source ' .. session_file ) 371-- contains all splits 372eq ( 3 , # api . nvim_tabpage_list_wins ( 0 )) 373end ) 374 375it ( 'restores buffer-local working directories' , function () 376-- "'" and " " in the names must survive the session file's string quoting and escaping. 377local tmpfile_base =file_prefix .. "-tmp'file" 378local quote_dir =file_prefix .. ".b'uf d" 379local session_file2 =session_file .. '2' 380local cwd_dir =fn . getcwd () 381mkdir ( quote_dir ) 382finally ( function () 383rmdir ( quote_dir ) 384os . remove ( session_file2 ) 385end ) 386 387command (( 'edit %s1' ): format ( tmpfile_base )) 388command (( 'edit %s2' ): format ( tmpfile_base )) 389command ( 'bcd ' .. fn . fnameescape ( quote_dir )) 390command ( 'b# ' ) 391command ( 'mksession ' .. session_file ) 392command ( 'set sessionoptions-=curdir' ) 393command ( 'mksession ' .. session_file2 ) 394 395-- Create a new test instance of Nvim. 396clear () 397 398command ( 'silent source ' .. session_file ) 399command (( 'b %s%s2' ): format ( get_pathsep (), tmpfile_base )) 400eq (( '%s%s%s' ): format ( cwd_dir , get_pathsep (), quote_dir ), fn . getcwd ()) 401 402-- Without "curdir" in 'sessionoptions' no buffer-local directory is restored. 403clear () 404command ( 'silent source ' .. session_file2 ) 405command (( 'b %s%s2' ): format ( get_pathsep (), tmpfile_base )) 406eq ( cwd_dir , fn . getcwd ()) 407eq ( 0 , fn . haslocaldir ( - 1 , - 1 , 0 )) 408end ) 409 410it ( 'restores buffer-local working directories when used with tabs' , function () 411local tmpfile_base =file_prefix .. '-tmpfile' 412local cwd_dir =fn . getcwd () 413 414command (( 'edit %s1' ): format ( tmpfile_base )) 415command ( 'tabnew ' ) 416command (( 'edit %s2' ): format ( tmpfile_base )) 417command ( 'tcd ' .. tab_dir ) 418command ( 'bcd ..' ) 419command ( 'bcd ' .. buf_dir ) 420command ( 'tabfirst' ) 421command ( 'mksession ' .. session_file ) 422 423-- Create a new test instance of Nvim. 424clear () 425 426command ( 'source ' .. session_file ) 427-- The ":tcd" during restore must not clear the buffer-local directory of the tab's 428-- first-loaded buffer: it must be set before that tab is ever (re)visited. 429eq ( 1 , fn . haslocaldir ( - 1 , - 1 , fn . bufnr (( '%s2' ): format ( tmpfile_base )))) 430-- First tab should have the original working directory. 431command ( 'tabnext 1' ) 432eq ( cwd_dir , fn . getcwd ()) 433-- Second tab should have the tab-local working directory. 434command ( 'tabnext 2' ) 435eq (( '%s%s%s' ): format ( cwd_dir , get_pathsep (), tab_dir ), fn . getcwd ( - 1 , 0 )) 436-- Buffer in second tab should have buffer-local working directory 437eq (( '%s%s%s' ): format ( cwd_dir , get_pathsep (), buf_dir ), fn . getcwd ( - 1 , - 1 , 0 )) 438end ) 439end )