blob: 25ec2b5ca5791d7a72dd3832fe31ea76b00d66da [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 Moolenaar1e115362019-01-09 23:01:02 +0100125func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200126 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)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100162 if has('win32')
163 " On Windows 'background' always defaults to dark, even though the terminal
164 " may use a light background. Therefore accept both white and black.
165 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200166 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100167 if &background == 'light'
168 call assert_equal('#ffffff', l[0].bg)
169 else
170 call assert_equal('#000000', l[0].bg)
171 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200172 endif
173
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200174 let l = term_getline(a:buf, -1)
175 call assert_equal('', l)
176 let l = term_getline(a:buf, 0)
177 call assert_equal('', l)
178 let l = term_getline(a:buf, 999)
179 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200180 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200181 call assert_equal('123', l)
182endfunc
183
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200184func Test_terminal_scrape_123()
185 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200186 let buf = term_start(cmd)
187
188 let termlist = term_list()
189 call assert_equal(1, len(termlist))
190 call assert_equal(buf, termlist[0])
191
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200192 " Nothing happens with invalid buffer number
193 call term_wait(1234)
194
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200195 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200196 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200197 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200198 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199 call Check_123(buf)
200
201 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100202 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200203 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200204 call term_wait(buf)
205 call Check_123(buf)
206
207 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200208 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200209endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200210
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200211func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200212 call writefile(["léttまrs"], 'Xtext')
213 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200214 " Run cmd with UTF-8 codepage to make the type command print the expected
215 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100216 let buf = term_start("cmd /K chcp 65001")
217 call term_sendkeys(buf, "type Xtext\<CR>")
218 call term_sendkeys(buf, "exit\<CR>")
219 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100221 let buf = term_start("cat Xtext")
222 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200223 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200224
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100225 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
226 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200227 call assert_true(len(l) >= 7)
228 call assert_equal('l', l[0].chars)
229 call assert_equal('é', l[1].chars)
230 call assert_equal(1, l[1].width)
231 call assert_equal('t', l[2].chars)
232 call assert_equal('t', l[3].chars)
233 call assert_equal('ま', l[4].chars)
234 call assert_equal(2, l[4].width)
235 call assert_equal('r', l[5].chars)
236 call assert_equal('s', l[6].chars)
237
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100238 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200239 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100240 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200241
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100242 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200243 call delete('Xtext')
244endfunc
245
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200246func Test_terminal_scroll()
247 call writefile(range(1, 200), 'Xtext')
248 if has('win32')
249 let cmd = 'cmd /c "type Xtext"'
250 else
251 let cmd = "cat Xtext"
252 endif
253 let buf = term_start(cmd)
254
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100255 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200256 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200257 call term_wait(buf)
258 if has('win32')
259 " TODO: this should not be needed
260 sleep 100m
261 endif
262
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200263 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200264 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200265 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200266 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200267 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200268 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200269 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200270
271 exe buf . 'bwipe'
272 call delete('Xtext')
273endfunc
274
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200275func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200276 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200277 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200278 call writefile(range(150), 'Xtext')
279 if has('win32')
280 call term_sendkeys(buf, "type Xtext\<CR>")
281 else
282 call term_sendkeys(buf, "cat Xtext\<CR>")
283 endif
284 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200285 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200286 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200287 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200288 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200289
290 call Stop_shell_in_terminal(buf)
291 call term_wait(buf)
292 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200293 set termwinscroll&
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200294endfunc
295
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200296func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200297 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200298
Bram Moolenaarb2412082017-08-20 18:09:14 +0200299 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 let size = term_getsize('')
301 bwipe!
302 call assert_equal(5, size[0])
303
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200304 call term_start(cmd, {'term_rows': 6})
305 let size = term_getsize('')
306 bwipe!
307 call assert_equal(6, size[0])
308
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200309 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200310 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200311 call assert_equal([5, 33], term_getsize(''))
312
313 call term_setsize('', 6, 0)
314 call assert_equal([6, 33], term_getsize(''))
315
316 call term_setsize('', 0, 35)
317 call assert_equal([6, 35], term_getsize(''))
318
319 call term_setsize('', 7, 30)
320 call assert_equal([7, 30], term_getsize(''))
321
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200322 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200323 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200324
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200325 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
326 let size = term_getsize('')
327 bwipe!
328 call assert_equal([6, 36], size)
329
Bram Moolenaarb2412082017-08-20 18:09:14 +0200330 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200331 let size = term_getsize('')
332 bwipe!
333 call assert_equal(20, size[1])
334
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200335 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
336 let size = term_getsize('')
337 bwipe!
338 call assert_equal(26, size[1])
339
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200340 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200341 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200342 let size = term_getsize('')
343 bwipe!
344 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200345
346 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
347 let size = term_getsize('')
348 bwipe!
349 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200350
351 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200352endfunc
353
354func Test_terminal_curwin()
355 let cmd = Get_cat_123_cmd()
356 call assert_equal(1, winnr('$'))
357
358 split dummy
359 exe 'terminal ++curwin ' . cmd
360 call assert_equal(2, winnr('$'))
361 bwipe!
362
363 split dummy
364 call term_start(cmd, {'curwin': 1})
365 call assert_equal(2, winnr('$'))
366 bwipe!
367
368 split dummy
369 call setline(1, 'change')
370 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
371 call assert_equal(2, winnr('$'))
372 exe 'terminal! ++curwin ' . cmd
373 call assert_equal(2, winnr('$'))
374 bwipe!
375
376 split dummy
377 call setline(1, 'change')
378 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
379 call assert_equal(2, winnr('$'))
380 bwipe!
381
382 split dummy
383 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200384 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200385endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200386
Bram Moolenaarff546792017-11-21 14:47:57 +0100387func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200388 if s:python != ''
389 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200390 " 500 was not enough for Travis
391 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200392 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200393 echo 'This will take five seconds...'
394 let waittime = 2000
395 if has('win32')
396 let cmd = $windir . '\system32\timeout.exe 1'
397 else
398 let cmd = 'sleep 1'
399 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200400 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100401 return [cmd, waittime]
402endfunc
403
404func Test_terminal_finish_open_close()
405 call assert_equal(1, winnr('$'))
406
407 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200408
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100409 " shell terminal closes automatically
410 terminal
411 let buf = bufnr('%')
412 call assert_equal(2, winnr('$'))
413 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200414 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100415 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200416 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100417
418 " shell terminal that does not close automatically
419 terminal ++noclose
420 let buf = bufnr('%')
421 call assert_equal(2, winnr('$'))
422 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200423 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100424 call Stop_shell_in_terminal(buf)
425 call assert_equal(2, winnr('$'))
426 quit
427 call assert_equal(1, winnr('$'))
428
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200429 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200430 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200431 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200432 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433
434 call term_start(cmd, {'term_finish': 'close'})
435 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200436 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200437 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200438 call assert_equal(1, winnr('$'))
439
440 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200441 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200442 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200443 bwipe
444
445 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200446 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200447 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200448 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200449
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200450 exe 'terminal ++hidden ++open ' . cmd
451 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200452 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200453 bwipe
454
455 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
456 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200457 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200458 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200459
460 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
461 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
462 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
463 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
464
465 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200466 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200467 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200468 call assert_equal(4, winheight(0))
469 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200470endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200471
472func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200473 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200474 return
475 endif
476 call mkdir('Xdir')
477 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200478 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200479
480 exe buf . 'bwipe'
481 call delete('Xdir', 'rf')
482endfunc
483
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200484func Test_terminal_cwd_failure()
485 " Case 1: Provided directory is not actually a directory. Attempt to make
486 " the file executable as well.
487 call writefile([], 'Xfile')
488 call setfperm('Xfile', 'rwx------')
489 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
490 call delete('Xfile')
491
492 " Case 2: Directory does not exist.
493 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
494
495 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100496 " Skip this for root, it will be accessible anyway.
497 if $USER != 'root'
498 call mkdir('XdirNoAccess', '', '0600')
499 " return early if the directory permissions could not be set properly
500 if getfperm('XdirNoAccess')[2] == 'x'
501 call delete('XdirNoAccess', 'rf')
502 return
503 endif
504 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
505 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200506 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200507endfunc
508
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100509func Test_terminal_servername()
510 if !has('clientserver')
511 return
512 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200513 call s:test_environment("VIM_SERVERNAME", v:servername)
514endfunc
515
516func Test_terminal_version()
517 call s:test_environment("VIM_TERMINAL", string(v:version))
518endfunc
519
520func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100521 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100522 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200523 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100524 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200525 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100526 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200527 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100528 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100529 call term_wait(buf)
530 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200531 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100532
Bram Moolenaar012eb662018-03-13 17:55:27 +0100533 exe buf . 'bwipe'
534 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100535endfunc
536
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200537func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100538 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200539 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200540 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100541 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100542 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100543 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100544 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100545 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100546 call term_wait(buf)
547 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200548 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200549
Bram Moolenaar012eb662018-03-13 17:55:27 +0100550 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200551endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200552
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200553func Test_terminal_list_args()
554 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
555 call assert_fails(buf . 'bwipe', 'E517')
556 exe buf . 'bwipe!'
557 call assert_equal("", bufname(buf))
558endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200559
560func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100561 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200562 if has('mac')
563 " The shell or something else has a problem dealing with more than 1000
564 " characters at the same time.
565 let len = 1000
566 else
567 let len = 5000
568 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200569
570 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100571 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200572 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100573 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200574
575 " On MS-Windows there is an extra empty line below "done". Find "done" in
576 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100577 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100578 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100579 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200580 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100581 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200582 endif
583 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200584
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100585 let g:job = term_getjob(buf)
586 call Stop_shell_in_terminal(buf)
587 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200588 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200589 bwipe
590endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200591
592func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200593 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200594 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200595 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200596 if has('win32')
597 " TODO: enable once writing to stdin works on MS-Windows
598 return
599 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200600 new
601 call setline(1, ['one', 'two', 'three'])
602 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200603 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200604 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200605 call assert_equal(['3', '3', '14'], nrs)
606 bwipe
607
Bram Moolenaardada6d22017-09-02 17:18:35 +0200608 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200609 call setline(1, ['one', 'two', 'three', 'four'])
610 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200611 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200612 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200613 call assert_equal(['2', '2', '10'], nrs)
614 bwipe
615
Bram Moolenaardada6d22017-09-02 17:18:35 +0200616 if executable('python')
617 new
618 call setline(1, ['print("hello")'])
619 1term ++eof=exit() python
620 " MS-Windows echoes the input, Unix doesn't.
621 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
622 if getline(1) =~ 'hello'
623 call assert_equal('hello', getline(1))
624 else
625 call assert_equal('hello', getline(line('$') - 1))
626 endif
627 bwipe
628
629 if has('win32')
630 new
631 call setline(1, ['print("hello")'])
632 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200633 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200634 call assert_equal('hello', getline(line('$') - 1))
635 bwipe
636 endif
637 endif
638
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200639 bwipe!
640endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200641
642func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200643 let buf = term_start('NONE', {})
644 call assert_notequal(0, buf)
645
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200646 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200647 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100648 if has('gui_running') && !has('win32')
649 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200650 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100651 else
652 " Otherwise using a job works on all systems.
653 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200654 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200655 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200656
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200657 bwipe!
658endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200659
660func Test_terminal_special_chars()
661 " this file name only works on Unix
662 if !has('unix')
663 return
664 endif
665 call mkdir('Xdir with spaces')
666 call writefile(['x'], 'Xdir with spaces/quoted"file')
667 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200668 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200669 call term_wait('')
670
671 call delete('Xdir with spaces', 'rf')
672 bwipe
673endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200674
675func Test_terminal_wrong_options()
676 call assert_fails('call term_start(&shell, {
677 \ "in_io": "file",
678 \ "in_name": "xxx",
679 \ "out_io": "file",
680 \ "out_name": "xxx",
681 \ "err_io": "file",
682 \ "err_name": "xxx"
683 \ })', 'E474:')
684 call assert_fails('call term_start(&shell, {
685 \ "out_buf": bufnr("%")
686 \ })', 'E474:')
687 call assert_fails('call term_start(&shell, {
688 \ "err_buf": bufnr("%")
689 \ })', 'E474:')
690endfunc
691
692func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200693 let cmd = Get_cat_123_cmd()
694 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
695 call term_wait(buf)
696 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
697 call assert_match('123', readfile('Xfile')[0])
698 let g:job = term_getjob(buf)
699 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
700 call delete('Xfile')
701 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200702
703 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200704 call writefile(['one line'], 'Xfile')
705 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
706 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200707 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200708 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200709 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200710 bwipe
711 call delete('Xfile')
712 endif
713endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200714
715func TerminalTmap(remap)
716 let buf = Run_shell_in_terminal({})
717 call assert_equal('t', mode())
718
719 if a:remap
720 tmap 123 456
721 else
722 tnoremap 123 456
723 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100724 " don't use abcde, it's an existing command
725 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200726 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100727 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200728 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200729 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200730 let lnum = term_getcursor(buf)[0]
731 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100732 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200733 else
734 call assert_match('456', term_getline(buf, lnum))
735 endif
736
737 call term_sendkeys(buf, "\r")
738 call Stop_shell_in_terminal(buf)
739 call term_wait(buf)
740
741 tunmap 123
742 tunmap 456
743 call assert_equal('', maparg('123', 't'))
744 close
745 unlet g:job
746endfunc
747
748func Test_terminal_tmap()
749 call TerminalTmap(1)
750 call TerminalTmap(0)
751endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200752
753func Test_terminal_wall()
754 let buf = Run_shell_in_terminal({})
755 wall
756 call Stop_shell_in_terminal(buf)
757 call term_wait(buf)
758 exe buf . 'bwipe'
759 unlet g:job
760endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200761
Bram Moolenaar7a760922018-02-19 23:10:02 +0100762func Test_terminal_wqall()
763 let buf = Run_shell_in_terminal({})
764 call assert_fails('wqall', 'E948')
765 call Stop_shell_in_terminal(buf)
766 call term_wait(buf)
767 exe buf . 'bwipe'
768 unlet g:job
769endfunc
770
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200771func Test_terminal_composing_unicode()
772 let save_enc = &encoding
773 set encoding=utf-8
774
775 if has('win32')
776 let cmd = "cmd /K chcp 65001"
777 let lnum = [3, 6, 9]
778 else
779 let cmd = &shell
780 let lnum = [1, 3, 5]
781 endif
782
783 enew
784 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100785 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200786 call term_wait(buf, 50)
787
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200788 if has('win32')
789 call assert_equal('cmd', job_info(g:job).cmd[0])
790 else
791 call assert_equal(&shell, job_info(g:job).cmd[0])
792 endif
793
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200794 " ascii + composing
795 let txt = "a\u0308bc"
796 call term_sendkeys(buf, "echo " . txt . "\r")
797 call term_wait(buf, 50)
798 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
799 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
800 let l = term_scrape(buf, lnum[0] + 1)
801 call assert_equal("a\u0308", l[0].chars)
802 call assert_equal("b", l[1].chars)
803 call assert_equal("c", l[2].chars)
804
805 " multibyte + composing
806 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
807 call term_sendkeys(buf, "echo " . txt . "\r")
808 call term_wait(buf, 50)
809 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
810 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
811 let l = term_scrape(buf, lnum[1] + 1)
812 call assert_equal("\u304b\u3099", l[0].chars)
813 call assert_equal("\u304e", l[1].chars)
814 call assert_equal("\u304f\u3099", l[2].chars)
815 call assert_equal("\u3052", l[3].chars)
816 call assert_equal("\u3053\u3099", l[4].chars)
817
818 " \u00a0 + composing
819 let txt = "abc\u00a0\u0308"
820 call term_sendkeys(buf, "echo " . txt . "\r")
821 call term_wait(buf, 50)
822 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
823 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
824 let l = term_scrape(buf, lnum[2] + 1)
825 call assert_equal("\u00a0\u0308", l[3].chars)
826
827 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200828 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200829 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100830 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200831 let &encoding = save_enc
832endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100833
834func Test_terminal_aucmd_on_close()
835 fun Nop()
836 let s:called = 1
837 endfun
838
839 aug repro
840 au!
841 au BufWinLeave * call Nop()
842 aug END
843
844 let [cmd, waittime] = s:get_sleep_cmd()
845
846 call assert_equal(1, winnr('$'))
847 new
848 call setline(1, ['one', 'two'])
849 exe 'term ++close ' . cmd
850 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200851 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100852 call assert_equal(1, s:called)
853 bwipe!
854
855 unlet s:called
856 au! repro
857 delfunc Nop
858endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100859
860func Test_terminal_term_start_empty_command()
861 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
862 call assert_fails(cmd, 'E474')
863 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
864 call assert_fails(cmd, 'E474')
865 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
866 call assert_fails(cmd, 'E474')
867 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
868 call assert_fails(cmd, 'E474')
869endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100870
871func Test_terminal_response_to_control_sequence()
872 if !has('unix')
873 return
874 endif
875
876 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200877 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100878
Bram Moolenaar086eb872018-03-25 21:24:12 +0200879 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200880 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100881
Bram Moolenaar086eb872018-03-25 21:24:12 +0200882 " Request the cursor position.
883 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100884
885 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200886 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100887
Bram Moolenaar086eb872018-03-25 21:24:12 +0200888 " End "cat" gently.
889 call term_sendkeys(buf, "\<CR>\<C-D>")
890
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100891 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100892 exe buf . 'bwipe'
893 unlet g:job
894endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100895
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100896" Run Vim, start a terminal in that Vim with the kill argument,
897" :qall works.
898func Run_terminal_qall_kill(line1, line2)
899 " 1. Open a terminal window and wait for the prompt to appear
900 " 2. set kill using term_setkill()
901 " 3. make Vim exit, it will kill the shell
902 let after = [
903 \ a:line1,
904 \ 'let buf = bufnr("%")',
905 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
906 \ ' sleep 10m',
907 \ 'endwhile',
908 \ a:line2,
909 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
910 \ 'qall',
911 \ ]
912 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100913 return
914 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100915 call assert_equal("done", readfile("Xdone")[0])
916 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100917endfunc
918
919" Run Vim in a terminal, then start a terminal in that Vim with a kill
920" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100921func Test_terminal_qall_kill_arg()
922 call Run_terminal_qall_kill('term ++kill=kill', '')
923endfunc
924
925" Run Vim, start a terminal in that Vim, set the kill argument with
926" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100927func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100928 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
929endfunc
930
931" Run Vim, start a terminal in that Vim without the kill argument,
932" check that :qall does not exit, :qall! does.
933func Test_terminal_qall_exit()
934 let after = [
935 \ 'term',
936 \ 'let buf = bufnr("%")',
937 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
938 \ ' sleep 10m',
939 \ 'endwhile',
940 \ 'set nomore',
941 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
942 \ 'qall',
943 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
944 \ 'cquit',
945 \ ]
946 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100947 return
948 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100949 call assert_equal("done", readfile("Xdone")[0])
950 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100951endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100952
953" Run Vim in a terminal, then start a terminal in that Vim without a kill
954" argument, check that :confirm qall works.
955func Test_terminal_qall_prompt()
956 if !CanRunVimInTerminal()
957 return
958 endif
959 let buf = RunVimInTerminal('', {})
960
961 " Open a terminal window and wait for the prompt to appear
962 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200963 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
964 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100965
966 " make Vim exit, it will prompt to kill the shell
967 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200968 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100969 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200970 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100971
972 " close the terminal window where Vim was running
973 quit
974endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100975
Bram Moolenaar012eb662018-03-13 17:55:27 +0100976func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100977 augroup repro
978 au!
979 au TerminalOpen * let s:called += 1
980 augroup END
981
982 let s:called = 0
983
984 " Open a terminal window with :terminal
985 terminal
986 call assert_equal(1, s:called)
987 bwipe!
988
989 " Open a terminal window with term_start()
990 call term_start(&shell)
991 call assert_equal(2, s:called)
992 bwipe!
993
994 " Open a hidden terminal buffer with :terminal
995 terminal ++hidden
996 call assert_equal(3, s:called)
997 for buf in term_list()
998 exe buf . "bwipe!"
999 endfor
1000
1001 " Open a hidden terminal buffer with term_start()
1002 let buf = term_start(&shell, {'hidden': 1})
1003 call assert_equal(4, s:called)
1004 exe buf . "bwipe!"
1005
1006 unlet s:called
1007 au! repro
1008endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001009
1010func Check_dump01(off)
1011 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1012 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001013 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001014endfunc
1015
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001016func Test_terminal_dumpwrite_composing()
1017 if !CanRunVimInTerminal()
1018 return
1019 endif
1020 let save_enc = &encoding
1021 set encoding=utf-8
1022 call assert_equal(1, winnr('$'))
1023
1024 let text = " a\u0300 e\u0302 o\u0308"
1025 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001026 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001027 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001028 call term_dumpwrite(buf, 'Xdump')
1029 let dumpline = readfile('Xdump')[0]
1030 call assert_match('|à| |ê| |ö', dumpline)
1031
1032 call StopVimInTerminal(buf)
1033 call delete('Xcomposing')
1034 call delete('Xdump')
1035 let &encoding = save_enc
1036endfunc
1037
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001038" just testing basic functionality.
1039func Test_terminal_dumpload()
1040 call assert_equal(1, winnr('$'))
1041 call term_dumpload('dumps/Test_popup_command_01.dump')
1042 call assert_equal(2, winnr('$'))
1043 call assert_equal(20, line('$'))
1044 call Check_dump01(0)
1045 quit
1046endfunc
1047
1048func Test_terminal_dumpdiff()
1049 call assert_equal(1, winnr('$'))
1050 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1051 call assert_equal(2, winnr('$'))
1052 call assert_equal(62, line('$'))
1053 call Check_dump01(0)
1054 call Check_dump01(42)
1055 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1056 quit
1057endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001058
1059func Test_terminal_dumpdiff_options()
1060 set laststatus=0
1061 call assert_equal(1, winnr('$'))
1062 let height = winheight(0)
1063 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1064 call assert_equal(2, winnr('$'))
1065 call assert_equal(height, winheight(winnr()))
1066 call assert_equal(33, winwidth(winnr()))
1067 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1068 quit
1069
1070 call assert_equal(1, winnr('$'))
1071 let width = winwidth(0)
1072 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1073 call assert_equal(2, winnr('$'))
1074 call assert_equal(width, winwidth(winnr()))
1075 call assert_equal(13, winheight(winnr()))
1076 call assert_equal('something else', bufname('%'))
1077 quit
1078
1079 call assert_equal(1, winnr('$'))
1080 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1081 call assert_equal(1, winnr('$'))
1082 bwipe
1083
1084 set laststatus&
1085endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001086
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001087func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001088 call assert_equal(1, winnr('$'))
1089
1090 " Use the title termcap entries to output the escape sequence.
1091 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001092 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001093 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001094 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001095 \ 'redraw',
1096 \ "set t_ts=",
1097 \ ], 'Xscript')
1098 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001099 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001100 call assert_equal('Xtextfile', expand('%:t'))
1101 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001102 return buf
1103endfunc
1104
1105func Test_terminal_api_drop_newwin()
1106 if !CanRunVimInTerminal()
1107 return
1108 endif
1109 let buf = Api_drop_common('')
1110 call assert_equal(0, &bin)
1111 call assert_equal('', &fenc)
1112
1113 call StopVimInTerminal(buf)
1114 call delete('Xscript')
1115 bwipe Xtextfile
1116endfunc
1117
1118func Test_terminal_api_drop_newwin_bin()
1119 if !CanRunVimInTerminal()
1120 return
1121 endif
1122 let buf = Api_drop_common(',{"bin":1}')
1123 call assert_equal(1, &bin)
1124
1125 call StopVimInTerminal(buf)
1126 call delete('Xscript')
1127 bwipe Xtextfile
1128endfunc
1129
1130func Test_terminal_api_drop_newwin_binary()
1131 if !CanRunVimInTerminal()
1132 return
1133 endif
1134 let buf = Api_drop_common(',{"binary":1}')
1135 call assert_equal(1, &bin)
1136
1137 call StopVimInTerminal(buf)
1138 call delete('Xscript')
1139 bwipe Xtextfile
1140endfunc
1141
1142func Test_terminal_api_drop_newwin_nobin()
1143 if !CanRunVimInTerminal()
1144 return
1145 endif
1146 set binary
1147 let buf = Api_drop_common(',{"nobin":1}')
1148 call assert_equal(0, &bin)
1149
1150 call StopVimInTerminal(buf)
1151 call delete('Xscript')
1152 bwipe Xtextfile
1153 set nobinary
1154endfunc
1155
1156func Test_terminal_api_drop_newwin_nobinary()
1157 if !CanRunVimInTerminal()
1158 return
1159 endif
1160 set binary
1161 let buf = Api_drop_common(',{"nobinary":1}')
1162 call assert_equal(0, &bin)
1163
1164 call StopVimInTerminal(buf)
1165 call delete('Xscript')
1166 bwipe Xtextfile
1167 set nobinary
1168endfunc
1169
1170func Test_terminal_api_drop_newwin_ff()
1171 if !CanRunVimInTerminal()
1172 return
1173 endif
1174 let buf = Api_drop_common(',{"ff":"dos"}')
1175 call assert_equal("dos", &ff)
1176
1177 call StopVimInTerminal(buf)
1178 call delete('Xscript')
1179 bwipe Xtextfile
1180endfunc
1181
1182func Test_terminal_api_drop_newwin_fileformat()
1183 if !CanRunVimInTerminal()
1184 return
1185 endif
1186 let buf = Api_drop_common(',{"fileformat":"dos"}')
1187 call assert_equal("dos", &ff)
1188
1189 call StopVimInTerminal(buf)
1190 call delete('Xscript')
1191 bwipe Xtextfile
1192endfunc
1193
1194func Test_terminal_api_drop_newwin_enc()
1195 if !CanRunVimInTerminal()
1196 return
1197 endif
1198 let buf = Api_drop_common(',{"enc":"utf-16"}')
1199 call assert_equal("utf-16", &fenc)
1200
1201 call StopVimInTerminal(buf)
1202 call delete('Xscript')
1203 bwipe Xtextfile
1204endfunc
1205
1206func Test_terminal_api_drop_newwin_encoding()
1207 if !CanRunVimInTerminal()
1208 return
1209 endif
1210 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1211 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001212
1213 call StopVimInTerminal(buf)
1214 call delete('Xscript')
1215 bwipe Xtextfile
1216endfunc
1217
1218func Test_terminal_api_drop_oldwin()
1219 if !CanRunVimInTerminal()
1220 return
1221 endif
1222 let firstwinid = win_getid()
1223 split Xtextfile
1224 let textfile_winid = win_getid()
1225 call assert_equal(2, winnr('$'))
1226 call win_gotoid(firstwinid)
1227
1228 " Use the title termcap entries to output the escape sequence.
1229 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001230 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001231 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1232 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1233 \ 'redraw',
1234 \ "set t_ts=",
1235 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001236 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001237 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001238 call assert_equal(textfile_winid, win_getid())
1239
1240 call StopVimInTerminal(buf)
1241 call delete('Xscript')
1242 bwipe Xtextfile
1243endfunc
1244
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001245func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001246 let g:called_bufnum = a:bufnum
1247 let g:called_arg = a:arg
1248endfunc
1249
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001250func WriteApiCall(funcname)
1251 " Use the title termcap entries to output the escape sequence.
1252 call writefile([
1253 \ 'set title',
1254 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1255 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1256 \ 'redraw',
1257 \ "set t_ts=",
1258 \ ], 'Xscript')
1259endfunc
1260
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001261func Test_terminal_api_call()
1262 if !CanRunVimInTerminal()
1263 return
1264 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001265
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001266 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001267 let buf = RunVimInTerminal('-S Xscript', {})
1268 call WaitFor({-> exists('g:called_bufnum')})
1269 call assert_equal(buf, g:called_bufnum)
1270 call assert_equal(['hello', 123], g:called_arg)
1271
1272 call StopVimInTerminal(buf)
1273 call delete('Xscript')
1274 unlet g:called_bufnum
1275 unlet g:called_arg
1276endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001277
1278func Test_terminal_api_call_fails()
1279 if !CanRunVimInTerminal()
1280 return
1281 endif
1282
1283 call WriteApiCall('TryThis')
1284 call ch_logfile('Xlog', 'w')
1285 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001286 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001287
1288 call StopVimInTerminal(buf)
1289 call delete('Xscript')
1290 call ch_logfile('', '')
1291 call delete('Xlog')
1292endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001293
Bram Moolenaara997b452018-04-17 23:24:06 +02001294let s:caught_e937 = 0
1295
1296func Tapi_Delete(bufnum, arg)
1297 try
1298 execute 'bdelete!' a:bufnum
1299 catch /E937:/
1300 let s:caught_e937 = 1
1301 endtry
1302endfunc
1303
1304func Test_terminal_api_call_fail_delete()
1305 if !CanRunVimInTerminal()
1306 return
1307 endif
1308
1309 call WriteApiCall('Tapi_Delete')
1310 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001311 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001312
1313 call StopVimInTerminal(buf)
1314 call delete('Xscript')
1315 call ch_logfile('', '')
1316endfunc
1317
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001318func Test_terminal_ansicolors_default()
1319 let colors = [
1320 \ '#000000', '#e00000',
1321 \ '#00e000', '#e0e000',
1322 \ '#0000e0', '#e000e0',
1323 \ '#00e0e0', '#e0e0e0',
1324 \ '#808080', '#ff4040',
1325 \ '#40ff40', '#ffff40',
1326 \ '#4040ff', '#ff40ff',
1327 \ '#40ffff', '#ffffff',
1328 \]
1329
1330 let buf = Run_shell_in_terminal({})
1331 call assert_equal(colors, term_getansicolors(buf))
1332 call Stop_shell_in_terminal(buf)
1333 call term_wait(buf)
1334
1335 exe buf . 'bwipe'
1336endfunc
1337
1338let s:test_colors = [
1339 \ '#616e64', '#0d0a79',
1340 \ '#6d610d', '#0a7373',
1341 \ '#690d0a', '#6d696e',
1342 \ '#0d0a6f', '#616e0d',
1343 \ '#0a6479', '#6d0d0a',
1344 \ '#617373', '#0d0a69',
1345 \ '#6d690d', '#0a6e6f',
1346 \ '#610d0a', '#6e6479',
1347 \]
1348
1349func Test_terminal_ansicolors_global()
1350 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1351 let buf = Run_shell_in_terminal({})
1352 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1353 call Stop_shell_in_terminal(buf)
1354 call term_wait(buf)
1355
1356 exe buf . 'bwipe'
1357 unlet g:terminal_ansi_colors
1358endfunc
1359
1360func Test_terminal_ansicolors_func()
1361 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1362 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1363 call assert_equal(s:test_colors, term_getansicolors(buf))
1364
1365 call term_setansicolors(buf, g:terminal_ansi_colors)
1366 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1367
1368 let colors = [
1369 \ 'ivory', 'AliceBlue',
1370 \ 'grey67', 'dark goldenrod',
1371 \ 'SteelBlue3', 'PaleVioletRed4',
1372 \ 'MediumPurple2', 'yellow2',
1373 \ 'RosyBrown3', 'OrangeRed2',
1374 \ 'white smoke', 'navy blue',
1375 \ 'grey47', 'gray97',
1376 \ 'MistyRose2', 'DodgerBlue4',
1377 \]
1378 call term_setansicolors(buf, colors)
1379
1380 let colors[4] = 'Invalid'
1381 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1382
1383 call Stop_shell_in_terminal(buf)
1384 call term_wait(buf)
1385 exe buf . 'bwipe'
1386endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001387
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001388func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001389 if !CanRunVimInTerminal()
1390 return
1391 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001392 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001393 let text = []
1394 for n in range(10)
1395 call add(text, repeat(n, 50))
1396 endfor
1397 call writefile(text, 'Xwinsize')
1398 let buf = RunVimInTerminal('Xwinsize', {})
1399 let win = bufwinid(buf)
1400 call assert_equal([6, 40], term_getsize(buf))
1401 call assert_equal(6, winheight(win))
1402 call assert_equal(40, winwidth(win))
1403
1404 " resizing the window doesn't resize the terminal.
1405 resize 10
1406 vertical resize 60
1407 call assert_equal([6, 40], term_getsize(buf))
1408 call assert_equal(10, winheight(win))
1409 call assert_equal(60, winwidth(win))
1410
1411 call StopVimInTerminal(buf)
1412 call delete('Xwinsize')
1413
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001414 call assert_fails('set termwinsize=40', 'E474')
1415 call assert_fails('set termwinsize=10+40', 'E474')
1416 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001417
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001418 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001419endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001420
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001421func Test_terminal_termwinsize_option_zero()
1422 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001423 let buf = Run_shell_in_terminal({})
1424 let win = bufwinid(buf)
1425 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1426 call Stop_shell_in_terminal(buf)
1427 call term_wait(buf)
1428 exe buf . 'bwipe'
1429
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001430 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001431 let buf = Run_shell_in_terminal({})
1432 let win = bufwinid(buf)
1433 call assert_equal([7, winwidth(win)], term_getsize(buf))
1434 call Stop_shell_in_terminal(buf)
1435 call term_wait(buf)
1436 exe buf . 'bwipe'
1437
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001438 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001439 let buf = Run_shell_in_terminal({})
1440 let win = bufwinid(buf)
1441 call assert_equal([winheight(win), 33], term_getsize(buf))
1442 call Stop_shell_in_terminal(buf)
1443 call term_wait(buf)
1444 exe buf . 'bwipe'
1445
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001446 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001447endfunc
1448
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001449func Test_terminal_termwinsize_mininmum()
1450 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001451 vsplit
1452 let buf = Run_shell_in_terminal({})
1453 let win = bufwinid(buf)
1454 call assert_inrange(10, 1000, winheight(win))
1455 call assert_inrange(50, 1000, winwidth(win))
1456 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1457
1458 resize 15
1459 vertical resize 60
1460 redraw
1461 call assert_equal([15, 60], term_getsize(buf))
1462 call assert_equal(15, winheight(win))
1463 call assert_equal(60, winwidth(win))
1464
1465 resize 7
1466 vertical resize 30
1467 redraw
1468 call assert_equal([10, 50], term_getsize(buf))
1469 call assert_equal(7, winheight(win))
1470 call assert_equal(30, winwidth(win))
1471
1472 call Stop_shell_in_terminal(buf)
1473 call term_wait(buf)
1474 exe buf . 'bwipe'
1475
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001476 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001477 let buf = Run_shell_in_terminal({})
1478 let win = bufwinid(buf)
1479 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1480 call Stop_shell_in_terminal(buf)
1481 call term_wait(buf)
1482 exe buf . 'bwipe'
1483
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001484 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001485endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001486
1487func Test_terminal_termwinkey()
1488 call assert_equal(1, winnr('$'))
1489 let thiswin = win_getid()
1490
1491 let buf = Run_shell_in_terminal({})
1492 let termwin = bufwinid(buf)
1493 set termwinkey=<C-L>
1494 call feedkeys("\<C-L>w", 'tx')
1495 call assert_equal(thiswin, win_getid())
1496 call feedkeys("\<C-W>w", 'tx')
1497
1498 let job = term_getjob(buf)
1499 call feedkeys("\<C-L>\<C-C>", 'tx')
1500 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1501endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001502
1503func Test_terminal_out_err()
1504 if !has('unix')
1505 return
1506 endif
1507 call writefile([
1508 \ '#!/bin/sh',
1509 \ 'echo "this is standard error" >&2',
1510 \ 'echo "this is standard out" >&1',
1511 \ ], 'Xechoerrout.sh')
1512 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1513
1514 let outfile = 'Xtermstdout'
1515 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001516
1517 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1518 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001519 call assert_equal('this is standard error', term_getline(buf, 1))
1520
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001521 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001522 exe buf . 'bwipe'
1523 call delete('Xechoerrout.sh')
1524 call delete(outfile)
1525endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001526
1527func Test_terminwinscroll()
1528 if !has('unix')
1529 return
1530 endif
1531
1532 " Let the terminal output more than 'termwinscroll' lines, some at the start
1533 " will be dropped.
1534 exe 'set termwinscroll=' . &lines
1535 let buf = term_start('/bin/sh')
1536 for i in range(1, &lines)
1537 call feedkeys("echo " . i . "\<CR>", 'xt')
1538 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1539 endfor
1540 " Go to Terminal-Normal mode to update the buffer.
1541 call feedkeys("\<C-W>N", 'xt')
1542 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1543
1544 " Every "echo nr" must only appear once
1545 let lines = getline(1, line('$'))
1546 for i in range(&lines - len(lines) / 2 + 2, &lines)
1547 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1548 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1549 endfor
1550
1551 exe buf . 'bwipe!'
1552endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001553
Bram Moolenaar875cf872018-07-08 20:49:07 +02001554" Resizing the terminal window caused an ml_get error.
1555" TODO: This does not reproduce the original problem.
1556func Test_terminal_resize()
1557 set statusline=x
1558 terminal
1559 call assert_equal(2, winnr('$'))
1560
1561 " Fill the terminal with text.
1562 if has('win32')
1563 call feedkeys("dir\<CR>", 'xt')
1564 else
1565 call feedkeys("ls\<CR>", 'xt')
1566 endif
1567 " Go to Terminal-Normal mode for a moment.
1568 call feedkeys("\<C-W>N", 'xt')
1569 " Open a new window
1570 call feedkeys("i\<C-W>n", 'xt')
1571 call assert_equal(3, winnr('$'))
1572 redraw
1573
1574 close
1575 call assert_equal(2, winnr('$'))
1576 call feedkeys("exit\<CR>", 'xt')
1577 set statusline&
1578endfunc
1579
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001580" must be nearly the last, we can't go back from GUI to terminal
1581func Test_zz1_terminal_in_gui()
1582 if !CanRunGui()
1583 return
1584 endif
1585
1586 " Ignore the "failed to create input context" error.
1587 call test_ignore_error('E285:')
1588
1589 gui -f
1590
1591 call assert_equal(1, winnr('$'))
1592 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1593 call Stop_shell_in_terminal(buf)
1594 call term_wait(buf)
1595
1596 " closing window wipes out the terminal buffer a with finished job
1597 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1598 call assert_equal("", bufname(buf))
1599
1600 unlet g:job
1601endfunc
1602
1603func Test_zz2_terminal_guioptions_bang()
1604 if !has('gui_running')
1605 return
1606 endif
1607 set guioptions+=!
1608
1609 let filename = 'Xtestscript'
1610 if has('win32')
1611 let filename .= '.bat'
1612 let prefix = ''
1613 let contents = ['@echo off', 'exit %1']
1614 else
1615 let filename .= '.sh'
1616 let prefix = './'
1617 let contents = ['#!/bin/sh', 'exit $1']
1618 endif
1619 call writefile(contents, filename)
1620 call setfperm(filename, 'rwxrwx---')
1621
1622 " Check if v:shell_error is equal to the exit status.
1623 let exitval = 0
1624 execute printf(':!%s%s %d', prefix, filename, exitval)
1625 call assert_equal(exitval, v:shell_error)
1626
1627 let exitval = 9
1628 execute printf(':!%s%s %d', prefix, filename, exitval)
1629 call assert_equal(exitval, v:shell_error)
1630
1631 set guioptions&
1632 call delete(filename)
1633endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001634
1635func Test_terminal_hidden()
1636 if !has('unix')
1637 return
1638 endif
1639 term ++hidden cat
1640 let bnr = bufnr('$')
1641 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1642 exe 'sbuf ' . bnr
1643 call assert_equal('terminal', &buftype)
1644 call term_sendkeys(bnr, "asdf\<CR>")
1645 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1646 call term_sendkeys(bnr, "\<C-D>")
1647 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1648 bwipe!
1649endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001650
1651func Test_terminal_hidden_and_close()
1652 if !has('unix')
1653 return
1654 endif
1655 call assert_equal(1, winnr('$'))
1656 term ++hidden ++close ls
1657 let bnr = bufnr('$')
1658 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1659 call WaitForAssert({-> assert_false(bufexists(bnr))})
1660 call assert_equal(1, winnr('$'))
1661endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001662
1663func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001664 let contents = [
1665 \ [ 'One', '', 'X' ],
1666 \ [ 'Two', '', '' ],
1667 \ [ 'Three' ] + repeat([''], 30)
1668 \ ]
1669
1670 for c in contents
1671 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001672 if has('win32')
1673 term cmd /c type Xfile
1674 else
1675 term cat Xfile
1676 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001677 let bnr = bufnr('$')
1678 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1679 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001680 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001681 call assert_equal(c, getline(1, line('$')))
1682 quit
1683 endfor
1684
1685 call delete('Xfile')
1686endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001687
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001688func Test_terminal_no_job()
1689 let term = term_start('false', {'term_finish': 'close'})
1690 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1691endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001692
1693func Test_term_gettitle()
1694 if !has('title') || empty(&t_ts)
1695 return
1696 endif
1697 " TODO: this fails on Travis
1698 return
1699
1700 " term_gettitle() returns an empty string for a non-terminal buffer
1701 " or for a non-existing buffer.
1702 call assert_equal('', term_gettitle(bufnr('%')))
1703 call assert_equal('', term_gettitle(bufnr('$') + 1))
1704
1705 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
1706 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
1707
1708 call term_sendkeys(term, ":e Xfoo\r")
1709 call WaitForAssert({-> assert_match('Xfoo (.*[/\\]testdir) - VIM', term_gettitle(term)) })
1710
1711 call term_sendkeys(term, ":set titlestring=foo\r")
1712 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
1713
1714 exe term . 'bwipe!'
1715endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01001716
1717" When drawing the statusline the cursor position may not have been updated
1718" yet.
1719" 1. create a terminal, make it show 2 lines
1720" 2. 0.5 sec later: leave terminal window, execute "i"
1721" 3. 0.5 sec later: clear terminal window, now it's 1 line
1722" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1723" 4. 0.5 sec later: should be done, clean up
1724func Test_terminal_statusline()
1725 if !has('unix')
1726 return
1727 endif
1728 set statusline=x
1729 terminal
1730 let tbuf = bufnr('')
1731 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1732 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1733 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1734 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1735
1736 sleep 2
1737 exe tbuf . 'bwipe!'
1738 au! BufLeave
1739 set statusline=
1740endfunc