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