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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
local helpers = require('test.functional.helpers')(after_each)
local thelpers = require('test.functional.terminal.helpers')
local assert_alive = helpers.assert_alive
local feed, clear, nvim = helpers.feed, helpers.clear, helpers.nvim
local poke_eventloop = helpers.poke_eventloop
local eval, feed_command, source = helpers.eval, helpers.feed_command, helpers.source
local pcall_err = helpers.pcall_err
local eq, neq = helpers.eq, helpers.neq
local meths = helpers.meths
local retry = helpers.retry
local write_file = helpers.write_file
local command = helpers.command
local exc_exec = helpers.exc_exec
local matches = helpers.matches
local exec_lua = helpers.exec_lua
local sleep = helpers.sleep
local funcs = helpers.funcs
local is_os = helpers.is_os
local skip = helpers.skip
describe(':terminal buffer', function()
local screen
before_each(function()
clear()
feed_command('set modifiable swapfile undolevels=20')
poke_eventloop()
screen = thelpers.screen_setup()
end)
it('terminal-mode forces various options', function()
feed([[<C-\><C-N>]])
command('setlocal cursorline cursorlineopt=both cursorcolumn scrolloff=4 sidescrolloff=7')
eq({ 'both', 1, 1, 4, 7 }, eval('[&l:cursorlineopt, &l:cursorline, &l:cursorcolumn, &l:scrolloff, &l:sidescrolloff]'))
eq('nt', eval('mode(1)'))
-- Enter terminal-mode ("insert" mode in :terminal).
feed('i')
eq('t', eval('mode(1)'))
eq({ 'number', 1, 0, 0, 0 }, eval('[&l:cursorlineopt, &l:cursorline, &l:cursorcolumn, &l:scrolloff, &l:sidescrolloff]'))
end)
it('terminal-mode does not change cursorlineopt if cursorline is disabled', function()
feed([[<C-\><C-N>]])
command('setlocal nocursorline cursorlineopt=both')
feed('i')
eq({ 0, 'both' }, eval('[&l:cursorline, &l:cursorlineopt]'))
end)
it('terminal-mode disables cursorline when cursorlineopt is only set to "line', function()
feed([[<C-\><C-N>]])
command('setlocal cursorline cursorlineopt=line')
feed('i')
eq({ 0, 'line' }, eval('[&l:cursorline, &l:cursorlineopt]'))
end)
describe('when a new file is edited', function()
before_each(function()
feed('<c-\\><c-n>:set bufhidden=wipe<cr>:enew<cr>')
screen:expect([[
^ |
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
:enew |
]])
end)
it('will hide the buffer, ignoring the bufhidden option', function()
feed(':bnext:l<esc>')
screen:expect([[
^ |
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
|
]])
end)
end)
describe('swap and undo', function()
before_each(function()
feed('<c-\\><c-n>')
screen:expect([[
tty ready |
{2:^ } |
|
|
|
|
|
]])
end)
it('does not create swap files', function()
local swapfile = nvim('exec', 'swapname', true):gsub('\n', '')
eq(nil, io.open(swapfile))
end)
it('does not create undofiles files', function()
local undofile = nvim('eval', 'undofile(bufname("%"))')
eq(nil, io.open(undofile))
end)
end)
it('cannot be modified directly', function()
feed('<c-\\><c-n>dd')
screen:expect([[
tty ready |
{2:^ } |
|
|
|
|
{8:E21: Cannot make changes, 'modifiable' is off} |
]])
end)
it('sends data to the terminal when the "put" operator is used', function()
feed('<c-\\><c-n>gg"ayj')
feed_command('let @a = "appended " . @a')
feed('"ap"ap')
screen:expect([[
^tty ready |
appended tty ready |
appended tty ready |
{2: } |
|
|
:let @a = "appended " . @a |
]])
-- operator count is also taken into consideration
feed('3"ap')
screen:expect([[
^tty ready |
appended tty ready |
appended tty ready |
appended tty ready |
appended tty ready |
appended tty ready |
:let @a = "appended " . @a |
]])
end)
it('sends data to the terminal when the ":put" command is used', function()
feed('<c-\\><c-n>gg"ayj')
feed_command('let @a = "appended " . @a')
feed_command('put a')
screen:expect([[
^tty ready |
appended tty ready |
{2: } |
|
|
|
:put a |
]])
-- line argument is only used to move the cursor
feed_command('6put a')
screen:expect([[
tty ready |
appended tty ready |
appended tty ready |
{2: } |
|
^ |
:6put a |
]])
end)
it('can be deleted', function()
feed('<c-\\><c-n>:bd!<cr>')
screen:expect([[
^ |
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
:bd! |
]])
feed_command('bnext')
screen:expect([[
^ |
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
{4:~ }|
:bnext |
]])
end)
it('handles loss of focus gracefully', function()
-- Change the statusline to avoid printing the file name, which varies.
nvim('set_option', 'statusline', '==========')
feed_command('set laststatus=0')
-- Save the buffer number of the terminal for later testing.
local tbuf = eval('bufnr("%")')
local exitcmd = helpers.is_os('win')
and "['cmd', '/c', 'exit']"
or "['sh', '-c', 'exit']"
source([[
function! SplitWindow(id, data, event)
new
call feedkeys("iabc\<Esc>")
endfunction
startinsert
call jobstart(]]..exitcmd..[[, {'on_exit': function("SplitWindow")})
call feedkeys("\<C-\>", 't') " vim will expect <C-n>, but be exited out of
" the terminal before it can be entered.
]])
-- We should be in a new buffer now.
screen:expect([[
ab^c |
{4:~ }|
{5:========== }|
rows: 2, cols: 50 |
{2: } |
{1:========== }|
|
]])
neq(tbuf, eval('bufnr("%")'))
feed_command('quit!') -- Should exit the new window, not the terminal.
eq(tbuf, eval('bufnr("%")'))
feed_command('set laststatus=1') -- Restore laststatus to the default.
end)
it('term_close() use-after-free #4393', function()
feed_command('terminal yes')
feed([[<C-\><C-n>]])
feed_command('bdelete!')
end)
describe('handles confirmations', function()
it('with :confirm', function()
feed_command('terminal')
feed('<c-\\><c-n>')
feed_command('confirm bdelete')
screen:expect{any='Close "term://'}
end)
it('with &confirm', function()
feed_command('terminal')
feed('<c-\\><c-n>')
feed_command('bdelete')
screen:expect{any='E89'}
feed('<cr>')
eq('terminal', eval('&buftype'))
feed_command('set confirm | bdelete')
screen:expect{any='Close "term://'}
feed('y')
neq('terminal', eval('&buftype'))
end)
end)
it('it works with set rightleft #11438', function()
skip(is_os('win'))
local columns = eval('&columns')
feed(string.rep('a', columns))
command('set rightleft')
screen:expect([[
ydaer ytt|
{1:a}aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
|
|
|
|
{3:-- TERMINAL --} |
]])
command('bdelete!')
end)
it('requires bang (!) to close a running job #15402', function()
eq('Vim(wqall):E948: Job still running', exc_exec('wqall'))
for _, cmd in ipairs({ 'bdelete', '%bdelete', 'bwipeout', 'bunload' }) do
matches('^Vim%('..cmd:gsub('%%', '')..'%):E89: term://.*tty%-test.* will be killed %(add %! to override%)$',
exc_exec(cmd))
end
command('call jobstop(&channel)')
assert(0 >= eval('jobwait([&channel], 1000)[0]'))
command('bdelete')
end)
it('stops running jobs with :quit', function()
-- Open in a new window to avoid terminating the nvim instance
command('split')
command('terminal')
command('set nohidden')
command('quit')
end)
it('does not segfault when pasting empty register #13955', function()
feed('<c-\\><c-n>')
feed_command('put a') -- register a is empty
helpers.assert_alive()
end)
it([[can use temporary normal mode <c-\><c-o>]], function()
eq('t', funcs.mode(1))
feed [[<c-\><c-o>]]
screen:expect{grid=[[
tty ready |
{2:^ } |
|
|
|
|
{3:-- (terminal) --} |
]]}
eq('ntT', funcs.mode(1))
feed [[:let g:x = 17]]
screen:expect{grid=[[
tty ready |
{2: } |
|
|
|
|
:let g:x = 17^ |
]]}
feed [[<cr>]]
screen:expect{grid=[[
tty ready |
{1: } |
|
|
|
|
{3:-- TERMINAL --} |
]]}
eq('t', funcs.mode(1))
end)
it('writing to an existing file with :w fails #13549', function()
eq('Vim(write):E13: File exists (add ! to override)',
pcall_err(command, 'write test/functional/fixtures/tty-test.c'))
end)
end)
describe('No heap-buffer-overflow when using', function()
local testfilename = 'Xtestfile-functional-terminal-buffers_spec'
before_each(function()
write_file(testfilename, "aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
end)
after_each(function()
os.remove(testfilename)
end)
it('termopen(echo) #3161', function()
feed_command('edit ' .. testfilename)
-- Move cursor away from the beginning of the line
feed('$')
-- Let termopen() modify the buffer
feed_command('call termopen("echo")')
assert_alive()
feed_command('bdelete!')
end)
end)
describe('No heap-buffer-overflow when', function()
it('set nowrap and send long line #11548', function()
feed_command('set nowrap')
feed_command('autocmd TermOpen * startinsert')
feed_command('call feedkeys("4000ai\\<esc>:terminal!\\<cr>")')
assert_alive()
end)
end)
describe('on_lines does not emit out-of-bounds line indexes when', function()
before_each(function()
clear()
exec_lua([[
function _G.register_callback(bufnr)
_G.cb_error = ''
vim.api.nvim_buf_attach(bufnr, false, {
on_lines = function(_, bufnr, _, firstline, _, _)
local status, msg = pcall(vim.api.nvim_buf_get_offset, bufnr, firstline)
if not status then
_G.cb_error = msg
end
end
})
end
]])
end)
it('creating a terminal buffer #16394', function()
feed_command('autocmd TermOpen * ++once call v:lua.register_callback(str2nr(expand("<abuf>")))')
feed_command('terminal')
sleep(500)
eq('', exec_lua([[return _G.cb_error]]))
end)
it('deleting a terminal buffer #16394', function()
feed_command('terminal')
sleep(500)
feed_command('lua _G.register_callback(0)')
feed_command('bdelete!')
eq('', exec_lua([[return _G.cb_error]]))
end)
it('runs TextChangedT event', function()
meths.set_var('called', 0)
command('autocmd TextChangedT * ++once let g:called = 1')
feed_command('terminal')
feed('iaa')
eq(1, meths.get_var('called'))
end)
end)
it('terminal truncates number of composing characters to 5', function()
clear()
local chan = meths.open_term(0, {})
local composing = ('a̳'):sub(2)
meths.chan_send(chan, 'a' .. composing:rep(8))
retry(nil, nil, function() eq('a' .. composing:rep(5), meths.get_current_line()) end)
end)
|