blob: f5b99bcc885a56e766ddf03443b8e4f1a36cecfb [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02002" This is split in two, because it can take a lot of time.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02003" See test_terminal2.vim and test_terminal3.vim for further tests.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02005source check.vim
6CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02007
8source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01009source screendump.vim
Bram Moolenaar91689ea2020-05-11 22:04:53 +020010source mouse.vim
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020011source term_util.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020012
Bram Moolenaarb81bc772017-08-11 22:45:01 +020013let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020014let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020015
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020016func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020017 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020018 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020019
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020020 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020021 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020022 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020023 call assert_match('%aR[^\n]*running]', execute('ls R'))
24 call assert_notmatch('%[^\n]*running]', execute('ls F'))
25 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar004a6782020-04-11 17:09:31 +020026 call assert_fails('set modifiable', 'E946:')
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020027
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020028 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020029 call TermWait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020030 call assert_equal('n', mode())
31 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020032 call assert_match('%aF[^\n]*finished]', execute('ls F'))
33 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
34 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035
Bram Moolenaar94053a52017-08-01 21:44:33 +020036 " closing window wipes out the terminal buffer a with finished job
37 close
38 call assert_equal("", bufname(buf))
39
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020040 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020041 unlet g:job
42endfunc
43
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020044func Test_terminal_TerminalWinOpen()
45 au TerminalWinOpen * let b:done = 'yes'
46 let buf = Run_shell_in_terminal({})
47 call assert_equal('yes', b:done)
48 call StopShellInTerminal(buf)
49 " closing window wipes out the terminal buffer with the finished job
50 close
51
52 if has("unix")
53 terminal ++hidden ++open sleep 1
54 sleep 1
55 call assert_fails("echo b:done", 'E121:')
56 endif
57
58 au! TerminalWinOpen
59endfunc
60
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020061func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020062 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020063 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020064 call TermWait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065
66 setlocal modifiable
67 exe "normal Axxx\<Esc>"
Bram Moolenaar28ee8922020-10-28 20:20:00 +010068 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020069 undo
70
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020071 exe buf . 'bwipe'
72 unlet g:job
73endfunc
74
Bram Moolenaar5b868a82019-02-25 06:11:53 +010075func Test_terminal_paste_register()
76 let @" = "text to paste"
77
78 let buf = Run_shell_in_terminal({})
79 " Wait for the shell to display a prompt
80 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
81
82 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
83 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020084 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010085
86 exe buf . 'bwipe!'
87 unlet g:job
88endfunc
89
Bram Moolenaar94053a52017-08-01 21:44:33 +020090func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020091 let buf = Run_shell_in_terminal({})
Bram Moolenaar28ee8922020-10-28 20:20:00 +010092 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaareb44a682017-08-03 22:44:55 +020093 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020094 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020095 call assert_equal("", bufname(buf))
96
97 unlet g:job
98endfunc
99
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200100func Test_terminal_split_quit()
101 let buf = Run_shell_in_terminal({})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200102 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200103 split
104 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200105 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200106 sleep 50m
107 call assert_equal('run', job_status(g:job))
108
109 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200110 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200111
112 exe buf . 'bwipe'
113 unlet g:job
114endfunc
115
Bram Moolenaar94053a52017-08-01 21:44:33 +0200116func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200117 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200118 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200119 quit
120 for nr in range(1, winnr('$'))
121 call assert_notequal(winbufnr(nr), buf)
122 endfor
123 call assert_true(bufloaded(buf))
124 call assert_true(buflisted(buf))
125
126 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200127 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200128 exe buf . 'bwipe'
129
130 unlet g:job
131endfunc
132
Bram Moolenaar1e115362019-01-09 23:01:02 +0100133func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200134 exe g:buf . 'bwipe!'
135 let g:buf = 0
136endfunc
137
Bram Moolenaar9d189612017-09-09 18:11:00 +0200138func Get_cat_123_cmd()
139 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100140 if !has('conpty')
141 return 'cmd /c "cls && color 2 && echo 123"'
142 else
143 " When clearing twice, extra sequence is not output.
144 return 'cmd /c "cls && cls && color 2 && echo 123"'
145 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200146 else
147 call writefile(["\<Esc>[32m123"], 'Xtext')
148 return "cat Xtext"
149 endif
150endfunc
151
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200152func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200153 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200154 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
155 let g:job = term_getjob(g:buf)
156
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200157 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
158 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200159 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100160 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200161 call delete('Xtext')
162endfunc
163
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200164func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200165 let l = term_scrape(a:buf, 0)
166 call assert_true(len(l) == 0)
167 let l = term_scrape(a:buf, 999)
168 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200169 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200170 call assert_true(len(l) > 0)
171 call assert_equal('1', l[0].chars)
172 call assert_equal('2', l[1].chars)
173 call assert_equal('3', l[2].chars)
174 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200175 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
176 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100177 if has('win32')
178 " On Windows 'background' always defaults to dark, even though the terminal
179 " may use a light background. Therefore accept both white and black.
180 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200181 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100182 if &background == 'light'
183 call assert_equal('#ffffff', l[0].bg)
184 else
185 call assert_equal('#000000', l[0].bg)
186 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200187 endif
188
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200189 let l = term_getline(a:buf, -1)
190 call assert_equal('', l)
191 let l = term_getline(a:buf, 0)
192 call assert_equal('', l)
193 let l = term_getline(a:buf, 999)
194 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200195 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200196 call assert_equal('123', l)
197endfunc
198
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200199func Test_terminal_scrape_123()
200 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200201 let buf = term_start(cmd)
202
203 let termlist = term_list()
204 call assert_equal(1, len(termlist))
205 call assert_equal(buf, termlist[0])
206
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200207 " Nothing happens with invalid buffer number
208 call term_wait(1234)
209
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200210 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200211 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200212 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200213 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200214 call Check_123(buf)
215
216 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100217 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200218 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200219 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200220 call Check_123(buf)
221
222 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200223 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200225
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200226func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200227 call writefile(["léttまrs"], 'Xtext')
228 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200229 " Run cmd with UTF-8 codepage to make the type command print the expected
230 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100231 let buf = term_start("cmd /K chcp 65001")
232 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200233 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100234 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100236 let buf = term_start("cat Xtext")
237 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200238 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200239
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100240 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
241 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200242 call assert_true(len(l) >= 7)
243 call assert_equal('l', l[0].chars)
244 call assert_equal('é', l[1].chars)
245 call assert_equal(1, l[1].width)
246 call assert_equal('t', l[2].chars)
247 call assert_equal('t', l[3].chars)
248 call assert_equal('ま', l[4].chars)
249 call assert_equal(2, l[4].width)
250 call assert_equal('r', l[5].chars)
251 call assert_equal('s', l[6].chars)
252
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100253 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200254 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200255 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200256
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100257 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200258 call delete('Xtext')
259endfunc
260
Bram Moolenaar8b896142020-08-02 15:05:05 +0200261func Test_terminal_one_column()
262 " This creates a terminal, displays a double-wide character and makes the
263 " window one column wide. This used to cause a crash.
264 let width = &columns
265 botright vert term
266 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200267 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200268 exe "set columns=" .. (width / 2)
269 redraw
270 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200271 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200272 exe "set columns=" .. width
273 exe buf . 'bwipe!'
274endfunc
275
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200276func Test_terminal_scroll()
277 call writefile(range(1, 200), 'Xtext')
278 if has('win32')
279 let cmd = 'cmd /c "type Xtext"'
280 else
281 let cmd = "cat Xtext"
282 endif
283 let buf = term_start(cmd)
284
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100285 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200286 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200287 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200288
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100289 " wait until the scrolling stops
290 while 1
291 let scrolled = buf->term_getscrolled()
292 sleep 20m
293 if scrolled == buf->term_getscrolled()
294 break
295 endif
296 endwhile
297
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200298 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200299 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200300 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200301 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200302 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200303 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200304
305 exe buf . 'bwipe'
306 call delete('Xtext')
307endfunc
308
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200309func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200310 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200311 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200312 call writefile(range(150), 'Xtext')
313 if has('win32')
314 call term_sendkeys(buf, "type Xtext\<CR>")
315 else
316 call term_sendkeys(buf, "cat Xtext\<CR>")
317 endif
318 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200319 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200320 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200321 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200322 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200323
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200324 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200325 call TermWait(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200326 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200327 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100328 call delete('Xtext')
329endfunc
330
331func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200332 " tail -f only works on Unix
333 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100334
335 call writefile(range(50), 'Xtext')
336 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100337 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100338 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100339 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100340 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
341 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100342 \ 'call feedkeys("\<C-W>N", "xt")',
343 \ ], 'XTest_postponed')
344 let buf = RunVimInTerminal('-S XTest_postponed', {})
345 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100346 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100347
348 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200349 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100350 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100351 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100352
353 " Back to Terminal-Job mode, text will scroll and show the extra line.
354 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100355 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100356
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100357 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100358 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200359 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100360 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100361 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200362 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100363 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100364 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
365
366 " check type of term_sendkeys() return value
367 echo type(tsk_ret)
368
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100369 call StopVimInTerminal(buf)
370 call delete('XTest_postponed')
371 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200372endfunc
373
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100374" Run diff on two dumps with different size.
375func Test_terminal_dumpdiff_size()
376 call assert_equal(1, winnr('$'))
377 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
378 call assert_equal(2, winnr('$'))
379 call assert_match('Test_incsearch_search_01.dump', getline(10))
380 call assert_match(' +++++$', getline(11))
381 call assert_match('Test_popup_command_01.dump', getline(31))
382 call assert_equal(repeat('+', 75), getline(30))
383 quit
384endfunc
385
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200386func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200387 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200388
Bram Moolenaarb2412082017-08-20 18:09:14 +0200389 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200390 let size = term_getsize('')
391 bwipe!
392 call assert_equal(5, size[0])
393
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200394 call term_start(cmd, {'term_rows': 6})
395 let size = term_getsize('')
396 bwipe!
397 call assert_equal(6, size[0])
398
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200399 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200400 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200401 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200402
403 call term_setsize('', 6, 0)
404 call assert_equal([6, 33], term_getsize(''))
405
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200406 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200407 call assert_equal([6, 35], term_getsize(''))
408
409 call term_setsize('', 7, 30)
410 call assert_equal([7, 30], term_getsize(''))
411
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200412 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200413 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200414
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200415 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
416 let size = term_getsize('')
417 bwipe!
418 call assert_equal([6, 36], size)
419
Bram Moolenaarb2412082017-08-20 18:09:14 +0200420 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200421 let size = term_getsize('')
422 bwipe!
423 call assert_equal(20, size[1])
424
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200425 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200426 let size = term_getsize('')
427 bwipe!
428 call assert_equal(26, size[1])
429
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200430 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200431 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200432 let size = term_getsize('')
433 bwipe!
434 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200435
436 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
437 let size = term_getsize('')
438 bwipe!
439 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200440
441 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200442endfunc
443
444func Test_terminal_curwin()
445 let cmd = Get_cat_123_cmd()
446 call assert_equal(1, winnr('$'))
447
Bram Moolenaarb1009092020-05-31 16:04:42 +0200448 split Xdummy
449 call setline(1, 'dummy')
450 write
451 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200452 exe 'terminal ++curwin ' . cmd
453 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200454 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200455 bwipe!
456
Bram Moolenaarb1009092020-05-31 16:04:42 +0200457 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200458 call term_start(cmd, {'curwin': 1})
459 call assert_equal(2, winnr('$'))
460 bwipe!
461
Bram Moolenaarb1009092020-05-31 16:04:42 +0200462 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200463 call setline(1, 'change')
464 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
465 call assert_equal(2, winnr('$'))
466 exe 'terminal! ++curwin ' . cmd
467 call assert_equal(2, winnr('$'))
468 bwipe!
469
Bram Moolenaarb1009092020-05-31 16:04:42 +0200470 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200471 call setline(1, 'change')
472 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
473 call assert_equal(2, winnr('$'))
474 bwipe!
475
Bram Moolenaarb1009092020-05-31 16:04:42 +0200476 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200477 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200478 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200479 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200480endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200481
Bram Moolenaarff546792017-11-21 14:47:57 +0100482func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200483 if s:python != ''
484 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200485 " 500 was not enough for Travis
486 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200487 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200488 echo 'This will take five seconds...'
489 let waittime = 2000
490 if has('win32')
491 let cmd = $windir . '\system32\timeout.exe 1'
492 else
493 let cmd = 'sleep 1'
494 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200495 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100496 return [cmd, waittime]
497endfunc
498
499func Test_terminal_finish_open_close()
500 call assert_equal(1, winnr('$'))
501
502 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200503
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100504 " shell terminal closes automatically
505 terminal
506 let buf = bufnr('%')
507 call assert_equal(2, winnr('$'))
508 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200509 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200510 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200511 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100512
513 " shell terminal that does not close automatically
514 terminal ++noclose
515 let buf = bufnr('%')
516 call assert_equal(2, winnr('$'))
517 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200518 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200519 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100520 call assert_equal(2, winnr('$'))
521 quit
522 call assert_equal(1, winnr('$'))
523
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200524 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200525 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200526 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200527 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200528
529 call term_start(cmd, {'term_finish': 'close'})
530 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200531 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200532 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200533 call assert_equal(1, winnr('$'))
534
535 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200536 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200537 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200538 bwipe
539
540 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200541 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200542 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200543 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200544
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200545 exe 'terminal ++hidden ++open ' . cmd
546 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200547 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200548 bwipe
549
550 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
551 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200552 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200553 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200554
555 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
556 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
557 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
558 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
559
560 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200561 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200562 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200563 call assert_equal(4, winheight(0))
564 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200565endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200566
567func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100568 if has('win32')
569 let cmd = 'cmd /c cd'
570 else
571 CheckExecutable pwd
572 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200573 endif
574 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100575 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200576 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200577
578 exe buf . 'bwipe'
579 call delete('Xdir', 'rf')
580endfunc
581
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200582func Test_terminal_cwd_failure()
583 " Case 1: Provided directory is not actually a directory. Attempt to make
584 " the file executable as well.
585 call writefile([], 'Xfile')
586 call setfperm('Xfile', 'rwx------')
587 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
588 call delete('Xfile')
589
590 " Case 2: Directory does not exist.
591 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
592
593 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100594 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200595 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100596 call mkdir('XdirNoAccess', '', '0600')
597 " return early if the directory permissions could not be set properly
598 if getfperm('XdirNoAccess')[2] == 'x'
599 call delete('XdirNoAccess', 'rf')
600 return
601 endif
602 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
603 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200604 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200605endfunc
606
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100607func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200608 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200609 call s:test_environment("VIM_SERVERNAME", v:servername)
610endfunc
611
612func Test_terminal_version()
613 call s:test_environment("VIM_TERMINAL", string(v:version))
614endfunc
615
616func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100617 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100618 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200619 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100620 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200621 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100622 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200623 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100624 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200625 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200626 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200627 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100628
Bram Moolenaar012eb662018-03-13 17:55:27 +0100629 exe buf . 'bwipe'
630 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100631endfunc
632
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200633func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100634 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200635 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200636 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100637 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100638 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100639 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100640 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100641 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200642 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200643 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200644 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200645
Bram Moolenaar012eb662018-03-13 17:55:27 +0100646 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200647endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200648
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200649func Test_terminal_list_args()
650 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100651 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200652 exe buf . 'bwipe!'
653 call assert_equal("", bufname(buf))
654endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200655
656func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100657 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100658 let wait_time = 5000
659 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100660 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200661 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100662 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200663 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100664 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100665 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100666 " NPFS is used in Windows, nonblocking mode does not work properly.
667 elseif has('win32')
668 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200669 else
670 let len = 5000
671 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200672
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100673 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100674 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100675 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200676 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100677 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200678
679 " On MS-Windows there is an extra empty line below "done". Find "done" in
680 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100681 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100682 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100683 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200684 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100685 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200686 endif
687 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200688
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100689 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200690 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200691 call TermWait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200692 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200693 bwipe
694endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200695
696func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100697 " TODO: enable once writing to stdin works on MS-Windows
698 CheckNotMSWindows
699 CheckExecutable wc
700
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200701 call setline(1, ['one', 'two', 'three'])
702 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200703 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200704 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200705 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100706 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200707
708 call setline(1, ['one', 'two', 'three', 'four'])
709 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200710 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200711 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200712 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100713 %bwipe!
714endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200715
Bram Moolenaar21109272020-01-30 16:27:20 +0100716func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200717 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200718
Bram Moolenaar21109272020-01-30 16:27:20 +0100719 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200720 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100721 " MS-Windows echoes the input, Unix doesn't.
722 if has('win32')
723 call WaitFor({-> getline('$') =~ 'exit(123)'})
724 call assert_equal('hello', getline(line('$') - 1))
725 else
726 call WaitFor({-> getline('$') =~ 'hello'})
727 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200728 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100729 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
730 %bwipe!
731endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200732
Bram Moolenaar21109272020-01-30 16:27:20 +0100733func Test_terminal_eof_arg_win32_ctrl_z()
734 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200735 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100736
737 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200738 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100739 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
740 call assert_match('\^Z', getline(line('$') - 1))
741 %bwipe!
742endfunc
743
744func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200745 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100746
747 " Check the last specified ++eof arg is used and should not memory leak.
748 new
749 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200750 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100751 " MS-Windows echoes the input, Unix doesn't.
752 if has('win32')
753 call WaitFor({-> getline('$') =~ 'exit(123)'})
754 call assert_equal('hello', getline(line('$') - 1))
755 else
756 call WaitFor({-> getline('$') =~ 'hello'})
757 call assert_equal('hello', getline('$'))
758 endif
759 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
760 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200761endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200762
763func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200764 let buf = term_start('NONE', {})
765 call assert_notequal(0, buf)
766
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200767 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200768 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100769 if has('gui_running') && !has('win32')
770 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200771 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100772 else
773 " Otherwise using a job works on all systems.
774 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200775 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200776 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200777
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200778 bwipe!
779endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200780
781func Test_terminal_special_chars()
782 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200783 CheckUnix
784
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200785 call mkdir('Xdir with spaces')
786 call writefile(['x'], 'Xdir with spaces/quoted"file')
787 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200788 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100789 " make sure the job has finished
790 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200791
792 call delete('Xdir with spaces', 'rf')
793 bwipe
794endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200795
796func Test_terminal_wrong_options()
797 call assert_fails('call term_start(&shell, {
798 \ "in_io": "file",
799 \ "in_name": "xxx",
800 \ "out_io": "file",
801 \ "out_name": "xxx",
802 \ "err_io": "file",
803 \ "err_name": "xxx"
804 \ })', 'E474:')
805 call assert_fails('call term_start(&shell, {
806 \ "out_buf": bufnr("%")
807 \ })', 'E474:')
808 call assert_fails('call term_start(&shell, {
809 \ "err_buf": bufnr("%")
810 \ })', 'E474:')
811endfunc
812
813func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200814 let cmd = Get_cat_123_cmd()
815 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200816 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100817 " ConPTY may precede escape sequence. There are things that are not so.
818 if !has('conpty')
819 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
820 call assert_match('123', readfile('Xfile')[0])
821 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200822 let g:job = term_getjob(buf)
823 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +0200824
825 if has('win32')
826 " On Windows we cannot delete a file being used by a process. When
827 " job_status() returns "dead", the process remains for a short time.
828 " Just wait for a moment.
829 sleep 50m
830 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200831 call delete('Xfile')
832 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200833
834 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200835 call writefile(['one line'], 'Xfile')
836 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200837 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200838 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200839 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200840 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200841 bwipe
842 call delete('Xfile')
843 endif
844endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200845
846func TerminalTmap(remap)
847 let buf = Run_shell_in_terminal({})
848 call assert_equal('t', mode())
849
850 if a:remap
851 tmap 123 456
852 else
853 tnoremap 123 456
854 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100855 " don't use abcde, it's an existing command
856 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200857 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100858 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200859 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200860 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200861 let lnum = term_getcursor(buf)[0]
862 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100863 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200864 else
865 call assert_match('456', term_getline(buf, lnum))
866 endif
867
868 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200869 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200870 call TermWait(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200871
872 tunmap 123
873 tunmap 456
874 call assert_equal('', maparg('123', 't'))
875 close
876 unlet g:job
877endfunc
878
879func Test_terminal_tmap()
880 call TerminalTmap(1)
881 call TerminalTmap(0)
882endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200883
884func Test_terminal_wall()
885 let buf = Run_shell_in_terminal({})
886 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200887 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200888 call TermWait(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200889 exe buf . 'bwipe'
890 unlet g:job
891endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200892
Bram Moolenaar7a760922018-02-19 23:10:02 +0100893func Test_terminal_wqall()
894 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200895 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200896 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200897 call TermWait(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100898 exe buf . 'bwipe'
899 unlet g:job
900endfunc
901
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200902func Test_terminal_composing_unicode()
903 let save_enc = &encoding
904 set encoding=utf-8
905
906 if has('win32')
907 let cmd = "cmd /K chcp 65001"
908 let lnum = [3, 6, 9]
909 else
910 let cmd = &shell
911 let lnum = [1, 3, 5]
912 endif
913
914 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +0200915 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100916 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200917 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200918
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200919 if has('win32')
920 call assert_equal('cmd', job_info(g:job).cmd[0])
921 else
922 call assert_equal(&shell, job_info(g:job).cmd[0])
923 endif
924
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200925 " ascii + composing
926 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200927 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200928 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200929 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200930 call term_sendkeys(buf, "\<cr>")
931 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200932 let l = term_scrape(buf, lnum[0] + 1)
933 call assert_equal("a\u0308", l[0].chars)
934 call assert_equal("b", l[1].chars)
935 call assert_equal("c", l[2].chars)
936
937 " multibyte + composing
938 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200939 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200940 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200941 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200942 call term_sendkeys(buf, "\<cr>")
943 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200944 let l = term_scrape(buf, lnum[1] + 1)
945 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar79ea6802020-05-17 15:09:27 +0200946 call assert_equal("\u304e", l[2].chars)
947 call assert_equal("\u304f\u3099", l[3].chars)
948 call assert_equal("\u3052", l[5].chars)
949 call assert_equal("\u3053\u3099", l[6].chars)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200950
951 " \u00a0 + composing
952 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200953 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200954 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200955 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200956 call term_sendkeys(buf, "\<cr>")
957 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200958 let l = term_scrape(buf, lnum[2] + 1)
959 call assert_equal("\u00a0\u0308", l[3].chars)
960
961 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200962 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200963 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100964 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200965 let &encoding = save_enc
966endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100967
968func Test_terminal_aucmd_on_close()
969 fun Nop()
970 let s:called = 1
971 endfun
972
973 aug repro
974 au!
975 au BufWinLeave * call Nop()
976 aug END
977
978 let [cmd, waittime] = s:get_sleep_cmd()
979
980 call assert_equal(1, winnr('$'))
981 new
982 call setline(1, ['one', 'two'])
983 exe 'term ++close ' . cmd
984 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200985 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100986 call assert_equal(1, s:called)
987 bwipe!
988
989 unlet s:called
990 au! repro
991 delfunc Nop
992endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100993
994func Test_terminal_term_start_empty_command()
995 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +0200996 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100997 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +0200998 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100999 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001000 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001001 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001002 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001003 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001004 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001005 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001006 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001007 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001008 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001009 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001010 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001011 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001012 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001013 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1014 call assert_fails(cmd, 'E475:')
1015 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001016 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001017 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001018 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1019 call assert_fails(cmd, 'E475:')
1020 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1021 call assert_fails(cmd, 'E730:')
1022 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001023 if has('gui_running') || has('termguicolors')
1024 call assert_fails(cmd, 'E475:')
1025 else
1026 call assert_fails(cmd, 'E254:')
1027 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001028 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001029endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001030
1031func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001032 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001033
1034 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001035 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001036
Bram Moolenaar086eb872018-03-25 21:24:12 +02001037 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001038 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001039
Bram Moolenaar086eb872018-03-25 21:24:12 +02001040 " Request the cursor position.
1041 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001042
1043 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001044 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001045
Bram Moolenaar086eb872018-03-25 21:24:12 +02001046 " End "cat" gently.
1047 call term_sendkeys(buf, "\<CR>\<C-D>")
1048
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001049 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001050 exe buf . 'bwipe'
1051 unlet g:job
1052endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001053
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001054" Run Vim, start a terminal in that Vim with the kill argument,
1055" :qall works.
1056func Run_terminal_qall_kill(line1, line2)
1057 " 1. Open a terminal window and wait for the prompt to appear
1058 " 2. set kill using term_setkill()
1059 " 3. make Vim exit, it will kill the shell
1060 let after = [
1061 \ a:line1,
1062 \ 'let buf = bufnr("%")',
1063 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1064 \ ' sleep 10m',
1065 \ 'endwhile',
1066 \ a:line2,
1067 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1068 \ 'qall',
1069 \ ]
1070 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001071 return
1072 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001073 call assert_equal("done", readfile("Xdone")[0])
1074 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001075endfunc
1076
1077" Run Vim in a terminal, then start a terminal in that Vim with a kill
1078" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001079func Test_terminal_qall_kill_arg()
1080 call Run_terminal_qall_kill('term ++kill=kill', '')
1081endfunc
1082
1083" Run Vim, start a terminal in that Vim, set the kill argument with
1084" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001085func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001086 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001087endfunc
1088
1089" Run Vim, start a terminal in that Vim without the kill argument,
1090" check that :qall does not exit, :qall! does.
1091func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001092 let after =<< trim [CODE]
1093 term
1094 let buf = bufnr("%")
1095 while term_getline(buf, 1) =~ "^\\s*$"
1096 sleep 10m
1097 endwhile
1098 set nomore
1099 au VimLeavePre * call writefile(["too early"], "Xdone")
1100 qall
1101 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1102 cquit
1103 [CODE]
1104
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001105 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001106 return
1107 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001108 call assert_equal("done", readfile("Xdone")[0])
1109 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001110endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001111
1112" Run Vim in a terminal, then start a terminal in that Vim without a kill
1113" argument, check that :confirm qall works.
1114func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001115 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001116 let buf = RunVimInTerminal('', {})
1117
1118 " Open a terminal window and wait for the prompt to appear
1119 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001120 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1121 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001122
1123 " make Vim exit, it will prompt to kill the shell
1124 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001125 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001126 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001127 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001128
1129 " close the terminal window where Vim was running
1130 quit
1131endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001132
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001133" Run Vim in a terminal, then start a terminal window with a shell and check
1134" that Vim exits if it is closed.
1135func Test_terminal_exit()
1136 CheckRunVimInTerminal
1137
1138 let lines =<< trim END
1139 let winid = win_getid()
1140 help
1141 term
1142 let termid = win_getid()
1143 call win_gotoid(winid)
1144 close
1145 call win_gotoid(termid)
1146 END
1147 call writefile(lines, 'XtermExit')
1148 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1149 let job = term_getjob(buf)
1150 call WaitForAssert({-> assert_equal("run", job_status(job))})
1151
1152 " quit the shell, it will make Vim exit
1153 call term_sendkeys(buf, "exit\<CR>")
1154 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1155
1156 call delete('XtermExit')
1157endfunc
1158
Bram Moolenaar012eb662018-03-13 17:55:27 +01001159func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001160 augroup repro
1161 au!
1162 au TerminalOpen * let s:called += 1
1163 augroup END
1164
1165 let s:called = 0
1166
1167 " Open a terminal window with :terminal
1168 terminal
1169 call assert_equal(1, s:called)
1170 bwipe!
1171
1172 " Open a terminal window with term_start()
1173 call term_start(&shell)
1174 call assert_equal(2, s:called)
1175 bwipe!
1176
1177 " Open a hidden terminal buffer with :terminal
1178 terminal ++hidden
1179 call assert_equal(3, s:called)
1180 for buf in term_list()
1181 exe buf . "bwipe!"
1182 endfor
1183
1184 " Open a hidden terminal buffer with term_start()
1185 let buf = term_start(&shell, {'hidden': 1})
1186 call assert_equal(4, s:called)
1187 exe buf . "bwipe!"
1188
1189 unlet s:called
1190 au! repro
1191endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001192
1193func Check_dump01(off)
1194 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1195 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001196 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001197endfunc
1198
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001199func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001200 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001201 let save_enc = &encoding
1202 set encoding=utf-8
1203 call assert_equal(1, winnr('$'))
1204
1205 let text = " a\u0300 e\u0302 o\u0308"
1206 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001207 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001208 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001209 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001210 let dumpline = readfile('Xdump')[0]
1211 call assert_match('|à| |ê| |ö', dumpline)
1212
1213 call StopVimInTerminal(buf)
1214 call delete('Xcomposing')
1215 call delete('Xdump')
1216 let &encoding = save_enc
1217endfunc
1218
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001219" Tests for failures in the term_dumpwrite() function
1220func Test_terminal_dumpwrite_errors()
1221 CheckRunVimInTerminal
1222 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1223 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001224 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001225 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1226 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1227 call writefile([], 'Xtest.dump')
1228 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1229 call delete('Xtest.dump')
1230 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1231 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001232 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001233 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001234 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001235 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1236 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1237 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1238endfunc
1239
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001240" just testing basic functionality.
1241func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001242 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001243 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001244 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001245 call assert_equal(2, winnr('$'))
1246 call assert_equal(20, line('$'))
1247 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001248
1249 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001250 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001251 call assert_equal(buf, buf2)
1252 call assert_notequal('one two three four five', trim(getline(1)))
1253
1254 " Load the first dump again in the same window
1255 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1256 call assert_equal(buf, buf2)
1257 call Check_dump01(0)
1258
1259 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1260 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1261 new
1262 let closedbuf = winbufnr('')
1263 quit
1264 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001265 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001266 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001267
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001268 quit
1269endfunc
1270
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001271func Test_terminal_dumpload_dump()
1272 CheckRunVimInTerminal
1273
1274 let lines =<< trim END
1275 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1276 END
1277 call writefile(lines, 'XtermDumpload')
1278 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1279 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1280
1281 call StopVimInTerminal(buf)
1282 call delete('XtermDumpload')
1283endfunc
1284
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001285func Test_terminal_dumpdiff()
1286 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001287 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001288 call assert_equal(2, winnr('$'))
1289 call assert_equal(62, line('$'))
1290 call Check_dump01(0)
1291 call Check_dump01(42)
1292 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1293 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001294
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001295 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001296 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1297 call writefile([], 'X1.dump')
1298 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1299 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001300endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001301
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001302func Test_terminal_dumpdiff_swap()
1303 call assert_equal(1, winnr('$'))
1304 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1305 call assert_equal(2, winnr('$'))
1306 call assert_equal(62, line('$'))
1307 call assert_match('Test_popup_command_01.dump', getline(21))
1308 call assert_match('Test_popup_command_03.dump', getline(42))
1309 call assert_match('Undo', getline(3))
1310 call assert_match('three four five', getline(45))
1311
1312 normal s
1313 call assert_match('Test_popup_command_03.dump', getline(21))
1314 call assert_match('Test_popup_command_01.dump', getline(42))
1315 call assert_match('three four five', getline(3))
1316 call assert_match('Undo', getline(45))
1317 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001318
1319 " Diff two terminal dump files with different number of rows
1320 " Swap the diffs
1321 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1322 call assert_match('Test_popup_command_01.dump', getline(21))
1323 call assert_match('Test_winline_rnu.dump', getline(42))
1324 normal s
1325 call assert_match('Test_winline_rnu.dump', getline(6))
1326 call assert_match('Test_popup_command_01.dump', getline(27))
1327 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001328endfunc
1329
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001330func Test_terminal_dumpdiff_options()
1331 set laststatus=0
1332 call assert_equal(1, winnr('$'))
1333 let height = winheight(0)
1334 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1335 call assert_equal(2, winnr('$'))
1336 call assert_equal(height, winheight(winnr()))
1337 call assert_equal(33, winwidth(winnr()))
1338 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1339 quit
1340
1341 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001342 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1343 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001344 call assert_equal(&columns, winwidth(0))
1345 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001346 call assert_equal('something else', bufname('%'))
1347 quit
1348
1349 call assert_equal(1, winnr('$'))
1350 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1351 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001352 call assert_fails("call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'bufnr': -1})", 'E475:')
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001353 bwipe
1354
1355 set laststatus&
1356endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001357
Bram Moolenaar10772302019-01-20 18:25:54 +01001358" When drawing the statusline the cursor position may not have been updated
1359" yet.
1360" 1. create a terminal, make it show 2 lines
1361" 2. 0.5 sec later: leave terminal window, execute "i"
1362" 3. 0.5 sec later: clear terminal window, now it's 1 line
1363" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1364" 4. 0.5 sec later: should be done, clean up
1365func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001366 CheckUnix
1367
Bram Moolenaar10772302019-01-20 18:25:54 +01001368 set statusline=x
1369 terminal
1370 let tbuf = bufnr('')
1371 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1372 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1373 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1374 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1375
1376 sleep 2
1377 exe tbuf . 'bwipe!'
1378 au! BufLeave
1379 set statusline=
1380endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001381
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001382func Test_terminal_window_focus()
1383 let winid1 = win_getid()
1384 terminal
1385 let winid2 = win_getid()
1386 call feedkeys("\<C-W>j", 'xt')
1387 call assert_equal(winid1, win_getid())
1388 call feedkeys("\<C-W>k", 'xt')
1389 call assert_equal(winid2, win_getid())
1390 " can use a cursor key here
1391 call feedkeys("\<C-W>\<Down>", 'xt')
1392 call assert_equal(winid1, win_getid())
1393 call feedkeys("\<C-W>\<Up>", 'xt')
1394 call assert_equal(winid2, win_getid())
1395
1396 bwipe!
1397endfunc
1398
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001399func Api_drop_common(options)
1400 call assert_equal(1, winnr('$'))
1401
1402 " Use the title termcap entries to output the escape sequence.
1403 call writefile([
1404 \ 'set title',
1405 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1406 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1407 \ 'redraw',
1408 \ "set t_ts=",
1409 \ ], 'Xscript')
1410 let buf = RunVimInTerminal('-S Xscript', {})
1411 call WaitFor({-> bufnr('Xtextfile') > 0})
1412 call assert_equal('Xtextfile', expand('%:t'))
1413 call assert_true(winnr('$') >= 3)
1414 return buf
1415endfunc
1416
1417func Test_terminal_api_drop_newwin()
1418 CheckRunVimInTerminal
1419 let buf = Api_drop_common('')
1420 call assert_equal(0, &bin)
1421 call assert_equal('', &fenc)
1422
1423 call StopVimInTerminal(buf)
1424 call delete('Xscript')
1425 bwipe Xtextfile
1426endfunc
1427
1428func Test_terminal_api_drop_newwin_bin()
1429 CheckRunVimInTerminal
1430 let buf = Api_drop_common(',{"bin":1}')
1431 call assert_equal(1, &bin)
1432
1433 call StopVimInTerminal(buf)
1434 call delete('Xscript')
1435 bwipe Xtextfile
1436endfunc
1437
1438func Test_terminal_api_drop_newwin_binary()
1439 CheckRunVimInTerminal
1440 let buf = Api_drop_common(',{"binary":1}')
1441 call assert_equal(1, &bin)
1442
1443 call StopVimInTerminal(buf)
1444 call delete('Xscript')
1445 bwipe Xtextfile
1446endfunc
1447
1448func Test_terminal_api_drop_newwin_nobin()
1449 CheckRunVimInTerminal
1450 set binary
1451 let buf = Api_drop_common(',{"nobin":1}')
1452 call assert_equal(0, &bin)
1453
1454 call StopVimInTerminal(buf)
1455 call delete('Xscript')
1456 bwipe Xtextfile
1457 set nobinary
1458endfunc
1459
1460func Test_terminal_api_drop_newwin_nobinary()
1461 CheckRunVimInTerminal
1462 set binary
1463 let buf = Api_drop_common(',{"nobinary":1}')
1464 call assert_equal(0, &bin)
1465
1466 call StopVimInTerminal(buf)
1467 call delete('Xscript')
1468 bwipe Xtextfile
1469 set nobinary
1470endfunc
1471
1472func Test_terminal_api_drop_newwin_ff()
1473 CheckRunVimInTerminal
1474 let buf = Api_drop_common(',{"ff":"dos"}')
1475 call assert_equal("dos", &ff)
1476
1477 call StopVimInTerminal(buf)
1478 call delete('Xscript')
1479 bwipe Xtextfile
1480endfunc
1481
1482func Test_terminal_api_drop_newwin_fileformat()
1483 CheckRunVimInTerminal
1484 let buf = Api_drop_common(',{"fileformat":"dos"}')
1485 call assert_equal("dos", &ff)
1486
1487 call StopVimInTerminal(buf)
1488 call delete('Xscript')
1489 bwipe Xtextfile
1490endfunc
1491
1492func Test_terminal_api_drop_newwin_enc()
1493 CheckRunVimInTerminal
1494 let buf = Api_drop_common(',{"enc":"utf-16"}')
1495 call assert_equal("utf-16", &fenc)
1496
1497 call StopVimInTerminal(buf)
1498 call delete('Xscript')
1499 bwipe Xtextfile
1500endfunc
1501
1502func Test_terminal_api_drop_newwin_encoding()
1503 CheckRunVimInTerminal
1504 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1505 call assert_equal("utf-16", &fenc)
1506
1507 call StopVimInTerminal(buf)
1508 call delete('Xscript')
1509 bwipe Xtextfile
1510endfunc
1511
1512func Test_terminal_api_drop_oldwin()
1513 CheckRunVimInTerminal
1514 let firstwinid = win_getid()
1515 split Xtextfile
1516 let textfile_winid = win_getid()
1517 call assert_equal(2, winnr('$'))
1518 call win_gotoid(firstwinid)
1519
1520 " Use the title termcap entries to output the escape sequence.
1521 call writefile([
1522 \ 'set title',
1523 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1524 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1525 \ 'redraw',
1526 \ "set t_ts=",
1527 \ ], 'Xscript')
1528 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1529 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1530 call assert_equal(textfile_winid, win_getid())
1531
1532 call StopVimInTerminal(buf)
1533 call delete('Xscript')
1534 bwipe Xtextfile
1535endfunc
1536
1537func Tapi_TryThis(bufnum, arg)
1538 let g:called_bufnum = a:bufnum
1539 let g:called_arg = a:arg
1540endfunc
1541
1542func WriteApiCall(funcname)
1543 " Use the title termcap entries to output the escape sequence.
1544 call writefile([
1545 \ 'set title',
1546 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1547 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1548 \ 'redraw',
1549 \ "set t_ts=",
1550 \ ], 'Xscript')
1551endfunc
1552
1553func Test_terminal_api_call()
1554 CheckRunVimInTerminal
1555
1556 unlet! g:called_bufnum
1557 unlet! g:called_arg
1558
1559 call WriteApiCall('Tapi_TryThis')
1560
1561 " Default
1562 let buf = RunVimInTerminal('-S Xscript', {})
1563 call WaitFor({-> exists('g:called_bufnum')})
1564 call assert_equal(buf, g:called_bufnum)
1565 call assert_equal(['hello', 123], g:called_arg)
1566 call StopVimInTerminal(buf)
1567
1568 unlet! g:called_bufnum
1569 unlet! g:called_arg
1570
1571 " Enable explicitly
1572 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1573 call WaitFor({-> exists('g:called_bufnum')})
1574 call assert_equal(buf, g:called_bufnum)
1575 call assert_equal(['hello', 123], g:called_arg)
1576 call StopVimInTerminal(buf)
1577
1578 unlet! g:called_bufnum
1579 unlet! g:called_arg
1580
1581 func! ApiCall_TryThis(bufnum, arg)
1582 let g:called_bufnum2 = a:bufnum
1583 let g:called_arg2 = a:arg
1584 endfunc
1585
1586 call WriteApiCall('ApiCall_TryThis')
1587
1588 " Use prefix match
1589 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1590 call WaitFor({-> exists('g:called_bufnum2')})
1591 call assert_equal(buf, g:called_bufnum2)
1592 call assert_equal(['hello', 123], g:called_arg2)
1593 call StopVimInTerminal(buf)
1594
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001595 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001596
1597 unlet! g:called_bufnum2
1598 unlet! g:called_arg2
1599
1600 call delete('Xscript')
1601 delfunction! ApiCall_TryThis
1602 unlet! g:called_bufnum2
1603 unlet! g:called_arg2
1604endfunc
1605
1606func Test_terminal_api_call_fails()
1607 CheckRunVimInTerminal
1608
1609 func! TryThis(bufnum, arg)
1610 let g:called_bufnum3 = a:bufnum
1611 let g:called_arg3 = a:arg
1612 endfunc
1613
1614 call WriteApiCall('TryThis')
1615
1616 unlet! g:called_bufnum3
1617 unlet! g:called_arg3
1618
1619 " Not permitted
1620 call ch_logfile('Xlog', 'w')
1621 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1622 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1623 call assert_false(exists('g:called_bufnum3'))
1624 call assert_false(exists('g:called_arg3'))
1625 call StopVimInTerminal(buf)
1626
1627 " No match
1628 call ch_logfile('Xlog', 'w')
1629 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1630 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1631 call assert_false(exists('g:called_bufnum3'))
1632 call assert_false(exists('g:called_arg3'))
1633 call StopVimInTerminal(buf)
1634
1635 call delete('Xscript')
1636 call ch_logfile('')
1637 call delete('Xlog')
1638 delfunction! TryThis
1639 unlet! g:called_bufnum3
1640 unlet! g:called_arg3
1641endfunc
1642
1643let s:caught_e937 = 0
1644
1645func Tapi_Delete(bufnum, arg)
1646 try
1647 execute 'bdelete!' a:bufnum
1648 catch /E937:/
1649 let s:caught_e937 = 1
1650 endtry
1651endfunc
1652
1653func Test_terminal_api_call_fail_delete()
1654 CheckRunVimInTerminal
1655
1656 call WriteApiCall('Tapi_Delete')
1657 let buf = RunVimInTerminal('-S Xscript', {})
1658 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1659
1660 call StopVimInTerminal(buf)
1661 call delete('Xscript')
1662 call ch_logfile('', '')
1663endfunc
1664
1665func Test_terminal_setapi_and_call()
1666 CheckRunVimInTerminal
1667
1668 call WriteApiCall('Tapi_TryThis')
1669 call ch_logfile('Xlog', 'w')
1670
1671 unlet! g:called_bufnum
1672 unlet! g:called_arg
1673
1674 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1675 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1676 call assert_false(exists('g:called_bufnum'))
1677 call assert_false(exists('g:called_arg'))
1678
1679 eval buf->term_setapi('Tapi_')
1680 call term_sendkeys(buf, ":set notitle\<CR>")
1681 call term_sendkeys(buf, ":source Xscript\<CR>")
1682 call WaitFor({-> exists('g:called_bufnum')})
1683 call assert_equal(buf, g:called_bufnum)
1684 call assert_equal(['hello', 123], g:called_arg)
1685
1686 call StopVimInTerminal(buf)
1687
1688 call delete('Xscript')
1689 call ch_logfile('')
1690 call delete('Xlog')
1691 unlet! g:called_bufnum
1692 unlet! g:called_arg
1693endfunc
1694
1695func Test_terminal_api_arg()
1696 CheckRunVimInTerminal
1697
1698 call WriteApiCall('Tapi_TryThis')
1699 call ch_logfile('Xlog', 'w')
1700
1701 unlet! g:called_bufnum
1702 unlet! g:called_arg
1703
1704 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1705 let buf = bufnr('%')
1706 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1707 call assert_false(exists('g:called_bufnum'))
1708 call assert_false(exists('g:called_arg'))
1709
1710 call StopVimInTerminal(buf)
1711
1712 call ch_logfile('Xlog', 'w')
1713
1714 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1715 let buf = bufnr('%')
1716 call WaitFor({-> exists('g:called_bufnum')})
1717 call assert_equal(buf, g:called_bufnum)
1718 call assert_equal(['hello', 123], g:called_arg)
1719
1720 call StopVimInTerminal(buf)
1721
1722 call delete('Xscript')
1723 call ch_logfile('')
1724 call delete('Xlog')
1725 unlet! g:called_bufnum
1726 unlet! g:called_arg
1727endfunc
1728
1729func Test_terminal_ansicolors_default()
1730 CheckFunction term_getansicolors
1731
1732 let colors = [
1733 \ '#000000', '#e00000',
1734 \ '#00e000', '#e0e000',
1735 \ '#0000e0', '#e000e0',
1736 \ '#00e0e0', '#e0e0e0',
1737 \ '#808080', '#ff4040',
1738 \ '#40ff40', '#ffff40',
1739 \ '#4040ff', '#ff40ff',
1740 \ '#40ffff', '#ffffff',
1741 \]
1742
1743 let buf = Run_shell_in_terminal({})
1744 call assert_equal(colors, term_getansicolors(buf))
1745 call StopShellInTerminal(buf)
1746 call TermWait(buf)
1747 call assert_equal([], term_getansicolors(buf))
1748
1749 exe buf . 'bwipe'
1750endfunc
1751
1752let s:test_colors = [
1753 \ '#616e64', '#0d0a79',
1754 \ '#6d610d', '#0a7373',
1755 \ '#690d0a', '#6d696e',
1756 \ '#0d0a6f', '#616e0d',
1757 \ '#0a6479', '#6d0d0a',
1758 \ '#617373', '#0d0a69',
1759 \ '#6d690d', '#0a6e6f',
1760 \ '#610d0a', '#6e6479',
1761 \]
1762
1763func Test_terminal_ansicolors_global()
1764 CheckFeature termguicolors
1765 CheckFunction term_getansicolors
1766
1767 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1768 let buf = Run_shell_in_terminal({})
1769 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1770 call StopShellInTerminal(buf)
1771 call TermWait(buf)
1772
1773 exe buf . 'bwipe'
1774 unlet g:terminal_ansi_colors
1775endfunc
1776
1777func Test_terminal_ansicolors_func()
1778 CheckFeature termguicolors
1779 CheckFunction term_getansicolors
1780
1781 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1782 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1783 call assert_equal(s:test_colors, term_getansicolors(buf))
1784
1785 call term_setansicolors(buf, g:terminal_ansi_colors)
1786 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
1787
1788 let colors = [
1789 \ 'ivory', 'AliceBlue',
1790 \ 'grey67', 'dark goldenrod',
1791 \ 'SteelBlue3', 'PaleVioletRed4',
1792 \ 'MediumPurple2', 'yellow2',
1793 \ 'RosyBrown3', 'OrangeRed2',
1794 \ 'white smoke', 'navy blue',
1795 \ 'grey47', 'gray97',
1796 \ 'MistyRose2', 'DodgerBlue4',
1797 \]
1798 eval buf->term_setansicolors(colors)
1799
1800 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001801 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001802 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
1803
1804 call StopShellInTerminal(buf)
1805 call TermWait(buf)
1806 call assert_equal(0, term_setansicolors(buf, []))
1807 exe buf . 'bwipe'
1808endfunc
1809
1810func Test_terminal_all_ansi_colors()
1811 CheckRunVimInTerminal
1812
1813 " Use all the ANSI colors.
1814 call writefile([
1815 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
1816 \ 'hi Tblack ctermfg=0 ctermbg=8',
1817 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1818 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1819 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1820 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1821 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1822 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1823 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1824 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1825 \ 'hi Tred ctermfg=9 ctermbg=1',
1826 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1827 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1828 \ 'hi Tblue ctermfg=12 ctermbg=4',
1829 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1830 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1831 \ 'hi Twhite ctermfg=15 ctermbg=7',
1832 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1833 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1834 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
1835 \ '',
1836 \ 'call matchadd("Tblack", "A")',
1837 \ 'call matchadd("Tdarkred", "B")',
1838 \ 'call matchadd("Tdarkgreen", "C")',
1839 \ 'call matchadd("Tbrown", "D")',
1840 \ 'call matchadd("Tdarkblue", "E")',
1841 \ 'call matchadd("Tdarkmagenta", "F")',
1842 \ 'call matchadd("Tdarkcyan", "G")',
1843 \ 'call matchadd("Tlightgrey", "H")',
1844 \ 'call matchadd("Tdarkgrey", "I")',
1845 \ 'call matchadd("Tred", "J")',
1846 \ 'call matchadd("Tgreen", "K")',
1847 \ 'call matchadd("Tyellow", "L")',
1848 \ 'call matchadd("Tblue", "M")',
1849 \ 'call matchadd("Tmagenta", "N")',
1850 \ 'call matchadd("Tcyan", "O")',
1851 \ 'call matchadd("Twhite", "P")',
1852 \ 'call matchadd("TdarkredBold", "X")',
1853 \ 'call matchadd("TgreenBold", "Y")',
1854 \ 'call matchadd("TmagentaBold", "Z")',
1855 \ 'redraw',
1856 \ ], 'Xcolorscript')
1857 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1858 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1859
1860 call term_sendkeys(buf, ":q\<CR>")
1861 call StopVimInTerminal(buf)
1862 call delete('Xcolorscript')
1863endfunc
1864
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001865
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001866" vim: shiftwidth=2 sts=2 expandtab