robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
4.0 KiB110 linesraw
1local n = require('test.functional.testnvim')()
2local t = require('test.testutil')
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local api = n.api
6local assert_alive = n.assert_alive
7local clear = n.clear
8local exec_lua = n.exec_lua
9
10local OSC_PREFIX = string.char(0x1b, 0x5d)
11local BEL = string.char(0x07)
12local ST = string.char(0x1b, 0x5c)
13local NUL = string.char(0x00)
14
15describe(':terminal', function()
16  before_each(clear)
17
18  it('handles invalid OSC terminators #30084', function()
19    local chan = api.nvim_open_term(0, {})
20    api.nvim_chan_send(chan, '\027]8;;https://example.com\027\\Example\027]8;;\027\n')
21    assert_alive()
22  end)
23
24  it('handles OSC-2 title setting', function()
25    -- OSC-2 should set title.
26    local chan = api.nvim_open_term(0, {})
27    local input = OSC_PREFIX .. '2;This title set with OSC 2' .. BEL
28    api.nvim_chan_send(chan, input)
29    --- @type string
30    local term_title = api.nvim_buf_get_var(0, 'term_title')
31    t.eq('This title set with OSC 2', term_title)
32    assert_alive()
33  end)
34
35  it('handles OSC-0 title and icon setting', function()
36    -- OSC-0 should set title and icon name to the same string. We currently ignore the icon name,
37    -- but the title should still be reflected.
38    local chan = api.nvim_open_term(0, {})
39    local input = OSC_PREFIX .. '0;This title set with OSC 0' .. BEL
40    api.nvim_chan_send(chan, input)
41    --- @type string
42    local term_title = api.nvim_buf_get_var(0, 'term_title')
43    t.eq('This title set with OSC 0', term_title)
44    assert_alive()
45  end)
46
47  it('handles control character following OSC prefix #34028', function()
48    local chan = api.nvim_open_term(0, {})
49    -- In order to test for the crash found in #34028 we need a ctrl char following the OSC_PREFIX
50    -- which causes `string_fragment()` to be called while in OSC_COMMAND mode, this caused
51    -- initial_string to be flipped back to false. At the end we need two more non-BEL control
52    -- characters, one to write into the 1 byte buffer, then another to trigger the callback one
53    -- more time so that realloc notices that it's internal data has been overwritten.
54    local input = OSC_PREFIX .. NUL .. '0;aaaaaaaaaaaaaaaaaaaaaaaaaaa' .. NUL .. NUL
55    api.nvim_chan_send(chan, input)
56    assert_alive()
57
58    -- On some platforms such as MacOS we need a longer string to reproduce the crash from #34028.
59    input = OSC_PREFIX .. NUL .. '0;'
60    for _ = 1, 256 do
61      input = input .. 'a'
62    end
63    input = input .. NUL .. NUL
64    api.nvim_chan_send(chan, input)
65    assert_alive()
66  end)
67
68  it('uses terminator matching query for OSC TermRequest #37018', function()
69    local chan = api.nvim_open_term(0, {})
70    exec_lua([[
71      vim.api.nvim_create_autocmd("TermRequest", {
72        callback = function(ev)
73          _G.osc10_response = {sequence = ev.data.sequence, terminator = ev.data.terminator }
74        end
75      })
76    ]])
77
78    local function send_osc_with_terminator(terminator)
79      local input = OSC_PREFIX .. '10;?' .. terminator
80      api.nvim_chan_send(chan, input)
81    end
82
83    send_osc_with_terminator(BEL)
84    --- @type string
85    t.eq(
86      { sequence = OSC_PREFIX .. '10;?', terminator = BEL },
87      exec_lua([[return _G.osc10_response]])
88    )
89
90    send_osc_with_terminator(ST)
91    --- @type string
92    t.eq(
93      { sequence = OSC_PREFIX .. '10;?', terminator = ST },
94      exec_lua([[return _G.osc10_response]])
95    )
96  end)
97
98  it('does not leak pending TermRequest on buffer destroy #39332', function()
99    -- Send all OSC sequences in one exec_lua so that the event loop does not drain between the sends.
100    exec_lua(function(prefix, st)
101      local buf = vim.api.nvim_create_buf(false, true)
102      local chan = vim.api.nvim_open_term(buf, {})
103      vim.api.nvim_create_autocmd('TermRequest', { buffer = buf, callback = function() end })
104      vim.api.nvim_chan_send(chan, prefix .. '7;file:///a' .. st)
105      vim.api.nvim_chan_send(chan, prefix .. '7;file:///b' .. st)
106      vim.api.nvim_buf_delete(buf, { force = true })
107    end, OSC_PREFIX, ST)
108    assert_alive()
109  end)
110end)