summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/terminal/parser_spec.lua
blob: 962ca24793d21cb063b8c18907e9be533445f1e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
local n = require('test.functional.testnvim')()

local api = n.api
local assert_alive = n.assert_alive
local clear = n.clear
local exec_lua = n.exec_lua

local OSC_PREFIX = string.char(0x1b, 0x5d)
local BEL = string.char(0x07)
local ST = string.char(0x1b, 0x5c)
local NUL = string.char(0x00)

describe(':terminal', function()
  before_each(clear)

  it('handles invalid OSC terminators #30084', function()
    local chan = api.nvim_open_term(0, {})
    api.nvim_chan_send(chan, '\027]8;;https://example.com\027\\Example\027]8;;\027\n')
    assert_alive()
  end)

  it('handles OSC-2 title setting', function()
    -- OSC-2 should set title.
    local chan = api.nvim_open_term(0, {})
    local input = OSC_PREFIX .. '2;This title set with OSC 2' .. BEL
    api.nvim_chan_send(chan, input)
    --- @type string
    local term_title = api.nvim_buf_get_var(0, 'term_title')
    assert.Equal(term_title, 'This title set with OSC 2')
    assert_alive()
  end)

  it('handles OSC-0 title and icon setting', function()
    -- OSC-0 should set title and icon name to the same string. We currently ignore the icon name,
    -- but the title should still be reflected.
    local chan = api.nvim_open_term(0, {})
    local input = OSC_PREFIX .. '0;This title set with OSC 0' .. BEL
    api.nvim_chan_send(chan, input)
    --- @type string
    local term_title = api.nvim_buf_get_var(0, 'term_title')
    assert.Equal(term_title, 'This title set with OSC 0')
    assert_alive()
  end)

  it('handles control character following OSC prefix #34028', function()
    local chan = api.nvim_open_term(0, {})
    -- In order to test for the crash found in #34028 we need a ctrl char following the OSC_PREFIX
    -- which causes `string_fragment()` to be called while in OSC_COMMAND mode, this caused
    -- initial_string to be flipped back to false. At the end we need two more non-BEL control
    -- characters, one to write into the 1 byte buffer, then another to trigger the callback one
    -- more time so that realloc notices that it's internal data has been overwritten.
    local input = OSC_PREFIX .. NUL .. '0;aaaaaaaaaaaaaaaaaaaaaaaaaaa' .. NUL .. NUL
    api.nvim_chan_send(chan, input)
    assert_alive()

    -- On some platforms such as MacOS we need a longer string to reproduce the crash from #34028.
    input = OSC_PREFIX .. NUL .. '0;'
    for _ = 1, 256 do
      input = input .. 'a'
    end
    input = input .. NUL .. NUL
    api.nvim_chan_send(chan, input)
    assert_alive()
  end)

  it('uses terminator matching query for OSC TermRequest #37018', function()
    local chan = api.nvim_open_term(0, {})
    exec_lua([[
      vim.api.nvim_create_autocmd("TermRequest", {
        callback = function(ev)
          _G.osc10_response = {sequence = ev.data.sequence, terminator = ev.data.terminator }
        end
      })
    ]])

    local function send_osc_with_terminator(terminator)
      local input = OSC_PREFIX .. '10;?' .. terminator
      api.nvim_chan_send(chan, input)
    end

    send_osc_with_terminator(BEL)
    --- @type string
    assert.eq(
      { sequence = OSC_PREFIX .. '10;?', terminator = BEL },
      exec_lua([[return _G.osc10_response]])
    )

    send_osc_with_terminator(ST)
    --- @type string
    assert.eq(
      { sequence = OSC_PREFIX .. '10;?', terminator = ST },
      exec_lua([[return _G.osc10_response]])
    )
  end)

  it('does not leak pending TermRequest on buffer destroy #39332', function()
    -- Send all OSC sequences in one exec_lua so that the event loop does not drain between the sends.
    exec_lua(function(prefix, st)
      local buf = vim.api.nvim_create_buf(false, true)
      local chan = vim.api.nvim_open_term(buf, {})
      vim.api.nvim_create_autocmd('TermRequest', { buffer = buf, callback = function() end })
      vim.api.nvim_chan_send(chan, prefix .. '7;file:///a' .. st)
      vim.api.nvim_chan_send(chan, prefix .. '7;file:///b' .. st)
      vim.api.nvim_buf_delete(buf, { force = true })
    end, OSC_PREFIX, ST)
    assert_alive()
  end)
end)