2021-02-17 23:25:51 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
|
|
|
local command = helpers.command
|
|
|
|
local insert = helpers.insert
|
|
|
|
local eq = helpers.eq
|
|
|
|
local clear = helpers.clear
|
|
|
|
local meths = helpers.meths
|
|
|
|
local feed = helpers.feed
|
|
|
|
local feed_command = helpers.feed_command
|
2021-06-02 18:07:51 -07:00
|
|
|
local write_file = helpers.write_file
|
2023-06-02 06:00:55 -07:00
|
|
|
local tmpname = helpers.tmpname
|
2021-06-02 18:07:51 -07:00
|
|
|
local exec = helpers.exec
|
2021-09-14 16:35:33 -07:00
|
|
|
local exc_exec = helpers.exc_exec
|
|
|
|
local exec_lua = helpers.exec_lua
|
2021-06-02 18:07:51 -07:00
|
|
|
local eval = helpers.eval
|
2021-06-20 10:07:04 -07:00
|
|
|
local exec_capture = helpers.exec_capture
|
|
|
|
local neq = helpers.neq
|
2022-08-14 17:28:20 -07:00
|
|
|
local matches = helpers.matches
|
|
|
|
local mkdir = helpers.mkdir
|
|
|
|
local rmdir = helpers.rmdir
|
2022-11-21 17:13:30 -07:00
|
|
|
local is_os = helpers.is_os
|
2021-02-17 23:25:51 -07:00
|
|
|
|
|
|
|
describe(':source', function()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
end)
|
|
|
|
|
2022-07-07 23:15:59 -07:00
|
|
|
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)
|
|
|
|
|
2022-08-14 17:28:20 -07:00
|
|
|
it("changing 'shellslash' changes the result of expand()", function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if not is_os('win') then
|
2022-08-14 17:28:20 -07:00
|
|
|
pending("'shellslash' only works on Windows")
|
|
|
|
return
|
|
|
|
end
|
2024-01-12 05:44:54 -07:00
|
|
|
meths.nvim_set_option_value('shellslash', false, {})
|
2022-08-14 17:28:20 -07:00
|
|
|
mkdir('Xshellslash')
|
2022-08-14 22:40:01 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(
|
|
|
|
[[Xshellslash/Xstack.vim]],
|
|
|
|
[[
|
2022-08-14 22:40:01 -07:00
|
|
|
let g:stack1 = expand('<stack>')
|
2022-08-14 17:28:20 -07:00
|
|
|
set shellslash
|
2022-08-14 22:40:01 -07:00
|
|
|
let g:stack2 = expand('<stack>')
|
2022-08-14 17:28:20 -07:00
|
|
|
set noshellslash
|
2022-08-14 22:40:01 -07:00
|
|
|
let g:stack3 = expand('<stack>')
|
2024-01-02 18:09:18 -07:00
|
|
|
]]
|
|
|
|
)
|
2022-08-14 17:28:20 -07:00
|
|
|
|
2022-08-14 22:40:01 -07:00
|
|
|
for _ = 1, 2 do
|
|
|
|
command([[source Xshellslash/Xstack.vim]])
|
2024-01-12 05:44:54 -07:00
|
|
|
matches([[Xshellslash\Xstack%.vim]], meths.nvim_get_var('stack1'))
|
|
|
|
matches([[Xshellslash/Xstack%.vim]], meths.nvim_get_var('stack2'))
|
|
|
|
matches([[Xshellslash\Xstack%.vim]], meths.nvim_get_var('stack3'))
|
2022-08-14 22:40:01 -07:00
|
|
|
end
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(
|
|
|
|
[[Xshellslash/Xstack.lua]],
|
|
|
|
[[
|
2022-08-14 22:40:01 -07:00
|
|
|
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>')
|
2024-01-02 18:09:18 -07:00
|
|
|
]]
|
|
|
|
)
|
2022-08-14 22:40:01 -07:00
|
|
|
|
|
|
|
for _ = 1, 2 do
|
|
|
|
command([[source Xshellslash/Xstack.lua]])
|
2024-01-12 05:44:54 -07:00
|
|
|
matches([[Xshellslash\Xstack%.lua]], meths.nvim_get_var('stack1'))
|
|
|
|
matches([[Xshellslash/Xstack%.lua]], meths.nvim_get_var('stack2'))
|
|
|
|
matches([[Xshellslash\Xstack%.lua]], meths.nvim_get_var('stack3'))
|
2022-08-14 22:40:01 -07:00
|
|
|
end
|
2022-08-14 17:28:20 -07:00
|
|
|
|
|
|
|
rmdir('Xshellslash')
|
|
|
|
end)
|
|
|
|
|
2021-02-17 23:25:51 -07:00
|
|
|
it('current buffer', function()
|
2021-09-14 16:35:33 -07:00
|
|
|
insert([[
|
|
|
|
let a = 2
|
|
|
|
let b = #{
|
|
|
|
\ k: "v"
|
|
|
|
"\ (o_o)
|
2021-10-09 14:13:11 -07:00
|
|
|
\ }
|
2021-10-13 16:40:46 -07:00
|
|
|
let c = expand("<SID>")->empty()
|
2021-10-09 14:13:11 -07:00
|
|
|
let s:s = 0zbeef.cafe
|
2021-10-13 16:40:46 -07:00
|
|
|
let d = s:s]])
|
2021-09-14 16:35:33 -07:00
|
|
|
|
2021-02-17 23:25:51 -07:00
|
|
|
command('source')
|
2023-03-25 19:49:32 -07:00
|
|
|
eq('2', exec_capture('echo a'))
|
|
|
|
eq("{'k': 'v'}", exec_capture('echo b'))
|
2021-10-13 16:40:46 -07:00
|
|
|
|
|
|
|
-- Script items are created only on script var access
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('1', exec_capture('echo c'))
|
|
|
|
eq('0zBEEFCAFE', exec_capture('echo d'))
|
2021-09-14 16:35:33 -07:00
|
|
|
|
|
|
|
exec('set cpoptions+=C')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq("Vim(let):E723: Missing end of Dictionary '}': ", exc_exec('source'))
|
2021-02-17 23:25:51 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('selection in current buffer', function()
|
2021-09-14 16:35:33 -07:00
|
|
|
insert([[
|
|
|
|
let a = 2
|
|
|
|
let a = 3
|
|
|
|
let a = 4
|
|
|
|
let b = #{
|
|
|
|
"\ (>_<)
|
|
|
|
\ K: "V"
|
2021-10-09 14:13:11 -07:00
|
|
|
\ }
|
|
|
|
function! s:C() abort
|
|
|
|
return expand("<SID>") .. "C()"
|
|
|
|
endfunction
|
|
|
|
let D = {-> s:C()}]])
|
2021-02-17 23:25:51 -07:00
|
|
|
|
|
|
|
-- Source the 2nd line only
|
|
|
|
feed('ggjV')
|
|
|
|
feed_command(':source')
|
2023-03-25 19:49:32 -07:00
|
|
|
eq('3', exec_capture('echo a'))
|
2021-02-17 23:25:51 -07:00
|
|
|
|
|
|
|
-- Source from 2nd line to end of file
|
|
|
|
feed('ggjVG')
|
|
|
|
feed_command(':source')
|
2023-03-25 19:49:32 -07:00
|
|
|
eq('4', exec_capture('echo a'))
|
|
|
|
eq("{'K': 'V'}", exec_capture('echo b'))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('<SNR>1_C()', exec_capture('echo D()'))
|
2021-10-09 14:13:11 -07:00
|
|
|
|
|
|
|
-- Source last line only
|
|
|
|
feed_command(':$source')
|
|
|
|
eq('Vim(echo):E117: Unknown function: s:C', exc_exec('echo D()'))
|
2021-09-14 16:35:33 -07:00
|
|
|
|
|
|
|
exec('set cpoptions+=C')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq("Vim(let):E723: Missing end of Dictionary '}': ", exc_exec("'<,'>source"))
|
2021-02-17 23:25:51 -07:00
|
|
|
end)
|
|
|
|
|
2021-09-15 05:17:07 -07:00
|
|
|
it('does not break if current buffer is modified while sourced', function()
|
|
|
|
insert [[
|
|
|
|
bwipeout!
|
|
|
|
let a = 123
|
|
|
|
]]
|
|
|
|
command('source')
|
2023-03-25 19:49:32 -07:00
|
|
|
eq('123', exec_capture('echo a'))
|
2021-09-15 05:17:07 -07:00
|
|
|
end)
|
|
|
|
|
2021-02-17 23:25:51 -07:00
|
|
|
it('multiline heredoc command', function()
|
2021-09-14 16:35:33 -07:00
|
|
|
insert([[
|
|
|
|
lua << EOF
|
|
|
|
y = 4
|
|
|
|
EOF]])
|
2021-02-17 23:25:51 -07:00
|
|
|
|
|
|
|
command('source')
|
2023-03-25 19:49:32 -07:00
|
|
|
eq('4', exec_capture('echo luaeval("y")'))
|
2021-02-17 23:25:51 -07:00
|
|
|
end)
|
2021-06-02 18:07:51 -07:00
|
|
|
|
|
|
|
it('can source lua files', function()
|
|
|
|
local test_file = 'test.lua'
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(
|
|
|
|
test_file,
|
|
|
|
[[
|
2022-08-14 22:40:01 -07:00
|
|
|
vim.g.sourced_lua = 1
|
|
|
|
vim.g.sfile_value = vim.fn.expand('<sfile>')
|
|
|
|
vim.g.stack_value = vim.fn.expand('<stack>')
|
2022-08-19 16:54:14 -07:00
|
|
|
vim.g.script_value = vim.fn.expand('<script>')
|
2024-01-02 18:09:18 -07:00
|
|
|
]]
|
|
|
|
)
|
2021-06-02 18:07:51 -07:00
|
|
|
|
2022-08-14 22:40:01 -07:00
|
|
|
command('set shellslash')
|
|
|
|
command('source ' .. test_file)
|
2021-06-02 18:07:51 -07:00
|
|
|
eq(1, eval('g:sourced_lua'))
|
2024-01-12 05:44:54 -07:00
|
|
|
matches([[/test%.lua$]], meths.nvim_get_var('sfile_value'))
|
|
|
|
matches([[/test%.lua$]], meths.nvim_get_var('stack_value'))
|
|
|
|
matches([[/test%.lua$]], meths.nvim_get_var('script_value'))
|
2022-08-14 22:40:01 -07:00
|
|
|
|
2021-06-02 18:07:51 -07:00
|
|
|
os.remove(test_file)
|
|
|
|
end)
|
|
|
|
|
2023-06-02 06:00:55 -07:00
|
|
|
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'))
|
2024-01-12 05:44:54 -07:00
|
|
|
eq(':source (no file)', meths.nvim_get_var('sfile_value'))
|
|
|
|
eq(':source (no file)', meths.nvim_get_var('stack_value'))
|
|
|
|
eq(':source (no file)', meths.nvim_get_var('script_value'))
|
2023-06-02 06:00:55 -07:00
|
|
|
end)
|
|
|
|
end
|
2022-08-14 22:40:01 -07:00
|
|
|
|
2023-06-02 06:00:55 -07:00
|
|
|
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)
|
2021-06-02 18:07:51 -07:00
|
|
|
end)
|
2021-06-20 10:07:04 -07:00
|
|
|
|
|
|
|
it("doesn't throw E484 for lua parsing/runtime errors", function()
|
|
|
|
local test_file = 'test.lua'
|
|
|
|
|
|
|
|
-- Does throw E484 for unreadable files
|
2024-01-02 18:09:18 -07:00
|
|
|
local ok, result = pcall(exec_capture, ':source ' .. test_file .. 'noexisting')
|
2021-06-20 10:07:04 -07:00
|
|
|
eq(false, ok)
|
2024-01-02 18:09:18 -07:00
|
|
|
neq(nil, result:find('E484'))
|
2021-06-20 10:07:04 -07:00
|
|
|
|
|
|
|
-- Doesn't throw for parsing error
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(test_file, 'vim.g.c = ')
|
|
|
|
ok, result = pcall(exec_capture, ':source ' .. test_file)
|
2021-06-20 10:07:04 -07:00
|
|
|
eq(false, ok)
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(nil, result:find('E484'))
|
2021-06-20 10:07:04 -07:00
|
|
|
os.remove(test_file)
|
|
|
|
|
|
|
|
-- Doesn't throw for runtime error
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(test_file, "error('Cause error anyway :D')")
|
|
|
|
ok, result = pcall(exec_capture, ':source ' .. test_file)
|
2021-06-20 10:07:04 -07:00
|
|
|
eq(false, ok)
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(nil, result:find('E484'))
|
2021-06-20 10:07:04 -07:00
|
|
|
os.remove(test_file)
|
|
|
|
end)
|
2021-02-17 23:25:51 -07:00
|
|
|
end)
|