robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
7.6 KiB249 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4local describe, it, before_each, after_each, finally =
5  t.describe, t.it, t.before_each, t.after_each, t.finally
6local uv = vim.uv
7
8local eq = t.eq
9local matches = t.matches
10local eval = n.eval
11local clear = n.clear
12local fn = n.fn
13local write_file = t.write_file
14local is_os = t.is_os
15
16describe('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      -- Repro from fdfa1ed (2017): the empty `echo ""` pipe makes scriptin (`-s -`) hit EOF
88      -- immediately and call closescript(); if scriptin held fd 0 directly, libuv asserts.
89      --
90      -- Note: can't use nvim_chan_send because pty stdin never EOFs.
91      local nvim_set = 'set noswapfile shortmess+=IFW fileformats=unix notermguicolors'
92      local shell_cmd = ([[echo "" | %s "%s" --clean --cmd "%s" -s -]]):format(
93        is_os('win') and '&' or '',
94        n.nvim_prog,
95        nvim_set
96      )
97      if is_os('win') then
98        -- Use PowerShell; cmd.exe mis-parses libuv's `\"…\"` (MSVCRT quoting).
99        n.set_shell_powershell()
100      end
101      local screen = Screen.new(40, 8)
102      fn.jobstart(shell_cmd, {
103        term = true,
104        env = { VIMRUNTIME = os.getenv('VIMRUNTIME') },
105      })
106      -- First screen confirms Nvim reached TUI (didn't crash closing scriptin).
107      screen:expect(
108        [[
109        ^                                        |
110        ~                                       |*4
111        {1:[No Name]             0,0-1          All}|
112                                                |*2
113      ]],
114        { [1] = { reverse = true } }
115      )
116
117      -- Exit with code 42 to avoid false positives.
118      n.feed('i:cq 42<CR>')
119
120      if is_os('win') then
121        -- XXX: `:cq 42` isn't reaching Nvim, or Nvim exits 1 anyway?
122        screen:expect({ any = '%[Process exited 1%]' })
123      else
124        screen:expect({ any = '%[Process exited 42%]' })
125      end
126      --[=[ Example of incorrect output:
127      screen:expect([[
128        ^nvim: /var/tmp/portage/dev-libs/libuv-1.|
129        10.2/work/libuv-1.10.2/src/unix/core.c:5|
130        19: uv__close: Assertion `fd > STDERR_FI|
131        LENO' failed.                           |
132                                                |
133        [Process exited 6]                      |
134                                                |*2
135      ]])
136      ]=]
137    end)
138
139    it('fails when trying to use nonexistent file with -s', function()
140      local p = n.spawn_wait(
141        '--cmd',
142        'set noswapfile shortmess+=IFW fileformats=unix',
143        '--cmd',
144        'language C',
145        '-s',
146        nonexistent_fname
147      )
148      eq(
149        'Cannot open for reading: "' .. nonexistent_fname .. '": no such file or directory\n',
150        --- TODO(justinmk): using `p.output` because Nvim emits CRLF even on non-Win. Emit LF instead?
151        p:output()
152      )
153      eq(2, p.status)
154    end)
155
156    it('errors out when trying to use -s twice', function()
157      write_file(fname, ':call setline(1, "1")\n:wqall!\n')
158      write_file(dollar_fname, ':call setline(1, "2")\n:wqall!\n')
159      local p = n.spawn_wait(
160        '--cmd',
161        'set noswapfile shortmess+=IFW fileformats=unix',
162        '--cmd',
163        'language C',
164        '-s',
165        fname,
166        '-s',
167        dollar_fname,
168        fname_2
169      )
170      --- TODO(justinmk): using `p.output` because Nvim emits CRLF even on non-Win. Emit LF instead?
171      eq('Attempt to open script file again: "-s ' .. dollar_fname .. '"\n', p:output())
172      eq(2, p.status)
173      eq(nil, uv.fs_stat(fname_2))
174    end)
175  end)
176
177  it('nvim -v, :version', function()
178    matches('Run ":verbose version"', fn.execute(':version'))
179    matches('fall%-back for %$VIM: .*Run :checkhealth', fn.execute(':verbose version'))
180    matches('Run "nvim %-V1 %-v"', n.spawn_wait('-v').stdout)
181    matches('fall%-back for %$VIM: .*Run :checkhealth', n.spawn_wait('-V1', '-v').stdout)
182  end)
183end)
184
185describe('vim._core', function()
186  it('works with "-u NONE" and no VIMRUNTIME', function()
187    clear {
188      args_rm = { '-u' },
189      args = { '-u', 'NONE' },
190      env = { VIMRUNTIME = 'non-existent' },
191    }
192
193    -- `vim.hl` is NOT a builtin module.
194    t.matches("^module 'vim%.hl' not found:", t.pcall_err(n.exec_lua, [[require('vim.hl')]]))
195
196    -- All `vim._core.*` modules are builtin.
197    t.eq(
198      { 'ex_session_restart', 'rebind_after_restart', 'serverlist' },
199      n.exec_lua([[local k = vim.tbl_keys(require('vim._core.server')); table.sort(k); return k]])
200    )
201    local expected = {
202      'vim.F',
203      'vim._core.cmdwin',
204      'vim._core.defaults',
205      'vim._core.editor',
206      'vim._core.ex_cmd',
207      'vim._core.exmode',
208      'vim._core.exrc',
209      'vim._core.help',
210      'vim._core.log',
211      'vim._core.marks',
212      'vim._core.mcursor',
213      'vim._core.options',
214      'vim._core.proc',
215      'vim._core.server',
216      'vim._core.shared',
217      'vim._core.spell',
218      'vim._core.stringbuffer',
219      'vim._core.swapfile',
220      'vim._core.system',
221      'vim._core.table',
222      'vim._core.tag',
223      'vim._core.time',
224      'vim._core.ui',
225      'vim._core.ui2',
226      'vim._core.util',
227      'vim._core.vimfn',
228      'vim._init_packages',
229      'vim.filetype',
230      'vim.fs',
231      'vim.inspect',
232      'vim.keymap',
233      'vim.loader',
234      'vim.text',
235      'vim.tty',
236    }
237    if n.exec_lua [[return not not _G.jit]] then
238      expected = vim.list_extend({
239        'ffi',
240        'jit.profile',
241        'jit.util',
242        'string.buffer',
243        'table.clear',
244        'table.new',
245      }, expected)
246    end
247    t.eq(expected, n.exec_lua([[local t = vim.tbl_keys(package.preload); table.sort(t); return t]]))
248  end)
249end)