robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
9.3 KiB352 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, finally = t.describe, t.it, t.before_each, t.finally
5local command = n.command
6local insert = n.insert
7local eq = t.eq
8local pcall_err = t.pcall_err
9local clear = n.clear
10local api = n.api
11local fn = n.fn
12local feed = n.feed
13local feed_command = n.feed_command
14local write_file = t.write_file
15local tmpname = t.tmpname
16local exec = n.exec
17local exec_lua = n.exec_lua
18local eval = n.eval
19local exec_capture = n.exec_capture
20local neq = t.neq
21local matches = t.matches
22local mkdir = t.mkdir
23local rmdir = n.rmdir
24local is_os = t.is_os
25
26describe(':source', function()
27  before_each(function()
28    clear()
29  end)
30
31  it('sourcing a file that is deleted and recreated is consistent vim-patch:8.1.0151', function()
32    local test_file = 'Xfile.vim'
33    local other_file = 'Xfoobar'
34    local script = [[
35      func Func()
36      endfunc
37    ]]
38    write_file(test_file, script)
39    command('source ' .. test_file)
40    os.remove(test_file)
41    write_file(test_file, script)
42    command('source ' .. test_file)
43    os.remove(test_file)
44    write_file(other_file, '')
45    write_file(test_file, script)
46    command('source ' .. test_file)
47    os.remove(other_file)
48    os.remove(test_file)
49  end)
50
51  it("changing 'shellslash' changes the result of expand()", function()
52    t.skip(not is_os('win'), "N/A: 'shellslash' only works on Windows")
53
54    api.nvim_set_option_value('shellslash', false, {})
55    mkdir('Xshellslash')
56
57    write_file(
58      [[Xshellslash/Xstack.vim]],
59      [[
60      let g:stack1 = expand('<stack>')
61      set shellslash
62      let g:stack2 = expand('<stack>')
63      set noshellslash
64      let g:stack3 = expand('<stack>')
65    ]]
66    )
67
68    for _ = 1, 2 do
69      command([[source Xshellslash/Xstack.vim]])
70      matches([[Xshellslash\Xstack%.vim]], api.nvim_get_var('stack1'))
71      matches([[Xshellslash/Xstack%.vim]], api.nvim_get_var('stack2'))
72      matches([[Xshellslash\Xstack%.vim]], api.nvim_get_var('stack3'))
73    end
74
75    write_file(
76      [[Xshellslash/Xstack.lua]],
77      [[
78      vim.g.stack1 = vim.fn.expand('<stack>')
79      vim.o.shellslash = true
80      vim.g.stack2 = vim.fn.expand('<stack>')
81      vim.o.shellslash = false
82      vim.g.stack3 = vim.fn.expand('<stack>')
83    ]]
84    )
85
86    for _ = 1, 2 do
87      command([[source Xshellslash/Xstack.lua]])
88      matches([[Xshellslash\Xstack%.lua]], api.nvim_get_var('stack1'))
89      matches([[Xshellslash/Xstack%.lua]], api.nvim_get_var('stack2'))
90      matches([[Xshellslash\Xstack%.lua]], api.nvim_get_var('stack3'))
91    end
92
93    rmdir('Xshellslash')
94  end)
95
96  it('current buffer', function()
97    insert([[
98      let a = 2
99      let b = #{
100        \ k: "v"
101       "\ (o_o)
102        \ }
103      let c = expand("<SID>")
104      let s:s = 0zbeef.cafe
105      let d = s:s]])
106
107    command('source')
108    eq('2', exec_capture('echo a'))
109    eq("{'k': 'v'}", exec_capture('echo b'))
110    eq('<SNR>1_', exec_capture('echo c'))
111    eq('0zBEEFCAFE', exec_capture('echo d'))
112
113    exec('set cpoptions+=C')
114    matches("Vim%(let%):E723: Missing end of Dictionary '%}'", pcall_err(command, 'source'))
115  end)
116
117  it('selection in current buffer', function()
118    insert([[
119      let a = 2
120      let a = 3
121      let a = 4
122      let b = #{
123       "\ (>_<)
124        \ K: "V"
125        \ }
126      function! s:C() abort
127        return expand("<SID>") .. "C()"
128      endfunction
129      let D = {-> s:C()}]])
130
131    -- Source the 2nd line only
132    feed('ggjV')
133    feed_command(':source')
134    eq('3', exec_capture('echo a'))
135
136    -- Source last line only
137    feed_command(':$source')
138    matches('Vim%(echo%):E117: Unknown function: s:C', pcall_err(command, 'echo D()'))
139
140    -- Source from 2nd line to end of file
141    feed('ggjVG')
142    feed_command(':source')
143    eq('4', exec_capture('echo a'))
144    eq("{'K': 'V'}", exec_capture('echo b'))
145    eq('<SNR>1_C()', exec_capture('echo D()'))
146
147    -- Source last line after the lines that define s:C() have been sourced
148    feed_command(':$source')
149    eq('<SNR>1_C()', exec_capture('echo D()'))
150
151    exec('set cpoptions+=C')
152    matches("Vim%(let%):E723: Missing end of Dictionary '%}'", pcall_err(command, "'<,'>source"))
153  end)
154
155  it('does not break if current buffer is modified while sourced', function()
156    insert [[
157      bwipeout!
158      let a = 123
159    ]]
160    command('source')
161    eq('123', exec_capture('echo a'))
162  end)
163
164  it('multiline heredoc command', function()
165    insert([[
166      lua << EOF
167      y = 4
168      EOF]])
169
170    command('source')
171    eq('4', exec_capture('echo luaeval("y")'))
172  end)
173
174  --- @param verbose boolean
175  local function test_source_lua_file(verbose)
176    local test_file = 'Xtest.lua'
177    write_file(
178      test_file,
179      [[
180      vim.g.sourced_lua = 1
181      vim.g.sfile_value = vim.fn.expand('<sfile>')
182      vim.g.stack_value = vim.fn.expand('<stack>')
183      vim.g.script_value = vim.fn.expand('<script>')
184      vim.g.script_id = tonumber(vim.fn.expand('<SID>'):match('<SNR>(%d+)_'))
185      vim.o.mouse = 'nv'
186    ]]
187    )
188
189    command('set shellslash')
190    command(('%ssource %s'):format(verbose and 'verbose ' or '', test_file))
191    eq(1, eval('g:sourced_lua'))
192    matches([[/Xtest%.lua$]], api.nvim_get_var('sfile_value'))
193    matches([[/Xtest%.lua$]], api.nvim_get_var('stack_value'))
194    matches([[/Xtest%.lua$]], api.nvim_get_var('script_value'))
195
196    local expected_sid = fn.getscriptinfo({ name = test_file })[1].sid
197    local sid = api.nvim_get_var('script_id')
198    eq(expected_sid, sid)
199    eq(sid, api.nvim_get_option_info2('mouse', {}).last_set_sid)
200
201    os.remove(test_file)
202  end
203
204  it('can source lua files', function()
205    test_source_lua_file(false)
206  end)
207
208  it('with :verbose modifier can source lua files', function()
209    test_source_lua_file(true)
210  end)
211
212  describe('can source current buffer', function()
213    local function test_source_lua_curbuf()
214      it('selected region', function()
215        insert([[
216          vim.g.b = 5
217          vim.g.b = 6
218          vim.g.b = 7
219          a = [=[
220           "\ a
221            \ b]=]
222        ]])
223        feed('dd')
224
225        feed('ggjV')
226        feed_command(':source')
227        eq(6, eval('g:b'))
228
229        feed('GVkk')
230        feed_command(':source')
231        eq(' "\\ a\n  \\ b', exec_lua('return _G.a'))
232      end)
233
234      it('whole buffer', function()
235        insert([[
236          vim.g.c = 10
237          vim.g.c = 11
238          vim.g.c = 12
239          a = [=[
240            \ 1
241           "\ 2]=]
242          vim.g.sfile_value = vim.fn.expand('<sfile>')
243          vim.g.stack_value = vim.fn.expand('<stack>')
244          vim.g.script_value = vim.fn.expand('<script>')
245        ]])
246        feed('dd')
247
248        feed_command(':source')
249        local filepath = fn.expand('%:p')
250        if filepath == '' then
251          filepath = ':source buffer=1'
252        end
253        eq(12, eval('g:c'))
254        eq('  \\ 1\n "\\ 2', exec_lua('return _G.a'))
255        eq(filepath, api.nvim_get_var('sfile_value'))
256        eq(filepath, api.nvim_get_var('stack_value'))
257        eq(filepath, api.nvim_get_var('script_value'))
258      end)
259    end
260
261    describe('with ft=lua', function()
262      before_each(function()
263        command('setlocal ft=lua')
264      end)
265      test_source_lua_curbuf()
266    end)
267
268    describe('with .lua extension', function()
269      before_each(function()
270        command('edit ' .. tmpname() .. '.lua')
271      end)
272      test_source_lua_curbuf()
273    end)
274  end)
275
276  it("doesn't throw E484 for lua parsing/runtime errors", function()
277    local test_file = 'Xtest.lua'
278
279    -- Does throw E484 for unreadable files
280    local ok, result = pcall(exec_capture, ':source ' .. test_file .. 'noexisting')
281    eq(false, ok)
282    neq(nil, result:find('E484'))
283
284    -- Doesn't throw for parsing error
285    write_file(test_file, 'vim.g.c = ')
286    ok, result = pcall(exec_capture, ':source ' .. test_file)
287    eq(false, ok)
288    eq(nil, result:find('E484'))
289    os.remove(test_file)
290
291    -- Doesn't throw for runtime error
292    write_file(test_file, "error('Cause error anyway :D')")
293    ok, result = pcall(exec_capture, ':source ' .. test_file)
294    eq(false, ok)
295    eq(nil, result:find('E484'))
296    os.remove(test_file)
297  end)
298
299  it('sources Lua/Vimscript codeblocks based on treesitter injection', function()
300    insert([[
301      *test.txt*  Test help file
302
303      Lua example: >lua
304        vim.g.test_lua = 42
305      <
306
307      Vim example: >vim
308        let g:test_vim = 99
309      <]])
310    command('setlocal filetype=help')
311
312    -- Source Lua codeblock (line 4 contains the Lua code)
313    command(':4source')
314    eq(42, eval('g:test_lua'))
315
316    -- Source Vimscript codeblock (line 8 contains the Vim code)
317    command(':8source')
318    eq(99, eval('g:test_vim'))
319
320    -- Test fallback without treesitter
321    command('enew')
322    insert([[let g:test_no_ts = 123]])
323    command('setlocal filetype=')
324    command('source')
325    eq(123, eval('g:test_no_ts'))
326  end)
327end)
328
329it('$HOME is not shortened in filepath in v:stacktrace from sourced file', function()
330  local xhome = t.fix_slashes(assert(vim.uv.cwd())) .. '/Xhome'
331  mkdir(xhome)
332  clear({ env = { HOME = xhome } })
333  finally(function()
334    rmdir(xhome)
335  end)
336  local filepath = xhome .. '/Xstacktrace.vim'
337  local script = [[
338    func Xfunc()
339      throw 'Exception from Xfunc'
340    endfunc
341  ]]
342  write_file(filepath, script)
343  exec('source ' .. filepath)
344  exec([[
345    try
346      call Xfunc()
347    catch
348      let g:stacktrace = v:stacktrace
349    endtry
350  ]])
351  eq(filepath, n.eval('g:stacktrace[-1].filepath'))
352end)