source_spec.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local command = helpers.command
  3. local insert = helpers.insert
  4. local eq = helpers.eq
  5. local clear = helpers.clear
  6. local meths = helpers.meths
  7. local feed = helpers.feed
  8. local feed_command = helpers.feed_command
  9. local write_file = helpers.write_file
  10. local exec = helpers.exec
  11. local exc_exec = helpers.exc_exec
  12. local exec_lua = helpers.exec_lua
  13. local eval = helpers.eval
  14. local exec_capture = helpers.exec_capture
  15. local neq = helpers.neq
  16. local matches = helpers.matches
  17. local mkdir = helpers.mkdir
  18. local rmdir = helpers.rmdir
  19. local is_os = helpers.is_os
  20. describe(':source', function()
  21. before_each(function()
  22. clear()
  23. end)
  24. it('sourcing a file that is deleted and recreated is consistent vim-patch:8.1.0151', function()
  25. local test_file = 'Xfile.vim'
  26. local other_file = 'Xfoobar'
  27. local script = [[
  28. func Func()
  29. endfunc
  30. ]]
  31. write_file(test_file, script)
  32. command('source ' .. test_file)
  33. os.remove(test_file)
  34. write_file(test_file, script)
  35. command('source ' .. test_file)
  36. os.remove(test_file)
  37. write_file(other_file, '')
  38. write_file(test_file, script)
  39. command('source ' .. test_file)
  40. os.remove(other_file)
  41. os.remove(test_file)
  42. end)
  43. it("changing 'shellslash' changes the result of expand()", function()
  44. if not is_os('win') then
  45. pending("'shellslash' only works on Windows")
  46. return
  47. end
  48. meths.set_option('shellslash', false)
  49. mkdir('Xshellslash')
  50. write_file([[Xshellslash/Xstack.vim]], [[
  51. let g:stack1 = expand('<stack>')
  52. set shellslash
  53. let g:stack2 = expand('<stack>')
  54. set noshellslash
  55. let g:stack3 = expand('<stack>')
  56. ]])
  57. for _ = 1, 2 do
  58. command([[source Xshellslash/Xstack.vim]])
  59. matches([[Xshellslash\Xstack%.vim]], meths.get_var('stack1'))
  60. matches([[Xshellslash/Xstack%.vim]], meths.get_var('stack2'))
  61. matches([[Xshellslash\Xstack%.vim]], meths.get_var('stack3'))
  62. end
  63. write_file([[Xshellslash/Xstack.lua]], [[
  64. vim.g.stack1 = vim.fn.expand('<stack>')
  65. vim.o.shellslash = true
  66. vim.g.stack2 = vim.fn.expand('<stack>')
  67. vim.o.shellslash = false
  68. vim.g.stack3 = vim.fn.expand('<stack>')
  69. ]])
  70. for _ = 1, 2 do
  71. command([[source Xshellslash/Xstack.lua]])
  72. matches([[Xshellslash\Xstack%.lua]], meths.get_var('stack1'))
  73. matches([[Xshellslash/Xstack%.lua]], meths.get_var('stack2'))
  74. matches([[Xshellslash\Xstack%.lua]], meths.get_var('stack3'))
  75. end
  76. rmdir('Xshellslash')
  77. end)
  78. it('current buffer', function()
  79. insert([[
  80. let a = 2
  81. let b = #{
  82. \ k: "v"
  83. "\ (o_o)
  84. \ }
  85. let c = expand("<SID>")->empty()
  86. let s:s = 0zbeef.cafe
  87. let d = s:s]])
  88. command('source')
  89. eq('2', exec_capture('echo a'))
  90. eq("{'k': 'v'}", exec_capture('echo b'))
  91. -- Script items are created only on script var access
  92. eq("1", exec_capture('echo c'))
  93. eq("0zBEEFCAFE", exec_capture('echo d'))
  94. exec('set cpoptions+=C')
  95. eq('Vim(let):E723: Missing end of Dictionary \'}\': ', exc_exec('source'))
  96. end)
  97. it('selection in current buffer', function()
  98. insert([[
  99. let a = 2
  100. let a = 3
  101. let a = 4
  102. let b = #{
  103. "\ (>_<)
  104. \ K: "V"
  105. \ }
  106. function! s:C() abort
  107. return expand("<SID>") .. "C()"
  108. endfunction
  109. let D = {-> s:C()}]])
  110. -- Source the 2nd line only
  111. feed('ggjV')
  112. feed_command(':source')
  113. eq('3', exec_capture('echo a'))
  114. -- Source from 2nd line to end of file
  115. feed('ggjVG')
  116. feed_command(':source')
  117. eq('4', exec_capture('echo a'))
  118. eq("{'K': 'V'}", exec_capture('echo b'))
  119. eq("<SNR>1_C()", exec_capture('echo D()'))
  120. -- Source last line only
  121. feed_command(':$source')
  122. eq('Vim(echo):E117: Unknown function: s:C', exc_exec('echo D()'))
  123. exec('set cpoptions+=C')
  124. eq('Vim(let):E723: Missing end of Dictionary \'}\': ', exc_exec("'<,'>source"))
  125. end)
  126. it('does not break if current buffer is modified while sourced', function()
  127. insert [[
  128. bwipeout!
  129. let a = 123
  130. ]]
  131. command('source')
  132. eq('123', exec_capture('echo a'))
  133. end)
  134. it('multiline heredoc command', function()
  135. insert([[
  136. lua << EOF
  137. y = 4
  138. EOF]])
  139. command('source')
  140. eq('4', exec_capture('echo luaeval("y")'))
  141. end)
  142. it('can source lua files', function()
  143. local test_file = 'test.lua'
  144. write_file(test_file, [[
  145. vim.g.sourced_lua = 1
  146. vim.g.sfile_value = vim.fn.expand('<sfile>')
  147. vim.g.stack_value = vim.fn.expand('<stack>')
  148. vim.g.script_value = vim.fn.expand('<script>')
  149. ]])
  150. command('set shellslash')
  151. command('source ' .. test_file)
  152. eq(1, eval('g:sourced_lua'))
  153. matches([[/test%.lua$]], meths.get_var('sfile_value'))
  154. matches([[/test%.lua$]], meths.get_var('stack_value'))
  155. matches([[/test%.lua$]], meths.get_var('script_value'))
  156. os.remove(test_file)
  157. end)
  158. it('can source selected region in lua file', function()
  159. local test_file = 'test.lua'
  160. write_file (test_file, [[
  161. vim.g.b = 5
  162. vim.g.b = 6
  163. vim.g.b = 7
  164. a = [=[
  165. "\ a
  166. \ b]=]
  167. ]])
  168. command('edit '..test_file)
  169. feed('ggjV')
  170. feed_command(':source')
  171. eq(6, eval('g:b'))
  172. feed('GVkk')
  173. feed_command(':source')
  174. eq(' "\\ a\n \\ b', exec_lua('return _G.a'))
  175. os.remove(test_file)
  176. end)
  177. it('can source current lua buffer without argument', function()
  178. local test_file = 'test.lua'
  179. write_file(test_file, [[
  180. vim.g.c = 10
  181. vim.g.c = 11
  182. vim.g.c = 12
  183. a = [=[
  184. \ 1
  185. "\ 2]=]
  186. vim.g.sfile_value = vim.fn.expand('<sfile>')
  187. vim.g.stack_value = vim.fn.expand('<stack>')
  188. vim.g.script_value = vim.fn.expand('<script>')
  189. ]])
  190. command('edit '..test_file)
  191. feed_command(':source')
  192. eq(12, eval('g:c'))
  193. eq(' \\ 1\n "\\ 2', exec_lua('return _G.a'))
  194. eq(':source (no file)', meths.get_var('sfile_value'))
  195. eq(':source (no file)', meths.get_var('stack_value'))
  196. eq(':source (no file)', meths.get_var('script_value'))
  197. os.remove(test_file)
  198. end)
  199. it("doesn't throw E484 for lua parsing/runtime errors", function()
  200. local test_file = 'test.lua'
  201. -- Does throw E484 for unreadable files
  202. local ok, result = pcall(exec_capture, ":source "..test_file ..'noexisting')
  203. eq(false, ok)
  204. neq(nil, result:find("E484"))
  205. -- Doesn't throw for parsing error
  206. write_file (test_file, "vim.g.c = ")
  207. ok, result = pcall(exec_capture, ":source "..test_file)
  208. eq(false, ok)
  209. eq(nil, result:find("E484"))
  210. os.remove(test_file)
  211. -- Doesn't throw for runtime error
  212. write_file (test_file, "error('Cause error anyway :D')")
  213. ok, result = pcall(exec_capture, ":source "..test_file)
  214. eq(false, ok)
  215. eq(nil, result:find("E484"))
  216. os.remove(test_file)
  217. end)
  218. end)