main_spec.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. local lfs = require('lfs')
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local Screen = require('test.functional.ui.screen')
  4. local eq = helpers.eq
  5. local feed = helpers.feed
  6. local eval = helpers.eval
  7. local clear = helpers.clear
  8. local funcs = helpers.funcs
  9. local nvim_prog_abs = helpers.nvim_prog_abs
  10. local write_file = helpers.write_file
  11. describe('Command-line option', function()
  12. describe('-s', function()
  13. local fname = 'Xtest-functional-core-main-s'
  14. local fname_2 = fname .. '.2'
  15. local nonexistent_fname = fname .. '.nonexistent'
  16. local dollar_fname = '$' .. fname
  17. before_each(function()
  18. clear()
  19. os.remove(fname)
  20. os.remove(dollar_fname)
  21. end)
  22. after_each(function()
  23. os.remove(fname)
  24. os.remove(dollar_fname)
  25. end)
  26. it('treats - as stdin', function()
  27. eq(nil, lfs.attributes(fname))
  28. funcs.system(
  29. {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
  30. '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
  31. '-s', '-', fname},
  32. {':call setline(1, "42")', ':wqall!', ''})
  33. eq(0, eval('v:shell_error'))
  34. local attrs = lfs.attributes(fname)
  35. eq(#('42\n'), attrs.size)
  36. end)
  37. it('does not expand $VAR', function()
  38. eq(nil, lfs.attributes(fname))
  39. eq(true, not not dollar_fname:find('%$%w+'))
  40. write_file(dollar_fname, ':call setline(1, "100500")\n:wqall!\n')
  41. funcs.system(
  42. {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
  43. '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
  44. '-s', dollar_fname, fname})
  45. eq(0, eval('v:shell_error'))
  46. local attrs = lfs.attributes(fname)
  47. eq(#('100500\n'), attrs.size)
  48. end)
  49. it('does not crash after reading from stdin in non-headless mode', function()
  50. if helpers.pending_win32(pending) then return end
  51. local screen = Screen.new(40, 8)
  52. screen:attach()
  53. local args = {
  54. nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE',
  55. '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
  56. '-s', '-'
  57. }
  58. -- Need to explicitly pipe to stdin so that the embedded Nvim instance doesn't try to read
  59. -- data from the terminal #18181
  60. funcs.termopen(string.format([[echo "" | %s]], table.concat(args, " ")))
  61. screen:expect([[
  62. ^ |
  63. {1:~ }|
  64. {1:~ }|
  65. {1:~ }|
  66. {1:~ }|
  67. {2:[No Name] 0,0-1 All}|
  68. |
  69. |
  70. ]], {
  71. [1] = {foreground = tonumber('0x4040ff'), fg_indexed=true},
  72. [2] = {bold = true, reverse = true}
  73. })
  74. feed('i:cq<CR>')
  75. screen:expect([[
  76. |
  77. [Process exited 1] |
  78. |
  79. |
  80. |
  81. |
  82. |
  83. -- TERMINAL -- |
  84. ]])
  85. --[=[ Example of incorrect output:
  86. screen:expect([[
  87. ^nvim: /var/tmp/portage/dev-libs/libuv-1.|
  88. 10.2/work/libuv-1.10.2/src/unix/core.c:5|
  89. 19: uv__close: Assertion `fd > STDERR_FI|
  90. LENO' failed. |
  91. |
  92. [Process exited 6] |
  93. |
  94. |
  95. ]])
  96. ]=]
  97. end)
  98. it('errors out when trying to use nonexistent file with -s', function()
  99. eq(
  100. 'Cannot open for reading: "'..nonexistent_fname..'": no such file or directory\n',
  101. funcs.system(
  102. {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
  103. '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
  104. '--cmd', 'language C',
  105. '-s', nonexistent_fname}))
  106. eq(2, eval('v:shell_error'))
  107. end)
  108. it('errors out when trying to use -s twice', function()
  109. write_file(fname, ':call setline(1, "1")\n:wqall!\n')
  110. write_file(dollar_fname, ':call setline(1, "2")\n:wqall!\n')
  111. eq(
  112. 'Attempt to open script file again: "-s '..dollar_fname..'"\n',
  113. funcs.system(
  114. {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
  115. '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
  116. '--cmd', 'language C',
  117. '-s', fname, '-s', dollar_fname, fname_2}))
  118. eq(2, eval('v:shell_error'))
  119. eq(nil, lfs.attributes(fname_2))
  120. end)
  121. end)
  122. end)