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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
" Tests for the terminal window.
" This is split in two, because it can take a lot of time.
" See test_terminal.vim and test_terminal3.vim for further tests.
source check.vim
CheckFeature terminal
source shared.vim
source screendump.vim
source mouse.vim
source term_util.vim
let $PROMPT_COMMAND=''
func Test_terminal_termwinsize_option_fixed()
CheckRunVimInTerminal
set termwinsize=6x40
let text = []
for n in range(10)
call add(text, repeat(n, 50))
endfor
call writefile(text, 'Xwinsize', 'D')
let buf = RunVimInTerminal('Xwinsize', {})
let win = bufwinid(buf)
call assert_equal([6, 40], term_getsize(buf))
call assert_equal(6, winheight(win))
call assert_equal(40, winwidth(win))
" resizing the window doesn't resize the terminal.
resize 10
vertical resize 60
call assert_equal([6, 40], term_getsize(buf))
call assert_equal(10, winheight(win))
call assert_equal(60, winwidth(win))
call StopVimInTerminal(buf)
call assert_fails('set termwinsize=40', 'E474:')
call assert_fails('set termwinsize=10+40', 'E474:')
call assert_fails('set termwinsize=abc', 'E474:')
set termwinsize=
endfunc
func Test_terminal_termwinsize_option_zero()
set termwinsize=0x0
let buf = Run_shell_in_terminal({})
let win = bufwinid(buf)
call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
call StopShellInTerminal(buf)
exe buf . 'bwipe'
set termwinsize=7x0
let buf = Run_shell_in_terminal({})
let win = bufwinid(buf)
call assert_equal([7, winwidth(win)], term_getsize(buf))
call StopShellInTerminal(buf)
exe buf . 'bwipe'
set termwinsize=0x33
let buf = Run_shell_in_terminal({})
let win = bufwinid(buf)
call assert_equal([winheight(win), 33], term_getsize(buf))
call StopShellInTerminal(buf)
exe buf . 'bwipe'
" This used to crash Vim
set termwinsize=10000*10000
let buf = Run_shell_in_terminal({})
let win = bufwinid(buf)
call assert_equal([1000, 1000], term_getsize(buf))
call StopShellInTerminal(buf)
exe buf . 'bwipe'
set termwinsize=
endfunc
func Test_terminal_termwinsize_minimum()
set termwinsize=10*50
vsplit
let buf = Run_shell_in_terminal({})
let win = bufwinid(buf)
call assert_inrange(10, 1000, winheight(win))
call assert_inrange(50, 1000, winwidth(win))
call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
resize 15
vertical resize 60
redraw
call assert_equal([15, 60], term_getsize(buf))
call assert_equal(15, winheight(win))
call assert_equal(60, winwidth(win))
resize 7
vertical resize 30
redraw
call assert_equal([10, 50], term_getsize(buf))
call assert_equal(7, winheight(win))
call assert_equal(30, winwidth(win))
call StopShellInTerminal(buf)
exe buf . 'bwipe'
set termwinsize=0*0
let buf = Run_shell_in_terminal({})
let win = bufwinid(buf)
call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
call StopShellInTerminal(buf)
exe buf . 'bwipe'
set termwinsize=
endfunc
func Test_terminal_termwinsize_overruled()
let cmd = GetDummyCmd()
set termwinsize=5x43
let buf = term_start(cmd, #{term_rows: 7, term_cols: 50})
call TermWait(buf)
call assert_equal([7, 50], term_getsize(buf))
exe "bwipe! " .. buf
let buf = term_start(cmd, #{term_cols: 50})
call TermWait(buf)
call assert_equal([5, 50], term_getsize(buf))
exe "bwipe! " .. buf
let buf = term_start(cmd, #{term_rows: 7})
call TermWait(buf)
call assert_equal([7, 43], term_getsize(buf))
exe "bwipe! " .. buf
set termwinsize=
endfunc
" hidden terminal must not change current window size
func Test_terminal_hidden_winsize()
let cmd = GetDummyCmd()
let rows = winheight(0)
let buf = term_start(cmd, #{hidden: 1, term_rows: 10})
call TermWait(buf)
call assert_equal(rows, winheight(0))
call assert_equal([10, &columns], term_getsize(buf))
exe "bwipe! " .. buf
endfunc
func Test_terminal_termwinkey()
" make three tabpages, terminal in the middle
0tabnew
tabnext
tabnew
tabprev
call assert_equal(1, winnr('$'))
call assert_equal(2, tabpagenr())
let thiswin = win_getid()
let buf = Run_shell_in_terminal({})
let termwin = bufwinid(buf)
set termwinkey=<C-L>
call feedkeys("\<C-L>w", 'tx')
call assert_equal(thiswin, win_getid())
call feedkeys("\<C-W>w", 'tx')
call assert_equal(termwin, win_getid())
if has('langmap')
set langmap=xjyk
call feedkeys("\<C-L>x", 'tx')
call assert_equal(thiswin, win_getid())
call feedkeys("\<C-W>y", 'tx')
call assert_equal(termwin, win_getid())
set langmap=
endif
call feedkeys("\<C-L>gt", "xt")
call assert_equal(3, tabpagenr())
tabprev
call assert_equal(2, tabpagenr())
call assert_equal(termwin, win_getid())
call feedkeys("\<C-L>gT", "xt")
call assert_equal(1, tabpagenr())
tabnext
call assert_equal(2, tabpagenr())
call assert_equal(termwin, win_getid())
let job = term_getjob(buf)
call feedkeys("\<C-L>\<C-C>", 'tx')
call WaitForAssert({-> assert_equal("dead", job_status(job))})
set termwinkey&
tabnext
tabclose
tabprev
tabclose
endfunc
func Test_terminal_out_err()
CheckUnix
call writefile([
\ '#!/bin/sh',
\ 'echo "this is standard error" >&2',
\ 'echo "this is standard out" >&1',
\ ], 'Xechoerrout.sh', 'D')
call setfperm('Xechoerrout.sh', 'rwxrwx---')
let outfile = 'Xtermstdout'
let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
call TermWait(buf)
call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
call assert_equal(['this is standard out'], readfile(outfile))
call assert_equal('this is standard error', term_getline(buf, 1))
call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
exe buf . 'bwipe'
call delete(outfile)
endfunc
func Test_termwinscroll()
CheckUnix
" TODO: Somehow this test sometimes hangs in the GUI
CheckNotGui
let g:test_is_flaky = 1
" Let the terminal output more than 'termwinscroll' lines, some at the start
" will be dropped.
exe 'set termwinscroll=' . &lines
let buf = term_start('/bin/sh')
call TermWait(buf)
for i in range(1, &lines)
call feedkeys("echo " . i . "\<CR>", 'xt')
call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
endfor
" Go to Terminal-Normal mode to update the buffer.
call feedkeys("\<C-W>N", 'xt')
call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
" Every "echo nr" must only appear once
let lines = getline(1, line('$'))
for i in range(&lines - len(lines) / 2 + 2, &lines)
let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
endfor
exe buf . 'bwipe!'
endfunc
" Resizing the terminal window caused an ml_get error.
" TODO: This does not reproduce the original problem.
" TODO: This test starts timing out in Github CI Gui test, why????
func Test_terminal_resize()
if has('gui_running') && expand('$GITHUB_ACTIONS') ==# 'true'
throw 'Skipped: FIXME: this test times-out in Github Actions CI with GUI. Why?'
endif
set statusline=x
terminal
call assert_equal(2, winnr('$'))
let buf = bufnr()
" Wait for the shell to display a prompt
call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
" Fill the terminal with text.
if has('win32')
call feedkeys("dir\<CR>", 'xt')
else
call feedkeys("ls\<CR>", 'xt')
endif
" Wait for some output
call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
" Go to Terminal-Normal mode for a moment.
call feedkeys("\<C-W>N", 'xt')
" Open a new window
call feedkeys("i\<C-W>n", 'xt')
call assert_equal(3, winnr('$'))
redraw
close
call assert_equal(2, winnr('$'))
call feedkeys("exit\<CR>", 'xt')
call TermWait(buf)
set statusline&
endfunc
" TODO: This test starts timing out in Github CI Gui test, why????
func Test_terminal_resize2()
CheckNotMSWindows
if has('gui_running') && expand('$GITHUB_ACTIONS') ==# 'true'
throw 'Skipped: FIXME: this test times-out in Github Actions CI with GUI. Why?'
endif
set statusline=x
terminal
call assert_equal(2, winnr('$'))
let buf = bufnr()
" Wait for the shell to display a prompt
call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
" This used to crash Vim
call feedkeys("printf '\033[8;99999;99999t'\<CR>", 'xt')
redraw
call feedkeys("exit\<CR>", 'xt')
call TermWait(buf)
set statusline&
endfunc
" must be nearly the last, we can't go back from GUI to terminal
func Test_zz1_terminal_in_gui()
CheckCanRunGui
" Ignore the "failed to create input context" error.
call test_ignore_error('E285:')
gui -f
call assert_equal(1, winnr('$'))
let buf = Run_shell_in_terminal({'term_finish': 'close'})
call StopShellInTerminal(buf)
" closing window wipes out the terminal buffer a with finished job
call WaitForAssert({-> assert_equal(1, winnr(
|