blob: ece8120d58b194ed0ff3235963275d29501c190e [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
570 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
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))
574 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200575endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200576
577func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100578 if has('win32')
579 let cmd = 'cmd /c cd'
580 else
581 CheckExecutable pwd
582 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200583 endif
584 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100585 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200586 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200587
588 exe buf . 'bwipe'
589 call delete('Xdir', 'rf')
590endfunc
591
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200592func Test_terminal_cwd_failure()
593 " Case 1: Provided directory is not actually a directory. Attempt to make
594 " the file executable as well.
595 call writefile([], 'Xfile')
596 call setfperm('Xfile', 'rwx------')
597 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
598 call delete('Xfile')
599
600 " Case 2: Directory does not exist.
601 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
602
603 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100604 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200605 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100606 call mkdir('XdirNoAccess', '', '0600')
607 " return early if the directory permissions could not be set properly
608 if getfperm('XdirNoAccess')[2] == 'x'
609 call delete('XdirNoAccess', 'rf')
610 return
611 endif
612 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
613 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200614 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200615endfunc
616
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100617func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200618 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200619 call s:test_environment("VIM_SERVERNAME", v:servername)
620endfunc
621
622func Test_terminal_version()
623 call s:test_environment("VIM_TERMINAL", string(v:version))
624endfunc
625
626func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100627 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100628 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200629 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100630 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200631 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100632 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200633 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100634 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200635 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200636 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200637 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100638
Bram Moolenaar012eb662018-03-13 17:55:27 +0100639 exe buf . 'bwipe'
640 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100641endfunc
642
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200643func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100644 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200645 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200646 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100647 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100648 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100649 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100650 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100651 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200652 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200653 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200654 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200655
Bram Moolenaar012eb662018-03-13 17:55:27 +0100656 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200657endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200658
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200659func Test_terminal_list_args()
660 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100661 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200662 exe buf . 'bwipe!'
663 call assert_equal("", bufname(buf))
664endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200665
666func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100667 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100668 let wait_time = 5000
669 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100670 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200671 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100672 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200673 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100674 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100675 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100676 " NPFS is used in Windows, nonblocking mode does not work properly.
677 elseif has('win32')
678 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200679 else
680 let len = 5000
681 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200682
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100683 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100684 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100685 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200686 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100687 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200688
689 " On MS-Windows there is an extra empty line below "done". Find "done" in
690 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100691 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100692 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100693 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200694 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100695 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200696 endif
697 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200698
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100699 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200700 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200701 call TermWait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200702 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200703 bwipe
704endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200705
706func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100707 " TODO: enable once writing to stdin works on MS-Windows
708 CheckNotMSWindows
709 CheckExecutable wc
710
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200711 call setline(1, ['one', 'two', 'three'])
712 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200713 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200714 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200715 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100716 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200717
718 call setline(1, ['one', 'two', 'three', 'four'])
719 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200720 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200721 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200722 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100723 %bwipe!
724endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200725
Bram Moolenaar21109272020-01-30 16:27:20 +0100726func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200727 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200728
Bram Moolenaar21109272020-01-30 16:27:20 +0100729 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200730 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100731 " MS-Windows echoes the input, Unix doesn't.
732 if has('win32')
733 call WaitFor({-> getline('$') =~ 'exit(123)'})
734 call assert_equal('hello', getline(line('$') - 1))
735 else
736 call WaitFor({-> getline('$') =~ 'hello'})
737 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200738 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100739 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
740 %bwipe!
741endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200742
Bram Moolenaar21109272020-01-30 16:27:20 +0100743func Test_terminal_eof_arg_win32_ctrl_z()
744 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200745 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100746
747 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200748 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100749 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
750 call assert_match('\^Z', getline(line('$') - 1))
751 %bwipe!
752endfunc
753
754func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200755 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100756
757 " Check the last specified ++eof arg is used and should not memory leak.
758 new
759 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200760 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100761 " MS-Windows echoes the input, Unix doesn't.
762 if has('win32')
763 call WaitFor({-> getline('$') =~ 'exit(123)'})
764 call assert_equal('hello', getline(line('$') - 1))
765 else
766 call WaitFor({-> getline('$') =~ 'hello'})
767 call assert_equal('hello', getline('$'))
768 endif
769 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
770 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200771endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200772
773func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200774 let buf = term_start('NONE', {})
775 call assert_notequal(0, buf)
776
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200777 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200778 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100779 if has('gui_running') && !has('win32')
780 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200781 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100782 else
783 " Otherwise using a job works on all systems.
784 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200785 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200786 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200787
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200788 bwipe!
789endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200790
791func Test_terminal_special_chars()
792 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200793 CheckUnix
794
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200795 call mkdir('Xdir with spaces')
796 call writefile(['x'], 'Xdir with spaces/quoted"file')
797 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200798 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100799 " make sure the job has finished
800 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200801
802 call delete('Xdir with spaces', 'rf')
803 bwipe
804endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200805
806func Test_terminal_wrong_options()
807 call assert_fails('call term_start(&shell, {
808 \ "in_io": "file",
809 \ "in_name": "xxx",
810 \ "out_io": "file",
811 \ "out_name": "xxx",
812 \ "err_io": "file",
813 \ "err_name": "xxx"
814 \ })', 'E474:')
815 call assert_fails('call term_start(&shell, {
816 \ "out_buf": bufnr("%")
817 \ })', 'E474:')
818 call assert_fails('call term_start(&shell, {
819 \ "err_buf": bufnr("%")
820 \ })', 'E474:')
821endfunc
822
823func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200824 let cmd = Get_cat_123_cmd()
825 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200826 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100827 " ConPTY may precede escape sequence. There are things that are not so.
828 if !has('conpty')
829 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
830 call assert_match('123', readfile('Xfile')[0])
831 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200832 let g:job = term_getjob(buf)
833 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +0200834
835 if has('win32')
836 " On Windows we cannot delete a file being used by a process. When
837 " job_status() returns "dead", the process remains for a short time.
838 " Just wait for a moment.
839 sleep 50m
840 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200841 call delete('Xfile')
842 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200843
844 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200845 call writefile(['one line'], 'Xfile')
846 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200847 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200848 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200849 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200850 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200851 bwipe
852 call delete('Xfile')
853 endif
854endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200855
856func TerminalTmap(remap)
857 let buf = Run_shell_in_terminal({})
858 call assert_equal('t', mode())
859
860 if a:remap
861 tmap 123 456
862 else
863 tnoremap 123 456
864 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100865 " don't use abcde, it's an existing command
866 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200867 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100868 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200869 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200870 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200871 let lnum = term_getcursor(buf)[0]
872 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100873 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200874 else
875 call assert_match('456', term_getline(buf, lnum))
876 endif
877
878 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200879 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200880 call TermWait(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200881
882 tunmap 123
883 tunmap 456
884 call assert_equal('', maparg('123', 't'))
885 close
886 unlet g:job
887endfunc
888
889func Test_terminal_tmap()
890 call TerminalTmap(1)
891 call TerminalTmap(0)
892endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200893
894func Test_terminal_wall()
895 let buf = Run_shell_in_terminal({})
896 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200897 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200898 call TermWait(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200899 exe buf . 'bwipe'
900 unlet g:job
901endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200902
Bram Moolenaar7a760922018-02-19 23:10:02 +0100903func Test_terminal_wqall()
904 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200905 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200906 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200907 call TermWait(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100908 exe buf . 'bwipe'
909 unlet g:job
910endfunc
911
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200912func Test_terminal_composing_unicode()
913 let save_enc = &encoding
914 set encoding=utf-8
915
916 if has('win32')
917 let cmd = "cmd /K chcp 65001"
918 let lnum = [3, 6, 9]
919 else
920 let cmd = &shell
921 let lnum = [1, 3, 5]
922 endif
923
924 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +0200925 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100926 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200927 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200928
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200929 if has('win32')
930 call assert_equal('cmd', job_info(g:job).cmd[0])
931 else
932 call assert_equal(&shell, job_info(g:job).cmd[0])
933 endif
934
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200935 " ascii + composing
936 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200937 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200938 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200939 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200940 call term_sendkeys(buf, "\<cr>")
941 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200942 let l = term_scrape(buf, lnum[0] + 1)
943 call assert_equal("a\u0308", l[0].chars)
944 call assert_equal("b", l[1].chars)
945 call assert_equal("c", l[2].chars)
946
947 " multibyte + composing
948 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200949 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200950 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200951 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200952 call term_sendkeys(buf, "\<cr>")
953 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200954 let l = term_scrape(buf, lnum[1] + 1)
955 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar79ea6802020-05-17 15:09:27 +0200956 call assert_equal("\u304e", l[2].chars)
957 call assert_equal("\u304f\u3099", l[3].chars)
958 call assert_equal("\u3052", l[5].chars)
959 call assert_equal("\u3053\u3099", l[6].chars)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200960
961 " \u00a0 + composing
962 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200963 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200964 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200965 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200966 call term_sendkeys(buf, "\<cr>")
967 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200968 let l = term_scrape(buf, lnum[2] + 1)
969 call assert_equal("\u00a0\u0308", l[3].chars)
970
971 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200972 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200973 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100974 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200975 let &encoding = save_enc
976endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100977
978func Test_terminal_aucmd_on_close()
979 fun Nop()
980 let s:called = 1
981 endfun
982
983 aug repro
984 au!
985 au BufWinLeave * call Nop()
986 aug END
987
988 let [cmd, waittime] = s:get_sleep_cmd()
989
990 call assert_equal(1, winnr('$'))
991 new
992 call setline(1, ['one', 'two'])
993 exe 'term ++close ' . cmd
994 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200995 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100996 call assert_equal(1, s:called)
997 bwipe!
998
999 unlet s:called
1000 au! repro
1001 delfunc Nop
1002endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001003
1004func Test_terminal_term_start_empty_command()
1005 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001006 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001007 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(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001012 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001013 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001014 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001015 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001016 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001017 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001018 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001019 let cmd = "call term_start('', {'term_kill' : []})"
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('', {'tty_type' : []})"
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' : 'abc'})"
1024 call assert_fails(cmd, 'E475:')
1025 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001026 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001027 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001028 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1029 call assert_fails(cmd, 'E475:')
1030 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1031 call assert_fails(cmd, 'E730:')
1032 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001033 if has('gui_running') || has('termguicolors')
1034 call assert_fails(cmd, 'E475:')
1035 else
1036 call assert_fails(cmd, 'E254:')
1037 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001038 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001039endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001040
1041func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001042 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001043
1044 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001045 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001046
Bram Moolenaar086eb872018-03-25 21:24:12 +02001047 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001048 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001049
Bram Moolenaar086eb872018-03-25 21:24:12 +02001050 " Request the cursor position.
1051 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001052
1053 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001054 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001055
Bram Moolenaar086eb872018-03-25 21:24:12 +02001056 " End "cat" gently.
1057 call term_sendkeys(buf, "\<CR>\<C-D>")
1058
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001059 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001060 exe buf . 'bwipe'
1061 unlet g:job
1062endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001063
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001064" Run Vim, start a terminal in that Vim with the kill argument,
1065" :qall works.
1066func Run_terminal_qall_kill(line1, line2)
1067 " 1. Open a terminal window and wait for the prompt to appear
1068 " 2. set kill using term_setkill()
1069 " 3. make Vim exit, it will kill the shell
1070 let after = [
1071 \ a:line1,
1072 \ 'let buf = bufnr("%")',
1073 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1074 \ ' sleep 10m',
1075 \ 'endwhile',
1076 \ a:line2,
1077 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1078 \ 'qall',
1079 \ ]
1080 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001081 return
1082 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001083 call assert_equal("done", readfile("Xdone")[0])
1084 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001085endfunc
1086
1087" Run Vim in a terminal, then start a terminal in that Vim with a kill
1088" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001089func Test_terminal_qall_kill_arg()
1090 call Run_terminal_qall_kill('term ++kill=kill', '')
1091endfunc
1092
1093" Run Vim, start a terminal in that Vim, set the kill argument with
1094" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001095func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001096 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001097endfunc
1098
1099" Run Vim, start a terminal in that Vim without the kill argument,
1100" check that :qall does not exit, :qall! does.
1101func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001102 let after =<< trim [CODE]
1103 term
1104 let buf = bufnr("%")
1105 while term_getline(buf, 1) =~ "^\\s*$"
1106 sleep 10m
1107 endwhile
1108 set nomore
1109 au VimLeavePre * call writefile(["too early"], "Xdone")
1110 qall
1111 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1112 cquit
1113 [CODE]
1114
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001115 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001116 return
1117 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001118 call assert_equal("done", readfile("Xdone")[0])
1119 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001120endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001121
1122" Run Vim in a terminal, then start a terminal in that Vim without a kill
1123" argument, check that :confirm qall works.
1124func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001125 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001126 let buf = RunVimInTerminal('', {})
1127
1128 " Open a terminal window and wait for the prompt to appear
1129 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001130 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1131 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001132
1133 " make Vim exit, it will prompt to kill the shell
1134 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001135 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001136 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001137 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001138
1139 " close the terminal window where Vim was running
1140 quit
1141endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001142
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001143" Run Vim in a terminal, then start a terminal window with a shell and check
1144" that Vim exits if it is closed.
1145func Test_terminal_exit()
1146 CheckRunVimInTerminal
1147
1148 let lines =<< trim END
1149 let winid = win_getid()
1150 help
1151 term
1152 let termid = win_getid()
1153 call win_gotoid(winid)
1154 close
1155 call win_gotoid(termid)
1156 END
1157 call writefile(lines, 'XtermExit')
1158 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1159 let job = term_getjob(buf)
1160 call WaitForAssert({-> assert_equal("run", job_status(job))})
1161
1162 " quit the shell, it will make Vim exit
1163 call term_sendkeys(buf, "exit\<CR>")
1164 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1165
1166 call delete('XtermExit')
1167endfunc
1168
Bram Moolenaar012eb662018-03-13 17:55:27 +01001169func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001170 augroup repro
1171 au!
1172 au TerminalOpen * let s:called += 1
1173 augroup END
1174
1175 let s:called = 0
1176
1177 " Open a terminal window with :terminal
1178 terminal
1179 call assert_equal(1, s:called)
1180 bwipe!
1181
1182 " Open a terminal window with term_start()
1183 call term_start(&shell)
1184 call assert_equal(2, s:called)
1185 bwipe!
1186
1187 " Open a hidden terminal buffer with :terminal
1188 terminal ++hidden
1189 call assert_equal(3, s:called)
1190 for buf in term_list()
1191 exe buf . "bwipe!"
1192 endfor
1193
1194 " Open a hidden terminal buffer with term_start()
1195 let buf = term_start(&shell, {'hidden': 1})
1196 call assert_equal(4, s:called)
1197 exe buf . "bwipe!"
1198
1199 unlet s:called
1200 au! repro
1201endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001202
1203func Check_dump01(off)
1204 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1205 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001206 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001207endfunc
1208
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001209func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001210 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001211 let save_enc = &encoding
1212 set encoding=utf-8
1213 call assert_equal(1, winnr('$'))
1214
1215 let text = " a\u0300 e\u0302 o\u0308"
1216 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001217 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001218 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001219 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001220 let dumpline = readfile('Xdump')[0]
1221 call assert_match('|à| |ê| |ö', dumpline)
1222
1223 call StopVimInTerminal(buf)
1224 call delete('Xcomposing')
1225 call delete('Xdump')
1226 let &encoding = save_enc
1227endfunc
1228
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001229" Tests for failures in the term_dumpwrite() function
1230func Test_terminal_dumpwrite_errors()
1231 CheckRunVimInTerminal
1232 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1233 let buf = RunVimInTerminal('', {})
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', '')", 'E715:')
1236 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1237 call writefile([], 'Xtest.dump')
1238 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1239 call delete('Xtest.dump')
1240 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1241 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001242 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001243 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001244 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001245 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1246 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1247 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1248endfunc
1249
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001250" just testing basic functionality.
1251func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001252 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001253 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001254 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001255 call assert_equal(2, winnr('$'))
1256 call assert_equal(20, line('$'))
1257 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001258
1259 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001260 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001261 call assert_equal(buf, buf2)
1262 call assert_notequal('one two three four five', trim(getline(1)))
1263
1264 " Load the first dump again in the same window
1265 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1266 call assert_equal(buf, buf2)
1267 call Check_dump01(0)
1268
1269 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1270 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1271 new
1272 let closedbuf = winbufnr('')
1273 quit
1274 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001275 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001276 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001277
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001278 quit
1279endfunc
1280
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001281func Test_terminal_dumpload_dump()
1282 CheckRunVimInTerminal
1283
1284 let lines =<< trim END
1285 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1286 END
1287 call writefile(lines, 'XtermDumpload')
1288 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1289 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1290
1291 call StopVimInTerminal(buf)
1292 call delete('XtermDumpload')
1293endfunc
1294
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001295func Test_terminal_dumpdiff()
1296 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001297 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001298 call assert_equal(2, winnr('$'))
1299 call assert_equal(62, line('$'))
1300 call Check_dump01(0)
1301 call Check_dump01(42)
1302 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1303 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001304
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001305 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001306 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1307 call writefile([], 'X1.dump')
1308 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1309 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001310endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001311
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001312func Test_terminal_dumpdiff_swap()
1313 call assert_equal(1, winnr('$'))
1314 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1315 call assert_equal(2, winnr('$'))
1316 call assert_equal(62, line('$'))
1317 call assert_match('Test_popup_command_01.dump', getline(21))
1318 call assert_match('Test_popup_command_03.dump', getline(42))
1319 call assert_match('Undo', getline(3))
1320 call assert_match('three four five', getline(45))
1321
1322 normal s
1323 call assert_match('Test_popup_command_03.dump', getline(21))
1324 call assert_match('Test_popup_command_01.dump', getline(42))
1325 call assert_match('three four five', getline(3))
1326 call assert_match('Undo', getline(45))
1327 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001328
1329 " Diff two terminal dump files with different number of rows
1330 " Swap the diffs
1331 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1332 call assert_match('Test_popup_command_01.dump', getline(21))
1333 call assert_match('Test_winline_rnu.dump', getline(42))
1334 normal s
1335 call assert_match('Test_winline_rnu.dump', getline(6))
1336 call assert_match('Test_popup_command_01.dump', getline(27))
1337 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001338endfunc
1339
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001340func Test_terminal_dumpdiff_options()
1341 set laststatus=0
1342 call assert_equal(1, winnr('$'))
1343 let height = winheight(0)
1344 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1345 call assert_equal(2, winnr('$'))
1346 call assert_equal(height, winheight(winnr()))
1347 call assert_equal(33, winwidth(winnr()))
1348 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1349 quit
1350
1351 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001352 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1353 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001354 call assert_equal(&columns, winwidth(0))
1355 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001356 call assert_equal('something else', bufname('%'))
1357 quit
1358
1359 call assert_equal(1, winnr('$'))
1360 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1361 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001362 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 +01001363 bwipe
1364
1365 set laststatus&
1366endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001367
Bram Moolenaar10772302019-01-20 18:25:54 +01001368" When drawing the statusline the cursor position may not have been updated
1369" yet.
1370" 1. create a terminal, make it show 2 lines
1371" 2. 0.5 sec later: leave terminal window, execute "i"
1372" 3. 0.5 sec later: clear terminal window, now it's 1 line
1373" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1374" 4. 0.5 sec later: should be done, clean up
1375func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001376 CheckUnix
1377
Bram Moolenaar10772302019-01-20 18:25:54 +01001378 set statusline=x
1379 terminal
1380 let tbuf = bufnr('')
1381 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1382 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1383 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1384 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1385
1386 sleep 2
1387 exe tbuf . 'bwipe!'
1388 au! BufLeave
1389 set statusline=
1390endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001391
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001392func Test_terminal_window_focus()
1393 let winid1 = win_getid()
1394 terminal
1395 let winid2 = win_getid()
1396 call feedkeys("\<C-W>j", 'xt')
1397 call assert_equal(winid1, win_getid())
1398 call feedkeys("\<C-W>k", 'xt')
1399 call assert_equal(winid2, win_getid())
1400 " can use a cursor key here
1401 call feedkeys("\<C-W>\<Down>", 'xt')
1402 call assert_equal(winid1, win_getid())
1403 call feedkeys("\<C-W>\<Up>", 'xt')
1404 call assert_equal(winid2, win_getid())
1405
1406 bwipe!
1407endfunc
1408
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001409func Api_drop_common(options)
1410 call assert_equal(1, winnr('$'))
1411
1412 " Use the title termcap entries to output the escape sequence.
1413 call writefile([
1414 \ 'set title',
1415 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1416 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1417 \ 'redraw',
1418 \ "set t_ts=",
1419 \ ], 'Xscript')
1420 let buf = RunVimInTerminal('-S Xscript', {})
1421 call WaitFor({-> bufnr('Xtextfile') > 0})
1422 call assert_equal('Xtextfile', expand('%:t'))
1423 call assert_true(winnr('$') >= 3)
1424 return buf
1425endfunc
1426
1427func Test_terminal_api_drop_newwin()
1428 CheckRunVimInTerminal
1429 let buf = Api_drop_common('')
1430 call assert_equal(0, &bin)
1431 call assert_equal('', &fenc)
1432
1433 call StopVimInTerminal(buf)
1434 call delete('Xscript')
1435 bwipe Xtextfile
1436endfunc
1437
1438func Test_terminal_api_drop_newwin_bin()
1439 CheckRunVimInTerminal
1440 let buf = Api_drop_common(',{"bin":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_binary()
1449 CheckRunVimInTerminal
1450 let buf = Api_drop_common(',{"binary":1}')
1451 call assert_equal(1, &bin)
1452
1453 call StopVimInTerminal(buf)
1454 call delete('Xscript')
1455 bwipe Xtextfile
1456endfunc
1457
1458func Test_terminal_api_drop_newwin_nobin()
1459 CheckRunVimInTerminal
1460 set binary
1461 let buf = Api_drop_common(',{"nobin":1}')
1462 call assert_equal(0, &bin)
1463
1464 call StopVimInTerminal(buf)
1465 call delete('Xscript')
1466 bwipe Xtextfile
1467 set nobinary
1468endfunc
1469
1470func Test_terminal_api_drop_newwin_nobinary()
1471 CheckRunVimInTerminal
1472 set binary
1473 let buf = Api_drop_common(',{"nobinary":1}')
1474 call assert_equal(0, &bin)
1475
1476 call StopVimInTerminal(buf)
1477 call delete('Xscript')
1478 bwipe Xtextfile
1479 set nobinary
1480endfunc
1481
1482func Test_terminal_api_drop_newwin_ff()
1483 CheckRunVimInTerminal
1484 let buf = Api_drop_common(',{"ff":"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_fileformat()
1493 CheckRunVimInTerminal
1494 let buf = Api_drop_common(',{"fileformat":"dos"}')
1495 call assert_equal("dos", &ff)
1496
1497 call StopVimInTerminal(buf)
1498 call delete('Xscript')
1499 bwipe Xtextfile
1500endfunc
1501
1502func Test_terminal_api_drop_newwin_enc()
1503 CheckRunVimInTerminal
1504 let buf = Api_drop_common(',{"enc":"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_newwin_encoding()
1513 CheckRunVimInTerminal
1514 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1515 call assert_equal("utf-16", &fenc)
1516
1517 call StopVimInTerminal(buf)
1518 call delete('Xscript')
1519 bwipe Xtextfile
1520endfunc
1521
1522func Test_terminal_api_drop_oldwin()
1523 CheckRunVimInTerminal
1524 let firstwinid = win_getid()
1525 split Xtextfile
1526 let textfile_winid = win_getid()
1527 call assert_equal(2, winnr('$'))
1528 call win_gotoid(firstwinid)
1529
1530 " Use the title termcap entries to output the escape sequence.
1531 call writefile([
1532 \ 'set title',
1533 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1534 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1535 \ 'redraw',
1536 \ "set t_ts=",
1537 \ ], 'Xscript')
1538 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1539 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1540 call assert_equal(textfile_winid, win_getid())
1541
1542 call StopVimInTerminal(buf)
1543 call delete('Xscript')
1544 bwipe Xtextfile
1545endfunc
1546
1547func Tapi_TryThis(bufnum, arg)
1548 let g:called_bufnum = a:bufnum
1549 let g:called_arg = a:arg
1550endfunc
1551
1552func WriteApiCall(funcname)
1553 " Use the title termcap entries to output the escape sequence.
1554 call writefile([
1555 \ 'set title',
1556 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1557 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1558 \ 'redraw',
1559 \ "set t_ts=",
1560 \ ], 'Xscript')
1561endfunc
1562
1563func Test_terminal_api_call()
1564 CheckRunVimInTerminal
1565
1566 unlet! g:called_bufnum
1567 unlet! g:called_arg
1568
1569 call WriteApiCall('Tapi_TryThis')
1570
1571 " Default
1572 let buf = RunVimInTerminal('-S Xscript', {})
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 " Enable explicitly
1582 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1583 call WaitFor({-> exists('g:called_bufnum')})
1584 call assert_equal(buf, g:called_bufnum)
1585 call assert_equal(['hello', 123], g:called_arg)
1586 call StopVimInTerminal(buf)
1587
1588 unlet! g:called_bufnum
1589 unlet! g:called_arg
1590
1591 func! ApiCall_TryThis(bufnum, arg)
1592 let g:called_bufnum2 = a:bufnum
1593 let g:called_arg2 = a:arg
1594 endfunc
1595
1596 call WriteApiCall('ApiCall_TryThis')
1597
1598 " Use prefix match
1599 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1600 call WaitFor({-> exists('g:called_bufnum2')})
1601 call assert_equal(buf, g:called_bufnum2)
1602 call assert_equal(['hello', 123], g:called_arg2)
1603 call StopVimInTerminal(buf)
1604
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001605 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001606
1607 unlet! g:called_bufnum2
1608 unlet! g:called_arg2
1609
1610 call delete('Xscript')
1611 delfunction! ApiCall_TryThis
1612 unlet! g:called_bufnum2
1613 unlet! g:called_arg2
1614endfunc
1615
1616func Test_terminal_api_call_fails()
1617 CheckRunVimInTerminal
1618
1619 func! TryThis(bufnum, arg)
1620 let g:called_bufnum3 = a:bufnum
1621 let g:called_arg3 = a:arg
1622 endfunc
1623
1624 call WriteApiCall('TryThis')
1625
1626 unlet! g:called_bufnum3
1627 unlet! g:called_arg3
1628
1629 " Not permitted
1630 call ch_logfile('Xlog', 'w')
1631 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1632 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1633 call assert_false(exists('g:called_bufnum3'))
1634 call assert_false(exists('g:called_arg3'))
1635 call StopVimInTerminal(buf)
1636
1637 " No match
1638 call ch_logfile('Xlog', 'w')
1639 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1640 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1641 call assert_false(exists('g:called_bufnum3'))
1642 call assert_false(exists('g:called_arg3'))
1643 call StopVimInTerminal(buf)
1644
1645 call delete('Xscript')
1646 call ch_logfile('')
1647 call delete('Xlog')
1648 delfunction! TryThis
1649 unlet! g:called_bufnum3
1650 unlet! g:called_arg3
1651endfunc
1652
1653let s:caught_e937 = 0
1654
1655func Tapi_Delete(bufnum, arg)
1656 try
1657 execute 'bdelete!' a:bufnum
1658 catch /E937:/
1659 let s:caught_e937 = 1
1660 endtry
1661endfunc
1662
1663func Test_terminal_api_call_fail_delete()
1664 CheckRunVimInTerminal
1665
1666 call WriteApiCall('Tapi_Delete')
1667 let buf = RunVimInTerminal('-S Xscript', {})
1668 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1669
1670 call StopVimInTerminal(buf)
1671 call delete('Xscript')
1672 call ch_logfile('', '')
1673endfunc
1674
1675func Test_terminal_setapi_and_call()
1676 CheckRunVimInTerminal
1677
1678 call WriteApiCall('Tapi_TryThis')
1679 call ch_logfile('Xlog', 'w')
1680
1681 unlet! g:called_bufnum
1682 unlet! g:called_arg
1683
1684 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1685 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1686 call assert_false(exists('g:called_bufnum'))
1687 call assert_false(exists('g:called_arg'))
1688
1689 eval buf->term_setapi('Tapi_')
1690 call term_sendkeys(buf, ":set notitle\<CR>")
1691 call term_sendkeys(buf, ":source Xscript\<CR>")
1692 call WaitFor({-> exists('g:called_bufnum')})
1693 call assert_equal(buf, g:called_bufnum)
1694 call assert_equal(['hello', 123], g:called_arg)
1695
1696 call StopVimInTerminal(buf)
1697
1698 call delete('Xscript')
1699 call ch_logfile('')
1700 call delete('Xlog')
1701 unlet! g:called_bufnum
1702 unlet! g:called_arg
1703endfunc
1704
1705func Test_terminal_api_arg()
1706 CheckRunVimInTerminal
1707
1708 call WriteApiCall('Tapi_TryThis')
1709 call ch_logfile('Xlog', 'w')
1710
1711 unlet! g:called_bufnum
1712 unlet! g:called_arg
1713
1714 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1715 let buf = bufnr('%')
1716 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1717 call assert_false(exists('g:called_bufnum'))
1718 call assert_false(exists('g:called_arg'))
1719
1720 call StopVimInTerminal(buf)
1721
1722 call ch_logfile('Xlog', 'w')
1723
1724 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1725 let buf = bufnr('%')
1726 call WaitFor({-> exists('g:called_bufnum')})
1727 call assert_equal(buf, g:called_bufnum)
1728 call assert_equal(['hello', 123], g:called_arg)
1729
1730 call StopVimInTerminal(buf)
1731
1732 call delete('Xscript')
1733 call ch_logfile('')
1734 call delete('Xlog')
1735 unlet! g:called_bufnum
1736 unlet! g:called_arg
1737endfunc
1738
1739func Test_terminal_ansicolors_default()
1740 CheckFunction term_getansicolors
1741
1742 let colors = [
1743 \ '#000000', '#e00000',
1744 \ '#00e000', '#e0e000',
1745 \ '#0000e0', '#e000e0',
1746 \ '#00e0e0', '#e0e0e0',
1747 \ '#808080', '#ff4040',
1748 \ '#40ff40', '#ffff40',
1749 \ '#4040ff', '#ff40ff',
1750 \ '#40ffff', '#ffffff',
1751 \]
1752
1753 let buf = Run_shell_in_terminal({})
1754 call assert_equal(colors, term_getansicolors(buf))
1755 call StopShellInTerminal(buf)
1756 call TermWait(buf)
1757 call assert_equal([], term_getansicolors(buf))
1758
1759 exe buf . 'bwipe'
1760endfunc
1761
1762let s:test_colors = [
1763 \ '#616e64', '#0d0a79',
1764 \ '#6d610d', '#0a7373',
1765 \ '#690d0a', '#6d696e',
1766 \ '#0d0a6f', '#616e0d',
1767 \ '#0a6479', '#6d0d0a',
1768 \ '#617373', '#0d0a69',
1769 \ '#6d690d', '#0a6e6f',
1770 \ '#610d0a', '#6e6479',
1771 \]
1772
1773func Test_terminal_ansicolors_global()
1774 CheckFeature termguicolors
1775 CheckFunction term_getansicolors
1776
1777 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1778 let buf = Run_shell_in_terminal({})
1779 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1780 call StopShellInTerminal(buf)
1781 call TermWait(buf)
1782
1783 exe buf . 'bwipe'
1784 unlet g:terminal_ansi_colors
1785endfunc
1786
1787func Test_terminal_ansicolors_func()
1788 CheckFeature termguicolors
1789 CheckFunction term_getansicolors
1790
1791 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1792 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1793 call assert_equal(s:test_colors, term_getansicolors(buf))
1794
1795 call term_setansicolors(buf, g:terminal_ansi_colors)
1796 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
1797
1798 let colors = [
1799 \ 'ivory', 'AliceBlue',
1800 \ 'grey67', 'dark goldenrod',
1801 \ 'SteelBlue3', 'PaleVioletRed4',
1802 \ 'MediumPurple2', 'yellow2',
1803 \ 'RosyBrown3', 'OrangeRed2',
1804 \ 'white smoke', 'navy blue',
1805 \ 'grey47', 'gray97',
1806 \ 'MistyRose2', 'DodgerBlue4',
1807 \]
1808 eval buf->term_setansicolors(colors)
1809
1810 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001811 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001812 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
1813
1814 call StopShellInTerminal(buf)
1815 call TermWait(buf)
1816 call assert_equal(0, term_setansicolors(buf, []))
1817 exe buf . 'bwipe'
1818endfunc
1819
1820func Test_terminal_all_ansi_colors()
1821 CheckRunVimInTerminal
1822
1823 " Use all the ANSI colors.
1824 call writefile([
1825 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
1826 \ 'hi Tblack ctermfg=0 ctermbg=8',
1827 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1828 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1829 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1830 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1831 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1832 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1833 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1834 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1835 \ 'hi Tred ctermfg=9 ctermbg=1',
1836 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1837 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1838 \ 'hi Tblue ctermfg=12 ctermbg=4',
1839 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1840 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1841 \ 'hi Twhite ctermfg=15 ctermbg=7',
1842 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1843 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1844 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
1845 \ '',
1846 \ 'call matchadd("Tblack", "A")',
1847 \ 'call matchadd("Tdarkred", "B")',
1848 \ 'call matchadd("Tdarkgreen", "C")',
1849 \ 'call matchadd("Tbrown", "D")',
1850 \ 'call matchadd("Tdarkblue", "E")',
1851 \ 'call matchadd("Tdarkmagenta", "F")',
1852 \ 'call matchadd("Tdarkcyan", "G")',
1853 \ 'call matchadd("Tlightgrey", "H")',
1854 \ 'call matchadd("Tdarkgrey", "I")',
1855 \ 'call matchadd("Tred", "J")',
1856 \ 'call matchadd("Tgreen", "K")',
1857 \ 'call matchadd("Tyellow", "L")',
1858 \ 'call matchadd("Tblue", "M")',
1859 \ 'call matchadd("Tmagenta", "N")',
1860 \ 'call matchadd("Tcyan", "O")',
1861 \ 'call matchadd("Twhite", "P")',
1862 \ 'call matchadd("TdarkredBold", "X")',
1863 \ 'call matchadd("TgreenBold", "Y")',
1864 \ 'call matchadd("TmagentaBold", "Z")',
1865 \ 'redraw',
1866 \ ], 'Xcolorscript')
1867 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1868 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1869
1870 call term_sendkeys(buf, ":q\<CR>")
1871 call StopVimInTerminal(buf)
1872 call delete('Xcolorscript')
1873endfunc
1874
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001875
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001876" vim: shiftwidth=2 sts=2 expandtab