mksession_spec.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local Screen = require('test.functional.ui.screen')
  3. local clear = helpers.clear
  4. local command = helpers.command
  5. local get_pathsep = helpers.get_pathsep
  6. local eq = helpers.eq
  7. local neq = helpers.neq
  8. local funcs = helpers.funcs
  9. local matches = helpers.matches
  10. local pesc = helpers.pesc
  11. local rmdir = helpers.rmdir
  12. local sleep = helpers.sleep
  13. local meths = helpers.meths
  14. local skip = helpers.skip
  15. local is_os = helpers.is_os
  16. local mkdir = helpers.mkdir
  17. local file_prefix = 'Xtest-functional-ex_cmds-mksession_spec'
  18. if helpers.skip(helpers.is_os('win')) then return end
  19. describe(':mksession', function()
  20. local session_file = file_prefix .. '.vim'
  21. local tab_dir = file_prefix .. '.d'
  22. before_each(function()
  23. clear()
  24. mkdir(tab_dir)
  25. end)
  26. after_each(function()
  27. os.remove(session_file)
  28. rmdir(tab_dir)
  29. end)
  30. it('restores same :terminal buf in splits', function()
  31. -- If the same :terminal is displayed in multiple windows, :mksession
  32. -- should restore it as such.
  33. -- Create three windows: first two from top show same terminal, third -
  34. -- another one (created earlier).
  35. command('terminal')
  36. command('split')
  37. command('terminal')
  38. command('split')
  39. command('mksession ' .. session_file)
  40. command('%bwipeout!')
  41. -- Create a new test instance of Nvim.
  42. clear()
  43. -- Restore session.
  44. command('source ' .. session_file)
  45. eq(funcs.winbufnr(1), funcs.winbufnr(2))
  46. neq(funcs.winbufnr(1), funcs.winbufnr(3))
  47. end)
  48. -- common testing procedure for testing "sessionoptions-=terminal"
  49. local function test_terminal_session_disabled(expected_buf_count)
  50. command('set sessionoptions-=terminal')
  51. command('mksession ' .. session_file)
  52. -- Create a new test instance of Nvim.
  53. clear()
  54. -- Restore session.
  55. command('source ' .. session_file)
  56. eq(expected_buf_count, #meths.list_bufs())
  57. end
  58. it(
  59. 'do not restore :terminal if not set in sessionoptions, terminal in current window #13078',
  60. function()
  61. local tmpfile_base = file_prefix .. '-tmpfile'
  62. command('edit ' .. tmpfile_base)
  63. command('terminal')
  64. local buf_count = #meths.list_bufs()
  65. eq(2, buf_count)
  66. eq('terminal', meths.buf_get_option(0, 'buftype'))
  67. test_terminal_session_disabled(2)
  68. -- no terminal should be set. As a side effect we end up with a blank buffer
  69. eq('', meths.buf_get_option(meths.list_bufs()[1], 'buftype'))
  70. eq('', meths.buf_get_option(meths.list_bufs()[2], 'buftype'))
  71. end
  72. )
  73. it('do not restore :terminal if not set in sessionoptions, terminal hidden #13078', function()
  74. command('terminal')
  75. local terminal_bufnr = meths.get_current_buf()
  76. local tmpfile_base = file_prefix .. '-tmpfile'
  77. -- make terminal hidden by opening a new file
  78. command('edit ' .. tmpfile_base .. '1')
  79. local buf_count = #meths.list_bufs()
  80. eq(2, buf_count)
  81. eq(1, funcs.getbufinfo(terminal_bufnr)[1].hidden)
  82. test_terminal_session_disabled(1)
  83. -- no terminal should exist here
  84. neq('', meths.buf_get_name(meths.list_bufs()[1]))
  85. end)
  86. it('do not restore :terminal if not set in sessionoptions, only buffer #13078', function()
  87. command('terminal')
  88. eq('terminal', meths.buf_get_option(0, 'buftype'))
  89. local buf_count = #meths.list_bufs()
  90. eq(1, buf_count)
  91. test_terminal_session_disabled(1)
  92. -- no terminal should be set
  93. eq('', meths.buf_get_option(0, 'buftype'))
  94. end)
  95. it('restores tab-local working directories', function()
  96. local tmpfile_base = file_prefix .. '-tmpfile'
  97. local cwd_dir = funcs.getcwd()
  98. -- :mksession does not save empty tabs, so create some buffers.
  99. command('edit ' .. tmpfile_base .. '1')
  100. command('tabnew')
  101. command('edit ' .. tmpfile_base .. '2')
  102. command('tcd ' .. tab_dir)
  103. command('tabfirst')
  104. command('mksession ' .. session_file)
  105. -- Create a new test instance of Nvim.
  106. clear()
  107. command('source ' .. session_file)
  108. -- First tab should have the original working directory.
  109. command('tabnext 1')
  110. eq(cwd_dir, funcs.getcwd())
  111. -- Second tab should have the tab-local working directory.
  112. command('tabnext 2')
  113. eq(cwd_dir .. get_pathsep() .. tab_dir, funcs.getcwd())
  114. end)
  115. it('restores buffers with tab-local CWD', function()
  116. local tmpfile_base = file_prefix .. '-tmpfile'
  117. local cwd_dir = funcs.getcwd()
  118. local session_path = cwd_dir .. get_pathsep() .. session_file
  119. command('edit ' .. tmpfile_base .. '1')
  120. command('tcd ' .. tab_dir)
  121. command('tabnew')
  122. command('edit ' .. cwd_dir .. get_pathsep() .. tmpfile_base .. '2')
  123. command('tabfirst')
  124. command('mksession ' .. session_path)
  125. -- Create a new test instance of Nvim.
  126. clear()
  127. -- Use :silent to avoid press-enter prompt due to long path
  128. command('silent source ' .. session_path)
  129. command('tabnext 1')
  130. eq(cwd_dir .. get_pathsep() .. tmpfile_base .. '1', funcs.expand('%:p'))
  131. command('tabnext 2')
  132. eq(cwd_dir .. get_pathsep() .. tmpfile_base .. '2', funcs.expand('%:p'))
  133. end)
  134. it('restores CWD for :terminal buffers #11288', function()
  135. local cwd_dir = funcs.fnamemodify('.', ':p:~'):gsub([[[\/]*$]], '')
  136. cwd_dir = cwd_dir:gsub([[\]], '/') -- :mksession always uses unix slashes.
  137. local session_path = cwd_dir .. '/' .. session_file
  138. command('cd ' .. tab_dir)
  139. command('terminal')
  140. command('cd ' .. cwd_dir)
  141. command('mksession ' .. session_path)
  142. command('%bwipeout!')
  143. if is_os('win') then
  144. sleep(100) -- Make sure all child processes have exited.
  145. end
  146. -- Create a new test instance of Nvim.
  147. clear()
  148. command('silent source ' .. session_path)
  149. local expected_cwd = cwd_dir .. '/' .. tab_dir
  150. matches('^term://' .. pesc(expected_cwd) .. '//%d+:', funcs.expand('%'))
  151. command('%bwipeout!')
  152. if is_os('win') then
  153. sleep(100) -- Make sure all child processes have exited.
  154. end
  155. end)
  156. it('restores CWD for :terminal buffer at root directory #16988', function()
  157. skip(is_os('win'), 'N/A for Windows')
  158. local screen
  159. local cwd_dir = funcs.fnamemodify('.', ':p:~'):gsub([[[\/]*$]], '')
  160. local session_path = cwd_dir .. '/' .. session_file
  161. screen = Screen.new(50, 6)
  162. screen:attach({ rgb = false })
  163. local expected_screen = [[
  164. ^/ |
  165. |
  166. [Process exited 0] |
  167. |
  168. |
  169. |
  170. ]]
  171. command('cd /')
  172. command('terminal echo $PWD')
  173. -- Verify that the terminal's working directory is "/".
  174. screen:expect(expected_screen)
  175. command('cd ' .. cwd_dir)
  176. command('mksession ' .. session_path)
  177. command('%bwipeout!')
  178. -- Create a new test instance of Nvim.
  179. clear()
  180. screen = Screen.new(50, 6)
  181. screen:attach({ rgb = false })
  182. command('silent source ' .. session_path)
  183. -- Verify that the terminal's working directory is "/".
  184. screen:expect(expected_screen)
  185. end)
  186. it('restores a session when there is a float #18432', function()
  187. local tmpfile = file_prefix .. '-tmpfile-float'
  188. command('edit ' .. tmpfile)
  189. local buf = meths.create_buf(false, true)
  190. local config = {
  191. relative = 'editor',
  192. focusable = false,
  193. width = 10,
  194. height = 3,
  195. row = 0,
  196. col = 1,
  197. style = 'minimal',
  198. }
  199. meths.open_win(buf, false, config)
  200. local cmdheight = meths.get_option('cmdheight')
  201. command('mksession ' .. session_file)
  202. -- Create a new test instance of Nvim.
  203. clear()
  204. command('source ' .. session_file)
  205. eq(tmpfile, funcs.expand('%'))
  206. -- Check that there is only a single window, which indicates the floating
  207. -- window was not restored.
  208. eq(1, funcs.winnr('$'))
  209. -- The command-line height should remain the same as it was.
  210. eq(cmdheight, meths.get_option('cmdheight'))
  211. os.remove(tmpfile)
  212. end)
  213. end)