shada_spec.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. -- Other ShaDa tests
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local t_shada = require('test.functional.shada.testutil')
  5. local uv = vim.uv
  6. local paths = t.paths
  7. local api, nvim_command, fn, eq = n.api, n.command, n.fn, t.eq
  8. local write_file, spawn, set_session, nvim_prog, exc_exec =
  9. t.write_file, n.spawn, n.set_session, n.nvim_prog, n.exc_exec
  10. local is_os = t.is_os
  11. local skip = t.skip
  12. local reset, clear, get_shada_rw = t_shada.reset, t_shada.clear, t_shada.get_shada_rw
  13. local read_shada_file = t_shada.read_shada_file
  14. local wshada, _, shada_fname, clean = get_shada_rw('Xtest-functional-shada-shada.shada')
  15. local dirname = 'Xtest-functional-shada-shada.d'
  16. local dirshada = dirname .. '/main.shada'
  17. describe('ShaDa support code', function()
  18. before_each(reset)
  19. after_each(function()
  20. clear()
  21. clean()
  22. uv.fs_rmdir(dirname)
  23. end)
  24. it('preserves `s` item size limit with unknown entries', function()
  25. wshada(
  26. '\100\000\207\000\000\000\000\000\000\004\000\218\003\253'
  27. .. ('-'):rep(1024 - 3)
  28. .. '\100\000\207\000\000\000\000\000\000\004\001\218\003\254'
  29. .. ('-'):rep(1025 - 3)
  30. )
  31. eq(0, exc_exec('wshada ' .. shada_fname))
  32. local found = 0
  33. for _, v in ipairs(read_shada_file(shada_fname)) do
  34. if v.type == 100 then
  35. found = found + 1
  36. end
  37. end
  38. eq(2, found)
  39. eq(0, exc_exec('set shada-=s10 shada+=s1'))
  40. eq(0, exc_exec('wshada ' .. shada_fname))
  41. found = 0
  42. for _, v in ipairs(read_shada_file(shada_fname)) do
  43. if v.type == 100 then
  44. found = found + 1
  45. end
  46. end
  47. eq(1, found)
  48. end)
  49. it('preserves `s` item size limit with instance history entries', function()
  50. local hist1 = ('-'):rep(1024 - 5)
  51. local hist2 = ('-'):rep(1025 - 5)
  52. nvim_command('set shada-=s10 shada+=s1')
  53. fn.histadd(':', hist1)
  54. fn.histadd(':', hist2)
  55. eq(0, exc_exec('wshada ' .. shada_fname))
  56. local found = 0
  57. for _, v in ipairs(read_shada_file(shada_fname)) do
  58. if v.type == 4 then
  59. found = found + 1
  60. eq(hist1, v.value[2])
  61. end
  62. end
  63. eq(1, found)
  64. end)
  65. it('leaves .tmp.a in-place when there is error in original ShaDa', function()
  66. wshada('Some text file')
  67. eq(
  68. 'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
  69. exc_exec('wshada ' .. shada_fname)
  70. )
  71. eq(1, read_shada_file(shada_fname .. '.tmp.a')[1].type)
  72. end)
  73. it(
  74. 'does not leave .tmp.a in-place when there is error in original ShaDa, but writing with bang',
  75. function()
  76. wshada('Some text file')
  77. eq(0, exc_exec('wshada! ' .. shada_fname))
  78. eq(1, read_shada_file(shada_fname)[1].type)
  79. eq(nil, uv.fs_stat(shada_fname .. '.tmp.a'))
  80. end
  81. )
  82. it('leaves .tmp.b in-place when there is error in original ShaDa and it has .tmp.a', function()
  83. wshada('Some text file')
  84. eq(
  85. 'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
  86. exc_exec('wshada ' .. shada_fname)
  87. )
  88. eq(
  89. 'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
  90. exc_exec('wshada ' .. shada_fname)
  91. )
  92. eq(1, read_shada_file(shada_fname .. '.tmp.a')[1].type)
  93. eq(1, read_shada_file(shada_fname .. '.tmp.b')[1].type)
  94. end)
  95. it(
  96. 'leaves .tmp.z in-place when there is error in original ShaDa and it has .tmp.a … .tmp.x',
  97. function()
  98. wshada('Some text file')
  99. local i = ('a'):byte()
  100. while i < ('z'):byte() do
  101. write_file(shada_fname .. ('.tmp.%c'):format(i), 'Some text file', true)
  102. i = i + 1
  103. end
  104. eq(
  105. 'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
  106. exc_exec('wshada ' .. shada_fname)
  107. )
  108. eq(1, read_shada_file(shada_fname .. '.tmp.z')[1].type)
  109. end
  110. )
  111. it('errors out when there are .tmp.a … .tmp.z ShaDa files', function()
  112. wshada('')
  113. local i = ('a'):byte()
  114. while i <= ('z'):byte() do
  115. write_file(shada_fname .. ('.tmp.%c'):format(i), '', true)
  116. i = i + 1
  117. end
  118. eq(
  119. 'Vim(wshada):E138: All Xtest-functional-shada-shada.shada.tmp.X files exist, cannot write ShaDa file!',
  120. exc_exec('wshada ' .. shada_fname)
  121. )
  122. end)
  123. it('reads correctly various timestamps', function()
  124. local msgpack = {
  125. '\100', -- Positive fixnum 100
  126. '\204\255', -- uint 8 255
  127. '\205\010\003', -- uint 16 2563
  128. '\206\255\010\030\004', -- uint 32 4278853124
  129. '\207\005\100\060\250\255\010\030\004', -- uint 64 388502516579048964
  130. }
  131. local s = '\100'
  132. local e = '\001\192'
  133. wshada(s .. table.concat(msgpack, e .. s) .. e)
  134. eq(0, exc_exec('wshada ' .. shada_fname))
  135. local found = 0
  136. local typ = vim.mpack.decode(s)
  137. for _, v in ipairs(read_shada_file(shada_fname)) do
  138. if v.type == typ then
  139. found = found + 1
  140. eq(vim.mpack.decode(msgpack[found]), v.timestamp)
  141. end
  142. end
  143. eq(#msgpack, found)
  144. end)
  145. local marklike = { [7] = true, [8] = true, [10] = true, [11] = true }
  146. local find_file = function(fname)
  147. local found = {}
  148. for _, v in ipairs(read_shada_file(shada_fname)) do
  149. if marklike[v.type] and v.value.f == fname then
  150. found[v.type] = (found[v.type] or 0) + 1
  151. elseif v.type == 9 then
  152. for _, b in ipairs(v.value) do
  153. if b.f == fname then
  154. found[v.type] = (found[v.type] or 0) + 1
  155. end
  156. end
  157. end
  158. end
  159. return found
  160. end
  161. it('correctly uses shada-r option', function()
  162. nvim_command('set shellslash')
  163. api.nvim_set_var('__home', paths.test_source_path)
  164. nvim_command('let $HOME = __home')
  165. nvim_command('unlet __home')
  166. nvim_command('edit ~/README.md')
  167. nvim_command('normal! GmAggmaAabc')
  168. nvim_command('undo')
  169. nvim_command('set shada+=%')
  170. nvim_command('wshada! ' .. shada_fname)
  171. local readme_fname = fn.resolve(paths.test_source_path) .. '/README.md'
  172. eq({ [7] = 2, [8] = 2, [9] = 1, [10] = 4, [11] = 1 }, find_file(readme_fname))
  173. nvim_command('set shada+=r~')
  174. nvim_command('wshada! ' .. shada_fname)
  175. eq({}, find_file(readme_fname))
  176. nvim_command('set shada-=r~')
  177. nvim_command('wshada! ' .. shada_fname)
  178. eq({ [7] = 2, [8] = 2, [9] = 1, [10] = 4, [11] = 1 }, find_file(readme_fname))
  179. nvim_command('set shada+=r' .. fn.escape(fn.escape(paths.test_source_path, '$~'), ' "\\,'))
  180. nvim_command('wshada! ' .. shada_fname)
  181. eq({}, find_file(readme_fname))
  182. end)
  183. it('correctly ignores case with shada-r option', function()
  184. nvim_command('set shellslash')
  185. local pwd = fn.getcwd()
  186. local relfname = 'абв/test'
  187. local fname = pwd .. '/' .. relfname
  188. api.nvim_set_var('__fname', fname)
  189. nvim_command('silent! edit `=__fname`')
  190. fn.setline(1, { 'a', 'b', 'c', 'd' })
  191. nvim_command('normal! GmAggmaAabc')
  192. nvim_command('undo')
  193. nvim_command('set shada+=%')
  194. nvim_command('wshada! ' .. shada_fname)
  195. eq({ [7] = 2, [8] = 2, [9] = 1, [10] = 4, [11] = 2 }, find_file(fname))
  196. nvim_command('set shada+=r' .. pwd .. '/АБВ')
  197. nvim_command('wshada! ' .. shada_fname)
  198. eq({}, find_file(fname))
  199. end)
  200. it('is able to set &shada after &viminfo', function()
  201. api.nvim_set_option_value('viminfo', "'10", {})
  202. eq("'10", api.nvim_get_option_value('viminfo', {}))
  203. eq("'10", api.nvim_get_option_value('shada', {}))
  204. api.nvim_set_option_value('shada', '', {})
  205. eq('', api.nvim_get_option_value('viminfo', {}))
  206. eq('', api.nvim_get_option_value('shada', {}))
  207. end)
  208. it('is able to set all& after setting &shada', function()
  209. api.nvim_set_option_value('shada', "'10", {})
  210. eq("'10", api.nvim_get_option_value('viminfo', {}))
  211. eq("'10", api.nvim_get_option_value('shada', {}))
  212. nvim_command('set all&')
  213. eq("!,'100,<50,s10,h", api.nvim_get_option_value('viminfo', {}))
  214. eq("!,'100,<50,s10,h", api.nvim_get_option_value('shada', {}))
  215. end)
  216. it('is able to set &shada after &viminfo using :set', function()
  217. nvim_command("set viminfo='10")
  218. eq("'10", api.nvim_get_option_value('viminfo', {}))
  219. eq("'10", api.nvim_get_option_value('shada', {}))
  220. nvim_command('set shada=')
  221. eq('', api.nvim_get_option_value('viminfo', {}))
  222. eq('', api.nvim_get_option_value('shada', {}))
  223. end)
  224. it('setting &shada gives proper error message on missing number', function()
  225. eq([[Vim(set):E526: Missing number after <">: shada="]], exc_exec([[set shada=\"]]))
  226. for _, c in ipairs({ "'", '/', ':', '<', '@', 's' }) do
  227. eq(
  228. ([[Vim(set):E526: Missing number after <%s>: shada=%s]]):format(c, c),
  229. exc_exec(([[set shada=%s]]):format(c))
  230. )
  231. end
  232. end)
  233. it('does not crash when ShaDa file directory is not writable', function()
  234. skip(is_os('win'))
  235. fn.mkdir(dirname, '', 0)
  236. eq(0, fn.filewritable(dirname))
  237. reset { shadafile = dirshada, args = { '--cmd', 'set shada=' } }
  238. api.nvim_set_option_value('shada', "'10", {})
  239. eq(
  240. 'Vim(wshada):E886: System error while opening ShaDa file '
  241. .. 'Xtest-functional-shada-shada.d/main.shada for reading to merge '
  242. .. 'before writing it: permission denied',
  243. exc_exec('wshada')
  244. )
  245. api.nvim_set_option_value('shada', '', {})
  246. end)
  247. end)
  248. describe('ShaDa support code', function()
  249. it('does not write NONE file', function()
  250. local session = spawn(
  251. { nvim_prog, '-u', 'NONE', '-i', 'NONE', '--embed', '--headless', '--cmd', 'qall' },
  252. true
  253. )
  254. session:close()
  255. eq(nil, uv.fs_stat('NONE'))
  256. eq(nil, uv.fs_stat('NONE.tmp.a'))
  257. end)
  258. it('does not read NONE file', function()
  259. write_file('NONE', '\005\001\015\131\161na\162rX\194\162rc\145\196\001-')
  260. local session = spawn({ nvim_prog, '-u', 'NONE', '-i', 'NONE', '--embed', '--headless' }, true)
  261. set_session(session)
  262. eq('', fn.getreg('a'))
  263. session:close()
  264. os.remove('NONE')
  265. end)
  266. end)