commands_spec.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. -- Test suite for checking :lua* commands
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local Screen = require('test.functional.ui.screen')
  4. local eq = helpers.eq
  5. local NIL = helpers.NIL
  6. local eval = helpers.eval
  7. local feed = helpers.feed
  8. local clear = helpers.clear
  9. local meths = helpers.meths
  10. local funcs = helpers.funcs
  11. local source = helpers.source
  12. local dedent = helpers.dedent
  13. local command = helpers.command
  14. local exc_exec = helpers.exc_exec
  15. local pcall_err = helpers.pcall_err
  16. local write_file = helpers.write_file
  17. local redir_exec = helpers.redir_exec
  18. local curbufmeths = helpers.curbufmeths
  19. before_each(clear)
  20. describe(':lua command', function()
  21. it('works', function()
  22. eq('', redir_exec(
  23. 'lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TEST"})'))
  24. eq({'', 'TEST'}, curbufmeths.get_lines(0, 100, false))
  25. source(dedent([[
  26. lua << EOF
  27. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TSET"})
  28. EOF]]))
  29. eq({'', 'TSET'}, curbufmeths.get_lines(0, 100, false))
  30. source(dedent([[
  31. lua << EOF
  32. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"SETT"})]]))
  33. eq({'', 'SETT'}, curbufmeths.get_lines(0, 100, false))
  34. source(dedent([[
  35. lua << EOF
  36. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  37. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  38. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  39. EOF]]))
  40. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  41. end)
  42. it('throws catchable errors', function()
  43. eq([[Vim(lua):E5107: Error loading lua [string ":lua"]:0: unexpected symbol near ')']],
  44. pcall_err(command, 'lua ()'))
  45. eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: TEST]],
  46. exc_exec('lua error("TEST")'))
  47. eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: Invalid buffer id: -10]],
  48. exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})'))
  49. eq({''}, curbufmeths.get_lines(0, 100, false))
  50. end)
  51. it('works with NULL errors', function()
  52. eq([=[Vim(lua):E5108: Error executing lua [NULL]]=],
  53. exc_exec('lua error(nil)'))
  54. end)
  55. it('accepts embedded NLs without heredoc', function()
  56. -- Such code is usually used for `:execute 'lua' {generated_string}`:
  57. -- heredocs do not work in this case.
  58. meths.command([[
  59. lua
  60. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  61. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  62. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  63. ]])
  64. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  65. end)
  66. it('preserves global and not preserves local variables', function()
  67. eq('', redir_exec('lua gvar = 42'))
  68. eq('', redir_exec('lua local lvar = 100500'))
  69. eq(NIL, funcs.luaeval('lvar'))
  70. eq(42, funcs.luaeval('gvar'))
  71. end)
  72. it('works with long strings', function()
  73. local s = ('x'):rep(100500)
  74. eq('\nE5107: Error loading lua [string ":lua"]:1: unfinished string near \'<eof>\'', redir_exec(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s})'):format(s)))
  75. eq({''}, curbufmeths.get_lines(0, -1, false))
  76. eq('', redir_exec(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s"})'):format(s)))
  77. eq({'', s}, curbufmeths.get_lines(0, -1, false))
  78. end)
  79. it('can show multiline error messages', function()
  80. local screen = Screen.new(40,10)
  81. screen:attach()
  82. screen:set_default_attr_ids({
  83. [1] = {bold = true, foreground = Screen.colors.Blue1},
  84. [2] = {bold = true, reverse = true},
  85. [3] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red},
  86. [4] = {bold = true, foreground = Screen.colors.SeaGreen4},
  87. })
  88. feed(':lua error("fail\\nmuch error\\nsuch details")<cr>')
  89. screen:expect{grid=[[
  90. |
  91. {1:~ }|
  92. {1:~ }|
  93. {1:~ }|
  94. {2: }|
  95. {3:E5108: Error executing lua [string ":lua}|
  96. {3:"]:1: fail} |
  97. {3:much error} |
  98. {3:such details} |
  99. {4:Press ENTER or type command to continue}^ |
  100. ]]}
  101. feed('<cr>')
  102. screen:expect{grid=[[
  103. ^ |
  104. {1:~ }|
  105. {1:~ }|
  106. {1:~ }|
  107. {1:~ }|
  108. {1:~ }|
  109. {1:~ }|
  110. {1:~ }|
  111. {1:~ }|
  112. |
  113. ]]}
  114. eq('E5108: Error executing lua [string ":lua"]:1: fail\nmuch error\nsuch details', eval('v:errmsg'))
  115. local status, err = pcall(command,'lua error("some error\\nin a\\nAPI command")')
  116. local expected = 'Vim(lua):E5108: Error executing lua [string ":lua"]:1: some error\nin a\nAPI command'
  117. eq(false, status)
  118. eq(expected, string.sub(err, -string.len(expected)))
  119. feed(':messages<cr>')
  120. screen:expect{grid=[[
  121. |
  122. {1:~ }|
  123. {1:~ }|
  124. {1:~ }|
  125. {2: }|
  126. {3:E5108: Error executing lua [string ":lua}|
  127. {3:"]:1: fail} |
  128. {3:much error} |
  129. {3:such details} |
  130. {4:Press ENTER or type command to continue}^ |
  131. ]]}
  132. end)
  133. end)
  134. describe(':luado command', function()
  135. it('works', function()
  136. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  137. eq('', redir_exec('luado lines = (lines or {}) lines[#lines + 1] = {linenr, line}'))
  138. eq({'ABC', 'def', 'gHi'}, curbufmeths.get_lines(0, -1, false))
  139. eq({{1, 'ABC'}, {2, 'def'}, {3, 'gHi'}}, funcs.luaeval('lines'))
  140. -- Automatic transformation of numbers
  141. eq('', redir_exec('luado return linenr'))
  142. eq({'1', '2', '3'}, curbufmeths.get_lines(0, -1, false))
  143. eq('', redir_exec('luado return ("<%02x>"):format(line:byte())'))
  144. eq({'<31>', '<32>', '<33>'}, curbufmeths.get_lines(0, -1, false))
  145. end)
  146. it('stops processing lines when suddenly out of lines', function()
  147. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  148. eq('', redir_exec('2,$luado runs = ((runs or 0) + 1) vim.api.nvim_command("%d")'))
  149. eq({''}, curbufmeths.get_lines(0, -1, false))
  150. eq(1, funcs.luaeval('runs'))
  151. end)
  152. it('works correctly when changing lines out of range', function()
  153. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  154. eq('\nE322: line number out of range: 1 past the end\nE320: Cannot find line 2',
  155. redir_exec('2,$luado vim.api.nvim_command("%d") return linenr'))
  156. eq({''}, curbufmeths.get_lines(0, -1, false))
  157. end)
  158. it('fails on errors', function()
  159. eq([[Vim(luado):E5109: Error loading lua: [string ":luado"]:1: unexpected symbol near ')']],
  160. exc_exec('luado ()'))
  161. eq([[Vim(luado):E5111: Error calling lua: [string ":luado"]:1: attempt to perform arithmetic on global 'liness' (a nil value)]],
  162. exc_exec('luado return liness + 1'))
  163. end)
  164. it('works with NULL errors', function()
  165. eq([=[Vim(luado):E5111: Error calling lua: [NULL]]=],
  166. exc_exec('luado error(nil)'))
  167. end)
  168. it('fails in sandbox when needed', function()
  169. curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"})
  170. eq('\nE48: Not allowed in sandbox: sandbox luado runs = (runs or 0) + 1',
  171. redir_exec('sandbox luado runs = (runs or 0) + 1'))
  172. eq(NIL, funcs.luaeval('runs'))
  173. end)
  174. it('works with long strings', function()
  175. local s = ('x'):rep(100500)
  176. eq('\nE5109: Error loading lua: [string ":luado"]:1: unfinished string near \'<eof>\'', redir_exec(('luado return "%s'):format(s)))
  177. eq({''}, curbufmeths.get_lines(0, -1, false))
  178. eq('', redir_exec(('luado return "%s"'):format(s)))
  179. eq({s}, curbufmeths.get_lines(0, -1, false))
  180. end)
  181. end)
  182. describe(':luafile', function()
  183. local fname = 'Xtest-functional-lua-commands-luafile'
  184. after_each(function()
  185. os.remove(fname)
  186. end)
  187. it('works', function()
  188. write_file(fname, [[
  189. vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
  190. vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
  191. vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
  192. ]])
  193. eq('', redir_exec('luafile ' .. fname))
  194. eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false))
  195. end)
  196. it('correctly errors out', function()
  197. write_file(fname, '()')
  198. eq(("Vim(luafile):E5112: Error while creating lua chunk: %s:1: unexpected symbol near ')'"):format(fname),
  199. exc_exec('luafile ' .. fname))
  200. write_file(fname, 'vimm.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})')
  201. eq(("Vim(luafile):E5113: Error while calling lua chunk: %s:1: attempt to index global 'vimm' (a nil value)"):format(fname),
  202. exc_exec('luafile ' .. fname))
  203. end)
  204. it('works with NULL errors', function()
  205. write_file(fname, 'error(nil)')
  206. eq([=[Vim(luafile):E5113: Error while calling lua chunk: [NULL]]=],
  207. exc_exec('luafile ' .. fname))
  208. end)
  209. end)