blob: cef379a575de07e562b852953ce9297a1db0510a [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 Moolenaar00806bc2020-11-05 19:36:38 +010044func Test_terminal_no_name()
45 let buf = Run_shell_in_terminal({})
46 call assert_match('^!', bufname(buf))
47 0file
48 call assert_equal("", bufname(buf))
49 call assert_match('\[No Name\]', execute('file'))
50 call StopShellInTerminal(buf)
51 call TermWait(buf)
52endfunc
53
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020054func Test_terminal_TerminalWinOpen()
55 au TerminalWinOpen * let b:done = 'yes'
56 let buf = Run_shell_in_terminal({})
57 call assert_equal('yes', b:done)
58 call StopShellInTerminal(buf)
59 " closing window wipes out the terminal buffer with the finished job
60 close
61
62 if has("unix")
63 terminal ++hidden ++open sleep 1
64 sleep 1
65 call assert_fails("echo b:done", 'E121:')
66 endif
67
68 au! TerminalWinOpen
69endfunc
70
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020073 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020074 call TermWait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020075
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
Bram Moolenaar28ee8922020-10-28 20:20:00 +010078 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020079 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar5b868a82019-02-25 06:11:53 +010085func Test_terminal_paste_register()
86 let @" = "text to paste"
87
88 let buf = Run_shell_in_terminal({})
89 " Wait for the shell to display a prompt
90 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
91
92 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
93 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020094 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010095
96 exe buf . 'bwipe!'
97 unlet g:job
98endfunc
99
Bram Moolenaar94053a52017-08-01 21:44:33 +0200100func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200101 let buf = Run_shell_in_terminal({})
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100102 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaareb44a682017-08-03 22:44:55 +0200103 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200104 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200105 call assert_equal("", bufname(buf))
106
107 unlet g:job
108endfunc
109
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200110func Test_terminal_split_quit()
111 let buf = Run_shell_in_terminal({})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200112 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200113 split
114 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200115 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200116 sleep 50m
117 call assert_equal('run', job_status(g:job))
118
119 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200120 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200121
122 exe buf . 'bwipe'
123 unlet g:job
124endfunc
125
Bram Moolenaar94053a52017-08-01 21:44:33 +0200126func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200127 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200128 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200129 quit
130 for nr in range(1, winnr('$'))
131 call assert_notequal(winbufnr(nr), buf)
132 endfor
133 call assert_true(bufloaded(buf))
134 call assert_true(buflisted(buf))
135
136 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200137 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200138 exe buf . 'bwipe'
139
140 unlet g:job
141endfunc
142
Bram Moolenaar1e115362019-01-09 23:01:02 +0100143func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200144 exe g:buf . 'bwipe!'
145 let g:buf = 0
146endfunc
147
Bram Moolenaar9d189612017-09-09 18:11:00 +0200148func Get_cat_123_cmd()
149 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100150 if !has('conpty')
151 return 'cmd /c "cls && color 2 && echo 123"'
152 else
153 " When clearing twice, extra sequence is not output.
154 return 'cmd /c "cls && cls && color 2 && echo 123"'
155 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200156 else
157 call writefile(["\<Esc>[32m123"], 'Xtext')
158 return "cat Xtext"
159 endif
160endfunc
161
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200163 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200164 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
165 let g:job = term_getjob(g:buf)
166
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200167 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
168 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200169 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100170 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200171 call delete('Xtext')
172endfunc
173
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200175 let l = term_scrape(a:buf, 0)
176 call assert_true(len(l) == 0)
177 let l = term_scrape(a:buf, 999)
178 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200179 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 call assert_true(len(l) > 0)
181 call assert_equal('1', l[0].chars)
182 call assert_equal('2', l[1].chars)
183 call assert_equal('3', l[2].chars)
184 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200185 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
186 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100187 if has('win32')
188 " On Windows 'background' always defaults to dark, even though the terminal
189 " may use a light background. Therefore accept both white and black.
190 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100192 if &background == 'light'
193 call assert_equal('#ffffff', l[0].bg)
194 else
195 call assert_equal('#000000', l[0].bg)
196 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200197 endif
198
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200199 let l = term_getline(a:buf, -1)
200 call assert_equal('', l)
201 let l = term_getline(a:buf, 0)
202 call assert_equal('', l)
203 let l = term_getline(a:buf, 999)
204 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200205 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200206 call assert_equal('123', l)
207endfunc
208
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200209func Test_terminal_scrape_123()
210 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200211 let buf = term_start(cmd)
212
213 let termlist = term_list()
214 call assert_equal(1, len(termlist))
215 call assert_equal(buf, termlist[0])
216
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200217 " Nothing happens with invalid buffer number
218 call term_wait(1234)
219
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200220 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200221 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200222 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200223 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 call Check_123(buf)
225
226 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100227 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200228 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200229 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200230 call Check_123(buf)
231
232 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200233 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200234endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200235
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200237 call writefile(["léttまrs"], 'Xtext')
238 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200239 " Run cmd with UTF-8 codepage to make the type command print the expected
240 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 let buf = term_start("cmd /K chcp 65001")
242 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200243 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200245 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let buf = term_start("cat Xtext")
247 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200248 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
251 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200252 call assert_true(len(l) >= 7)
253 call assert_equal('l', l[0].chars)
254 call assert_equal('é', l[1].chars)
255 call assert_equal(1, l[1].width)
256 call assert_equal('t', l[2].chars)
257 call assert_equal('t', l[3].chars)
258 call assert_equal('ま', l[4].chars)
259 call assert_equal(2, l[4].width)
260 call assert_equal('r', l[5].chars)
261 call assert_equal('s', l[6].chars)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200265 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100267 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268 call delete('Xtext')
269endfunc
270
Bram Moolenaar8b896142020-08-02 15:05:05 +0200271func Test_terminal_one_column()
272 " This creates a terminal, displays a double-wide character and makes the
273 " window one column wide. This used to cause a crash.
274 let width = &columns
275 botright vert term
276 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200277 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200278 exe "set columns=" .. (width / 2)
279 redraw
280 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200281 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200282 exe "set columns=" .. width
283 exe buf . 'bwipe!'
284endfunc
285
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200286func Test_terminal_scroll()
287 call writefile(range(1, 200), 'Xtext')
288 if has('win32')
289 let cmd = 'cmd /c "type Xtext"'
290 else
291 let cmd = "cat Xtext"
292 endif
293 let buf = term_start(cmd)
294
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100295 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200296 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200297 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200298
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100299 " wait until the scrolling stops
300 while 1
301 let scrolled = buf->term_getscrolled()
302 sleep 20m
303 if scrolled == buf->term_getscrolled()
304 break
305 endif
306 endwhile
307
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200308 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200309 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200310 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200311 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200312 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200313 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200314
315 exe buf . 'bwipe'
316 call delete('Xtext')
317endfunc
318
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200319func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200320 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200321 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200322 call writefile(range(150), 'Xtext')
323 if has('win32')
324 call term_sendkeys(buf, "type Xtext\<CR>")
325 else
326 call term_sendkeys(buf, "cat Xtext\<CR>")
327 endif
328 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200329 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200330 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200331 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200332 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200333
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200334 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200335 call TermWait(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200336 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200337 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100338 call delete('Xtext')
339endfunc
340
341func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200342 " tail -f only works on Unix
343 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100344
345 call writefile(range(50), 'Xtext')
346 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100347 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100348 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100349 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100350 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
351 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100352 \ 'call feedkeys("\<C-W>N", "xt")',
353 \ ], 'XTest_postponed')
354 let buf = RunVimInTerminal('-S XTest_postponed', {})
355 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100356 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100357
358 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200359 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100360 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100361 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100362
363 " Back to Terminal-Job mode, text will scroll and show the extra line.
364 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100365 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100366
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100367 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100368 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200369 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100370 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100371 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200372 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100373 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100374 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
375
376 " check type of term_sendkeys() return value
377 echo type(tsk_ret)
378
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100379 call StopVimInTerminal(buf)
380 call delete('XTest_postponed')
381 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200382endfunc
383
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100384" Run diff on two dumps with different size.
385func Test_terminal_dumpdiff_size()
386 call assert_equal(1, winnr('$'))
387 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
388 call assert_equal(2, winnr('$'))
389 call assert_match('Test_incsearch_search_01.dump', getline(10))
390 call assert_match(' +++++$', getline(11))
391 call assert_match('Test_popup_command_01.dump', getline(31))
392 call assert_equal(repeat('+', 75), getline(30))
393 quit
394endfunc
395
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200396func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200397 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200398
Bram Moolenaarb2412082017-08-20 18:09:14 +0200399 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400 let size = term_getsize('')
401 bwipe!
402 call assert_equal(5, size[0])
403
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200404 call term_start(cmd, {'term_rows': 6})
405 let size = term_getsize('')
406 bwipe!
407 call assert_equal(6, size[0])
408
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200409 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200410 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200411 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200412
413 call term_setsize('', 6, 0)
414 call assert_equal([6, 33], term_getsize(''))
415
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200416 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200417 call assert_equal([6, 35], term_getsize(''))
418
419 call term_setsize('', 7, 30)
420 call assert_equal([7, 30], term_getsize(''))
421
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200422 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200423 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200424
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200425 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
426 let size = term_getsize('')
427 bwipe!
428 call assert_equal([6, 36], size)
429
Bram Moolenaarb2412082017-08-20 18:09:14 +0200430 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200431 let size = term_getsize('')
432 bwipe!
433 call assert_equal(20, size[1])
434
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200435 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200436 let size = term_getsize('')
437 bwipe!
438 call assert_equal(26, size[1])
439
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200440 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200441 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200442 let size = term_getsize('')
443 bwipe!
444 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200445
446 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
447 let size = term_getsize('')
448 bwipe!
449 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200450
451 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200452endfunc
453
454func Test_terminal_curwin()
455 let cmd = Get_cat_123_cmd()
456 call assert_equal(1, winnr('$'))
457
Bram Moolenaarb1009092020-05-31 16:04:42 +0200458 split Xdummy
459 call setline(1, 'dummy')
460 write
461 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200462 exe 'terminal ++curwin ' . cmd
463 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200464 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200465 bwipe!
466
Bram Moolenaarb1009092020-05-31 16:04:42 +0200467 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200468 call term_start(cmd, {'curwin': 1})
469 call assert_equal(2, winnr('$'))
470 bwipe!
471
Bram Moolenaarb1009092020-05-31 16:04:42 +0200472 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200473 call setline(1, 'change')
474 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
475 call assert_equal(2, winnr('$'))
476 exe 'terminal! ++curwin ' . cmd
477 call assert_equal(2, winnr('$'))
478 bwipe!
479
Bram Moolenaarb1009092020-05-31 16:04:42 +0200480 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200481 call setline(1, 'change')
482 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
483 call assert_equal(2, winnr('$'))
484 bwipe!
485
Bram Moolenaarb1009092020-05-31 16:04:42 +0200486 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200487 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200488 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200489 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200490endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200491
Bram Moolenaarff546792017-11-21 14:47:57 +0100492func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200493 if s:python != ''
494 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200495 " 500 was not enough for Travis
496 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200497 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200498 echo 'This will take five seconds...'
499 let waittime = 2000
500 if has('win32')
501 let cmd = $windir . '\system32\timeout.exe 1'
502 else
503 let cmd = 'sleep 1'
504 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100506 return [cmd, waittime]
507endfunc
508
509func Test_terminal_finish_open_close()
510 call assert_equal(1, winnr('$'))
511
512 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200513
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100514 " shell terminal closes automatically
515 terminal
516 let buf = bufnr('%')
517 call assert_equal(2, winnr('$'))
518 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200519 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200520 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200521 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100522
523 " shell terminal that does not close automatically
524 terminal ++noclose
525 let buf = bufnr('%')
526 call assert_equal(2, winnr('$'))
527 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200528 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200529 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100530 call assert_equal(2, winnr('$'))
531 quit
532 call assert_equal(1, winnr('$'))
533
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200534 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200535 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200536 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200537 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200538
539 call term_start(cmd, {'term_finish': 'close'})
540 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200541 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200542 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200543 call assert_equal(1, winnr('$'))
544
545 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200546 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200547 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200548 bwipe
549
550 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200551 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200552 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200553 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200554
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200555 exe 'terminal ++hidden ++open ' . cmd
556 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200557 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200558 bwipe
559
560 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
561 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200562 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200563 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200564
565 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
566 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
567 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
568 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
569
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100570 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d | let g:result = "opened the buffer in a window"'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200571 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200572 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200573 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100574 call assert_equal('opened the buffer in a window', g:result)
575 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200576 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200577endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200578
579func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100580 if has('win32')
581 let cmd = 'cmd /c cd'
582 else
583 CheckExecutable pwd
584 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200585 endif
586 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100587 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200588 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200589
590 exe buf . 'bwipe'
591 call delete('Xdir', 'rf')
592endfunc
593
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200594func Test_terminal_cwd_failure()
595 " Case 1: Provided directory is not actually a directory. Attempt to make
596 " the file executable as well.
597 call writefile([], 'Xfile')
598 call setfperm('Xfile', 'rwx------')
599 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
600 call delete('Xfile')
601
602 " Case 2: Directory does not exist.
603 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
604
605 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100606 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200607 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100608 call mkdir('XdirNoAccess', '', '0600')
609 " return early if the directory permissions could not be set properly
610 if getfperm('XdirNoAccess')[2] == 'x'
611 call delete('XdirNoAccess', 'rf')
612 return
613 endif
614 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
615 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200616 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200617endfunc
618
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100619func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200620 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200621 call s:test_environment("VIM_SERVERNAME", v:servername)
622endfunc
623
624func Test_terminal_version()
625 call s:test_environment("VIM_TERMINAL", string(v:version))
626endfunc
627
628func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100629 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100630 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200631 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100632 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200633 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100634 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200635 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100636 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200637 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200638 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200639 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100640
Bram Moolenaar012eb662018-03-13 17:55:27 +0100641 exe buf . 'bwipe'
642 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100643endfunc
644
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200645func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100646 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200647 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200648 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100649 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100650 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100651 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100652 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100653 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200654 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200655 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200656 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200657
Bram Moolenaar012eb662018-03-13 17:55:27 +0100658 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200659endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200660
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200661func Test_terminal_list_args()
662 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100663 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200664 exe buf . 'bwipe!'
665 call assert_equal("", bufname(buf))
666endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200667
668func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100669 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100670 let wait_time = 5000
671 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100672 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200673 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100674 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200675 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100676 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100677 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100678 " NPFS is used in Windows, nonblocking mode does not work properly.
679 elseif has('win32')
680 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200681 else
682 let len = 5000
683 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200684
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100685 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100686 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100687 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200688 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100689 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200690
691 " On MS-Windows there is an extra empty line below "done". Find "done" in
692 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100693 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100694 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100695 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200696 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100697 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200698 endif
699 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200700
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100701 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200702 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200703 call TermWait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200704 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200705 bwipe
706endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200707
708func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100709 " TODO: enable once writing to stdin works on MS-Windows
710 CheckNotMSWindows
711 CheckExecutable wc
712
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200713 call setline(1, ['one', 'two', 'three'])
714 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200715 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200716 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200717 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100718 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200719
720 call setline(1, ['one', 'two', 'three', 'four'])
721 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200722 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200723 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200724 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100725 %bwipe!
726endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200727
Bram Moolenaar21109272020-01-30 16:27:20 +0100728func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200729 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200730
Bram Moolenaar21109272020-01-30 16:27:20 +0100731 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200732 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100733 " MS-Windows echoes the input, Unix doesn't.
734 if has('win32')
735 call WaitFor({-> getline('$') =~ 'exit(123)'})
736 call assert_equal('hello', getline(line('$') - 1))
737 else
738 call WaitFor({-> getline('$') =~ 'hello'})
739 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200740 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100741 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
742 %bwipe!
743endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200744
Bram Moolenaar21109272020-01-30 16:27:20 +0100745func Test_terminal_eof_arg_win32_ctrl_z()
746 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200747 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100748
749 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200750 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100751 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
752 call assert_match('\^Z', getline(line('$') - 1))
753 %bwipe!
754endfunc
755
756func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200757 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100758
759 " Check the last specified ++eof arg is used and should not memory leak.
760 new
761 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200762 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100763 " MS-Windows echoes the input, Unix doesn't.
764 if has('win32')
765 call WaitFor({-> getline('$') =~ 'exit(123)'})
766 call assert_equal('hello', getline(line('$') - 1))
767 else
768 call WaitFor({-> getline('$') =~ 'hello'})
769 call assert_equal('hello', getline('$'))
770 endif
771 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
772 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200773endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200774
775func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200776 let buf = term_start('NONE', {})
777 call assert_notequal(0, buf)
778
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200779 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200780 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100781 if has('gui_running') && !has('win32')
782 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200783 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100784 else
785 " Otherwise using a job works on all systems.
786 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200787 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200788 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200789
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200790 bwipe!
791endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200792
793func Test_terminal_special_chars()
794 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200795 CheckUnix
796
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200797 call mkdir('Xdir with spaces')
798 call writefile(['x'], 'Xdir with spaces/quoted"file')
799 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200800 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100801 " make sure the job has finished
802 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200803
804 call delete('Xdir with spaces', 'rf')
805 bwipe
806endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200807
808func Test_terminal_wrong_options()
809 call assert_fails('call term_start(&shell, {
810 \ "in_io": "file",
811 \ "in_name": "xxx",
812 \ "out_io": "file",
813 \ "out_name": "xxx",
814 \ "err_io": "file",
815 \ "err_name": "xxx"
816 \ })', 'E474:')
817 call assert_fails('call term_start(&shell, {
818 \ "out_buf": bufnr("%")
819 \ })', 'E474:')
820 call assert_fails('call term_start(&shell, {
821 \ "err_buf": bufnr("%")
822 \ })', 'E474:')
823endfunc
824
825func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200826 let cmd = Get_cat_123_cmd()
827 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200828 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100829 " ConPTY may precede escape sequence. There are things that are not so.
830 if !has('conpty')
831 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
832 call assert_match('123', readfile('Xfile')[0])
833 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200834 let g:job = term_getjob(buf)
835 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +0200836
837 if has('win32')
838 " On Windows we cannot delete a file being used by a process. When
839 " job_status() returns "dead", the process remains for a short time.
840 " Just wait for a moment.
841 sleep 50m
842 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200843 call delete('Xfile')
844 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200845
846 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200847 call writefile(['one line'], 'Xfile')
848 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200849 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200850 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200851 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200852 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200853 bwipe
854 call delete('Xfile')
855 endif
856endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200857
858func TerminalTmap(remap)
859 let buf = Run_shell_in_terminal({})
860 call assert_equal('t', mode())
861
862 if a:remap
863 tmap 123 456
864 else
865 tnoremap 123 456
866 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100867 " don't use abcde, it's an existing command
868 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200869 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100870 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200871 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200872 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200873 let lnum = term_getcursor(buf)[0]
874 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100875 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200876 else
877 call assert_match('456', term_getline(buf, lnum))
878 endif
879
880 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200881 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200882 call TermWait(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200883
884 tunmap 123
885 tunmap 456
886 call assert_equal('', maparg('123', 't'))
887 close
888 unlet g:job
889endfunc
890
891func Test_terminal_tmap()
892 call TerminalTmap(1)
893 call TerminalTmap(0)
894endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200895
896func Test_terminal_wall()
897 let buf = Run_shell_in_terminal({})
898 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200899 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200900 call TermWait(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200901 exe buf . 'bwipe'
902 unlet g:job
903endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200904
Bram Moolenaar7a760922018-02-19 23:10:02 +0100905func Test_terminal_wqall()
906 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200907 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200908 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200909 call TermWait(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100910 exe buf . 'bwipe'
911 unlet g:job
912endfunc
913
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200914func Test_terminal_composing_unicode()
915 let save_enc = &encoding
916 set encoding=utf-8
917
918 if has('win32')
919 let cmd = "cmd /K chcp 65001"
920 let lnum = [3, 6, 9]
921 else
922 let cmd = &shell
923 let lnum = [1, 3, 5]
924 endif
925
926 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +0200927 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100928 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200929 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200930
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200931 if has('win32')
932 call assert_equal('cmd', job_info(g:job).cmd[0])
933 else
934 call assert_equal(&shell, job_info(g:job).cmd[0])
935 endif
936
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200937 " ascii + composing
938 let txt = "a\u0308bc"
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[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200942 call term_sendkeys(buf, "\<cr>")
943 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200944 let l = term_scrape(buf, lnum[0] + 1)
945 call assert_equal("a\u0308", l[0].chars)
946 call assert_equal("b", l[1].chars)
947 call assert_equal("c", l[2].chars)
948
949 " multibyte + composing
950 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200951 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200952 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200953 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200954 call term_sendkeys(buf, "\<cr>")
955 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200956 let l = term_scrape(buf, lnum[1] + 1)
957 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar79ea6802020-05-17 15:09:27 +0200958 call assert_equal("\u304e", l[2].chars)
959 call assert_equal("\u304f\u3099", l[3].chars)
960 call assert_equal("\u3052", l[5].chars)
961 call assert_equal("\u3053\u3099", l[6].chars)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200962
963 " \u00a0 + composing
964 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200965 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200966 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200967 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200968 call term_sendkeys(buf, "\<cr>")
969 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200970 let l = term_scrape(buf, lnum[2] + 1)
971 call assert_equal("\u00a0\u0308", l[3].chars)
972
973 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200974 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200975 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100976 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200977 let &encoding = save_enc
978endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100979
980func Test_terminal_aucmd_on_close()
981 fun Nop()
982 let s:called = 1
983 endfun
984
985 aug repro
986 au!
987 au BufWinLeave * call Nop()
988 aug END
989
990 let [cmd, waittime] = s:get_sleep_cmd()
991
992 call assert_equal(1, winnr('$'))
993 new
994 call setline(1, ['one', 'two'])
995 exe 'term ++close ' . cmd
996 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200997 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100998 call assert_equal(1, s:called)
999 bwipe!
1000
1001 unlet s:called
1002 au! repro
1003 delfunc Nop
1004endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001005
1006func Test_terminal_term_start_empty_command()
1007 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001008 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001009 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001010 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001011 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001012 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001013 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001014 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001015 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001016 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001017 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001018 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001019 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001020 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001021 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001022 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001023 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001024 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001025 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1026 call assert_fails(cmd, 'E475:')
1027 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001028 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001029 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001030 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1031 call assert_fails(cmd, 'E475:')
1032 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1033 call assert_fails(cmd, 'E730:')
1034 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001035 if has('gui_running') || has('termguicolors')
1036 call assert_fails(cmd, 'E475:')
1037 else
1038 call assert_fails(cmd, 'E254:')
1039 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001040 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001041endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001042
1043func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001044 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001045
1046 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001047 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001048
Bram Moolenaar086eb872018-03-25 21:24:12 +02001049 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001050 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001051
Bram Moolenaar086eb872018-03-25 21:24:12 +02001052 " Request the cursor position.
1053 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001054
1055 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001056 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001057
Bram Moolenaar086eb872018-03-25 21:24:12 +02001058 " End "cat" gently.
1059 call term_sendkeys(buf, "\<CR>\<C-D>")
1060
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001061 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001062 exe buf . 'bwipe'
1063 unlet g:job
1064endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001065
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001066" Run Vim, start a terminal in that Vim with the kill argument,
1067" :qall works.
1068func Run_terminal_qall_kill(line1, line2)
1069 " 1. Open a terminal window and wait for the prompt to appear
1070 " 2. set kill using term_setkill()
1071 " 3. make Vim exit, it will kill the shell
1072 let after = [
1073 \ a:line1,
1074 \ 'let buf = bufnr("%")',
1075 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1076 \ ' sleep 10m',
1077 \ 'endwhile',
1078 \ a:line2,
1079 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1080 \ 'qall',
1081 \ ]
1082 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001083 return
1084 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001085 call assert_equal("done", readfile("Xdone")[0])
1086 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001087endfunc
1088
1089" Run Vim in a terminal, then start a terminal in that Vim with a kill
1090" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001091func Test_terminal_qall_kill_arg()
1092 call Run_terminal_qall_kill('term ++kill=kill', '')
1093endfunc
1094
1095" Run Vim, start a terminal in that Vim, set the kill argument with
1096" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001097func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001098 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001099endfunc
1100
1101" Run Vim, start a terminal in that Vim without the kill argument,
1102" check that :qall does not exit, :qall! does.
1103func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001104 let after =<< trim [CODE]
1105 term
1106 let buf = bufnr("%")
1107 while term_getline(buf, 1) =~ "^\\s*$"
1108 sleep 10m
1109 endwhile
1110 set nomore
1111 au VimLeavePre * call writefile(["too early"], "Xdone")
1112 qall
1113 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1114 cquit
1115 [CODE]
1116
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001117 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001118 return
1119 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001120 call assert_equal("done", readfile("Xdone")[0])
1121 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001122endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001123
1124" Run Vim in a terminal, then start a terminal in that Vim without a kill
1125" argument, check that :confirm qall works.
1126func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001127 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001128 let buf = RunVimInTerminal('', {})
1129
1130 " Open a terminal window and wait for the prompt to appear
1131 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001132 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1133 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001134
1135 " make Vim exit, it will prompt to kill the shell
1136 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001137 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001138 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001139 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001140
1141 " close the terminal window where Vim was running
1142 quit
1143endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001144
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001145" Run Vim in a terminal, then start a terminal window with a shell and check
1146" that Vim exits if it is closed.
1147func Test_terminal_exit()
1148 CheckRunVimInTerminal
1149
1150 let lines =<< trim END
1151 let winid = win_getid()
1152 help
1153 term
1154 let termid = win_getid()
1155 call win_gotoid(winid)
1156 close
1157 call win_gotoid(termid)
1158 END
1159 call writefile(lines, 'XtermExit')
1160 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1161 let job = term_getjob(buf)
1162 call WaitForAssert({-> assert_equal("run", job_status(job))})
1163
1164 " quit the shell, it will make Vim exit
1165 call term_sendkeys(buf, "exit\<CR>")
1166 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1167
1168 call delete('XtermExit')
1169endfunc
1170
Bram Moolenaar012eb662018-03-13 17:55:27 +01001171func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001172 augroup repro
1173 au!
1174 au TerminalOpen * let s:called += 1
1175 augroup END
1176
1177 let s:called = 0
1178
1179 " Open a terminal window with :terminal
1180 terminal
1181 call assert_equal(1, s:called)
1182 bwipe!
1183
1184 " Open a terminal window with term_start()
1185 call term_start(&shell)
1186 call assert_equal(2, s:called)
1187 bwipe!
1188
1189 " Open a hidden terminal buffer with :terminal
1190 terminal ++hidden
1191 call assert_equal(3, s:called)
1192 for buf in term_list()
1193 exe buf . "bwipe!"
1194 endfor
1195
1196 " Open a hidden terminal buffer with term_start()
1197 let buf = term_start(&shell, {'hidden': 1})
1198 call assert_equal(4, s:called)
1199 exe buf . "bwipe!"
1200
1201 unlet s:called
1202 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001203endfunc
1204
1205func Test_open_term_from_cmd()
1206 CheckUnix
1207 CheckRunVimInTerminal
1208
1209 let lines =<< trim END
1210 call setline(1, ['a', 'b', 'c'])
1211 3
1212 set incsearch
1213 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1214 END
1215 call writefile(lines, 'Xopenterm')
1216 let buf = RunVimInTerminal('-S Xopenterm', {})
1217
1218 " this opens a window, incsearch should not use the old cursor position
1219 call term_sendkeys(buf, "/\<F3>")
1220 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1221 call term_sendkeys(buf, "\<Esc>")
1222 call term_sendkeys(buf, ":q\<CR>")
1223
1224 call StopVimInTerminal(buf)
1225 call delete('Xopenterm')
1226endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001227
Bram Moolenaar02764712020-11-14 20:21:55 +01001228func Test_terminal_popup_with_cmd()
1229 " this was crashing
1230 let buf = term_start(&shell, #{hidden: v:true})
1231 let s:winid = popup_create(buf, {})
1232 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1233 call feedkeys("\<F3>", 'xt')
1234
1235 tunmap <F3>
1236 exe 'bwipe! ' .. buf
1237 unlet s:winid
1238endfunc
1239
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001240func Test_terminal_popup_bufload()
1241 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1242 let winid = popup_create(termbuf, {})
1243 sleep 50m
1244
1245 let newbuf = bufadd('')
1246 call bufload(newbuf)
1247 call setbufline(newbuf, 1, 'foobar')
1248
1249 " must not have switched to another window
1250 call assert_equal(winid, win_getid())
1251
Bram Moolenaare6329e42020-11-16 21:10:34 +01001252 call StopShellInTerminal(termbuf)
1253 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001254 exe 'bwipe! ' .. newbuf
1255endfunc
1256
Bram Moolenaare41decc2020-11-14 21:34:59 +01001257func Test_terminal_popup_insert_cmd()
1258 CheckUnix
1259
1260 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1261 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001262 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001263 endfunc
1264 call feedkeys("i\<F3>")
1265 sleep 10m
1266 call assert_equal('n', mode())
1267
1268 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001269 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001270 delfunc StartTermInPopup
1271 iunmap <F3>
1272endfunc
1273
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001274func Check_dump01(off)
1275 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1276 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001277 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001278endfunc
1279
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001280func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001281 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001282 let save_enc = &encoding
1283 set encoding=utf-8
1284 call assert_equal(1, winnr('$'))
1285
1286 let text = " a\u0300 e\u0302 o\u0308"
1287 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001288 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001289 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001290 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001291 let dumpline = readfile('Xdump')[0]
1292 call assert_match('|à| |ê| |ö', dumpline)
1293
1294 call StopVimInTerminal(buf)
1295 call delete('Xcomposing')
1296 call delete('Xdump')
1297 let &encoding = save_enc
1298endfunc
1299
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001300" Tests for failures in the term_dumpwrite() function
1301func Test_terminal_dumpwrite_errors()
1302 CheckRunVimInTerminal
1303 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1304 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001305 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001306 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1307 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1308 call writefile([], 'Xtest.dump')
1309 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1310 call delete('Xtest.dump')
1311 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1312 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001313 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001314 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001315 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001316 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1317 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1318 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1319endfunc
1320
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001321" just testing basic functionality.
1322func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001323 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001324 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001325 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001326 call assert_equal(2, winnr('$'))
1327 call assert_equal(20, line('$'))
1328 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001329
1330 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001331 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001332 call assert_equal(buf, buf2)
1333 call assert_notequal('one two three four five', trim(getline(1)))
1334
1335 " Load the first dump again in the same window
1336 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1337 call assert_equal(buf, buf2)
1338 call Check_dump01(0)
1339
1340 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1341 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1342 new
1343 let closedbuf = winbufnr('')
1344 quit
1345 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001346 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001347 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001348
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001349 quit
1350endfunc
1351
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001352func Test_terminal_dumpload_dump()
1353 CheckRunVimInTerminal
1354
1355 let lines =<< trim END
1356 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1357 END
1358 call writefile(lines, 'XtermDumpload')
1359 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1360 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1361
1362 call StopVimInTerminal(buf)
1363 call delete('XtermDumpload')
1364endfunc
1365
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001366func Test_terminal_dumpdiff()
1367 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001368 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001369 call assert_equal(2, winnr('$'))
1370 call assert_equal(62, line('$'))
1371 call Check_dump01(0)
1372 call Check_dump01(42)
1373 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1374 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001375
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001376 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001377 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1378 call writefile([], 'X1.dump')
1379 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1380 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001381endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001382
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001383func Test_terminal_dumpdiff_swap()
1384 call assert_equal(1, winnr('$'))
1385 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1386 call assert_equal(2, winnr('$'))
1387 call assert_equal(62, line('$'))
1388 call assert_match('Test_popup_command_01.dump', getline(21))
1389 call assert_match('Test_popup_command_03.dump', getline(42))
1390 call assert_match('Undo', getline(3))
1391 call assert_match('three four five', getline(45))
1392
1393 normal s
1394 call assert_match('Test_popup_command_03.dump', getline(21))
1395 call assert_match('Test_popup_command_01.dump', getline(42))
1396 call assert_match('three four five', getline(3))
1397 call assert_match('Undo', getline(45))
1398 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001399
1400 " Diff two terminal dump files with different number of rows
1401 " Swap the diffs
1402 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1403 call assert_match('Test_popup_command_01.dump', getline(21))
1404 call assert_match('Test_winline_rnu.dump', getline(42))
1405 normal s
1406 call assert_match('Test_winline_rnu.dump', getline(6))
1407 call assert_match('Test_popup_command_01.dump', getline(27))
1408 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001409endfunc
1410
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001411func Test_terminal_dumpdiff_options()
1412 set laststatus=0
1413 call assert_equal(1, winnr('$'))
1414 let height = winheight(0)
1415 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1416 call assert_equal(2, winnr('$'))
1417 call assert_equal(height, winheight(winnr()))
1418 call assert_equal(33, winwidth(winnr()))
1419 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1420 quit
1421
1422 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001423 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1424 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001425 call assert_equal(&columns, winwidth(0))
1426 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001427 call assert_equal('something else', bufname('%'))
1428 quit
1429
1430 call assert_equal(1, winnr('$'))
1431 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1432 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001433 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 +01001434 bwipe
1435
1436 set laststatus&
1437endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001438
Bram Moolenaar10772302019-01-20 18:25:54 +01001439" When drawing the statusline the cursor position may not have been updated
1440" yet.
1441" 1. create a terminal, make it show 2 lines
1442" 2. 0.5 sec later: leave terminal window, execute "i"
1443" 3. 0.5 sec later: clear terminal window, now it's 1 line
1444" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1445" 4. 0.5 sec later: should be done, clean up
1446func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001447 CheckUnix
1448
Bram Moolenaar10772302019-01-20 18:25:54 +01001449 set statusline=x
1450 terminal
1451 let tbuf = bufnr('')
1452 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1453 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1454 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1455 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1456
1457 sleep 2
1458 exe tbuf . 'bwipe!'
1459 au! BufLeave
1460 set statusline=
1461endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001462
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001463func Test_terminal_window_focus()
1464 let winid1 = win_getid()
1465 terminal
1466 let winid2 = win_getid()
1467 call feedkeys("\<C-W>j", 'xt')
1468 call assert_equal(winid1, win_getid())
1469 call feedkeys("\<C-W>k", 'xt')
1470 call assert_equal(winid2, win_getid())
1471 " can use a cursor key here
1472 call feedkeys("\<C-W>\<Down>", 'xt')
1473 call assert_equal(winid1, win_getid())
1474 call feedkeys("\<C-W>\<Up>", 'xt')
1475 call assert_equal(winid2, win_getid())
1476
1477 bwipe!
1478endfunc
1479
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001480func Api_drop_common(options)
1481 call assert_equal(1, winnr('$'))
1482
1483 " Use the title termcap entries to output the escape sequence.
1484 call writefile([
1485 \ 'set title',
1486 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1487 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1488 \ 'redraw',
1489 \ "set t_ts=",
1490 \ ], 'Xscript')
1491 let buf = RunVimInTerminal('-S Xscript', {})
1492 call WaitFor({-> bufnr('Xtextfile') > 0})
1493 call assert_equal('Xtextfile', expand('%:t'))
1494 call assert_true(winnr('$') >= 3)
1495 return buf
1496endfunc
1497
1498func Test_terminal_api_drop_newwin()
1499 CheckRunVimInTerminal
1500 let buf = Api_drop_common('')
1501 call assert_equal(0, &bin)
1502 call assert_equal('', &fenc)
1503
1504 call StopVimInTerminal(buf)
1505 call delete('Xscript')
1506 bwipe Xtextfile
1507endfunc
1508
1509func Test_terminal_api_drop_newwin_bin()
1510 CheckRunVimInTerminal
1511 let buf = Api_drop_common(',{"bin":1}')
1512 call assert_equal(1, &bin)
1513
1514 call StopVimInTerminal(buf)
1515 call delete('Xscript')
1516 bwipe Xtextfile
1517endfunc
1518
1519func Test_terminal_api_drop_newwin_binary()
1520 CheckRunVimInTerminal
1521 let buf = Api_drop_common(',{"binary":1}')
1522 call assert_equal(1, &bin)
1523
1524 call StopVimInTerminal(buf)
1525 call delete('Xscript')
1526 bwipe Xtextfile
1527endfunc
1528
1529func Test_terminal_api_drop_newwin_nobin()
1530 CheckRunVimInTerminal
1531 set binary
1532 let buf = Api_drop_common(',{"nobin":1}')
1533 call assert_equal(0, &bin)
1534
1535 call StopVimInTerminal(buf)
1536 call delete('Xscript')
1537 bwipe Xtextfile
1538 set nobinary
1539endfunc
1540
1541func Test_terminal_api_drop_newwin_nobinary()
1542 CheckRunVimInTerminal
1543 set binary
1544 let buf = Api_drop_common(',{"nobinary":1}')
1545 call assert_equal(0, &bin)
1546
1547 call StopVimInTerminal(buf)
1548 call delete('Xscript')
1549 bwipe Xtextfile
1550 set nobinary
1551endfunc
1552
1553func Test_terminal_api_drop_newwin_ff()
1554 CheckRunVimInTerminal
1555 let buf = Api_drop_common(',{"ff":"dos"}')
1556 call assert_equal("dos", &ff)
1557
1558 call StopVimInTerminal(buf)
1559 call delete('Xscript')
1560 bwipe Xtextfile
1561endfunc
1562
1563func Test_terminal_api_drop_newwin_fileformat()
1564 CheckRunVimInTerminal
1565 let buf = Api_drop_common(',{"fileformat":"dos"}')
1566 call assert_equal("dos", &ff)
1567
1568 call StopVimInTerminal(buf)
1569 call delete('Xscript')
1570 bwipe Xtextfile
1571endfunc
1572
1573func Test_terminal_api_drop_newwin_enc()
1574 CheckRunVimInTerminal
1575 let buf = Api_drop_common(',{"enc":"utf-16"}')
1576 call assert_equal("utf-16", &fenc)
1577
1578 call StopVimInTerminal(buf)
1579 call delete('Xscript')
1580 bwipe Xtextfile
1581endfunc
1582
1583func Test_terminal_api_drop_newwin_encoding()
1584 CheckRunVimInTerminal
1585 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1586 call assert_equal("utf-16", &fenc)
1587
1588 call StopVimInTerminal(buf)
1589 call delete('Xscript')
1590 bwipe Xtextfile
1591endfunc
1592
1593func Test_terminal_api_drop_oldwin()
1594 CheckRunVimInTerminal
1595 let firstwinid = win_getid()
1596 split Xtextfile
1597 let textfile_winid = win_getid()
1598 call assert_equal(2, winnr('$'))
1599 call win_gotoid(firstwinid)
1600
1601 " Use the title termcap entries to output the escape sequence.
1602 call writefile([
1603 \ 'set title',
1604 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1605 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1606 \ 'redraw',
1607 \ "set t_ts=",
1608 \ ], 'Xscript')
1609 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1610 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1611 call assert_equal(textfile_winid, win_getid())
1612
1613 call StopVimInTerminal(buf)
1614 call delete('Xscript')
1615 bwipe Xtextfile
1616endfunc
1617
1618func Tapi_TryThis(bufnum, arg)
1619 let g:called_bufnum = a:bufnum
1620 let g:called_arg = a:arg
1621endfunc
1622
1623func WriteApiCall(funcname)
1624 " Use the title termcap entries to output the escape sequence.
1625 call writefile([
1626 \ 'set title',
1627 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1628 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1629 \ 'redraw',
1630 \ "set t_ts=",
1631 \ ], 'Xscript')
1632endfunc
1633
1634func Test_terminal_api_call()
1635 CheckRunVimInTerminal
1636
1637 unlet! g:called_bufnum
1638 unlet! g:called_arg
1639
1640 call WriteApiCall('Tapi_TryThis')
1641
1642 " Default
1643 let buf = RunVimInTerminal('-S Xscript', {})
1644 call WaitFor({-> exists('g:called_bufnum')})
1645 call assert_equal(buf, g:called_bufnum)
1646 call assert_equal(['hello', 123], g:called_arg)
1647 call StopVimInTerminal(buf)
1648
1649 unlet! g:called_bufnum
1650 unlet! g:called_arg
1651
1652 " Enable explicitly
1653 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1654 call WaitFor({-> exists('g:called_bufnum')})
1655 call assert_equal(buf, g:called_bufnum)
1656 call assert_equal(['hello', 123], g:called_arg)
1657 call StopVimInTerminal(buf)
1658
1659 unlet! g:called_bufnum
1660 unlet! g:called_arg
1661
1662 func! ApiCall_TryThis(bufnum, arg)
1663 let g:called_bufnum2 = a:bufnum
1664 let g:called_arg2 = a:arg
1665 endfunc
1666
1667 call WriteApiCall('ApiCall_TryThis')
1668
1669 " Use prefix match
1670 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1671 call WaitFor({-> exists('g:called_bufnum2')})
1672 call assert_equal(buf, g:called_bufnum2)
1673 call assert_equal(['hello', 123], g:called_arg2)
1674 call StopVimInTerminal(buf)
1675
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001676 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001677
1678 unlet! g:called_bufnum2
1679 unlet! g:called_arg2
1680
1681 call delete('Xscript')
1682 delfunction! ApiCall_TryThis
1683 unlet! g:called_bufnum2
1684 unlet! g:called_arg2
1685endfunc
1686
1687func Test_terminal_api_call_fails()
1688 CheckRunVimInTerminal
1689
1690 func! TryThis(bufnum, arg)
1691 let g:called_bufnum3 = a:bufnum
1692 let g:called_arg3 = a:arg
1693 endfunc
1694
1695 call WriteApiCall('TryThis')
1696
1697 unlet! g:called_bufnum3
1698 unlet! g:called_arg3
1699
1700 " Not permitted
1701 call ch_logfile('Xlog', 'w')
1702 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1703 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1704 call assert_false(exists('g:called_bufnum3'))
1705 call assert_false(exists('g:called_arg3'))
1706 call StopVimInTerminal(buf)
1707
1708 " No match
1709 call ch_logfile('Xlog', 'w')
1710 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1711 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1712 call assert_false(exists('g:called_bufnum3'))
1713 call assert_false(exists('g:called_arg3'))
1714 call StopVimInTerminal(buf)
1715
1716 call delete('Xscript')
1717 call ch_logfile('')
1718 call delete('Xlog')
1719 delfunction! TryThis
1720 unlet! g:called_bufnum3
1721 unlet! g:called_arg3
1722endfunc
1723
1724let s:caught_e937 = 0
1725
1726func Tapi_Delete(bufnum, arg)
1727 try
1728 execute 'bdelete!' a:bufnum
1729 catch /E937:/
1730 let s:caught_e937 = 1
1731 endtry
1732endfunc
1733
1734func Test_terminal_api_call_fail_delete()
1735 CheckRunVimInTerminal
1736
1737 call WriteApiCall('Tapi_Delete')
1738 let buf = RunVimInTerminal('-S Xscript', {})
1739 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1740
1741 call StopVimInTerminal(buf)
1742 call delete('Xscript')
1743 call ch_logfile('', '')
1744endfunc
1745
1746func Test_terminal_setapi_and_call()
1747 CheckRunVimInTerminal
1748
1749 call WriteApiCall('Tapi_TryThis')
1750 call ch_logfile('Xlog', 'w')
1751
1752 unlet! g:called_bufnum
1753 unlet! g:called_arg
1754
1755 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1756 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1757 call assert_false(exists('g:called_bufnum'))
1758 call assert_false(exists('g:called_arg'))
1759
1760 eval buf->term_setapi('Tapi_')
1761 call term_sendkeys(buf, ":set notitle\<CR>")
1762 call term_sendkeys(buf, ":source Xscript\<CR>")
1763 call WaitFor({-> exists('g:called_bufnum')})
1764 call assert_equal(buf, g:called_bufnum)
1765 call assert_equal(['hello', 123], g:called_arg)
1766
1767 call StopVimInTerminal(buf)
1768
1769 call delete('Xscript')
1770 call ch_logfile('')
1771 call delete('Xlog')
1772 unlet! g:called_bufnum
1773 unlet! g:called_arg
1774endfunc
1775
1776func Test_terminal_api_arg()
1777 CheckRunVimInTerminal
1778
1779 call WriteApiCall('Tapi_TryThis')
1780 call ch_logfile('Xlog', 'w')
1781
1782 unlet! g:called_bufnum
1783 unlet! g:called_arg
1784
1785 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1786 let buf = bufnr('%')
1787 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1788 call assert_false(exists('g:called_bufnum'))
1789 call assert_false(exists('g:called_arg'))
1790
1791 call StopVimInTerminal(buf)
1792
1793 call ch_logfile('Xlog', 'w')
1794
1795 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1796 let buf = bufnr('%')
1797 call WaitFor({-> exists('g:called_bufnum')})
1798 call assert_equal(buf, g:called_bufnum)
1799 call assert_equal(['hello', 123], g:called_arg)
1800
1801 call StopVimInTerminal(buf)
1802
1803 call delete('Xscript')
1804 call ch_logfile('')
1805 call delete('Xlog')
1806 unlet! g:called_bufnum
1807 unlet! g:called_arg
1808endfunc
1809
1810func Test_terminal_ansicolors_default()
1811 CheckFunction term_getansicolors
1812
1813 let colors = [
1814 \ '#000000', '#e00000',
1815 \ '#00e000', '#e0e000',
1816 \ '#0000e0', '#e000e0',
1817 \ '#00e0e0', '#e0e0e0',
1818 \ '#808080', '#ff4040',
1819 \ '#40ff40', '#ffff40',
1820 \ '#4040ff', '#ff40ff',
1821 \ '#40ffff', '#ffffff',
1822 \]
1823
1824 let buf = Run_shell_in_terminal({})
1825 call assert_equal(colors, term_getansicolors(buf))
1826 call StopShellInTerminal(buf)
1827 call TermWait(buf)
1828 call assert_equal([], term_getansicolors(buf))
1829
1830 exe buf . 'bwipe'
1831endfunc
1832
1833let s:test_colors = [
1834 \ '#616e64', '#0d0a79',
1835 \ '#6d610d', '#0a7373',
1836 \ '#690d0a', '#6d696e',
1837 \ '#0d0a6f', '#616e0d',
1838 \ '#0a6479', '#6d0d0a',
1839 \ '#617373', '#0d0a69',
1840 \ '#6d690d', '#0a6e6f',
1841 \ '#610d0a', '#6e6479',
1842 \]
1843
1844func Test_terminal_ansicolors_global()
1845 CheckFeature termguicolors
1846 CheckFunction term_getansicolors
1847
1848 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1849 let buf = Run_shell_in_terminal({})
1850 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1851 call StopShellInTerminal(buf)
1852 call TermWait(buf)
1853
1854 exe buf . 'bwipe'
1855 unlet g:terminal_ansi_colors
1856endfunc
1857
1858func Test_terminal_ansicolors_func()
1859 CheckFeature termguicolors
1860 CheckFunction term_getansicolors
1861
1862 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1863 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1864 call assert_equal(s:test_colors, term_getansicolors(buf))
1865
1866 call term_setansicolors(buf, g:terminal_ansi_colors)
1867 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
1868
1869 let colors = [
1870 \ 'ivory', 'AliceBlue',
1871 \ 'grey67', 'dark goldenrod',
1872 \ 'SteelBlue3', 'PaleVioletRed4',
1873 \ 'MediumPurple2', 'yellow2',
1874 \ 'RosyBrown3', 'OrangeRed2',
1875 \ 'white smoke', 'navy blue',
1876 \ 'grey47', 'gray97',
1877 \ 'MistyRose2', 'DodgerBlue4',
1878 \]
1879 eval buf->term_setansicolors(colors)
1880
1881 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001882 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001883 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
1884
1885 call StopShellInTerminal(buf)
1886 call TermWait(buf)
1887 call assert_equal(0, term_setansicolors(buf, []))
1888 exe buf . 'bwipe'
1889endfunc
1890
1891func Test_terminal_all_ansi_colors()
1892 CheckRunVimInTerminal
1893
1894 " Use all the ANSI colors.
1895 call writefile([
1896 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
1897 \ 'hi Tblack ctermfg=0 ctermbg=8',
1898 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1899 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1900 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1901 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1902 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1903 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1904 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1905 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1906 \ 'hi Tred ctermfg=9 ctermbg=1',
1907 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1908 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1909 \ 'hi Tblue ctermfg=12 ctermbg=4',
1910 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1911 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1912 \ 'hi Twhite ctermfg=15 ctermbg=7',
1913 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1914 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1915 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
1916 \ '',
1917 \ 'call matchadd("Tblack", "A")',
1918 \ 'call matchadd("Tdarkred", "B")',
1919 \ 'call matchadd("Tdarkgreen", "C")',
1920 \ 'call matchadd("Tbrown", "D")',
1921 \ 'call matchadd("Tdarkblue", "E")',
1922 \ 'call matchadd("Tdarkmagenta", "F")',
1923 \ 'call matchadd("Tdarkcyan", "G")',
1924 \ 'call matchadd("Tlightgrey", "H")',
1925 \ 'call matchadd("Tdarkgrey", "I")',
1926 \ 'call matchadd("Tred", "J")',
1927 \ 'call matchadd("Tgreen", "K")',
1928 \ 'call matchadd("Tyellow", "L")',
1929 \ 'call matchadd("Tblue", "M")',
1930 \ 'call matchadd("Tmagenta", "N")',
1931 \ 'call matchadd("Tcyan", "O")',
1932 \ 'call matchadd("Twhite", "P")',
1933 \ 'call matchadd("TdarkredBold", "X")',
1934 \ 'call matchadd("TgreenBold", "Y")',
1935 \ 'call matchadd("TmagentaBold", "Z")',
1936 \ 'redraw',
1937 \ ], 'Xcolorscript')
1938 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1939 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1940
1941 call term_sendkeys(buf, ":q\<CR>")
1942 call StopVimInTerminal(buf)
1943 call delete('Xcolorscript')
1944endfunc
1945
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001946
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001947" vim: shiftwidth=2 sts=2 expandtab