blob: 3d06eb3876ca128a223e0633353bcb25c839d326 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004 finish
5endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
11
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar35422f42017-08-05 16:33:56 +020028 let string = string({'job': term_getjob(buf)})
29 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020042 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
43 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020046 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020048 call assert_match('%aR[^\n]*running]', execute('ls R'))
49 call assert_notmatch('%[^\n]*running]', execute('ls F'))
50 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051
Bram Moolenaar94053a52017-08-01 21:44:33 +020052 call Stop_shell_in_terminal(buf)
53 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054 call assert_equal('n', mode())
55 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020056 call assert_match('%aF[^\n]*finished]', execute('ls F'))
57 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
58 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 " closing window wipes out the terminal buffer a with finished job
61 close
62 call assert_equal("", bufname(buf))
63
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020064 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 unlet g:job
66endfunc
67
68func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020069 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020070 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071 call term_wait(buf)
72
73 setlocal modifiable
74 exe "normal Axxx\<Esc>"
75 call assert_fails(buf . 'bwipe', 'E517')
76 undo
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078 exe buf . 'bwipe'
79 unlet g:job
80endfunc
81
Bram Moolenaar94053a52017-08-01 21:44:33 +020082func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020083 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020084 call assert_fails(buf . 'bwipe', 'E517')
85 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020086 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020087 call assert_equal("", bufname(buf))
88
89 unlet g:job
90endfunc
91
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020092func Test_terminal_split_quit()
93 let buf = Run_shell_in_terminal({})
94 call term_wait(buf)
95 split
96 quit!
97 call term_wait(buf)
98 sleep 50m
99 call assert_equal('run', job_status(g:job))
100
101 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200102 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200103
104 exe buf . 'bwipe'
105 unlet g:job
106endfunc
107
Bram Moolenaar94053a52017-08-01 21:44:33 +0200108func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200109 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200110 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200111 quit
112 for nr in range(1, winnr('$'))
113 call assert_notequal(winbufnr(nr), buf)
114 endfor
115 call assert_true(bufloaded(buf))
116 call assert_true(buflisted(buf))
117
118 exe 'split ' . buf . 'buf'
119 call Stop_shell_in_terminal(buf)
120 exe buf . 'bwipe'
121
122 unlet g:job
123endfunc
124
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200125func! s:Nasty_exit_cb(job, st)
126 exe g:buf . 'bwipe!'
127 let g:buf = 0
128endfunc
129
Bram Moolenaar9d189612017-09-09 18:11:00 +0200130func Get_cat_123_cmd()
131 if has('win32')
132 return 'cmd /c "cls && color 2 && echo 123"'
133 else
134 call writefile(["\<Esc>[32m123"], 'Xtext')
135 return "cat Xtext"
136 endif
137endfunc
138
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200139func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200140 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200141 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
142 let g:job = term_getjob(g:buf)
143
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200144 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
145 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200146 unlet g:buf
147 unlet g:job
148 call delete('Xtext')
149endfunc
150
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200151func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200152 let l = term_scrape(a:buf, 0)
153 call assert_true(len(l) == 0)
154 let l = term_scrape(a:buf, 999)
155 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200156 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200157 call assert_true(len(l) > 0)
158 call assert_equal('1', l[0].chars)
159 call assert_equal('2', l[1].chars)
160 call assert_equal('3', l[2].chars)
161 call assert_equal('#00e000', l[0].fg)
162 if &background == 'light'
163 call assert_equal('#ffffff', l[0].bg)
164 else
165 call assert_equal('#000000', l[0].bg)
166 endif
167
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200168 let l = term_getline(a:buf, -1)
169 call assert_equal('', l)
170 let l = term_getline(a:buf, 0)
171 call assert_equal('', l)
172 let l = term_getline(a:buf, 999)
173 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200174 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200175 call assert_equal('123', l)
176endfunc
177
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200178func Test_terminal_scrape_123()
179 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 let buf = term_start(cmd)
181
182 let termlist = term_list()
183 call assert_equal(1, len(termlist))
184 call assert_equal(buf, termlist[0])
185
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200186 " Nothing happens with invalid buffer number
187 call term_wait(1234)
188
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200189 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200190 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200191 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200192 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200193 call Check_123(buf)
194
195 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100196 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200197 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200198 call term_wait(buf)
199 call Check_123(buf)
200
201 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200202 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200203endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200204
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200205func Test_terminal_scrape_multibyte()
206 if !has('multi_byte')
207 return
208 endif
209 call writefile(["léttまrs"], 'Xtext')
210 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200211 " Run cmd with UTF-8 codepage to make the type command print the expected
212 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100213 let buf = term_start("cmd /K chcp 65001")
214 call term_sendkeys(buf, "type Xtext\<CR>")
215 call term_sendkeys(buf, "exit\<CR>")
216 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200217 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100218 let buf = term_start("cat Xtext")
219 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200221
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100222 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
223 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200224 call assert_true(len(l) >= 7)
225 call assert_equal('l', l[0].chars)
226 call assert_equal('é', l[1].chars)
227 call assert_equal(1, l[1].width)
228 call assert_equal('t', l[2].chars)
229 call assert_equal('t', l[3].chars)
230 call assert_equal('ま', l[4].chars)
231 call assert_equal(2, l[4].width)
232 call assert_equal('r', l[5].chars)
233 call assert_equal('s', l[6].chars)
234
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100235 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200236 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100237 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200238
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100239 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200240 call delete('Xtext')
241endfunc
242
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200243func Test_terminal_scroll()
244 call writefile(range(1, 200), 'Xtext')
245 if has('win32')
246 let cmd = 'cmd /c "type Xtext"'
247 else
248 let cmd = "cat Xtext"
249 endif
250 let buf = term_start(cmd)
251
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100252 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200253 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200254 call term_wait(buf)
255 if has('win32')
256 " TODO: this should not be needed
257 sleep 100m
258 endif
259
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200260 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200261 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200262 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200263 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200264 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200265 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200266 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200267
268 exe buf . 'bwipe'
269 call delete('Xtext')
270endfunc
271
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200272func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200273 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200274 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200275 call writefile(range(150), 'Xtext')
276 if has('win32')
277 call term_sendkeys(buf, "type Xtext\<CR>")
278 else
279 call term_sendkeys(buf, "cat Xtext\<CR>")
280 endif
281 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200282 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200283 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200284 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200285 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200286
287 call Stop_shell_in_terminal(buf)
288 call term_wait(buf)
289 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200290 set termwinscroll&
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200291endfunc
292
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200293func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200294 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200295
Bram Moolenaarb2412082017-08-20 18:09:14 +0200296 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200297 let size = term_getsize('')
298 bwipe!
299 call assert_equal(5, size[0])
300
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200301 call term_start(cmd, {'term_rows': 6})
302 let size = term_getsize('')
303 bwipe!
304 call assert_equal(6, size[0])
305
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200306 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200307 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200308 call assert_equal([5, 33], term_getsize(''))
309
310 call term_setsize('', 6, 0)
311 call assert_equal([6, 33], term_getsize(''))
312
313 call term_setsize('', 0, 35)
314 call assert_equal([6, 35], term_getsize(''))
315
316 call term_setsize('', 7, 30)
317 call assert_equal([7, 30], term_getsize(''))
318
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200319 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200320 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200321
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200322 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
323 let size = term_getsize('')
324 bwipe!
325 call assert_equal([6, 36], size)
326
Bram Moolenaarb2412082017-08-20 18:09:14 +0200327 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200328 let size = term_getsize('')
329 bwipe!
330 call assert_equal(20, size[1])
331
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200332 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
333 let size = term_getsize('')
334 bwipe!
335 call assert_equal(26, size[1])
336
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200337 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200338 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200339 let size = term_getsize('')
340 bwipe!
341 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200342
343 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
344 let size = term_getsize('')
345 bwipe!
346 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200347
348 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200349endfunc
350
351func Test_terminal_curwin()
352 let cmd = Get_cat_123_cmd()
353 call assert_equal(1, winnr('$'))
354
355 split dummy
356 exe 'terminal ++curwin ' . cmd
357 call assert_equal(2, winnr('$'))
358 bwipe!
359
360 split dummy
361 call term_start(cmd, {'curwin': 1})
362 call assert_equal(2, winnr('$'))
363 bwipe!
364
365 split dummy
366 call setline(1, 'change')
367 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
368 call assert_equal(2, winnr('$'))
369 exe 'terminal! ++curwin ' . cmd
370 call assert_equal(2, winnr('$'))
371 bwipe!
372
373 split dummy
374 call setline(1, 'change')
375 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
376 call assert_equal(2, winnr('$'))
377 bwipe!
378
379 split dummy
380 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200381 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200382endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200383
Bram Moolenaarff546792017-11-21 14:47:57 +0100384func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200385 if s:python != ''
386 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200387 " 500 was not enough for Travis
388 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200389 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200390 echo 'This will take five seconds...'
391 let waittime = 2000
392 if has('win32')
393 let cmd = $windir . '\system32\timeout.exe 1'
394 else
395 let cmd = 'sleep 1'
396 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200397 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100398 return [cmd, waittime]
399endfunc
400
401func Test_terminal_finish_open_close()
402 call assert_equal(1, winnr('$'))
403
404 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200405
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100406 " shell terminal closes automatically
407 terminal
408 let buf = bufnr('%')
409 call assert_equal(2, winnr('$'))
410 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200411 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100412 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200413 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100414
415 " shell terminal that does not close automatically
416 terminal ++noclose
417 let buf = bufnr('%')
418 call assert_equal(2, winnr('$'))
419 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200420 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100421 call Stop_shell_in_terminal(buf)
422 call assert_equal(2, winnr('$'))
423 quit
424 call assert_equal(1, winnr('$'))
425
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200426 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200427 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200428 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200429 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200430
431 call term_start(cmd, {'term_finish': 'close'})
432 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200434 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200435 call assert_equal(1, winnr('$'))
436
437 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200438 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200439 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200440 bwipe
441
442 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200443 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200444 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200445 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200447 exe 'terminal ++hidden ++open ' . cmd
448 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200449 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200450 bwipe
451
452 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
453 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200454 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200455 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200456
457 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
458 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
459 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
460 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
461
462 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200463 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200464 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200465 call assert_equal(4, winheight(0))
466 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200467endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200468
469func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200470 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200471 return
472 endif
473 call mkdir('Xdir')
474 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200475 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200476
477 exe buf . 'bwipe'
478 call delete('Xdir', 'rf')
479endfunc
480
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100481func Test_terminal_servername()
482 if !has('clientserver')
483 return
484 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100485 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100486 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200487 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100488 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100489 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100490 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100491 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100492 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100493 call term_wait(buf)
494 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100495 call WaitFor('getline(2) == v:servername')
496 call assert_equal(v:servername, getline(2))
497
Bram Moolenaar012eb662018-03-13 17:55:27 +0100498 exe buf . 'bwipe'
499 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100500endfunc
501
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200502func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100503 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200504 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200505 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100506 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100507 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100508 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100509 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100510 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100511 call term_wait(buf)
512 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200513 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200514
Bram Moolenaar012eb662018-03-13 17:55:27 +0100515 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200516endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200517
518" must be last, we can't go back from GUI to terminal
519func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200520 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200521 return
522 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200523
524 " Ignore the "failed to create input context" error.
525 call test_ignore_error('E285:')
526
Bram Moolenaar679653e2017-08-13 14:13:19 +0200527 gui -f
528
529 call assert_equal(1, winnr('$'))
530 let buf = Run_shell_in_terminal({'term_finish': 'close'})
531 call Stop_shell_in_terminal(buf)
532 call term_wait(buf)
533
534 " closing window wipes out the terminal buffer a with finished job
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200535 call WaitForAssert({-> assert_equal(1, winnr('$'))})
Bram Moolenaar679653e2017-08-13 14:13:19 +0200536 call assert_equal("", bufname(buf))
537
538 unlet g:job
539endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200540
541func Test_terminal_list_args()
542 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
543 call assert_fails(buf . 'bwipe', 'E517')
544 exe buf . 'bwipe!'
545 call assert_equal("", bufname(buf))
546endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200547
548func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100549 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200550 if has('mac')
551 " The shell or something else has a problem dealing with more than 1000
552 " characters at the same time.
553 let len = 1000
554 else
555 let len = 5000
556 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200557
558 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100559 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200560 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100561 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200562
563 " On MS-Windows there is an extra empty line below "done". Find "done" in
564 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100565 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100566 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100567 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200568 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100569 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200570 endif
571 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200572
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100573 let g:job = term_getjob(buf)
574 call Stop_shell_in_terminal(buf)
575 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200576 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200577 bwipe
578endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200579
580func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200581 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200582 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200583 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200584 if has('win32')
585 " TODO: enable once writing to stdin works on MS-Windows
586 return
587 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200588 new
589 call setline(1, ['one', 'two', 'three'])
590 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200591 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200592 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200593 call assert_equal(['3', '3', '14'], nrs)
594 bwipe
595
Bram Moolenaardada6d22017-09-02 17:18:35 +0200596 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200597 call setline(1, ['one', 'two', 'three', 'four'])
598 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200599 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200600 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200601 call assert_equal(['2', '2', '10'], nrs)
602 bwipe
603
Bram Moolenaardada6d22017-09-02 17:18:35 +0200604 if executable('python')
605 new
606 call setline(1, ['print("hello")'])
607 1term ++eof=exit() python
608 " MS-Windows echoes the input, Unix doesn't.
609 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
610 if getline(1) =~ 'hello'
611 call assert_equal('hello', getline(1))
612 else
613 call assert_equal('hello', getline(line('$') - 1))
614 endif
615 bwipe
616
617 if has('win32')
618 new
619 call setline(1, ['print("hello")'])
620 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200621 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200622 call assert_equal('hello', getline(line('$') - 1))
623 bwipe
624 endif
625 endif
626
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200627 bwipe!
628endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200629
630func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200631 " Todo: make this work in the GUI
632 if !has('gui_running')
633 return
634 endif
635 let buf = term_start('NONE', {})
636 call assert_notequal(0, buf)
637
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200638 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200639 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200640 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200641 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200642 else
643 call system('echo "look here" > ' . pty)
644 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200645 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200646
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200647 bwipe!
648endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200649
650func Test_terminal_special_chars()
651 " this file name only works on Unix
652 if !has('unix')
653 return
654 endif
655 call mkdir('Xdir with spaces')
656 call writefile(['x'], 'Xdir with spaces/quoted"file')
657 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200658 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200659 call term_wait('')
660
661 call delete('Xdir with spaces', 'rf')
662 bwipe
663endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200664
665func Test_terminal_wrong_options()
666 call assert_fails('call term_start(&shell, {
667 \ "in_io": "file",
668 \ "in_name": "xxx",
669 \ "out_io": "file",
670 \ "out_name": "xxx",
671 \ "err_io": "file",
672 \ "err_name": "xxx"
673 \ })', 'E474:')
674 call assert_fails('call term_start(&shell, {
675 \ "out_buf": bufnr("%")
676 \ })', 'E474:')
677 call assert_fails('call term_start(&shell, {
678 \ "err_buf": bufnr("%")
679 \ })', 'E474:')
680endfunc
681
682func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200683 let cmd = Get_cat_123_cmd()
684 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
685 call term_wait(buf)
686 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
687 call assert_match('123', readfile('Xfile')[0])
688 let g:job = term_getjob(buf)
689 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
690 call delete('Xfile')
691 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200692
693 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200694 call writefile(['one line'], 'Xfile')
695 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
696 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200697 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200698 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200699 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200700 bwipe
701 call delete('Xfile')
702 endif
703endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200704
705func TerminalTmap(remap)
706 let buf = Run_shell_in_terminal({})
707 call assert_equal('t', mode())
708
709 if a:remap
710 tmap 123 456
711 else
712 tnoremap 123 456
713 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100714 " don't use abcde, it's an existing command
715 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200716 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100717 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200718 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200719 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200720 let lnum = term_getcursor(buf)[0]
721 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100722 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200723 else
724 call assert_match('456', term_getline(buf, lnum))
725 endif
726
727 call term_sendkeys(buf, "\r")
728 call Stop_shell_in_terminal(buf)
729 call term_wait(buf)
730
731 tunmap 123
732 tunmap 456
733 call assert_equal('', maparg('123', 't'))
734 close
735 unlet g:job
736endfunc
737
738func Test_terminal_tmap()
739 call TerminalTmap(1)
740 call TerminalTmap(0)
741endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200742
743func Test_terminal_wall()
744 let buf = Run_shell_in_terminal({})
745 wall
746 call Stop_shell_in_terminal(buf)
747 call term_wait(buf)
748 exe buf . 'bwipe'
749 unlet g:job
750endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200751
Bram Moolenaar7a760922018-02-19 23:10:02 +0100752func Test_terminal_wqall()
753 let buf = Run_shell_in_terminal({})
754 call assert_fails('wqall', 'E948')
755 call Stop_shell_in_terminal(buf)
756 call term_wait(buf)
757 exe buf . 'bwipe'
758 unlet g:job
759endfunc
760
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200761func Test_terminal_composing_unicode()
762 let save_enc = &encoding
763 set encoding=utf-8
764
765 if has('win32')
766 let cmd = "cmd /K chcp 65001"
767 let lnum = [3, 6, 9]
768 else
769 let cmd = &shell
770 let lnum = [1, 3, 5]
771 endif
772
773 enew
774 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100775 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200776 call term_wait(buf, 50)
777
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200778 if has('win32')
779 call assert_equal('cmd', job_info(g:job).cmd[0])
780 else
781 call assert_equal(&shell, job_info(g:job).cmd[0])
782 endif
783
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200784 " ascii + composing
785 let txt = "a\u0308bc"
786 call term_sendkeys(buf, "echo " . txt . "\r")
787 call term_wait(buf, 50)
788 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
789 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
790 let l = term_scrape(buf, lnum[0] + 1)
791 call assert_equal("a\u0308", l[0].chars)
792 call assert_equal("b", l[1].chars)
793 call assert_equal("c", l[2].chars)
794
795 " multibyte + composing
796 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
797 call term_sendkeys(buf, "echo " . txt . "\r")
798 call term_wait(buf, 50)
799 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
800 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
801 let l = term_scrape(buf, lnum[1] + 1)
802 call assert_equal("\u304b\u3099", l[0].chars)
803 call assert_equal("\u304e", l[1].chars)
804 call assert_equal("\u304f\u3099", l[2].chars)
805 call assert_equal("\u3052", l[3].chars)
806 call assert_equal("\u3053\u3099", l[4].chars)
807
808 " \u00a0 + composing
809 let txt = "abc\u00a0\u0308"
810 call term_sendkeys(buf, "echo " . txt . "\r")
811 call term_wait(buf, 50)
812 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
813 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
814 let l = term_scrape(buf, lnum[2] + 1)
815 call assert_equal("\u00a0\u0308", l[3].chars)
816
817 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200818 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200819 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100820 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200821 let &encoding = save_enc
822endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100823
824func Test_terminal_aucmd_on_close()
825 fun Nop()
826 let s:called = 1
827 endfun
828
829 aug repro
830 au!
831 au BufWinLeave * call Nop()
832 aug END
833
834 let [cmd, waittime] = s:get_sleep_cmd()
835
836 call assert_equal(1, winnr('$'))
837 new
838 call setline(1, ['one', 'two'])
839 exe 'term ++close ' . cmd
840 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200841 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100842 call assert_equal(1, s:called)
843 bwipe!
844
845 unlet s:called
846 au! repro
847 delfunc Nop
848endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100849
850func Test_terminal_term_start_empty_command()
851 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
852 call assert_fails(cmd, 'E474')
853 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
854 call assert_fails(cmd, 'E474')
855 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
856 call assert_fails(cmd, 'E474')
857 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
858 call assert_fails(cmd, 'E474')
859endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100860
861func Test_terminal_response_to_control_sequence()
862 if !has('unix')
863 return
864 endif
865
866 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200867 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100868
Bram Moolenaar086eb872018-03-25 21:24:12 +0200869 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200870 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100871
Bram Moolenaar086eb872018-03-25 21:24:12 +0200872 " Request the cursor position.
873 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100874
875 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200876 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100877
Bram Moolenaar086eb872018-03-25 21:24:12 +0200878 " End "cat" gently.
879 call term_sendkeys(buf, "\<CR>\<C-D>")
880
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100881 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100882 exe buf . 'bwipe'
883 unlet g:job
884endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100885
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100886" Run Vim, start a terminal in that Vim with the kill argument,
887" :qall works.
888func Run_terminal_qall_kill(line1, line2)
889 " 1. Open a terminal window and wait for the prompt to appear
890 " 2. set kill using term_setkill()
891 " 3. make Vim exit, it will kill the shell
892 let after = [
893 \ a:line1,
894 \ 'let buf = bufnr("%")',
895 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
896 \ ' sleep 10m',
897 \ 'endwhile',
898 \ a:line2,
899 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
900 \ 'qall',
901 \ ]
902 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100903 return
904 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100905 call assert_equal("done", readfile("Xdone")[0])
906 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100907endfunc
908
909" Run Vim in a terminal, then start a terminal in that Vim with a kill
910" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100911func Test_terminal_qall_kill_arg()
912 call Run_terminal_qall_kill('term ++kill=kill', '')
913endfunc
914
915" Run Vim, start a terminal in that Vim, set the kill argument with
916" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100917func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100918 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
919endfunc
920
921" Run Vim, start a terminal in that Vim without the kill argument,
922" check that :qall does not exit, :qall! does.
923func Test_terminal_qall_exit()
924 let after = [
925 \ 'term',
926 \ 'let buf = bufnr("%")',
927 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
928 \ ' sleep 10m',
929 \ 'endwhile',
930 \ 'set nomore',
931 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
932 \ 'qall',
933 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
934 \ 'cquit',
935 \ ]
936 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100937 return
938 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100939 call assert_equal("done", readfile("Xdone")[0])
940 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100941endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100942
943" Run Vim in a terminal, then start a terminal in that Vim without a kill
944" argument, check that :confirm qall works.
945func Test_terminal_qall_prompt()
946 if !CanRunVimInTerminal()
947 return
948 endif
949 let buf = RunVimInTerminal('', {})
950
951 " Open a terminal window and wait for the prompt to appear
952 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200953 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
954 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100955
956 " make Vim exit, it will prompt to kill the shell
957 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200958 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100959 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200960 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100961
962 " close the terminal window where Vim was running
963 quit
964endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100965
Bram Moolenaar012eb662018-03-13 17:55:27 +0100966func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100967 augroup repro
968 au!
969 au TerminalOpen * let s:called += 1
970 augroup END
971
972 let s:called = 0
973
974 " Open a terminal window with :terminal
975 terminal
976 call assert_equal(1, s:called)
977 bwipe!
978
979 " Open a terminal window with term_start()
980 call term_start(&shell)
981 call assert_equal(2, s:called)
982 bwipe!
983
984 " Open a hidden terminal buffer with :terminal
985 terminal ++hidden
986 call assert_equal(3, s:called)
987 for buf in term_list()
988 exe buf . "bwipe!"
989 endfor
990
991 " Open a hidden terminal buffer with term_start()
992 let buf = term_start(&shell, {'hidden': 1})
993 call assert_equal(4, s:called)
994 exe buf . "bwipe!"
995
996 unlet s:called
997 au! repro
998endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +0100999
1000func Check_dump01(off)
1001 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1002 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001003 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001004endfunc
1005
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001006func Test_terminal_dumpwrite_composing()
1007 if !CanRunVimInTerminal()
1008 return
1009 endif
1010 let save_enc = &encoding
1011 set encoding=utf-8
1012 call assert_equal(1, winnr('$'))
1013
1014 let text = " a\u0300 e\u0302 o\u0308"
1015 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001016 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001017 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001018 call term_dumpwrite(buf, 'Xdump')
1019 let dumpline = readfile('Xdump')[0]
1020 call assert_match('|à| |ê| |ö', dumpline)
1021
1022 call StopVimInTerminal(buf)
1023 call delete('Xcomposing')
1024 call delete('Xdump')
1025 let &encoding = save_enc
1026endfunc
1027
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001028" just testing basic functionality.
1029func Test_terminal_dumpload()
1030 call assert_equal(1, winnr('$'))
1031 call term_dumpload('dumps/Test_popup_command_01.dump')
1032 call assert_equal(2, winnr('$'))
1033 call assert_equal(20, line('$'))
1034 call Check_dump01(0)
1035 quit
1036endfunc
1037
1038func Test_terminal_dumpdiff()
1039 call assert_equal(1, winnr('$'))
1040 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1041 call assert_equal(2, winnr('$'))
1042 call assert_equal(62, line('$'))
1043 call Check_dump01(0)
1044 call Check_dump01(42)
1045 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1046 quit
1047endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001048
1049func Test_terminal_dumpdiff_options()
1050 set laststatus=0
1051 call assert_equal(1, winnr('$'))
1052 let height = winheight(0)
1053 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1054 call assert_equal(2, winnr('$'))
1055 call assert_equal(height, winheight(winnr()))
1056 call assert_equal(33, winwidth(winnr()))
1057 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1058 quit
1059
1060 call assert_equal(1, winnr('$'))
1061 let width = winwidth(0)
1062 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1063 call assert_equal(2, winnr('$'))
1064 call assert_equal(width, winwidth(winnr()))
1065 call assert_equal(13, winheight(winnr()))
1066 call assert_equal('something else', bufname('%'))
1067 quit
1068
1069 call assert_equal(1, winnr('$'))
1070 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1071 call assert_equal(1, winnr('$'))
1072 bwipe
1073
1074 set laststatus&
1075endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001076
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001077func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001078 call assert_equal(1, winnr('$'))
1079
1080 " Use the title termcap entries to output the escape sequence.
1081 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001082 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001083 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001084 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001085 \ 'redraw',
1086 \ "set t_ts=",
1087 \ ], 'Xscript')
1088 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001089 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001090 call assert_equal('Xtextfile', expand('%:t'))
1091 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001092 return buf
1093endfunc
1094
1095func Test_terminal_api_drop_newwin()
1096 if !CanRunVimInTerminal()
1097 return
1098 endif
1099 let buf = Api_drop_common('')
1100 call assert_equal(0, &bin)
1101 call assert_equal('', &fenc)
1102
1103 call StopVimInTerminal(buf)
1104 call delete('Xscript')
1105 bwipe Xtextfile
1106endfunc
1107
1108func Test_terminal_api_drop_newwin_bin()
1109 if !CanRunVimInTerminal()
1110 return
1111 endif
1112 let buf = Api_drop_common(',{"bin":1}')
1113 call assert_equal(1, &bin)
1114
1115 call StopVimInTerminal(buf)
1116 call delete('Xscript')
1117 bwipe Xtextfile
1118endfunc
1119
1120func Test_terminal_api_drop_newwin_binary()
1121 if !CanRunVimInTerminal()
1122 return
1123 endif
1124 let buf = Api_drop_common(',{"binary":1}')
1125 call assert_equal(1, &bin)
1126
1127 call StopVimInTerminal(buf)
1128 call delete('Xscript')
1129 bwipe Xtextfile
1130endfunc
1131
1132func Test_terminal_api_drop_newwin_nobin()
1133 if !CanRunVimInTerminal()
1134 return
1135 endif
1136 set binary
1137 let buf = Api_drop_common(',{"nobin":1}')
1138 call assert_equal(0, &bin)
1139
1140 call StopVimInTerminal(buf)
1141 call delete('Xscript')
1142 bwipe Xtextfile
1143 set nobinary
1144endfunc
1145
1146func Test_terminal_api_drop_newwin_nobinary()
1147 if !CanRunVimInTerminal()
1148 return
1149 endif
1150 set binary
1151 let buf = Api_drop_common(',{"nobinary":1}')
1152 call assert_equal(0, &bin)
1153
1154 call StopVimInTerminal(buf)
1155 call delete('Xscript')
1156 bwipe Xtextfile
1157 set nobinary
1158endfunc
1159
1160func Test_terminal_api_drop_newwin_ff()
1161 if !CanRunVimInTerminal()
1162 return
1163 endif
1164 let buf = Api_drop_common(',{"ff":"dos"}')
1165 call assert_equal("dos", &ff)
1166
1167 call StopVimInTerminal(buf)
1168 call delete('Xscript')
1169 bwipe Xtextfile
1170endfunc
1171
1172func Test_terminal_api_drop_newwin_fileformat()
1173 if !CanRunVimInTerminal()
1174 return
1175 endif
1176 let buf = Api_drop_common(',{"fileformat":"dos"}')
1177 call assert_equal("dos", &ff)
1178
1179 call StopVimInTerminal(buf)
1180 call delete('Xscript')
1181 bwipe Xtextfile
1182endfunc
1183
1184func Test_terminal_api_drop_newwin_enc()
1185 if !CanRunVimInTerminal()
1186 return
1187 endif
1188 let buf = Api_drop_common(',{"enc":"utf-16"}')
1189 call assert_equal("utf-16", &fenc)
1190
1191 call StopVimInTerminal(buf)
1192 call delete('Xscript')
1193 bwipe Xtextfile
1194endfunc
1195
1196func Test_terminal_api_drop_newwin_encoding()
1197 if !CanRunVimInTerminal()
1198 return
1199 endif
1200 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1201 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001202
1203 call StopVimInTerminal(buf)
1204 call delete('Xscript')
1205 bwipe Xtextfile
1206endfunc
1207
1208func Test_terminal_api_drop_oldwin()
1209 if !CanRunVimInTerminal()
1210 return
1211 endif
1212 let firstwinid = win_getid()
1213 split Xtextfile
1214 let textfile_winid = win_getid()
1215 call assert_equal(2, winnr('$'))
1216 call win_gotoid(firstwinid)
1217
1218 " Use the title termcap entries to output the escape sequence.
1219 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001220 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001221 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1222 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1223 \ 'redraw',
1224 \ "set t_ts=",
1225 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001226 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001227 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001228 call assert_equal(textfile_winid, win_getid())
1229
1230 call StopVimInTerminal(buf)
1231 call delete('Xscript')
1232 bwipe Xtextfile
1233endfunc
1234
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001235func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001236 let g:called_bufnum = a:bufnum
1237 let g:called_arg = a:arg
1238endfunc
1239
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001240func WriteApiCall(funcname)
1241 " Use the title termcap entries to output the escape sequence.
1242 call writefile([
1243 \ 'set title',
1244 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1245 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1246 \ 'redraw',
1247 \ "set t_ts=",
1248 \ ], 'Xscript')
1249endfunc
1250
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001251func Test_terminal_api_call()
1252 if !CanRunVimInTerminal()
1253 return
1254 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001255
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001256 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001257 let buf = RunVimInTerminal('-S Xscript', {})
1258 call WaitFor({-> exists('g:called_bufnum')})
1259 call assert_equal(buf, g:called_bufnum)
1260 call assert_equal(['hello', 123], g:called_arg)
1261
1262 call StopVimInTerminal(buf)
1263 call delete('Xscript')
1264 unlet g:called_bufnum
1265 unlet g:called_arg
1266endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001267
1268func Test_terminal_api_call_fails()
1269 if !CanRunVimInTerminal()
1270 return
1271 endif
1272
1273 call WriteApiCall('TryThis')
1274 call ch_logfile('Xlog', 'w')
1275 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001276 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001277
1278 call StopVimInTerminal(buf)
1279 call delete('Xscript')
1280 call ch_logfile('', '')
1281 call delete('Xlog')
1282endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001283
Bram Moolenaara997b452018-04-17 23:24:06 +02001284let s:caught_e937 = 0
1285
1286func Tapi_Delete(bufnum, arg)
1287 try
1288 execute 'bdelete!' a:bufnum
1289 catch /E937:/
1290 let s:caught_e937 = 1
1291 endtry
1292endfunc
1293
1294func Test_terminal_api_call_fail_delete()
1295 if !CanRunVimInTerminal()
1296 return
1297 endif
1298
1299 call WriteApiCall('Tapi_Delete')
1300 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001301 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001302
1303 call StopVimInTerminal(buf)
1304 call delete('Xscript')
1305 call ch_logfile('', '')
1306endfunc
1307
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001308func Test_terminal_ansicolors_default()
1309 let colors = [
1310 \ '#000000', '#e00000',
1311 \ '#00e000', '#e0e000',
1312 \ '#0000e0', '#e000e0',
1313 \ '#00e0e0', '#e0e0e0',
1314 \ '#808080', '#ff4040',
1315 \ '#40ff40', '#ffff40',
1316 \ '#4040ff', '#ff40ff',
1317 \ '#40ffff', '#ffffff',
1318 \]
1319
1320 let buf = Run_shell_in_terminal({})
1321 call assert_equal(colors, term_getansicolors(buf))
1322 call Stop_shell_in_terminal(buf)
1323 call term_wait(buf)
1324
1325 exe buf . 'bwipe'
1326endfunc
1327
1328let s:test_colors = [
1329 \ '#616e64', '#0d0a79',
1330 \ '#6d610d', '#0a7373',
1331 \ '#690d0a', '#6d696e',
1332 \ '#0d0a6f', '#616e0d',
1333 \ '#0a6479', '#6d0d0a',
1334 \ '#617373', '#0d0a69',
1335 \ '#6d690d', '#0a6e6f',
1336 \ '#610d0a', '#6e6479',
1337 \]
1338
1339func Test_terminal_ansicolors_global()
1340 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1341 let buf = Run_shell_in_terminal({})
1342 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1343 call Stop_shell_in_terminal(buf)
1344 call term_wait(buf)
1345
1346 exe buf . 'bwipe'
1347 unlet g:terminal_ansi_colors
1348endfunc
1349
1350func Test_terminal_ansicolors_func()
1351 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1352 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1353 call assert_equal(s:test_colors, term_getansicolors(buf))
1354
1355 call term_setansicolors(buf, g:terminal_ansi_colors)
1356 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1357
1358 let colors = [
1359 \ 'ivory', 'AliceBlue',
1360 \ 'grey67', 'dark goldenrod',
1361 \ 'SteelBlue3', 'PaleVioletRed4',
1362 \ 'MediumPurple2', 'yellow2',
1363 \ 'RosyBrown3', 'OrangeRed2',
1364 \ 'white smoke', 'navy blue',
1365 \ 'grey47', 'gray97',
1366 \ 'MistyRose2', 'DodgerBlue4',
1367 \]
1368 call term_setansicolors(buf, colors)
1369
1370 let colors[4] = 'Invalid'
1371 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1372
1373 call Stop_shell_in_terminal(buf)
1374 call term_wait(buf)
1375 exe buf . 'bwipe'
1376endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001377
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001378func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001379 if !CanRunVimInTerminal()
1380 return
1381 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001382 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001383 let text = []
1384 for n in range(10)
1385 call add(text, repeat(n, 50))
1386 endfor
1387 call writefile(text, 'Xwinsize')
1388 let buf = RunVimInTerminal('Xwinsize', {})
1389 let win = bufwinid(buf)
1390 call assert_equal([6, 40], term_getsize(buf))
1391 call assert_equal(6, winheight(win))
1392 call assert_equal(40, winwidth(win))
1393
1394 " resizing the window doesn't resize the terminal.
1395 resize 10
1396 vertical resize 60
1397 call assert_equal([6, 40], term_getsize(buf))
1398 call assert_equal(10, winheight(win))
1399 call assert_equal(60, winwidth(win))
1400
1401 call StopVimInTerminal(buf)
1402 call delete('Xwinsize')
1403
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001404 call assert_fails('set termwinsize=40', 'E474')
1405 call assert_fails('set termwinsize=10+40', 'E474')
1406 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001407
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001408 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001409endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001410
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001411func Test_terminal_termwinsize_option_zero()
1412 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001413 let buf = Run_shell_in_terminal({})
1414 let win = bufwinid(buf)
1415 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1416 call Stop_shell_in_terminal(buf)
1417 call term_wait(buf)
1418 exe buf . 'bwipe'
1419
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001420 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001421 let buf = Run_shell_in_terminal({})
1422 let win = bufwinid(buf)
1423 call assert_equal([7, winwidth(win)], term_getsize(buf))
1424 call Stop_shell_in_terminal(buf)
1425 call term_wait(buf)
1426 exe buf . 'bwipe'
1427
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001428 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001429 let buf = Run_shell_in_terminal({})
1430 let win = bufwinid(buf)
1431 call assert_equal([winheight(win), 33], term_getsize(buf))
1432 call Stop_shell_in_terminal(buf)
1433 call term_wait(buf)
1434 exe buf . 'bwipe'
1435
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001436 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001437endfunc
1438
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001439func Test_terminal_termwinsize_mininmum()
1440 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001441 vsplit
1442 let buf = Run_shell_in_terminal({})
1443 let win = bufwinid(buf)
1444 call assert_inrange(10, 1000, winheight(win))
1445 call assert_inrange(50, 1000, winwidth(win))
1446 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1447
1448 resize 15
1449 vertical resize 60
1450 redraw
1451 call assert_equal([15, 60], term_getsize(buf))
1452 call assert_equal(15, winheight(win))
1453 call assert_equal(60, winwidth(win))
1454
1455 resize 7
1456 vertical resize 30
1457 redraw
1458 call assert_equal([10, 50], term_getsize(buf))
1459 call assert_equal(7, winheight(win))
1460 call assert_equal(30, winwidth(win))
1461
1462 call Stop_shell_in_terminal(buf)
1463 call term_wait(buf)
1464 exe buf . 'bwipe'
1465
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001466 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001467 let buf = Run_shell_in_terminal({})
1468 let win = bufwinid(buf)
1469 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1470 call Stop_shell_in_terminal(buf)
1471 call term_wait(buf)
1472 exe buf . 'bwipe'
1473
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001474 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001475endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001476
1477func Test_terminal_termwinkey()
1478 call assert_equal(1, winnr('$'))
1479 let thiswin = win_getid()
1480
1481 let buf = Run_shell_in_terminal({})
1482 let termwin = bufwinid(buf)
1483 set termwinkey=<C-L>
1484 call feedkeys("\<C-L>w", 'tx')
1485 call assert_equal(thiswin, win_getid())
1486 call feedkeys("\<C-W>w", 'tx')
1487
1488 let job = term_getjob(buf)
1489 call feedkeys("\<C-L>\<C-C>", 'tx')
1490 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1491endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001492
1493func Test_terminal_out_err()
1494 if !has('unix')
1495 return
1496 endif
1497 call writefile([
1498 \ '#!/bin/sh',
1499 \ 'echo "this is standard error" >&2',
1500 \ 'echo "this is standard out" >&1',
1501 \ ], 'Xechoerrout.sh')
1502 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1503
1504 let outfile = 'Xtermstdout'
1505 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
1506 call WaitForAssert({-> assert_inrange(1, 2, len(readfile(outfile)))})
1507 call assert_equal("this is standard out", readfile(outfile)[0])
1508 call assert_equal('this is standard error', term_getline(buf, 1))
1509
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001510 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001511 exe buf . 'bwipe'
1512 call delete('Xechoerrout.sh')
1513 call delete(outfile)
1514endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001515
1516func Test_terminwinscroll()
1517 if !has('unix')
1518 return
1519 endif
1520
1521 " Let the terminal output more than 'termwinscroll' lines, some at the start
1522 " will be dropped.
1523 exe 'set termwinscroll=' . &lines
1524 let buf = term_start('/bin/sh')
1525 for i in range(1, &lines)
1526 call feedkeys("echo " . i . "\<CR>", 'xt')
1527 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1528 endfor
1529 " Go to Terminal-Normal mode to update the buffer.
1530 call feedkeys("\<C-W>N", 'xt')
1531 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1532
1533 " Every "echo nr" must only appear once
1534 let lines = getline(1, line('$'))
1535 for i in range(&lines - len(lines) / 2 + 2, &lines)
1536 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1537 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1538 endfor
1539
1540 exe buf . 'bwipe!'
1541endfunc