main_spec.lua (7449B)
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 6 local eq = t.eq 7 local matches = t.matches 8 local feed = n.feed 9 local eval = n.eval 10 local clear = n.clear 11 local fn = n.fn 12 local write_file = t.write_file 13 local is_os = t.is_os 14 local skip = t.skip 15 16 describe('command-line option', function() 17 describe('-s', function() 18 local fname = 'Xtest-functional-core-main-s' 19 local fname_2 = fname .. '.2' 20 local nonexistent_fname = fname .. '.nonexistent' 21 local dollar_fname = '$' .. fname 22 23 before_each(function() 24 clear() 25 os.remove(fname) 26 os.remove(dollar_fname) 27 end) 28 29 after_each(function() 30 os.remove(fname) 31 os.remove(dollar_fname) 32 end) 33 34 it('treats - as stdin', function() 35 eq(nil, uv.fs_stat(fname)) 36 fn.system({ 37 n.nvim_prog, 38 '-u', 39 'NONE', 40 '-i', 41 'NONE', 42 '--headless', 43 '--cmd', 44 'set noswapfile shortmess+=IFW fileformats=unix', 45 '-s', 46 '-', 47 fname, 48 }, { ':call setline(1, "42")', ':wqall!', '' }) 49 eq(0, eval('v:shell_error')) 50 local attrs = uv.fs_stat(fname) 51 eq(#'42\n', attrs.size) 52 end) 53 54 it('does not expand $VAR', function() 55 eq(nil, uv.fs_stat(fname)) 56 eq(true, not not dollar_fname:find('%$%w+')) 57 write_file(dollar_fname, ':call setline(1, "100500")\n:wqall!\n') 58 local p = n.spawn_wait( 59 '--cmd', 60 'set noswapfile shortmess+=IFW fileformats=unix', 61 '-s', 62 dollar_fname, 63 fname 64 ) 65 eq(0, p.status) 66 local attrs = uv.fs_stat(fname) 67 eq(#'100500\n', attrs.size) 68 end) 69 70 it('does not crash when running completion in Ex mode', function() 71 local p = 72 n.spawn_wait('--clean', '-e', '-s', '--cmd', 'exe "norm! i\\<C-X>\\<C-V>"', '--cmd', 'qa!') 73 eq(0, p.status) 74 end) 75 76 it('does not crash when running completion from -l script', function() 77 local lua_fname = 'Xinscompl.lua' 78 write_file(lua_fname, [=[vim.cmd([[exe "norm! i\<C-X>\<C-V>"]])]=]) 79 finally(function() 80 os.remove(lua_fname) 81 end) 82 local p = n.spawn_wait('--clean', '-l', lua_fname) 83 eq(0, p.status) 84 end) 85 86 it('does not crash after reading from stdin in non-headless mode', function() 87 skip(is_os('win')) 88 local screen = Screen.new(40, 8) 89 local args = { 90 n.nvim_prog, 91 '-u', 92 'NONE', 93 '-i', 94 'NONE', 95 '--cmd', 96 '"set noswapfile shortmess+=IFW fileformats=unix notermguicolors"', 97 '-s', 98 '-', 99 } 100 101 -- Need to explicitly pipe to stdin so that the embedded Nvim instance doesn't try to read 102 -- data from the terminal #18181 103 fn.jobstart(string.format([[echo "" | %s]], table.concat(args, ' ')), { 104 term = true, 105 env = { VIMRUNTIME = os.getenv('VIMRUNTIME') }, 106 }) 107 screen:expect( 108 [[ 109 ^ | 110 ~ |*4 111 {1:[No Name] 0,0-1 All}| 112 |*2 113 ]], 114 { 115 [1] = { reverse = true }, 116 } 117 ) 118 feed('i:cq<CR>') 119 screen:expect([[ 120 | 121 [Process exited 1]^ | 122 |*5 123 {5:-- TERMINAL --} | 124 ]]) 125 --[=[ Example of incorrect output: 126 screen:expect([[ 127 ^nvim: /var/tmp/portage/dev-libs/libuv-1.| 128 10.2/work/libuv-1.10.2/src/unix/core.c:5| 129 19: uv__close: Assertion `fd > STDERR_FI| 130 LENO' failed. | 131 | 132 [Process exited 6] | 133 |*2 134 ]]) 135 ]=] 136 end) 137 138 it('fails when trying to use nonexistent file with -s', function() 139 local p = n.spawn_wait( 140 '--cmd', 141 'set noswapfile shortmess+=IFW fileformats=unix', 142 '--cmd', 143 'language C', 144 '-s', 145 nonexistent_fname 146 ) 147 eq( 148 'Cannot open for reading: "' .. nonexistent_fname .. '": no such file or directory\n', 149 --- TODO(justinmk): using `p.output` because Nvim emits CRLF even on non-Win. Emit LF instead? 150 p:output() 151 ) 152 eq(2, p.status) 153 end) 154 155 it('errors out when trying to use -s twice', function() 156 write_file(fname, ':call setline(1, "1")\n:wqall!\n') 157 write_file(dollar_fname, ':call setline(1, "2")\n:wqall!\n') 158 local p = n.spawn_wait( 159 '--cmd', 160 'set noswapfile shortmess+=IFW fileformats=unix', 161 '--cmd', 162 'language C', 163 '-s', 164 fname, 165 '-s', 166 dollar_fname, 167 fname_2 168 ) 169 --- TODO(justinmk): using `p.output` because Nvim emits CRLF even on non-Win. Emit LF instead? 170 eq('Attempt to open script file again: "-s ' .. dollar_fname .. '"\n', p:output()) 171 eq(2, p.status) 172 eq(nil, uv.fs_stat(fname_2)) 173 end) 174 end) 175 176 it('nvim -v, :version', function() 177 matches('Run ":verbose version"', fn.execute(':version')) 178 matches('fall%-back for %$VIM: .*Run :checkhealth', fn.execute(':verbose version')) 179 matches('Run "nvim %-V1 %-v"', n.spawn_wait('-v').stdout) 180 matches('fall%-back for %$VIM: .*Run :checkhealth', n.spawn_wait('-V1', '-v').stdout) 181 end) 182 183 if is_os('win') then 184 for _, prefix in ipairs({ '~/', '~\\' }) do 185 it('expands ' .. prefix .. ' on Windows', function() 186 local fname = os.getenv('USERPROFILE') .. '\\nvim_test.txt' 187 finally(function() 188 os.remove(fname) 189 end) 190 write_file(fname, 'some text') 191 eq( 192 'some text', 193 fn.system({ 194 n.nvim_prog, 195 '-es', 196 '+%print', 197 '+q', 198 prefix .. 'nvim_test.txt', 199 }):gsub('\n', '') 200 ) 201 end) 202 end 203 end 204 end) 205 206 describe('vim._core', function() 207 it('works with "-u NONE" and no VIMRUNTIME', function() 208 clear { 209 args_rm = { '-u' }, 210 args = { '-u', 'NONE' }, 211 env = { VIMRUNTIME = 'non-existent' }, 212 } 213 214 -- `vim.hl` is NOT a builtin module. 215 t.matches("^module 'vim%.hl' not found:", t.pcall_err(n.exec_lua, [[require('vim.hl')]])) 216 217 -- All `vim._core.*` modules are builtin. 218 t.eq({ 'serverlist' }, n.exec_lua([[return vim.tbl_keys(require('vim._core.server'))]])) 219 local expected = { 220 'vim.F', 221 'vim._core.defaults', 222 'vim._core.editor', 223 'vim._core.ex_cmd', 224 'vim._core.exrc', 225 'vim._core.help', 226 'vim._core.options', 227 'vim._core.server', 228 'vim._core.shared', 229 'vim._core.stringbuffer', 230 'vim._core.system', 231 'vim._core.ui2', 232 'vim._core.util', 233 'vim._init_packages', 234 'vim.filetype', 235 'vim.fs', 236 'vim.inspect', 237 'vim.keymap', 238 'vim.loader', 239 'vim.text', 240 } 241 if n.exec_lua [[return not not _G.jit]] then 242 expected = vim.list_extend({ 243 'ffi', 244 'jit.profile', 245 'jit.util', 246 'string.buffer', 247 'table.clear', 248 'table.new', 249 }, expected) 250 end 251 t.eq(expected, n.exec_lua([[local t = vim.tbl_keys(package.preload); table.sort(t); return t]])) 252 end) 253 end)