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 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('') set shellslash let g:stack2 = expand('') set noshellslash let g:stack3 = expand('') ]] ) 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('') vim.o.shellslash = true vim.g.stack2 = vim.fn.expand('') vim.o.shellslash = false vim.g.stack3 = vim.fn.expand('') ]] ) 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("")->empty() let s:s = 0zbeef.cafe let d = s:s]]) command('source') eq('2', exec_capture('echo a')) eq("{'k': 'v'}", exec_capture('echo b')) -- Script items are created only on script var access eq('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("") .. "C()" endfunction let D = {-> s:C()}]]) -- Source the 2nd line only feed('ggjV') feed_command(':source') eq('3', exec_capture('echo a')) -- 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('1_C()', exec_capture('echo D()')) -- Source last line only feed_command(':$source') eq('Vim(echo):E117: Unknown function: s:C', exc_exec('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) it('can source lua files', function() local test_file = 'test.lua' write_file( test_file, [[ vim.g.sourced_lua = 1 vim.g.sfile_value = vim.fn.expand('') vim.g.stack_value = vim.fn.expand('') vim.g.script_value = vim.fn.expand('