neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

parser_spec.lua (3376B)


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