main_spec.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local uv = vim.uv
  5. local eq = t.eq
  6. local matches = t.matches
  7. local feed = n.feed
  8. local eval = n.eval
  9. local clear = n.clear
  10. local fn = n.fn
  11. local write_file = t.write_file
  12. local is_os = t.is_os
  13. local skip = t.skip
  14. describe('command-line option', function()
  15. describe('-s', function()
  16. local fname = 'Xtest-functional-core-main-s'
  17. local fname_2 = fname .. '.2'
  18. local nonexistent_fname = fname .. '.nonexistent'
  19. local dollar_fname = '$' .. fname
  20. before_each(function()
  21. clear()
  22. os.remove(fname)
  23. os.remove(dollar_fname)
  24. end)
  25. after_each(function()
  26. os.remove(fname)
  27. os.remove(dollar_fname)
  28. end)
  29. it('treats - as stdin', function()
  30. eq(nil, uv.fs_stat(fname))
  31. fn.system({
  32. n.nvim_prog,
  33. '-u',
  34. 'NONE',
  35. '-i',
  36. 'NONE',
  37. '--headless',
  38. '--cmd',
  39. 'set noswapfile shortmess+=IFW fileformats=unix',
  40. '-s',
  41. '-',
  42. fname,
  43. }, { ':call setline(1, "42")', ':wqall!', '' })
  44. eq(0, eval('v:shell_error'))
  45. local attrs = uv.fs_stat(fname)
  46. eq(#'42\n', attrs.size)
  47. end)
  48. it('does not expand $VAR', function()
  49. eq(nil, uv.fs_stat(fname))
  50. eq(true, not not dollar_fname:find('%$%w+'))
  51. write_file(dollar_fname, ':call setline(1, "100500")\n:wqall!\n')
  52. local p = n.spawn_wait(
  53. '--cmd',
  54. 'set noswapfile shortmess+=IFW fileformats=unix',
  55. '-s',
  56. dollar_fname,
  57. fname
  58. )
  59. eq(0, p.status)
  60. local attrs = uv.fs_stat(fname)
  61. eq(#'100500\n', attrs.size)
  62. end)
  63. it('does not crash when running completion in Ex mode', function()
  64. local p =
  65. n.spawn_wait('--clean', '-e', '-s', '--cmd', 'exe "norm! i\\<C-X>\\<C-V>"', '--cmd', 'qa!')
  66. eq(0, p.status)
  67. end)
  68. it('does not crash when running completion from -l script', function()
  69. local lua_fname = 'Xinscompl.lua'
  70. write_file(lua_fname, [=[vim.cmd([[exe "norm! i\<C-X>\<C-V>"]])]=])
  71. finally(function()
  72. os.remove(lua_fname)
  73. end)
  74. local p = n.spawn_wait('--clean', '-l', lua_fname)
  75. eq(0, p.status)
  76. end)
  77. it('does not crash after reading from stdin in non-headless mode', function()
  78. skip(is_os('win'))
  79. local screen = Screen.new(40, 8)
  80. local args = {
  81. n.nvim_prog,
  82. '-u',
  83. 'NONE',
  84. '-i',
  85. 'NONE',
  86. '--cmd',
  87. '"set noswapfile shortmess+=IFW fileformats=unix notermguicolors"',
  88. '-s',
  89. '-',
  90. }
  91. -- Need to explicitly pipe to stdin so that the embedded Nvim instance doesn't try to read
  92. -- data from the terminal #18181
  93. fn.jobstart(string.format([[echo "" | %s]], table.concat(args, ' ')), {
  94. term = true,
  95. env = { VIMRUNTIME = os.getenv('VIMRUNTIME') },
  96. })
  97. screen:expect(
  98. [[
  99. ^ |
  100. ~ |*4
  101. {1:[No Name] 0,0-1 All}|
  102. |*2
  103. ]],
  104. {
  105. [1] = { reverse = true },
  106. }
  107. )
  108. feed('i:cq<CR>')
  109. screen:expect([[
  110. |
  111. [Process exited 1]^ |
  112. |*5
  113. {5:-- TERMINAL --} |
  114. ]])
  115. --[=[ Example of incorrect output:
  116. screen:expect([[
  117. ^nvim: /var/tmp/portage/dev-libs/libuv-1.|
  118. 10.2/work/libuv-1.10.2/src/unix/core.c:5|
  119. 19: uv__close: Assertion `fd > STDERR_FI|
  120. LENO' failed. |
  121. |
  122. [Process exited 6] |
  123. |*2
  124. ]])
  125. ]=]
  126. end)
  127. it('fails when trying to use nonexistent file with -s', function()
  128. local p = n.spawn_wait(
  129. '--cmd',
  130. 'set noswapfile shortmess+=IFW fileformats=unix',
  131. '--cmd',
  132. 'language C',
  133. '-s',
  134. nonexistent_fname
  135. )
  136. eq(
  137. 'Cannot open for reading: "' .. nonexistent_fname .. '": no such file or directory\n',
  138. --- TODO(justinmk): using `p.output` because Nvim emits CRLF even on non-Win. Emit LF instead?
  139. p:output()
  140. )
  141. eq(2, p.status)
  142. end)
  143. it('errors out when trying to use -s twice', function()
  144. write_file(fname, ':call setline(1, "1")\n:wqall!\n')
  145. write_file(dollar_fname, ':call setline(1, "2")\n:wqall!\n')
  146. local p = n.spawn_wait(
  147. '--cmd',
  148. 'set noswapfile shortmess+=IFW fileformats=unix',
  149. '--cmd',
  150. 'language C',
  151. '-s',
  152. fname,
  153. '-s',
  154. dollar_fname,
  155. fname_2
  156. )
  157. --- TODO(justinmk): using `p.output` because Nvim emits CRLF even on non-Win. Emit LF instead?
  158. eq('Attempt to open script file again: "-s ' .. dollar_fname .. '"\n', p:output())
  159. eq(2, p.status)
  160. eq(nil, uv.fs_stat(fname_2))
  161. end)
  162. end)
  163. it('nvim -v, :version', function()
  164. matches('Run ":verbose version"', fn.execute(':version'))
  165. matches('fall%-back for %$VIM: .*Run :checkhealth', fn.execute(':verbose version'))
  166. matches('Run "nvim %-V1 %-v"', n.spawn_wait('-v').stdout)
  167. matches('fall%-back for %$VIM: .*Run :checkhealth', n.spawn_wait('-V1', '-v').stdout)
  168. end)
  169. if is_os('win') then
  170. for _, prefix in ipairs({ '~/', '~\\' }) do
  171. it('expands ' .. prefix .. ' on Windows', function()
  172. local fname = os.getenv('USERPROFILE') .. '\\nvim_test.txt'
  173. finally(function()
  174. os.remove(fname)
  175. end)
  176. write_file(fname, 'some text')
  177. eq(
  178. 'some text',
  179. fn.system({
  180. n.nvim_prog,
  181. '-es',
  182. '+%print',
  183. '+q',
  184. prefix .. 'nvim_test.txt',
  185. }):gsub('\n', '')
  186. )
  187. end)
  188. end
  189. end
  190. end)