robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
11.1 KiB360 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4local describe, it, after_each, pending, finally =
5  t.describe, t.it, t.after_each, t.pending, t.finally
6local uv = vim.uv
7
8local api = n.api
9local feed = n.feed
10local eq = t.eq
11local neq = t.neq
12local clear = n.clear
13local ok = t.ok
14local fn = n.fn
15local nvim_prog = n.nvim_prog
16local retry = t.retry
17local write_file = t.write_file
18local assert_log = t.assert_log
19local check_close = n.check_close
20local is_os = t.is_os
21
22local testlog = 'Xtest-embed-log'
23
24local function test_embed(ext_linegrid)
25  local screen
26  local function startup(...)
27    clear { args_rm = { '--headless' }, args = { ... } }
28
29    -- attach immediately after startup, for early UI
30    screen = Screen.new(60, 8, { ext_linegrid = ext_linegrid })
31    screen:add_extra_attr_ids {
32      [100] = { foreground = Screen.colors.NvimDarkCyan },
33      [101] = { foreground = Screen.colors.NvimDarkRed },
34      [102] = {
35        background = Screen.colors.NvimLightGrey4,
36        foreground = Screen.colors.NvimDarkGrey2,
37      },
38    }
39  end
40
41  it('can display errors', function()
42    startup('--cmd', 'echoerr invalid+')
43    screen:expect([[
44                                                                  |*4
45      {3:                                                            }|
46      {9:Error in pre-vimrc command line:}                            |
47      {9:E121: Undefined variable: invalid}                           |
48      {6:Press ENTER or type command to continue}^                     |
49    ]])
50
51    feed('<cr>')
52    screen:expect([[
53      ^                                                            |
54      {1:~                                                           }|*6
55                                                                  |
56    ]])
57  end)
58
59  it("doesn't erase output when setting color scheme", function()
60    if t.is_os('openbsd') then
61      pending('FIXME #10804')
62    end
63    startup('--cmd', 'echoerr "foo"', '--cmd', 'color default', '--cmd', 'echoerr "bar"')
64    screen:expect([[
65                                                                  |*3
66      {102:                                                            }|
67      {9:Error in pre-vimrc command line:}                            |
68      {9:foo}                                                         |
69      {101:bar}                                                         |
70      {100:Press ENTER or type command to continue}^                     |
71    ]])
72  end)
73
74  it("doesn't erase output when setting Normal colors", function()
75    startup('--cmd', 'echoerr "foo"', '--cmd', 'hi Normal guibg=Green', '--cmd', 'echoerr "bar"')
76    screen:expect {
77      grid = [[
78                                                                  |*3
79      {3:                                                            }|
80      {9:Error in pre-vimrc command line:}                            |
81      {9:foo}                                                         |
82      {9:bar}                                                         |
83      {6:Press ENTER or type command to continue}^                     |
84    ]],
85      condition = function()
86        eq(Screen.colors.Green, screen.default_colors.rgb_bg)
87      end,
88    }
89  end)
90end
91
92describe('--embed UI on startup (ext_linegrid=true)', function()
93  test_embed(true)
94end)
95describe('--embed UI on startup (ext_linegrid=false)', function()
96  test_embed(false)
97end)
98
99describe('--embed UI', function()
100  after_each(function()
101    check_close()
102    os.remove(testlog)
103  end)
104
105  it('can pass stdin', function()
106    local pipe = assert(uv.pipe())
107
108    local writer = assert(uv.new_pipe(false))
109    writer:open(pipe.write)
110
111    clear { args_rm = { '--headless' }, io_extra = pipe.read, env = { NVIM_LOG_FILE = testlog } }
112
113    -- attach immediately after startup, for early UI
114    -- rpc_async: Avoid hanging. #24888
115    local screen = Screen.new(40, 8, { stdin_fd = 3 }, false)
116    screen.rpc_async = true -- Avoid hanging. #24888
117    screen:attach()
118
119    writer:write 'hello nvim\nfrom external input\n'
120    writer:shutdown(function()
121      writer:close()
122    end)
123
124    screen:expect [[
125      ^hello nvim                              |
126      from external input                     |
127      {1:~                                       }|*5
128                                              |
129    ]]
130
131    -- stdin (rpc input) still works
132    feed 'o'
133    screen:expect [[
134      hello nvim                              |
135      ^                                        |
136      from external input                     |
137      {1:~                                       }|*4
138      {5:-- INSERT --}                            |
139    ]]
140
141    if not is_os('win') then
142      assert_log('Failed to get flags on descriptor 3: Bad file descriptor', testlog, 100)
143    end
144  end)
145
146  it('can pass stdin to -q - #17523', function()
147    write_file(
148      'Xbadfile.c',
149      [[
150      /* some file with an error */
151      main() {
152        functionCall(arg; arg, arg);
153        return 666
154      }
155      ]]
156    )
157    finally(function()
158      os.remove('Xbadfile.c')
159    end)
160
161    local pipe = assert(uv.pipe())
162
163    local writer = assert(uv.new_pipe(false))
164    writer:open(pipe.write)
165
166    clear { args_rm = { '--headless' }, args = { '-q', '-' }, io_extra = pipe.read }
167
168    -- attach immediately after startup, for early UI
169    local screen = Screen.new(60, 8, { stdin_fd = 3 }, false)
170    screen.rpc_async = true -- Avoid hanging. #24888
171    screen:attach()
172
173    writer:write [[Xbadfile.c:4:12: error: expected ';' before '}' token]]
174    writer:shutdown(function()
175      writer:close()
176    end)
177
178    screen:expect [[
179      /* some file with an error */                               |
180      main() {                                                    |
181        functionCall(arg; arg, arg);                              |
182        return 66^6                                                |
183      }                                                           |
184      {1:~                                                           }|*2
185      (1 of 1): error: expected ';' before '}' token              |
186    ]]
187
188    -- stdin (rpc input) still works
189    feed 'A'
190    screen:expect [[
191      /* some file with an error */                               |
192      main() {                                                    |
193        functionCall(arg; arg, arg);                              |
194        return 666^                                                |
195      }                                                           |
196      {1:~                                                           }|*2
197      {5:-- INSERT --}                                                |
198    ]]
199
200    eq('-', api.nvim_get_option_value('errorfile', {}))
201  end)
202
203  it('only sets background colors once even if overridden', function()
204    local screen, current, seen
205    local function handle_default_colors_set(_, _, rgb_bg, _, _, _)
206      seen[rgb_bg] = true
207      current = rgb_bg
208    end
209    local function startup(...)
210      seen = {}
211      current = nil
212      clear { args_rm = { '--headless' }, args = { ... } }
213
214      -- attach immediately after startup, for early UI
215      screen = Screen.new(40, 8)
216      screen._handle_default_colors_set = handle_default_colors_set
217    end
218
219    startup()
220    screen:expect {
221      condition = function()
222        eq(16777215, current)
223      end,
224    }
225    eq({ [16777215] = true }, seen)
226
227    -- NB: by accident how functional/testutil.lua currently handles the default color scheme, the
228    -- above is sufficient to test the behavior. But in case that workaround is removed, we need
229    -- a test with an explicit override like below, so do it to remain safe.
230    startup('--cmd', 'hi NORMAL guibg=#FF00FF')
231    screen:expect {
232      condition = function()
233        eq(16711935, current)
234      end,
235    }
236    eq({ [16711935] = true }, seen) -- we only saw the last one, despite 16777215 was set internally earlier
237  end)
238
239  it('updates cwd of attached UI #21771', function()
240    clear { args_rm = { '--headless' } }
241    api.nvim_set_current_dir(t.paths.test_source_path)
242
243    local screen = Screen.new(40, 8)
244
245    screen:expect {
246      condition = function()
247        eq(t.paths.test_source_path, screen.pwd)
248      end,
249    }
250
251    -- Change global cwd
252    n.command(string.format('cd %s/src/nvim', t.paths.test_source_path))
253
254    screen:expect {
255      condition = function()
256        eq(string.format('%s/src/nvim', t.paths.test_source_path), screen.pwd)
257      end,
258    }
259
260    -- Split the window and change the cwd in the split
261    n.command('new')
262    n.command(string.format('lcd %s/test', t.paths.test_source_path))
263
264    screen:expect {
265      condition = function()
266        eq(string.format('%s/test', t.paths.test_source_path), screen.pwd)
267      end,
268    }
269
270    -- Move to the original window
271    n.command('wincmd p')
272
273    screen:expect {
274      condition = function()
275        eq(string.format('%s/src/nvim', t.paths.test_source_path), screen.pwd)
276      end,
277    }
278
279    -- Change global cwd again
280    n.command(string.format('cd %s', t.paths.test_source_path))
281
282    screen:expect {
283      condition = function()
284        eq(t.paths.test_source_path, screen.pwd)
285      end,
286    }
287  end)
288
289  it('closing stdio with another remote UI does not leak memory #36392', function()
290    clear({ args_rm = { '--headless' } })
291    Screen.new()
292    eq(1, #api.nvim_list_uis())
293    local server = api.nvim_get_vvar('servername')
294    local other_session = n.connect(server)
295    Screen.new(nil, nil, nil, other_session)
296    eq(2, #api.nvim_list_uis())
297    check_close()
298    other_session:close()
299  end)
300end)
301
302describe('--embed --listen UI', function()
303  it('waits for connection on listening address', function()
304    clear()
305    local child_server = assert(n.new_pipename())
306    fn.jobstart({
307      nvim_prog,
308      '--embed',
309      '--listen',
310      child_server,
311      '--clean',
312      '--cmd',
313      'colorscheme vim',
314    })
315    retry(nil, nil, function()
316      neq(nil, uv.fs_stat(child_server))
317    end)
318
319    local child_session = n.connect(child_server)
320
321    local info_ok, api_info = child_session:request('nvim_get_api_info')
322    ok(info_ok)
323    eq(2, #api_info)
324    ok(api_info[1] > 2, 'channel_id > 2', api_info[1])
325
326    child_session:request(
327      'nvim_exec2',
328      [[
329      let g:evs = []
330      autocmd UIEnter * call add(g:evs, $"UIEnter:{v:event.chan}")
331      autocmd VimEnter * call add(g:evs, "VimEnter")
332    ]],
333      {}
334    )
335
336    -- VimEnter and UIEnter shouldn't be triggered until after attach
337    local var_ok, var = child_session:request('nvim_get_var', 'evs')
338    ok(var_ok)
339    eq({}, var)
340
341    local child_screen = Screen.new(40, 6, nil, child_session)
342    child_screen:expect {
343      grid = [[
344      ^                                        |
345      {1:~                                       }|*3
346      {2:[No Name]             0,0-1          All}|
347                                              |
348    ]],
349      attr_ids = {
350        [1] = { foreground = Screen.colors.Blue, bold = true },
351        [2] = { reverse = true, bold = true },
352      },
353    }
354
355    -- VimEnter and UIEnter should now be triggered
356    var_ok, var = child_session:request('nvim_get_var', 'evs')
357    ok(var_ok)
358    eq({ 'VimEnter', ('UIEnter:%d'):format(api_info[1]) }, var)
359  end)
360end)