blob: 3e8ff86f1661a5ba72c9b99a9d21f56f252b5ed4 [file] [log] [blame]
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02001" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02003" See test_terminal.vim and test_terminal3.vim for further tests.
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02004
5source check.vim
6CheckFeature terminal
7
8source shared.vim
9source screendump.vim
10source mouse.vim
11source term_util.vim
12
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020013let $PROMPT_COMMAND=''
14
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020015func Test_terminal_termwinsize_option_fixed()
16 CheckRunVimInTerminal
17 set termwinsize=6x40
18 let text = []
19 for n in range(10)
20 call add(text, repeat(n, 50))
21 endfor
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010022 call writefile(text, 'Xwinsize', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020023 let buf = RunVimInTerminal('Xwinsize', {})
24 let win = bufwinid(buf)
25 call assert_equal([6, 40], term_getsize(buf))
26 call assert_equal(6, winheight(win))
27 call assert_equal(40, winwidth(win))
28
29 " resizing the window doesn't resize the terminal.
30 resize 10
31 vertical resize 60
32 call assert_equal([6, 40], term_getsize(buf))
33 call assert_equal(10, winheight(win))
34 call assert_equal(60, winwidth(win))
35
36 call StopVimInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020037
Bram Moolenaare2e40752020-09-04 21:18:46 +020038 call assert_fails('set termwinsize=40', 'E474:')
39 call assert_fails('set termwinsize=10+40', 'E474:')
40 call assert_fails('set termwinsize=abc', 'E474:')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020041
42 set termwinsize=
43endfunc
44
45func Test_terminal_termwinsize_option_zero()
46 set termwinsize=0x0
47 let buf = Run_shell_in_terminal({})
48 let win = bufwinid(buf)
49 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
50 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020051 exe buf . 'bwipe'
52
53 set termwinsize=7x0
54 let buf = Run_shell_in_terminal({})
55 let win = bufwinid(buf)
56 call assert_equal([7, winwidth(win)], term_getsize(buf))
57 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020058 exe buf . 'bwipe'
59
60 set termwinsize=0x33
61 let buf = Run_shell_in_terminal({})
62 let win = bufwinid(buf)
63 call assert_equal([winheight(win), 33], term_getsize(buf))
64 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020065 exe buf . 'bwipe'
66
Christian Brabandtaa64ba12023-09-19 21:05:20 +020067 " This used to crash Vim
68 set termwinsize=10000*10000
69 let buf = Run_shell_in_terminal({})
70 let win = bufwinid(buf)
71 call assert_equal([1000, 1000], term_getsize(buf))
72 call StopShellInTerminal(buf)
73 exe buf . 'bwipe'
74
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020075 set termwinsize=
76endfunc
77
78func Test_terminal_termwinsize_minimum()
79 set termwinsize=10*50
80 vsplit
81 let buf = Run_shell_in_terminal({})
82 let win = bufwinid(buf)
83 call assert_inrange(10, 1000, winheight(win))
84 call assert_inrange(50, 1000, winwidth(win))
85 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
86
87 resize 15
88 vertical resize 60
89 redraw
90 call assert_equal([15, 60], term_getsize(buf))
91 call assert_equal(15, winheight(win))
92 call assert_equal(60, winwidth(win))
93
94 resize 7
95 vertical resize 30
96 redraw
97 call assert_equal([10, 50], term_getsize(buf))
98 call assert_equal(7, winheight(win))
99 call assert_equal(30, winwidth(win))
100
101 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200102 exe buf . 'bwipe'
103
104 set termwinsize=0*0
105 let buf = Run_shell_in_terminal({})
106 let win = bufwinid(buf)
107 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
108 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200109 exe buf . 'bwipe'
110
111 set termwinsize=
112endfunc
113
Bram Moolenaarb936b792020-09-04 18:34:09 +0200114func Test_terminal_termwinsize_overruled()
115 let cmd = GetDummyCmd()
116 set termwinsize=5x43
117 let buf = term_start(cmd, #{term_rows: 7, term_cols: 50})
118 call TermWait(buf)
119 call assert_equal([7, 50], term_getsize(buf))
120 exe "bwipe! " .. buf
121
122 let buf = term_start(cmd, #{term_cols: 50})
123 call TermWait(buf)
124 call assert_equal([5, 50], term_getsize(buf))
125 exe "bwipe! " .. buf
126
127 let buf = term_start(cmd, #{term_rows: 7})
128 call TermWait(buf)
129 call assert_equal([7, 43], term_getsize(buf))
130 exe "bwipe! " .. buf
131
132 set termwinsize=
133endfunc
134
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200135" hidden terminal must not change current window size
136func Test_terminal_hidden_winsize()
137 let cmd = GetDummyCmd()
138 let rows = winheight(0)
139 let buf = term_start(cmd, #{hidden: 1, term_rows: 10})
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000140 call TermWait(buf)
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200141 call assert_equal(rows, winheight(0))
142 call assert_equal([10, &columns], term_getsize(buf))
143 exe "bwipe! " .. buf
144endfunc
145
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200146func Test_terminal_termwinkey()
147 " make three tabpages, terminal in the middle
148 0tabnew
149 tabnext
150 tabnew
151 tabprev
152 call assert_equal(1, winnr('$'))
153 call assert_equal(2, tabpagenr())
154 let thiswin = win_getid()
155
156 let buf = Run_shell_in_terminal({})
157 let termwin = bufwinid(buf)
158 set termwinkey=<C-L>
159 call feedkeys("\<C-L>w", 'tx')
160 call assert_equal(thiswin, win_getid())
161 call feedkeys("\<C-W>w", 'tx')
162 call assert_equal(termwin, win_getid())
163
164 if has('langmap')
165 set langmap=xjyk
166 call feedkeys("\<C-L>x", 'tx')
167 call assert_equal(thiswin, win_getid())
168 call feedkeys("\<C-W>y", 'tx')
169 call assert_equal(termwin, win_getid())
170 set langmap=
171 endif
172
173 call feedkeys("\<C-L>gt", "xt")
174 call assert_equal(3, tabpagenr())
175 tabprev
176 call assert_equal(2, tabpagenr())
177 call assert_equal(termwin, win_getid())
178
179 call feedkeys("\<C-L>gT", "xt")
180 call assert_equal(1, tabpagenr())
181 tabnext
182 call assert_equal(2, tabpagenr())
183 call assert_equal(termwin, win_getid())
184
185 let job = term_getjob(buf)
186 call feedkeys("\<C-L>\<C-C>", 'tx')
187 call WaitForAssert({-> assert_equal("dead", job_status(job))})
188
189 set termwinkey&
190 tabnext
191 tabclose
192 tabprev
193 tabclose
194endfunc
195
196func Test_terminal_out_err()
197 CheckUnix
198
199 call writefile([
200 \ '#!/bin/sh',
201 \ 'echo "this is standard error" >&2',
202 \ 'echo "this is standard out" >&1',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100203 \ ], 'Xechoerrout.sh', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200204 call setfperm('Xechoerrout.sh', 'rwxrwx---')
205
206 let outfile = 'Xtermstdout'
207 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000208 call TermWait(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200209
210 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
211 call assert_equal(['this is standard out'], readfile(outfile))
212 call assert_equal('this is standard error', term_getline(buf, 1))
213
214 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
215 exe buf . 'bwipe'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200216 call delete(outfile)
217endfunc
218
219func Test_termwinscroll()
220 CheckUnix
Bram Moolenaarf65927f2020-07-11 14:04:28 +0200221 " TODO: Somehow this test sometimes hangs in the GUI
222 CheckNotGui
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100223 let g:test_is_flaky = 1
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200224
225 " Let the terminal output more than 'termwinscroll' lines, some at the start
226 " will be dropped.
227 exe 'set termwinscroll=' . &lines
228 let buf = term_start('/bin/sh')
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000229 call TermWait(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200230 for i in range(1, &lines)
231 call feedkeys("echo " . i . "\<CR>", 'xt')
232 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
233 endfor
234 " Go to Terminal-Normal mode to update the buffer.
235 call feedkeys("\<C-W>N", 'xt')
236 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
237
238 " Every "echo nr" must only appear once
239 let lines = getline(1, line('$'))
240 for i in range(&lines - len(lines) / 2 + 2, &lines)
241 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
242 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
243 endfor
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200244 exe buf . 'bwipe!'
245endfunc
246
h-east3a3a2c92024-12-15 19:36:11 +0100247func Test_termwinscroll_topline()
Christian Brabandta74d5502025-04-29 18:21:31 +0200248 " TODO: why does this fail on Appveyor and Github?
249 CheckNotMSWindows
Hirohito Higashifa9753a2025-04-27 15:36:43 +0200250
h-east3a3a2c92024-12-15 19:36:11 +0100251 set termwinscroll=1000 mouse=a
252 terminal
253 call assert_equal(2, winnr('$'))
254 let buf = bufnr()
255 call WaitFor({-> !empty(term_getline(buf, 1))})
256
257 let num1 = &termwinscroll / 100 * 99
258 call writefile(range(num1), 'Xtext', 'D')
259 if has('win32')
260 call term_sendkeys(buf, "type Xtext\<CR>")
261 else
262 call term_sendkeys(buf, "cat Xtext\<CR>")
263 endif
264 let rows = term_getsize(buf)[0]
265 " On MS-Windows there is an empty line, check both last line and above it.
266 call WaitForAssert({-> assert_match(string(num1 - 1), term_getline(buf, rows - 1) .. term_getline(buf, rows - 2))})
267 call feedkeys("\<C-W>N", 'xt')
268 call feedkeys("i", 'xt')
269
270 let num2 = &termwinscroll / 100 * 10
271 call writefile(range(num2), 'Xtext', 'D')
272 if has('win32')
273 call term_sendkeys(buf, "timeout /t 1 && type Xtext\<CR>")
274 else
275 call term_sendkeys(buf, "sleep 1; cat Xtext\<CR>")
276 endif
277 " Change the normal window to the current window with keystrokes.
278 call feedkeys("\<C-W>w", 'xt')
279 call WaitForAssert({-> assert_notequal(buf, bufnr())})
280 let rows = term_getsize(buf)[0]
281 " On MS-Windows there is an empty line, check both last line and above it.
282 call WaitForAssert({-> assert_match(string(num2 - 1), term_getline(buf, rows - 1) .. term_getline(buf, rows - 2))})
283 " Change the terminal window to the current window using mouse operation.
284 call test_setmouse(1, 1)
285 call feedkeys("\<LeftMouse>", "xt")
286 call WaitForAssert({-> assert_equal(buf, bufnr())})
287 " Before the fix, E340 and E315 would occur multiple times at this point.
288 let wm = winheight(0) * 2
289 let num3 = num1 + num2 - (num1 / 10) - wm
290 call assert_inrange(num3 - wm, num3 + wm, getwininfo(bufwinid(buf))[0].topline)
291 exe buf . 'bwipe!'
292 set termwinscroll& mouse&
293endfunc
294
Hirohito Higashi3219da52025-02-01 14:48:35 +0100295func Test_termwinscroll_topline2()
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100296 " calling the terminal API doesn't work on Windows
297 CheckNotMSWindows
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100298
Christian Brabandt44a21352025-02-02 09:03:00 +0100299 set termwinscroll=50000 mouse=a
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100300 set shell=sh
Hirohito Higashi3219da52025-02-01 14:48:35 +0100301 let norm_winid = win_getid()
302 terminal
303 call assert_equal(2, winnr('$'))
304 let buf = bufnr()
305 let win = winnr()
306 call WaitFor({-> !empty(term_getline(buf, 1))})
307
308 let num1 = &termwinscroll / 1000 * 999
309 call writefile(range(num1), 'Xtext', 'D')
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100310 call term_sendkeys(buf, "cat Xtext\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100311 call term_sendkeys(buf, "printf '" .. TermNotifyParentCmd(v:false) .. "'\<cr>")
Hirohito Higashi3219da52025-02-01 14:48:35 +0100312 let rows = term_getsize(buf)[0]
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100313 let cnt = 0
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100314 while !g:child_notification && cnt <= 50000
315 " Spin wait to process the terminal print as quickly as possible. This is
316 " more efficient than calling WaitForChildNotification() as we don't want
317 " to sleep here as the print is I/O-bound.
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100318 let cnt += 1
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100319 call term_wait(buf, 0)
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100320 endwhile
Hirohito Higashi3219da52025-02-01 14:48:35 +0100321 call WaitForAssert({-> assert_match(string(num1 - 1), term_getline(buf, rows - 1) .. '\|' .. term_getline(buf, rows - 2))})
322 call feedkeys("\<C-W>N", 'xt')
323 call feedkeys("i", 'xt')
324
325 let num2 = &termwinscroll / 1000 * 8
326 call writefile(range(num2), 'Xtext', 'D')
Christian Brabandtaae1bfb2025-02-09 17:10:30 +0100327 call term_sendkeys(buf, "sleep 2; cat Xtext\<CR>")
Hirohito Higashi3219da52025-02-01 14:48:35 +0100328 let winrow = get(get(filter(getwininfo(), 'v:val.winid == norm_winid'), 0, {}), 'winrow', -1)
329
330 call test_setmouse(winrow, 1)
331 call feedkeys("\<LeftMouse>", "xt")
332 call WaitForAssert({-> assert_notequal(buf, bufnr())})
333
334 " Change the terminal window row size
335 call win_move_statusline(win,1)
336 " Before the fix, E340 and E315 would occur multiple times at this point.
337 let winrow2 = get(get(filter(getwininfo(), 'v:val.winid == norm_winid'), 0, {}), 'winrow', -1)
338 call assert_equal(winrow + 1, winrow2)
339
340 call test_setmouse(1, 1)
341 call feedkeys("\<LeftMouse>", "xt")
342 call WaitForAssert({-> assert_equal(buf, bufnr())})
343
344 exe buf . 'bwipe!'
345 set termwinscroll& mouse& sh&
346endfunc
347
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200348" Resizing the terminal window caused an ml_get error.
349" TODO: This does not reproduce the original problem.
Christian Brabandt926c3f42023-12-01 16:59:32 +0000350" TODO: This test starts timing out in Github CI Gui test, why????
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200351func Test_terminal_resize()
Christian Brabandt926c3f42023-12-01 16:59:32 +0000352 if has('gui_running') && expand('$GITHUB_ACTIONS') ==# 'true'
353 throw 'Skipped: FIXME: this test times-out in Github Actions CI with GUI. Why?'
354 endif
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200355 set statusline=x
356 terminal
357 call assert_equal(2, winnr('$'))
Bram Moolenaarc54f3472021-03-23 19:22:12 +0100358 let buf = bufnr()
359
360 " Wait for the shell to display a prompt
361 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200362
363 " Fill the terminal with text.
364 if has('win32')
365 call feedkeys("dir\<CR>", 'xt')
366 else
367 call feedkeys("ls\<CR>", 'xt')
368 endif
Bram Moolenaarc54f3472021-03-23 19:22:12 +0100369 " Wait for some output
370 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
371
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200372 " Go to Terminal-Normal mode for a moment.
373 call feedkeys("\<C-W>N", 'xt')
374 " Open a new window
375 call feedkeys("i\<C-W>n", 'xt')
376 call assert_equal(3, winnr('$'))
377 redraw
378
379 close
380 call assert_equal(2, winnr('$'))
381 call feedkeys("exit\<CR>", 'xt')
Bram Moolenaarc54f3472021-03-23 19:22:12 +0100382 call TermWait(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200383 set statusline&
384endfunc
385
Christian Brabandtc089c382023-12-03 16:48:29 +0000386" TODO: This test starts timing out in Github CI Gui test, why????
Christian Brabandtaa64ba12023-09-19 21:05:20 +0200387func Test_terminal_resize2()
388 CheckNotMSWindows
Christian Brabandtc089c382023-12-03 16:48:29 +0000389 if has('gui_running') && expand('$GITHUB_ACTIONS') ==# 'true'
390 throw 'Skipped: FIXME: this test times-out in Github Actions CI with GUI. Why?'
391 endif
Christian Brabandtaa64ba12023-09-19 21:05:20 +0200392 set statusline=x
393 terminal
394 call assert_equal(2, winnr('$'))
395 let buf = bufnr()
396
397 " Wait for the shell to display a prompt
398 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
399
400 " This used to crash Vim
401 call feedkeys("printf '\033[8;99999;99999t'\<CR>", 'xt')
402 redraw
403
404 call feedkeys("exit\<CR>", 'xt')
405 call TermWait(buf)
406 set statusline&
407endfunc
408
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200409" must be nearly the last, we can't go back from GUI to terminal
410func Test_zz1_terminal_in_gui()
411 CheckCanRunGui
412
413 " Ignore the "failed to create input context" error.
414 call test_ignore_error('E285:')
415
416 gui -f
417
418 call assert_equal(1, winnr('$'))
419 let buf = Run_shell_in_terminal({'term_finish': 'close'})
420 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200421
422 " closing window wipes out the terminal buffer a with finished job
423 call WaitForAssert({-> assert_equal(1, winnr('$'))})
424 call assert_equal("", bufname(buf))
425
426 unlet g:job
427endfunc
428
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100429func Test_zz2_terminal_guioptions_bang()
430 CheckGui
431 set guioptions+=!
432
433 let filename = 'Xtestscript'
434 if has('win32')
435 let filename .= '.bat'
436 let prefix = ''
437 let contents = ['@echo off', 'exit %1']
438 else
439 let filename .= '.sh'
440 let prefix = './'
441 let contents = ['#!/bin/sh', 'exit $1']
442 endif
443 call writefile(contents, filename, 'D')
444 call setfperm(filename, 'rwxrwx---')
445
446 " Check if v:shell_error is equal to the exit status.
447 let exitval = 0
448 execute printf(':!%s%s %d', prefix, filename, exitval)
449 call assert_equal(exitval, v:shell_error)
450
451 let exitval = 9
452 execute printf(':!%s%s %d', prefix, filename, exitval)
453 call assert_equal(exitval, v:shell_error)
454
455 set guioptions&
456endfunc
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200457
458func Test_terminal_hidden()
459 CheckUnix
460
461 term ++hidden cat
462 let bnr = bufnr('$')
463 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
464 exe 'sbuf ' . bnr
465 call assert_equal('terminal', &buftype)
466 call term_sendkeys(bnr, "asdf\<CR>")
467 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
468 call term_sendkeys(bnr, "\<C-D>")
469 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
470 bwipe!
471endfunc
472
473func Test_terminal_switch_mode()
474 term
475 let bnr = bufnr('$')
476 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200477 " In the GUI the first switch sometimes doesn't work. Switch twice to avoid
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100478 " flakiness.
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200479 call feedkeys("\<C-W>N", 'xt')
480 call feedkeys("A", 'xt')
481 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200482 call feedkeys("\<C-W>N", 'xt')
483 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
484 call feedkeys("A", 'xt')
485 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
486 call feedkeys("\<C-\>\<C-N>", 'xt')
487 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
488 call feedkeys("I", 'xt')
489 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
490 call feedkeys("\<C-W>Nv", 'xt')
491 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
492 call feedkeys("I", 'xt')
493 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
494 call feedkeys("\<C-W>Nv", 'xt')
495 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
496 call feedkeys("A", 'xt')
497 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
498 bwipe!
499endfunc
500
501func Test_terminal_normal_mode()
502 CheckRunVimInTerminal
503
504 " Run Vim in a terminal and open a terminal window to run Vim in.
505 let lines =<< trim END
506 call setline(1, range(11111, 11122))
507 3
508 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100509 call writefile(lines, 'XtermNormal', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200510 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
511 call TermWait(buf)
512
513 call term_sendkeys(buf, "\<C-W>N")
514 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
515 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
516
517 call term_sendkeys(buf, ":set culopt=number\r")
518 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
519
520 call term_sendkeys(buf, ":set culopt=line\r")
521 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
522
523 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
524 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
525 call StopVimInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200526endfunc
527
528func Test_terminal_hidden_and_close()
529 CheckUnix
530
531 call assert_equal(1, winnr('$'))
532 term ++hidden ++close ls
533 let bnr = bufnr('$')
534 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
535 call WaitForAssert({-> assert_false(bufexists(bnr))})
536 call assert_equal(1, winnr('$'))
537endfunc
538
539func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200540 if has('conpty')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200541 throw 'Skipped: fail on ConPTY'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200542 endif
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100543 let g:test_is_flaky = 1
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200544 let contents = [
545 \ [ 'One', '', 'X' ],
546 \ [ 'Two', '', '' ],
547 \ [ 'Three' ] + repeat([''], 30)
548 \ ]
549
550 for c in contents
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100551 call writefile(c, 'Xdntfile', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200552 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200553 term cmd /D /c type Xdntfile
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200554 else
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100555 term cat Xdntfile
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200556 endif
557 let bnr = bufnr('$')
558 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
559 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
560 sleep 100m
561 call assert_equal(c, getline(1, line('$')))
562 quit
563 endfor
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200564endfunc
565
Bram Moolenaarb936b792020-09-04 18:34:09 +0200566func GetDummyCmd()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200567 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200568 return 'cmd /D /c ""'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200569 else
570 CheckExecutable false
Bram Moolenaarb936b792020-09-04 18:34:09 +0200571 return 'false'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200572 endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200573endfunc
574
575func Test_terminal_no_job()
576 let cmd = GetDummyCmd()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200577 let term = term_start(cmd, {'term_finish': 'close'})
578 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
579endfunc
580
581func Test_term_getcursor()
582 CheckUnix
583
584 let buf = Run_shell_in_terminal({})
585
586 " Wait for the shell to display a prompt.
587 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
588
589 " Hide the cursor.
590 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
591 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
592
593 " Show the cursor.
594 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
595 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
596
597 " Change color of cursor.
598 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
599 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
600 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
601 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
602 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
603
604 " Make cursor a blinking block.
605 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
606 call WaitForAssert({-> assert_equal([1, 1],
607 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
608
609 " Make cursor a steady block.
610 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
611 call WaitForAssert({-> assert_equal([0, 1],
612 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
613
614 " Make cursor a blinking underline.
615 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
616 call WaitForAssert({-> assert_equal([1, 2],
617 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
618
619 " Make cursor a steady underline.
620 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
621 call WaitForAssert({-> assert_equal([0, 2],
622 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
623
624 " Make cursor a blinking vertical bar.
625 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
626 call WaitForAssert({-> assert_equal([1, 3],
627 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
628
629 " Make cursor a steady vertical bar.
630 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
631 call WaitForAssert({-> assert_equal([0, 3],
632 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
633
634 call StopShellInTerminal(buf)
635endfunc
636
637" Test for term_gettitle()
Christian Brabandt6a46c192024-02-24 15:56:34 +0100638" Known to be flaky on Mac-OS X and the GH runners
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200639func Test_term_gettitle()
640 " term_gettitle() returns an empty string for a non-terminal buffer
641 " and for a non-existing buffer.
642 call assert_equal('', bufnr('%')->term_gettitle())
643 call assert_equal('', term_gettitle(bufnr('$') + 1))
644
645 if !has('title') || empty(&t_ts)
646 throw "Skipped: can't get/set title"
647 endif
Christian Brabandt6a46c192024-02-24 15:56:34 +0100648 if has('osx') && !empty($CI) && system('uname -m') =~# 'arm64'
649 " This test often fails with the following error message on Github runners
650 " MacOS-14
651 " '^\\[No Name\\] - VIM\\d*$' does not match 'e] - VIM'
652 " Why? Is the terminal that runs Vim too small?
653 throw 'Skipped: FIXME: Running this test on M1 Mac fails on GitHub Actions'
654 endif
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200655
656 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000657 call TermWait(term)
Bram Moolenaar3bb79dc2021-12-12 18:50:19 +0000658 " When Vim is running as a server then the title ends in VIM{number}, thus
659 " optionally match a number after "VIM".
660 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d*$', term_gettitle(term)) })
661 call term_sendkeys(term, ":e Xfoo\r")
662 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d*$', term_gettitle(term)) })
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200663
664 call term_sendkeys(term, ":set titlestring=foo\r")
665 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
666
667 exe term . 'bwipe!'
668endfunc
669
670func Test_term_gettty()
671 let buf = Run_shell_in_terminal({})
672 let gettty = term_gettty(buf)
673
674 if has('unix') && executable('tty')
675 " Find tty using the tty shell command.
676 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
677 call term_sendkeys(buf, "tty\r")
678 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
679 let tty = term_getline(buf, 2)
680 call assert_equal(tty, gettty)
681 endif
682
683 let gettty0 = term_gettty(buf, 0)
684 let gettty1 = term_gettty(buf, 1)
685
686 call assert_equal(gettty, gettty0)
687 call assert_equal(job_info(g:job).tty_out, gettty0)
688 call assert_equal(job_info(g:job).tty_in, gettty1)
689
690 if has('unix')
691 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
692 " are identical according to :help term_gettty()
693 call assert_equal(gettty0, gettty1)
694 call assert_match('^/dev/', gettty)
695 else
696 " ConPTY works on anonymous pipe.
697 if !has('conpty')
698 call assert_match('^\\\\.\\pipe\\', gettty0)
699 call assert_match('^\\\\.\\pipe\\', gettty1)
700 endif
701 endif
702
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200703 call assert_fails('call term_gettty(buf, 2)', 'E475:')
704 call assert_fails('call term_gettty(buf, -1)', 'E475:')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200705
706 call assert_equal('', term_gettty(buf + 1))
707
708 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200709 exe buf . 'bwipe'
710endfunc
711
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200712
713" vim: shiftwidth=2 sts=2 expandtab