robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

taorefactor(path): pathcmp() #41035f0a0182285

master
12.4 KiB364 linesraw
1-- Other ShaDa tests
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4local t_shada = require('test.functional.shada.testutil')
5local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
6local uv = vim.uv
7local paths = t.paths
8local pcall_err = t.pcall_err
9
10local api, nvim_command, fn, eq = n.api, n.command, n.fn, t.eq
11local write_file, set_session = t.write_file, n.set_session
12local is_os = t.is_os
13local skip = t.skip
14local feed = n.feed
15
16local reset, clear, get_shada_rw = t_shada.reset, t_shada.clear, t_shada.get_shada_rw
17local read_shada_file = t_shada.read_shada_file
18
19local wshada, _, shada_fname, clean = get_shada_rw('Xtest-functional-shada-shada.shada')
20
21local dirname = 'Xtest-functional-shada-shada.d'
22local dirshada = dirname .. '/main.shada'
23
24describe('ShaDa support code', function()
25  before_each(reset)
26  after_each(function()
27    clear()
28    clean()
29    uv.fs_rmdir(dirname)
30  end)
31
32  it('preserves `s` item size limit with unknown entries', function()
33    wshada(
34      '\100\000\207\000\000\000\000\000\000\004\000\218\003\253'
35        .. ('-'):rep(1024 - 3)
36        .. '\100\000\207\000\000\000\000\000\000\004\001\218\003\254'
37        .. ('-'):rep(1025 - 3)
38    )
39    n.command('wshada ' .. shada_fname)
40    local found = 0
41    for _, v in ipairs(read_shada_file(shada_fname)) do
42      if v.type == 100 then
43        found = found + 1
44      end
45    end
46    eq(2, found)
47    n.command('set shada-=s10 shada+=s1')
48    n.command('wshada ' .. shada_fname)
49    found = 0
50    for _, v in ipairs(read_shada_file(shada_fname)) do
51      if v.type == 100 then
52        found = found + 1
53      end
54    end
55    eq(1, found)
56  end)
57
58  it('preserves `s` item size limit with instance history entries', function()
59    local hist1 = ('-'):rep(1024 - 5)
60    local hist2 = ('-'):rep(1025 - 5)
61    nvim_command('set shada-=s10 shada+=s1')
62    fn.histadd(':', hist1)
63    fn.histadd(':', hist2)
64    n.command('wshada ' .. shada_fname)
65    local found = 0
66    for _, v in ipairs(read_shada_file(shada_fname)) do
67      if v.type == 4 then
68        found = found + 1
69        eq(hist1, v.value[2])
70      end
71    end
72    eq(1, found)
73  end)
74
75  it('leaves .tmp.a in-place when there is error in original ShaDa', function()
76    wshada('Some text file')
77    eq(
78      'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
79      t.pcall_err(n.command, 'wshada ' .. shada_fname)
80    )
81    eq(1, read_shada_file(shada_fname .. '.tmp.a')[1].type)
82  end)
83
84  it(
85    'does not leave .tmp.a in-place when there is error in original ShaDa, but writing with bang',
86    function()
87      wshada('Some text file')
88      n.command('wshada! ' .. shada_fname)
89      eq(1, read_shada_file(shada_fname)[1].type)
90      eq(nil, uv.fs_stat(shada_fname .. '.tmp.a'))
91    end
92  )
93
94  it('leaves .tmp.b in-place when there is error in original ShaDa and it has .tmp.a', function()
95    wshada('Some text file')
96    eq(
97      'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
98      t.pcall_err(n.command, 'wshada ' .. shada_fname)
99    )
100    eq(
101      'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
102      t.pcall_err(n.command, 'wshada ' .. shada_fname)
103    )
104    eq(1, read_shada_file(shada_fname .. '.tmp.a')[1].type)
105    eq(1, read_shada_file(shada_fname .. '.tmp.b')[1].type)
106  end)
107
108  it(
109    'leaves .tmp.z in-place when there is error in original ShaDa and it has .tmp.a … .tmp.x',
110    function()
111      wshada('Some text file')
112      local i = ('a'):byte()
113      while i < ('z'):byte() do
114        write_file(shada_fname .. ('.tmp.%c'):format(i), 'Some text file', true)
115        i = i + 1
116      end
117      eq(
118        'Vim(wshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
119        t.pcall_err(n.command, 'wshada ' .. shada_fname)
120      )
121      eq(1, read_shada_file(shada_fname .. '.tmp.z')[1].type)
122    end
123  )
124
125  it('errors out when there are .tmp.a … .tmp.z ShaDa files', function()
126    wshada('')
127    local i = ('a'):byte()
128    while i <= ('z'):byte() do
129      write_file(shada_fname .. ('.tmp.%c'):format(i), '', true)
130      i = i + 1
131    end
132    eq(
133      'Vim(wshada):E138: All Xtest-functional-shada-shada.shada.tmp.X files exist, cannot write ShaDa file!',
134      t.pcall_err(n.command, 'wshada ' .. shada_fname)
135    )
136  end)
137
138  it('reads correctly various timestamps', function()
139    local msgpack = {
140      '\100', -- Positive fixnum 100
141      '\204\255', -- uint 8 255
142      '\205\010\003', -- uint 16 2563
143      '\206\255\010\030\004', -- uint 32 4278853124
144      '\207\005\100\060\250\255\010\030\004', -- uint 64 388502516579048964
145    }
146    local s = '\100'
147    local e = '\001\192'
148    wshada(s .. table.concat(msgpack, e .. s) .. e)
149    n.command('wshada ' .. shada_fname)
150    local found = 0
151    local typ = vim.mpack.decode(s)
152    for _, v in ipairs(read_shada_file(shada_fname)) do
153      if v.type == typ then
154        found = found + 1
155        eq(vim.mpack.decode(msgpack[found]), v.timestamp)
156      end
157    end
158    eq(#msgpack, found)
159  end)
160
161  local marklike = { [7] = true, [8] = true, [10] = true, [11] = true }
162  local find_file = function(fname)
163    local found = {}
164    for _, v in ipairs(read_shada_file(shada_fname)) do
165      if marklike[v.type] and v.value.f == fname then
166        found[v.type] = (found[v.type] or 0) + 1
167      elseif v.type == 9 then
168        for _, b in ipairs(v.value) do
169          if b.f == fname then
170            found[v.type] = (found[v.type] or 0) + 1
171          end
172        end
173      end
174    end
175    return found
176  end
177
178  it('correctly uses shada-r option', function()
179    nvim_command('set shellslash')
180    api.nvim_set_var('__home', paths.test_source_path)
181    nvim_command('let $HOME = __home')
182    nvim_command('unlet __home')
183    nvim_command('edit ~/README.md')
184    nvim_command('normal! GmAggmaAabc')
185    nvim_command('undo')
186    nvim_command('set shada+=%')
187    nvim_command('wshada! ' .. shada_fname)
188    local readme_fname = fn.resolve(paths.test_source_path) .. '/README.md'
189    eq({ [7] = 2, [8] = 2, [9] = 1, [10] = 4, [11] = 1 }, find_file(readme_fname))
190    nvim_command('set shada+=r~')
191    nvim_command('wshada! ' .. shada_fname)
192    eq({}, find_file(readme_fname))
193    nvim_command('set shada-=r~')
194    nvim_command('wshada! ' .. shada_fname)
195    eq({ [7] = 2, [8] = 2, [9] = 1, [10] = 4, [11] = 1 }, find_file(readme_fname))
196    nvim_command('set shada+=r' .. fn.escape(fn.escape(paths.test_source_path, '$~'), ' "\\,'))
197    nvim_command('wshada! ' .. shada_fname)
198    eq({}, find_file(readme_fname))
199  end)
200
201  it('correctly ignores case with shada-r option', function()
202    nvim_command('set shellslash')
203    local pwd = fn.getcwd()
204    local relfname = 'абв/test'
205    local fname = pwd .. '/' .. relfname
206    api.nvim_set_var('__fname', fname)
207    nvim_command('silent! edit `=__fname`')
208    fn.setline(1, { 'a', 'b', 'c', 'd' })
209    nvim_command('normal! GmAggmaAabc')
210    nvim_command('undo')
211    nvim_command('set shada+=%')
212    nvim_command('wshada! ' .. shada_fname)
213    eq({ [7] = 2, [8] = 2, [9] = 1, [10] = 4, [11] = 2 }, find_file(fname))
214    nvim_command('set shada+=r' .. pwd .. '/АБВ')
215    nvim_command('wshada! ' .. shada_fname)
216    eq({}, find_file(fname))
217  end)
218
219  it("does not store 'nobuflisted' buffer", function()
220    nvim_command('set shellslash')
221    local fname = fn.getcwd() .. '/file'
222    api.nvim_set_var('__fname', fname)
223    nvim_command('edit `=__fname`')
224    api.nvim_set_option_value('buflisted', false, {})
225    nvim_command('wshada! ' .. shada_fname)
226    eq({}, find_file(fname))
227    -- Set 'buflisted', then check again.
228    api.nvim_set_option_value('buflisted', true, {})
229    nvim_command('wshada! ' .. shada_fname)
230    eq({ [7] = 1, [8] = 1, [10] = 1 }, find_file(fname))
231  end)
232
233  it('is able to set &shada after &viminfo', function()
234    api.nvim_set_option_value('viminfo', "'10", {})
235    eq("'10", api.nvim_get_option_value('viminfo', {}))
236    eq("'10", api.nvim_get_option_value('shada', {}))
237    api.nvim_set_option_value('shada', '', {})
238    eq('', api.nvim_get_option_value('viminfo', {}))
239    eq('', api.nvim_get_option_value('shada', {}))
240  end)
241
242  it('is able to set all& after setting &shada', function()
243    api.nvim_set_option_value('shada', "'10", {})
244    eq("'10", api.nvim_get_option_value('viminfo', {}))
245    eq("'10", api.nvim_get_option_value('shada', {}))
246    nvim_command('set all&')
247    eq("!,'100,<50,s10,h,r/tmp/,r/private/", api.nvim_get_option_value('viminfo', {}))
248    eq("!,'100,<50,s10,h,r/tmp/,r/private/", api.nvim_get_option_value('shada', {}))
249  end)
250
251  it('is able to set &shada after &viminfo using :set', function()
252    nvim_command("set viminfo='10")
253    eq("'10", api.nvim_get_option_value('viminfo', {}))
254    eq("'10", api.nvim_get_option_value('shada', {}))
255    nvim_command('set shada=')
256    eq('', api.nvim_get_option_value('viminfo', {}))
257    eq('', api.nvim_get_option_value('shada', {}))
258  end)
259
260  it('setting &shada gives proper error message on missing number', function()
261    eq(
262      [[Vim(set):E526: Missing number after <">: shada="]],
263      t.pcall_err(n.command, [[set shada=\"]])
264    )
265    for _, c in ipairs({ "'", '/', ':', '<', '@', 's' }) do
266      eq(
267        ([[Vim(set):E526: Missing number after <%s>: shada=%s]]):format(c, c),
268        t.pcall_err(n.command, ([[set shada=%s]]):format(c))
269      )
270    end
271  end)
272
273  it('":wshada/:rshada [filename]" works when shadafile=NONE', function()
274    nvim_command('set shadafile=NONE')
275    nvim_command('wshada ' .. shada_fname)
276    eq(1, read_shada_file(shada_fname)[1].type)
277
278    wshada('Some text file')
279    eq(
280      'Vim(rshada):E576: Error while reading ShaDa file: last entry specified that it occupies 109 bytes, but file ended earlier',
281      t.pcall_err(n.command, 'rshada ' .. shada_fname)
282    )
283  end)
284
285  it(':wshada/:rshada without arguments is no-op when shadafile=NONE', function()
286    nvim_command('set shadafile=NONE')
287    nvim_command('wshada')
288    nvim_command('rshada')
289  end)
290
291  it('does not crash when ShaDa file directory is not writable', function()
292    skip(is_os('win'))
293
294    fn.mkdir(dirname, '', '0')
295    eq(0, fn.filewritable(dirname))
296    reset { shadafile = dirshada, args = { '--cmd', 'set shada=' } }
297    api.nvim_set_option_value('shada', "'10", {})
298    eq(
299      'Vim(wshada):E886: System error while opening ShaDa file '
300        .. 'Xtest-functional-shada-shada.d/main.shada for reading to merge '
301        .. 'before writing it: permission denied',
302      t.pcall_err(n.command, 'wshada')
303    )
304    api.nvim_set_option_value('shada', '', {})
305  end)
306
307  it('deduplicates items on case-insensitive systems', function()
308    local file = ('%s/nonexistent/héllo'):format(dirname)
309    nvim_command(('edit %s'):format(file))
310    file = api.nvim_buf_get_name(0)
311
312    local file_upper = fn.toupper(file)
313    if t.is_os('win') then
314      file_upper = file_upper:sub(3) -- drop drive letter
315    end
316
317    nvim_command(('edit %s'):format(file_upper))
318    feed('i1<Esc>')
319    nvim_command(('wshada! %s'):format(dirshada))
320    nvim_command('bw!')
321
322    -- path_cmp() ignores a single trailing slash
323    local file_slash = ('%s/'):format(file)
324    nvim_command(('edit %s'):format(file_slash))
325    feed('i123<Esc>')
326    nvim_command('mark a')
327    nvim_command(('wshada %s'):format(dirshada))
328    nvim_command('bw!')
329    nvim_command(('rshada! %s'):format(dirshada))
330    local oldfiles = api.nvim_get_vvar('oldfiles')
331    -- Both filenames appear in shada file, but iteration order is unspecified.
332    if t.is_os('win') or t.is_os('mac') then
333      eq(1, #oldfiles)
334      t.ok(oldfiles[1] == file or oldfiles[1] == file_slash)
335    else
336      eq(2, #oldfiles)
337      t.ok(oldfiles[1] == file or oldfiles[1] == file_upper)
338    end
339  end)
340end)
341
342describe('ShaDa support code', function()
343  it('does not write NONE file', function()
344    local session = n.new_session(false, {
345      merge = false,
346      args = { '-u', 'NONE', '-i', 'NONE', '--embed', '--headless', '--cmd', 'qall' },
347    })
348    session:close()
349    eq(nil, uv.fs_stat('NONE'))
350    eq(nil, uv.fs_stat('NONE.tmp.a'))
351  end)
352
353  it('does not read NONE file', function()
354    write_file('NONE', '\005\001\015\131\161na\162rX\194\162rc\145\196\001-')
355    local session = n.new_session(
356      false,
357      { merge = false, args = { '-u', 'NONE', '-i', 'NONE', '--embed', '--headless' } }
358    )
359    set_session(session)
360    eq('', fn.getreg('a'))
361    session:close()
362    os.remove('NONE')
363  end)
364end)