blob: dac90aa0e75974b675bc116a7a834866f5e40a3d [file] [log] [blame]
Bram Moolenaarf386f082019-07-29 23:03:03 +02001" Test for channel and job functions.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002
Bram Moolenaarf386f082019-07-29 23:03:03 +02003" When +channel is supported then +job is too, so we don't check for that.
Bram Moolenaar4641a122019-07-29 22:10:23 +02004source check.vim
5CheckFeature channel
Bram Moolenaare2469252016-02-04 10:54:34 +01006
Bram Moolenaar321efdd2016-07-15 17:09:11 +02007source shared.vim
Bram Moolenaar4641a122019-07-29 22:10:23 +02008source screendump.vim
Bram Moolenaar321efdd2016-07-15 17:09:11 +02009
10let s:python = PythonProg()
11if s:python == ''
Bram Moolenaar37175402017-03-18 20:18:45 +010012 " Can't run this test without Python.
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020013 throw 'Skipped: Python command missing'
Bram Moolenaard7ece102016-02-02 23:23:02 +010014endif
15
Bram Moolenaar37175402017-03-18 20:18:45 +010016" Uncomment the next line to see what happens. Output is in
17" src/testdir/channellog.
Bram Moolenaarf386f082019-07-29 23:03:03 +020018" Add ch_log() calls where you want to see what happens.
Bram Moolenaar37175402017-03-18 20:18:45 +010019" call ch_logfile('channellog', 'w')
20
Bram Moolenaare74e8e72016-02-16 22:01:30 +010021let s:chopt = {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010022
Bram Moolenaar4b96df52020-01-26 22:00:26 +010023" Run "testfunc" after starting the server and stop the server afterwards.
Bram Moolenaar81661fb2016-02-18 22:23:34 +010024func s:run_server(testfunc, ...)
Bram Moolenaar321efdd2016-07-15 17:09:11 +020025 call RunServer('test_channel.py', a:testfunc, a:000)
Bram Moolenaar373a8762020-03-19 19:44:32 +010026
27 " communicating with a server can be flaky
28 let g:test_is_flaky = 1
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010029endfunc
30
Bram Moolenaar819524702018-02-27 19:10:00 +010031" Return a list of open files.
32" Can be used to make sure no resources leaked.
33" Returns an empty list on systems where this is not supported.
34func s:get_resources()
35 let pid = getpid()
36
Bram Moolenaar39536dd2019-01-29 22:58:21 +010037 if executable('lsof')
Bram Moolenaar819524702018-02-27 19:10:00 +010038 return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
39 elseif isdirectory('/proc/' . pid . '/fd/')
40 return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
41 else
42 return []
43 endif
44endfunc
45
Bram Moolenaar321efdd2016-07-15 17:09:11 +020046let g:Ch_responseMsg = ''
47func Ch_requestHandler(handle, msg)
48 let g:Ch_responseHandle = a:handle
49 let g:Ch_responseMsg = a:msg
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010050endfunc
51
Bram Moolenaar321efdd2016-07-15 17:09:11 +020052func Ch_communicate(port)
Bram Moolenaar5643db82016-12-03 14:29:10 +010053 " Avoid dropping messages, since we don't use a callback here.
54 let s:chopt.drop = 'never'
Bram Moolenaar0b146882018-09-06 16:27:24 +020055 " Also add the noblock flag to try it out.
56 let s:chopt.noblock = 1
Bram Moolenaard6a8d482016-02-10 20:32:20 +010057 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar5643db82016-12-03 14:29:10 +010058 unlet s:chopt.drop
Bram Moolenaar0b146882018-09-06 16:27:24 +020059 unlet s:chopt.noblock
Bram Moolenaar77073442016-02-13 23:23:53 +010060 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +010061 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010062 return
63 endif
Bram Moolenaarf386f082019-07-29 23:03:03 +020064
65 " check that getjob without a job is handled correctly
66 call assert_equal('no process', string(ch_getjob(handle)))
67
Bram Moolenaar570497a2019-08-22 22:55:13 +020068 let dict = handle->ch_info()
Bram Moolenaar03602ec2016-03-20 20:57:45 +010069 call assert_true(dict.id != 0)
70 call assert_equal('open', dict.status)
71 call assert_equal(a:port, string(dict.port))
72 call assert_equal('open', dict.sock_status)
73 call assert_equal('socket', dict.sock_io)
74
Bram Moolenaard7ece102016-02-02 23:23:02 +010075 " Simple string request and reply.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010076 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaard7ece102016-02-02 23:23:02 +010077
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010078 " Malformed command should be ignored.
Bram Moolenaarba61ac02016-03-20 16:40:37 +010079 call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
80 call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
81 call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
82
83 " split command should work
84 call assert_equal('ok', ch_evalexpr(handle, 'split'))
Bram Moolenaar321efdd2016-07-15 17:09:11 +020085 call WaitFor('exists("g:split")')
Bram Moolenaarba61ac02016-03-20 16:40:37 +010086 call assert_equal(123, g:split)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +010087
Bram Moolenaarf1f07922016-08-26 17:58:53 +020088 " string with ][ should work
89 call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
90
Bram Moolenaar4b785f62016-11-29 21:54:44 +010091 " nothing to read now
92 call assert_equal(0, ch_canread(handle))
93
Bram Moolenaarf1f07922016-08-26 17:58:53 +020094 " sending three messages quickly then reading should work
95 for i in range(3)
96 call ch_sendexpr(handle, 'echo hello ' . i)
97 endfor
98 call assert_equal('hello 0', ch_read(handle)[1])
99 call assert_equal('hello 1', ch_read(handle)[1])
100 call assert_equal('hello 2', ch_read(handle)[1])
101
Bram Moolenaard7ece102016-02-02 23:23:02 +0100102 " Request that triggers sending two ex commands. These will usually be
103 " handled before getting the response, but it's not guaranteed, thus wait a
104 " tiny bit for the commands to get executed.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100105 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200106 call WaitForAssert({-> assert_equal("added2", getline("$"))})
Bram Moolenaard7ece102016-02-02 23:23:02 +0100107 call assert_equal('added1', getline(line('$') - 1))
Bram Moolenaard7ece102016-02-02 23:23:02 +0100108
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100109 " Request command "foo bar", which fails silently.
110 call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200111 call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
Bram Moolenaarc4dcd602016-03-26 22:56:46 +0100112
Bram Moolenaarda94fdf2016-03-03 18:09:10 +0100113 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200114 call WaitForAssert({-> assert_equal('added more', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100115
Bram Moolenaara07fec92016-02-05 21:04:08 +0100116 " Send a request with a specific handler.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200117 call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
118 call WaitFor('exists("g:Ch_responseHandle")')
119 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100120 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100121 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200122 call assert_equal(handle, g:Ch_responseHandle)
123 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100124 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200125 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaara07fec92016-02-05 21:04:08 +0100126
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200127 let g:Ch_responseMsg = ''
128 call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
129 call WaitFor('exists("g:Ch_responseHandle")')
130 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100131 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar77073442016-02-13 23:23:53 +0100132 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200133 call assert_equal(handle, g:Ch_responseHandle)
134 unlet g:Ch_responseHandle
Bram Moolenaar77073442016-02-13 23:23:53 +0100135 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200136 call assert_equal('got it', g:Ch_responseMsg)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100137
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200138 " Using lambda.
139 let g:Ch_responseMsg = ''
140 call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
141 call WaitFor('exists("g:Ch_responseHandle")')
142 if !exists('g:Ch_responseHandle')
Bram Moolenaar37175402017-03-18 20:18:45 +0100143 call assert_report('g:Ch_responseHandle was not set')
Bram Moolenaar069c1e72016-07-15 21:25:08 +0200144 else
145 call assert_equal(handle, g:Ch_responseHandle)
146 unlet g:Ch_responseHandle
147 endif
148 call assert_equal('got it', g:Ch_responseMsg)
149
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100150 " Collect garbage, tests that our handle isn't collected.
Bram Moolenaar574860b2016-05-24 17:33:34 +0200151 call test_garbagecollect_now()
Bram Moolenaar38fd4bb2016-03-06 16:38:28 +0100152
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100153 " check setting options (without testing the effect)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200154 eval handle->ch_setoptions({'callback': 's:NotUsed'})
Bram Moolenaar1f6ef662016-02-19 22:59:44 +0100155 call ch_setoptions(handle, {'timeout': 1111})
Bram Moolenaarb6b52522016-02-20 23:30:07 +0100156 call ch_setoptions(handle, {'mode': 'json'})
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100157 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
Bram Moolenaar0ba75a92016-02-19 23:21:26 +0100158 call ch_setoptions(handle, {'callback': ''})
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100159 call ch_setoptions(handle, {'drop': 'never'})
160 call ch_setoptions(handle, {'drop': 'auto'})
161 call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", "E475")
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100162
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163 " Send an eval request that works.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100164 call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100165 sleep 10m
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100166 call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100167
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100168 " Send an eval request with special characters.
169 call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
170 sleep 10m
171 call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
172
173 " Send an eval request to get a line with special characters.
174 call setline(3, "a\nb\<CR>c\x01d\x7fe")
175 call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
176 sleep 10m
177 call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
178
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100179 " Send an eval request that fails.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100180 call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100181 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100182 call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100183
Bram Moolenaar55fab432016-02-07 16:53:13 +0100184 " Send an eval request that works but can't be encoded.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100185 call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100186 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100187 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar55fab432016-02-07 16:53:13 +0100188
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100189 " Send a bad eval request. There will be no response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100190 call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100191 sleep 10m
Bram Moolenaarfa8b2e12016-03-26 22:19:27 +0100192 call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100193
Bram Moolenaarf4160862016-02-05 23:09:12 +0100194 " Send an expr request
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100195 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200196 call WaitForAssert({-> assert_equal('three', getline('$'))})
Bram Moolenaarf4160862016-02-05 23:09:12 +0100197 call assert_equal('one', getline(line('$') - 2))
198 call assert_equal('two', getline(line('$') - 1))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100199
200 " Request a redraw, we don't check for the effect.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100201 call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
202 call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100203
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100204 call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100205
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100206 " Reading while there is nothing available.
Bram Moolenaar9186a272016-02-23 19:34:01 +0100207 call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100208 if exists('*reltimefloat')
209 let start = reltime()
210 call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
211 let elapsed = reltime(start)
212 call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
213 endif
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100214
215 " Send without waiting for a response, then wait for a response.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100216 call ch_sendexpr(handle, 'wait a bit')
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100217 let resp = ch_read(handle)
218 call assert_equal(type([]), type(resp))
219 call assert_equal(type(11), type(resp[0]))
220 call assert_equal('waited', resp[1])
221
Bram Moolenaard7ece102016-02-02 23:23:02 +0100222 " make the server quit, can't check if this works, should not hang.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100223 call ch_sendexpr(handle, '!quit!')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100224endfunc
Bram Moolenaard7ece102016-02-02 23:23:02 +0100225
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100226func Test_communicate()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100227 call ch_log('Test_communicate()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200228 call s:run_server('Ch_communicate')
Bram Moolenaard7ece102016-02-02 23:23:02 +0100229endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100230
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100231" Test that we can open two channels.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200232func Ch_two_channels(port)
Bram Moolenaar39b21272016-02-10 23:28:21 +0100233 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaarf562e722016-07-19 17:25:25 +0200234 call assert_equal(v:t_channel, type(handle))
Bram Moolenaar570497a2019-08-22 22:55:13 +0200235 if handle->ch_status() == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100236 call assert_report("Can't open channel")
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100237 return
238 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100239
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100240 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100241
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100242 let newhandle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100243 if ch_status(newhandle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100244 call assert_report("Can't open second channel")
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100245 return
246 endif
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100247 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
248 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100249
250 call ch_close(handle)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100251 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100252
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100253 call ch_close(newhandle)
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200254 call assert_fails("call ch_close(newhandle)", 'E906:')
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100255endfunc
256
257func Test_two_channels()
Bram Moolenaar570497a2019-08-22 22:55:13 +0200258 eval 'Test_two_channels()'->ch_log()
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200259 call s:run_server('Ch_two_channels')
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100260endfunc
261
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100262" Test that a server crash is handled gracefully.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200263func Ch_server_crash(port)
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100264 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100265 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100266 call assert_report("Can't open channel")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100267 return
268 endif
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100269
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100270 call ch_evalexpr(handle, '!crash!')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100271
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100272 sleep 10m
Bram Moolenaard6a8d482016-02-10 20:32:20 +0100273endfunc
274
275func Test_server_crash()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100276 call ch_log('Test_server_crash()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200277 call s:run_server('Ch_server_crash')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100278endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100279
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100280"""""""""
281
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200282func Ch_handler(chan, msg)
Bram Moolenaar65e08ee2016-12-01 16:41:50 +0100283 call ch_log('Ch_handler()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200284 unlet g:Ch_reply
285 let g:Ch_reply = a:msg
Bram Moolenaarf6157282016-02-10 21:07:14 +0100286endfunc
287
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200288func Ch_channel_handler(port)
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100289 let handle = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar77073442016-02-13 23:23:53 +0100290 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100291 call assert_report("Can't open channel")
Bram Moolenaarf6157282016-02-10 21:07:14 +0100292 return
293 endif
294
295 " Test that it works while waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100296 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200297 call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100298
299 " Test that it works while not waiting on a numbered message.
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100300 call ch_sendexpr(handle, 'call me again')
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200301 call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
Bram Moolenaarf6157282016-02-10 21:07:14 +0100302endfunc
303
304func Test_channel_handler()
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100305 call ch_log('Test_channel_handler()')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200306 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200307 let s:chopt.callback = 'Ch_handler'
308 call s:run_server('Ch_channel_handler')
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200309 let g:Ch_reply = ""
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200310 let s:chopt.callback = function('Ch_handler')
311 call s:run_server('Ch_channel_handler')
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +0100312 unlet s:chopt.callback
Bram Moolenaarf6157282016-02-10 21:07:14 +0100313endfunc
314
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100315"""""""""
316
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200317let g:Ch_reply = ''
318func Ch_zeroHandler(chan, msg)
319 unlet g:Ch_reply
320 let g:Ch_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100321endfunc
322
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200323let g:Ch_zero_reply = ''
324func Ch_oneHandler(chan, msg)
325 unlet g:Ch_zero_reply
326 let g:Ch_zero_reply = a:msg
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100327endfunc
328
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200329func Ch_channel_zero(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +0200330 let handle = ('localhost:' .. a:port)->ch_open(s:chopt)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100331 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100332 call assert_report("Can't open channel")
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100333 return
334 endif
335
336 " Check that eval works.
337 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
338
339 " Check that eval works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200340 let g:Ch_reply = ''
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100341 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100342 if s:has_handler
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200343 call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100344 else
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100345 sleep 20m
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200346 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100347 endif
348
349 " Check that handler works if a zero id message is sent back.
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200350 let g:Ch_reply = ''
351 let g:Ch_zero_reply = ''
352 call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200353 call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100354 if s:has_handler
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200355 call assert_equal('zero index', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100356 else
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200357 call assert_equal('', g:Ch_reply)
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100358 endif
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100359endfunc
360
361func Test_zero_reply()
362 call ch_log('Test_zero_reply()')
363 " Run with channel handler
364 let s:has_handler = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200365 let s:chopt.callback = 'Ch_zeroHandler'
366 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100367 unlet s:chopt.callback
368
369 " Run without channel handler
370 let s:has_handler = 0
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200371 call s:run_server('Ch_channel_zero')
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100372endfunc
373
374"""""""""
375
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200376let g:Ch_reply1 = ""
377func Ch_handleRaw1(chan, msg)
378 unlet g:Ch_reply1
379 let g:Ch_reply1 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100380endfunc
381
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200382let g:Ch_reply2 = ""
383func Ch_handleRaw2(chan, msg)
384 unlet g:Ch_reply2
385 let g:Ch_reply2 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100386endfunc
387
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200388let g:Ch_reply3 = ""
389func Ch_handleRaw3(chan, msg)
390 unlet g:Ch_reply3
391 let g:Ch_reply3 = a:msg
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100392endfunc
393
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200394func Ch_raw_one_time_callback(port)
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100395 let handle = ch_open('localhost:' . a:port, s:chopt)
396 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +0100397 call assert_report("Can't open channel")
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100398 return
399 endif
400 call ch_setoptions(handle, {'mode': 'raw'})
401
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100402 " The messages are sent raw, we do our own JSON strings here.
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200403 call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200404 call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
Bram Moolenaardd74ab92016-08-26 19:20:26 +0200405 call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
406 call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200407 call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100408 " wait for the 200 msec delayed reply
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200409 call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100410endfunc
411
412func Test_raw_one_time_callback()
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100413 call ch_log('Test_raw_one_time_callback()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200414 call s:run_server('Ch_raw_one_time_callback')
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100415endfunc
416
417"""""""""
418
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100419" Test that trying to connect to a non-existing port fails quickly.
420func Test_connect_waittime()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100421 CheckFunction reltimefloat
Bram Moolenaar373a8762020-03-19 19:44:32 +0100422 " this is timing sensitive
423 let g:test_is_flaky = 1
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100424
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100425 call ch_log('Test_connect_waittime()')
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100426 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100427 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100428 if ch_status(handle) != "fail"
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100429 " Oops, port does exists.
430 call ch_close(handle)
431 else
432 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100433 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100434 endif
435
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100436 " We intend to use a socket that doesn't exist and wait for half a second
437 " before giving up. If the socket does exist it can fail in various ways.
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100438 " Check for "Connection reset by peer" to avoid flakiness.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100439 let start = reltime()
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100440 try
441 let handle = ch_open('localhost:9867', {'waittime': 500})
442 if ch_status(handle) != "fail"
443 " Oops, port does exists.
444 call ch_close(handle)
445 else
Bram Moolenaarac42afd2016-03-12 13:48:49 +0100446 " Failed connection should wait about 500 msec. Can be longer if the
447 " computer is busy with other things.
Bram Moolenaar772153f2019-03-04 12:09:49 +0100448 call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100449 endif
450 catch
451 if v:exception !~ 'Connection reset by peer'
Bram Moolenaar37175402017-03-18 20:18:45 +0100452 call assert_report("Caught exception: " . v:exception)
Bram Moolenaar08298fa2016-02-21 13:01:53 +0100453 endif
454 endtry
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100455endfunc
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100456
Bram Moolenaard6547fc2016-03-03 19:35:02 +0100457"""""""""
458
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100459func Test_raw_pipe()
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100460 " Add a dummy close callback to avoid that messages are dropped when calling
461 " ch_canread().
Bram Moolenaar0b146882018-09-06 16:27:24 +0200462 " Also test the non-blocking option.
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100463 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaar0b146882018-09-06 16:27:24 +0200464 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
Bram Moolenaarf562e722016-07-19 17:25:25 +0200465 call assert_equal(v:t_job, type(job))
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100466 call assert_equal("run", job_status(job))
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200467
468 call assert_equal("open", ch_status(job))
469 call assert_equal("open", ch_status(job), {"part": "out"})
470 call assert_equal("open", ch_status(job), {"part": "err"})
471 call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
472 call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
473
474 let dict = ch_info(job)
475 call assert_true(dict.id != 0)
476 call assert_equal('open', dict.status)
477 call assert_equal('open', dict.out_status)
478 call assert_equal('RAW', dict.out_mode)
479 call assert_equal('pipe', dict.out_io)
480 call assert_equal('open', dict.err_status)
481 call assert_equal('RAW', dict.err_mode)
482 call assert_equal('pipe', dict.err_io)
483
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100484 try
Bram Moolenaar151f6562016-03-07 21:19:38 +0100485 " For a change use the job where a channel is expected.
486 call ch_sendraw(job, "echo something\n")
487 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100488 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
489
Bram Moolenaar151f6562016-03-07 21:19:38 +0100490 call ch_sendraw(job, "double this\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200491 let g:handle = job->job_getchannel()
492 call WaitFor('g:handle->ch_canread()')
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100493 unlet g:handle
Bram Moolenaar151f6562016-03-07 21:19:38 +0100494 let msg = ch_readraw(job)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100495 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
496
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200497 let g:Ch_reply = ""
498 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200499 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
Bram Moolenaar6fc82272016-08-28 19:26:43 +0200500
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200501 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
502 call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
503 call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
504 call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
505
Bram Moolenaar570497a2019-08-22 22:55:13 +0200506 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100507 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
508 finally
509 call job_stop(job)
510 endtry
Bram Moolenaar8950a562016-03-12 15:22:55 +0100511
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200512 let g:Ch_job = job
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200513 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar570497a2019-08-22 22:55:13 +0200514 let info = job->job_info()
Bram Moolenaar8950a562016-03-12 15:22:55 +0100515 call assert_equal("dead", info.status)
516 call assert_equal("term", info.stoponexit)
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200517 call assert_equal(2, len(info.cmd))
518 call assert_equal("test_channel_pipe.py", info.cmd[1])
519
520 let found = 0
521 for j in job_info()
522 if j == job
523 let found += 1
524 endif
525 endfor
526 call assert_equal(1, found)
Bram Moolenaar8b633132020-03-20 18:20:51 +0100527
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200528 call assert_fails("call job_stop('abc')", 'E475:')
529 call assert_fails("call job_stop(job, [])", 'E474:')
530 call assert_fails("call job_stop(test_null_job())", 'E916:')
531
Bram Moolenaar8b633132020-03-20 18:20:51 +0100532 " Try to use the job and channel where a number is expected. This is not
533 " related to testing the raw pipe. This test is here just to reuse the
534 " already created job/channel.
535 let ch = job_getchannel(job)
536 call assert_fails('let i = job + 1', 'E910:')
537 call assert_fails('let j = ch + 1', 'E913:')
538 call assert_fails('echo 2.0 == job', 'E911:')
539 call assert_fails('echo 2.0 == ch', 'E914:')
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100540endfunc
541
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100542func Test_raw_pipe_blob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100543 " Add a dummy close callback to avoid that messages are dropped when calling
544 " ch_canread().
545 " Also test the non-blocking option.
546 let job = job_start(s:python . " test_channel_pipe.py",
547 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
548 call assert_equal(v:t_job, type(job))
549 call assert_equal("run", job_status(job))
550
551 call assert_equal("open", ch_status(job))
552 call assert_equal("open", ch_status(job), {"part": "out"})
553
554 try
555 " Create a blob with the echo command and write it.
556 let blob = 0z00
557 let cmd = "echo something\n"
558 for i in range(0, len(cmd) - 1)
559 let blob[i] = char2nr(cmd[i])
560 endfor
561 call assert_equal(len(cmd), len(blob))
562 call ch_sendraw(job, blob)
563
564 " Read a blob with the reply.
Bram Moolenaar570497a2019-08-22 22:55:13 +0200565 let msg = job->ch_readblob()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100566 let expected = 'something'
567 for i in range(0, len(expected) - 1)
568 call assert_equal(char2nr(expected[i]), msg[i])
569 endfor
570
571 let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
572 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
573 finally
574 call job_stop(job)
575 endtry
576
577 let g:Ch_job = job
578 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
579 let info = job_info(job)
580 call assert_equal("dead", info.status)
581endfunc
582
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100583func Test_nl_pipe()
Bram Moolenaar1adda342016-03-12 15:39:40 +0100584 let job = job_start([s:python, "test_channel_pipe.py"])
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100585 call assert_equal("run", job_status(job))
586 try
587 let handle = job_getchannel(job)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100588 call ch_sendraw(handle, "echo something\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200589 call assert_equal("something", handle->ch_readraw())
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100590
Bram Moolenaarc25558b2016-03-03 21:02:23 +0100591 call ch_sendraw(handle, "echoerr wrong\n")
592 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
593
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100594 call ch_sendraw(handle, "double this\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100595 call assert_equal("this", ch_readraw(handle))
596 call assert_equal("AND this", ch_readraw(handle))
597
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200598 call ch_sendraw(handle, "split this line\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200599 call assert_equal("this linethis linethis line", handle->ch_read())
Bram Moolenaarbbe8d912016-06-05 16:10:57 +0200600
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100601 let reply = ch_evalraw(handle, "quit\n")
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +0100602 call assert_equal("Goodbye!", reply)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100603 finally
604 call job_stop(job)
605 endtry
606endfunc
Bram Moolenaar3bece9f2016-02-15 20:39:46 +0100607
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200608func Stop_g_job()
609 call job_stop(g:job)
610 if has('win32')
611 " On MS-Windows the server must close the file handle before we are able
612 " to delete the file.
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200613 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200614 sleep 10m
615 endif
616endfunc
617
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100618func Test_nl_read_file()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100619 call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200620 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100621 \ {'in_io': 'file', 'in_name': 'Xinput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200622 call assert_equal("run", job_status(g:job))
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100623 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200624 let handle = job_getchannel(g:job)
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100625 call assert_equal("something", ch_readraw(handle))
626 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
627 call assert_equal("this", ch_readraw(handle))
628 call assert_equal("AND this", ch_readraw(handle))
629 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200630 call Stop_g_job()
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100631 call delete('Xinput')
632 endtry
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200633 call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
Bram Moolenaarb69fccf2016-03-06 23:06:25 +0100634endfunc
635
Bram Moolenaare98d1212016-03-08 15:37:41 +0100636func Test_nl_write_out_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200637 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100638 \ {'out_io': 'file', 'out_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200639 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100640 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200641 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100642 call ch_sendraw(handle, "echo line one\n")
643 call ch_sendraw(handle, "echo line two\n")
644 call ch_sendraw(handle, "double this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200645 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100646 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200647 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100648 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100649 call delete('Xoutput')
650 endtry
651endfunc
652
653func Test_nl_write_err_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200654 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100655 \ {'err_io': 'file', 'err_name': 'Xoutput'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200656 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100657 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200658 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100659 call ch_sendraw(handle, "echoerr line one\n")
660 call ch_sendraw(handle, "echoerr line two\n")
661 call ch_sendraw(handle, "doubleerr this\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200662 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100663 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200664 call Stop_g_job()
Bram Moolenaare98d1212016-03-08 15:37:41 +0100665 call delete('Xoutput')
666 endtry
667endfunc
668
669func Test_nl_write_both_file()
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200670 let g:job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100671 \ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200672 call assert_equal("run", job_status(g:job))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100673 try
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200674 let handle = job_getchannel(g:job)
Bram Moolenaare98d1212016-03-08 15:37:41 +0100675 call ch_sendraw(handle, "echoerr line one\n")
676 call ch_sendraw(handle, "echo line two\n")
677 call ch_sendraw(handle, "double this\n")
678 call ch_sendraw(handle, "doubleerr that\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200679 call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
Bram Moolenaare98d1212016-03-08 15:37:41 +0100680 finally
Bram Moolenaar641ad6c2016-09-01 18:32:11 +0200681 call Stop_g_job()
Bram Moolenaar819524702018-02-27 19:10:00 +0100682 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
Bram Moolenaare98d1212016-03-08 15:37:41 +0100683 call delete('Xoutput')
684 endtry
685endfunc
686
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200687func BufCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200688 let g:Ch_bufClosed = 'yes'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200689endfunc
690
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200691func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200692 let g:Ch_bufClosed = 'no'
Bram Moolenaar01d46e42016-06-02 19:06:25 +0200693 let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200694 let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100695 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100696 let options['out_name'] = 'pipe-output'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200697 if a:do_msg
698 let expected[0] = 'Reading from channel output...'
699 else
700 let options['out_msg'] = 0
701 call remove(expected, 0)
702 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100703 else
704 sp pipe-output
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100705 let options['out_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100706 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200707 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100708 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200709 if a:nomod
710 let options['out_modifiable'] = 0
711 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100712 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100713 call assert_equal("run", job_status(job))
714 try
715 let handle = job_getchannel(job)
716 call ch_sendraw(handle, "echo line one\n")
717 call ch_sendraw(handle, "echo line two\n")
718 call ch_sendraw(handle, "double this\n")
719 call ch_sendraw(handle, "quit\n")
720 sp pipe-output
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100721 call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200722 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200723 if a:nomod
724 call assert_equal(0, &modifiable)
725 else
726 call assert_equal(1, &modifiable)
727 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200728 call assert_equal('yes', g:Ch_bufClosed)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100729 bwipe!
730 finally
731 call job_stop(job)
732 endtry
733endfunc
734
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100735func Test_pipe_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200736 call Run_test_pipe_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100737endfunc
738
739func Test_pipe_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200740 call Run_test_pipe_to_buffer(0, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100741endfunc
742
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200743func Test_pipe_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200744 call Run_test_pipe_to_buffer(1, 1, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200745endfunc
746
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200747func Test_pipe_to_buffer_name_nomsg()
748 call Run_test_pipe_to_buffer(1, 0, 1)
749endfunc
750
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200751func Test_close_output_buffer()
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200752 enew!
753 let test_lines = ['one', 'two']
754 call setline(1, test_lines)
755 call ch_log('Test_close_output_buffer()')
756 let options = {'out_io': 'buffer'}
757 let options['out_name'] = 'buffer-output'
758 let options['out_msg'] = 0
759 split buffer-output
760 let job = job_start(s:python . " test_channel_write.py", options)
761 call assert_equal("run", job_status(job))
762 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200763 call WaitForAssert({-> assert_equal(3, line('$'))})
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200764 quit!
765 sleep 100m
766 " Make sure the write didn't happen to the wrong buffer.
767 call assert_equal(test_lines, getline(1, line('$')))
768 call assert_equal(-1, bufwinnr('buffer-output'))
769 sbuf buffer-output
770 call assert_notequal(-1, bufwinnr('buffer-output'))
771 sleep 100m
772 close " no more writes
773 bwipe!
774 finally
775 call job_stop(job)
776 endtry
777endfunc
778
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200779func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100780 let options = {'err_io': 'buffer'}
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200781 let expected = ['', 'line one', 'line two', 'this', 'AND this']
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100782 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100783 let options['err_name'] = 'pipe-err'
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200784 if a:do_msg
785 let expected[0] = 'Reading from channel error...'
786 else
787 let options['err_msg'] = 0
788 call remove(expected, 0)
789 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100790 else
791 sp pipe-err
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100792 let options['err_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100793 quit
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200794 call remove(expected, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100795 endif
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200796 if a:nomod
797 let options['err_modifiable'] = 0
798 endif
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100799 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100800 call assert_equal("run", job_status(job))
801 try
802 let handle = job_getchannel(job)
803 call ch_sendraw(handle, "echoerr line one\n")
804 call ch_sendraw(handle, "echoerr line two\n")
805 call ch_sendraw(handle, "doubleerr this\n")
806 call ch_sendraw(handle, "quit\n")
807 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200808 call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200809 if a:nomod
810 call assert_equal(0, &modifiable)
811 else
812 call assert_equal(1, &modifiable)
813 endif
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100814 bwipe!
815 finally
816 call job_stop(job)
817 endtry
818endfunc
819
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100820func Test_pipe_err_to_buffer_name()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200821 call Run_test_pipe_err_to_buffer(1, 0, 1)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100822endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100823
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100824func Test_pipe_err_to_buffer_nr()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200825 call Run_test_pipe_err_to_buffer(0, 0, 1)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200826endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100827
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200828func Test_pipe_err_to_buffer_name_nomod()
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200829 call Run_test_pipe_err_to_buffer(1, 1, 1)
830endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100831
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200832func Test_pipe_err_to_buffer_name_nomsg()
833 call Run_test_pipe_err_to_buffer(1, 0, 0)
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100834endfunc
Bram Moolenaare2956092019-01-25 21:01:17 +0100835
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100836func Test_pipe_both_to_buffer()
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100837 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100838 \ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100839 call assert_equal("run", job_status(job))
840 try
841 let handle = job_getchannel(job)
842 call ch_sendraw(handle, "echo line one\n")
843 call ch_sendraw(handle, "echoerr line two\n")
844 call ch_sendraw(handle, "double this\n")
845 call ch_sendraw(handle, "doubleerr that\n")
846 call ch_sendraw(handle, "quit\n")
847 sp pipe-err
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200848 call WaitForAssert({-> assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))})
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100849 bwipe!
850 finally
851 call job_stop(job)
852 endtry
853endfunc
854
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100855func Run_test_pipe_from_buffer(use_name)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100856 sp pipe-input
857 call setline(1, ['echo one', 'echo two', 'echo three'])
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200858 let options = {'in_io': 'buffer', 'block_write': 1}
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100859 if a:use_name
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100860 let options['in_name'] = 'pipe-input'
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100861 else
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100862 let options['in_buf'] = bufnr('%')
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100863 endif
Bram Moolenaar014069a2016-03-03 22:51:40 +0100864
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100865 let job = job_start(s:python . " test_channel_pipe.py", options)
Bram Moolenaar014069a2016-03-03 22:51:40 +0100866 call assert_equal("run", job_status(job))
867 try
868 let handle = job_getchannel(job)
869 call assert_equal('one', ch_read(handle))
870 call assert_equal('two', ch_read(handle))
871 call assert_equal('three', ch_read(handle))
872 bwipe!
873 finally
874 call job_stop(job)
875 endtry
Bram Moolenaar014069a2016-03-03 22:51:40 +0100876endfunc
877
Bram Moolenaar29fd0382016-03-09 23:14:07 +0100878func Test_pipe_from_buffer_name()
879 call Run_test_pipe_from_buffer(1)
880endfunc
881
882func Test_pipe_from_buffer_nr()
883 call Run_test_pipe_from_buffer(0)
884endfunc
885
Bram Moolenaar0874a832016-09-01 15:11:51 +0200886func Run_pipe_through_sort(all, use_buffer)
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200887 CheckExecutable sort
888
Bram Moolenaar0874a832016-09-01 15:11:51 +0200889 let options = {'out_io': 'buffer', 'out_name': 'sortout'}
890 if a:use_buffer
891 split sortin
892 call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
893 let options.in_io = 'buffer'
894 let options.in_name = 'sortin'
895 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200896 if !a:all
897 let options.in_top = 2
898 let options.in_bot = 4
899 endif
Bram Moolenaare2956092019-01-25 21:01:17 +0100900 let job = job_start('sort', options)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200901
902 if !a:use_buffer
Bram Moolenaare2956092019-01-25 21:01:17 +0100903 call assert_equal("run", job_status(job))
904 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200905 eval job->ch_close_in()
Bram Moolenaar0874a832016-09-01 15:11:51 +0200906 endif
907
Bram Moolenaare2956092019-01-25 21:01:17 +0100908 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar0874a832016-09-01 15:11:51 +0200909
Bram Moolenaard8b55492016-09-01 14:35:22 +0200910 sp sortout
Bram Moolenaarf7f3e322016-09-03 18:47:24 +0200911 call WaitFor('line("$") > 3')
Bram Moolenaard8b55492016-09-01 14:35:22 +0200912 call assert_equal('Reading from channel output...', getline(1))
913 if a:all
914 call assert_equal(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], getline(2, 6))
915 else
916 call assert_equal(['aaa', 'bbb', 'ddd'], getline(2, 4))
917 endif
918
Bram Moolenaare2956092019-01-25 21:01:17 +0100919 call job_stop(job)
Bram Moolenaar0874a832016-09-01 15:11:51 +0200920 if a:use_buffer
921 bwipe! sortin
922 endif
Bram Moolenaard8b55492016-09-01 14:35:22 +0200923 bwipe! sortout
924endfunc
925
926func Test_pipe_through_sort_all()
927 call ch_log('Test_pipe_through_sort_all()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200928 call Run_pipe_through_sort(1, 1)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200929endfunc
930
931func Test_pipe_through_sort_some()
932 call ch_log('Test_pipe_through_sort_some()')
Bram Moolenaar0874a832016-09-01 15:11:51 +0200933 call Run_pipe_through_sort(0, 1)
934endfunc
935
936func Test_pipe_through_sort_feed()
937 call ch_log('Test_pipe_through_sort_feed()')
938 call Run_pipe_through_sort(1, 0)
Bram Moolenaard8b55492016-09-01 14:35:22 +0200939endfunc
940
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100941func Test_pipe_to_nameless_buffer()
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100942 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100943 \ {'out_io': 'buffer'})
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100944 call assert_equal("run", job_status(job))
945 try
946 let handle = job_getchannel(job)
947 call ch_sendraw(handle, "echo line one\n")
948 call ch_sendraw(handle, "echo line two\n")
Bram Moolenaar570497a2019-08-22 22:55:13 +0200949 exe handle->ch_getbufnr("out") .. 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200950 call WaitFor('line("$") >= 3')
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100951 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
952 bwipe!
953 finally
954 call job_stop(job)
955 endtry
956endfunc
957
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100958func Test_pipe_to_buffer_json()
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100959 CheckFunction reltimefloat
960
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100961 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100962 \ {'out_io': 'buffer', 'out_mode': 'json'})
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100963 call assert_equal("run", job_status(job))
964 try
965 let handle = job_getchannel(job)
966 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
967 call ch_sendraw(handle, "echo [-2, 12.34]\n")
968 exe ch_getbufnr(handle, "out") . 'sbuf'
Bram Moolenaar321efdd2016-07-15 17:09:11 +0200969 call WaitFor('line("$") >= 3')
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +0100970 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
971 bwipe!
972 finally
973 call job_stop(job)
974 endtry
975endfunc
976
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100977" Wait a little while for the last line, minus "offset", to equal "line".
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100978func s:wait_for_last_line(line, offset)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100979 for i in range(100)
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100980 if getline(line('$') - a:offset) == a:line
981 break
982 endif
Bram Moolenaar9fe885e2016-03-08 16:06:55 +0100983 sleep 10m
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100984 endfor
985endfunc
986
987func Test_pipe_io_two_buffers()
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100988 " Create two buffers, one to read from and one to write to.
989 split pipe-output
990 set buftype=nofile
991 split pipe-input
992 set buftype=nofile
993
994 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100995 \ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200996 \ 'out_io': 'buffer', 'out_name': 'pipe-output',
997 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +0100998 call assert_equal("run", job_status(job))
999 try
1000 exe "normal Gaecho hello\<CR>"
1001 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001002 call s:wait_for_last_line('hello', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001003 call assert_equal('hello', getline('$'))
1004
1005 exe bufwinnr('pipe-input') . "wincmd w"
1006 exe "normal Gadouble this\<CR>"
1007 exe bufwinnr('pipe-output') . "wincmd w"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001008 call s:wait_for_last_line('AND this', 0)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001009 call assert_equal('this', getline(line('$') - 1))
1010 call assert_equal('AND this', getline('$'))
1011
1012 bwipe!
1013 exe bufwinnr('pipe-input') . "wincmd w"
1014 bwipe!
1015 finally
1016 call job_stop(job)
1017 endtry
1018endfunc
1019
1020func Test_pipe_io_one_buffer()
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001021 " Create one buffer to read from and to write to.
1022 split pipe-io
1023 set buftype=nofile
1024
1025 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001026 \ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001027 \ 'out_io': 'buffer', 'out_name': 'pipe-io',
1028 \ 'block_write': 1})
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001029 call assert_equal("run", job_status(job))
1030 try
1031 exe "normal Goecho hello\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001032 call s:wait_for_last_line('hello', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001033 call assert_equal('hello', getline(line('$') - 1))
1034
1035 exe "normal Gadouble this\<CR>"
Bram Moolenaar9fe885e2016-03-08 16:06:55 +01001036 call s:wait_for_last_line('AND this', 1)
Bram Moolenaar3f39f642016-03-06 21:35:57 +01001037 call assert_equal('this', getline(line('$') - 2))
1038 call assert_equal('AND this', getline(line('$') - 1))
1039
1040 bwipe!
1041 finally
1042 call job_stop(job)
1043 endtry
1044endfunc
1045
Bram Moolenaar4641a122019-07-29 22:10:23 +02001046func Test_write_to_buffer_and_scroll()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001047 CheckScreendump
1048
Bram Moolenaar4641a122019-07-29 22:10:23 +02001049 let lines =<< trim END
1050 new Xscrollbuffer
1051 call setline(1, range(1, 200))
1052 $
1053 redraw
1054 wincmd w
1055 call deletebufline('Xscrollbuffer', 1, '$')
1056 if has('win32')
1057 let cmd = ['cmd', '/c', 'echo sometext']
1058 else
1059 let cmd = [&shell, &shellcmdflag, 'echo sometext']
1060 endif
1061 call job_start(cmd, #{out_io: 'buffer', out_name: 'Xscrollbuffer'})
1062 END
1063 call writefile(lines, 'XtestBufferScroll')
1064 let buf = RunVimInTerminal('-S XtestBufferScroll', #{rows: 10})
Bram Moolenaarf386f082019-07-29 23:03:03 +02001065 call term_wait(buf, 100)
Bram Moolenaar4641a122019-07-29 22:10:23 +02001066 call VerifyScreenDump(buf, 'Test_job_buffer_scroll_1', {})
1067
1068 " clean up
1069 call StopVimInTerminal(buf)
1070 call delete('XtestBufferScroll')
1071endfunc
1072
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001073func Test_pipe_null()
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001074 " We cannot check that no I/O works, we only check that the job starts
1075 " properly.
1076 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001077 \ {'in_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001078 call assert_equal("run", job_status(job))
1079 try
1080 call assert_equal('something', ch_read(job))
1081 finally
1082 call job_stop(job)
1083 endtry
1084
1085 let job = job_start(s:python . " test_channel_pipe.py err-out",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001086 \ {'out_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001087 call assert_equal("run", job_status(job))
1088 try
1089 call assert_equal('err-out', ch_read(job, {"part": "err"}))
1090 finally
1091 call job_stop(job)
1092 endtry
1093
1094 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001095 \ {'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001096 call assert_equal("run", job_status(job))
1097 try
1098 call assert_equal('something', ch_read(job))
1099 finally
1100 call job_stop(job)
1101 endtry
1102
1103 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001104 \ {'out_io': 'null', 'err_io': 'out'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001105 call assert_equal("run", job_status(job))
1106 call job_stop(job)
1107
1108 let job = job_start(s:python . " test_channel_pipe.py something",
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001109 \ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
Bram Moolenaarf65333c2016-03-08 18:27:21 +01001110 call assert_equal("run", job_status(job))
1111 call assert_equal('channel fail', string(job_getchannel(job)))
1112 call assert_equal('fail', ch_status(job))
1113 call job_stop(job)
1114endfunc
1115
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001116func Test_pipe_to_buffer_raw()
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001117 let options = {'out_mode': 'raw', 'out_io': 'buffer', 'out_name': 'testout'}
1118 split testout
1119 let job = job_start([s:python, '-c',
1120 \ 'import sys; [sys.stdout.write(".") and sys.stdout.flush() for _ in range(10000)]'], options)
Bram Moolenaare2956092019-01-25 21:01:17 +01001121 " the job may be done quickly, also accept "dead"
1122 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001123 call WaitFor('len(join(getline(1, "$"), "")) >= 10000')
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001124 try
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001125 let totlen = 0
1126 for line in getline(1, '$')
1127 call assert_equal('', substitute(line, '^\.*', '', ''))
1128 let totlen += len(line)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001129 endfor
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001130 call assert_equal(10000, totlen)
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001131 finally
1132 call job_stop(job)
1133 bwipe!
1134 endtry
1135endfunc
1136
Bram Moolenaarde279892016-03-11 22:19:44 +01001137func Test_reuse_channel()
Bram Moolenaarde279892016-03-11 22:19:44 +01001138 let job = job_start(s:python . " test_channel_pipe.py")
1139 call assert_equal("run", job_status(job))
1140 let handle = job_getchannel(job)
1141 try
1142 call ch_sendraw(handle, "echo something\n")
1143 call assert_equal("something", ch_readraw(handle))
1144 finally
1145 call job_stop(job)
1146 endtry
1147
1148 let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
1149 call assert_equal("run", job_status(job))
1150 let handle = job_getchannel(job)
1151 try
1152 call ch_sendraw(handle, "echo again\n")
1153 call assert_equal("again", ch_readraw(handle))
1154 finally
1155 call job_stop(job)
1156 endtry
1157endfunc
1158
Bram Moolenaar75f72652016-03-20 22:16:56 +01001159func Test_out_cb()
Bram Moolenaar75f72652016-03-20 22:16:56 +01001160 let dict = {'thisis': 'dict: '}
1161 func dict.outHandler(chan, msg) dict
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001162 if type(a:msg) == v:t_string
1163 let g:Ch_outmsg = self.thisis . a:msg
1164 else
1165 let g:Ch_outobj = a:msg
1166 endif
Bram Moolenaar75f72652016-03-20 22:16:56 +01001167 endfunc
1168 func dict.errHandler(chan, msg) dict
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001169 let g:Ch_errmsg = self.thisis . a:msg
Bram Moolenaar75f72652016-03-20 22:16:56 +01001170 endfunc
1171 let job = job_start(s:python . " test_channel_pipe.py",
1172 \ {'out_cb': dict.outHandler,
Bram Moolenaare2956092019-01-25 21:01:17 +01001173 \ 'out_mode': 'json',
1174 \ 'err_cb': dict.errHandler,
1175 \ 'err_mode': 'json'})
Bram Moolenaar75f72652016-03-20 22:16:56 +01001176 call assert_equal("run", job_status(job))
1177 try
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001178 let g:Ch_outmsg = ''
1179 let g:Ch_errmsg = ''
Bram Moolenaar75f72652016-03-20 22:16:56 +01001180 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1181 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001182 call WaitForAssert({-> assert_equal("dict: hello", g:Ch_outmsg)})
1183 call WaitForAssert({-> assert_equal("dict: there", g:Ch_errmsg)})
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001184
1185 " Receive a json object split in pieces
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001186 let g:Ch_outobj = ''
Bram Moolenaar9ae3bbd2020-02-19 14:31:33 +01001187 call ch_sendraw(job, "echosplit [0, {\"one\": 1,| \"tw|o\": 2, \"three\": 3|}]\n")
Bram Moolenaarbf54dbe2020-03-29 16:18:58 +02001188 " For unknown reasons this can be very slow on Mac.
1189 if has('mac')
1190 let timeout = 20000
1191 else
1192 let timeout = 5000
1193 endif
1194 call WaitForAssert({-> assert_equal({'one': 1, 'two': 2, 'three': 3}, g:Ch_outobj)}, timeout)
Bram Moolenaar75f72652016-03-20 22:16:56 +01001195 finally
1196 call job_stop(job)
1197 endtry
1198endfunc
1199
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001200func Test_out_close_cb()
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001201 let s:counter = 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001202 let g:Ch_msg1 = ''
1203 let g:Ch_closemsg = 0
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001204 func! OutHandler(chan, msg)
Bram Moolenaard75263c2016-04-30 16:07:23 +02001205 if s:counter == 1
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001206 let g:Ch_msg1 = a:msg
Bram Moolenaard75263c2016-04-30 16:07:23 +02001207 endif
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001208 let s:counter += 1
1209 endfunc
1210 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001211 let g:Ch_closemsg = s:counter
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001212 let s:counter += 1
1213 endfunc
1214 let job = job_start(s:python . " test_channel_pipe.py quit now",
1215 \ {'out_cb': 'OutHandler',
Bram Moolenaare2956092019-01-25 21:01:17 +01001216 \ 'close_cb': 'CloseHandler'})
1217 " the job may be done quickly, also accept "dead"
1218 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001219 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001220 call WaitForAssert({-> assert_equal('quit', g:Ch_msg1)})
1221 call WaitForAssert({-> assert_equal(2, g:Ch_closemsg)})
Bram Moolenaarb2658a12016-04-26 17:16:24 +02001222 finally
1223 call job_stop(job)
1224 delfunc OutHandler
1225 delfunc CloseHandler
1226 endtry
1227endfunc
1228
Bram Moolenaar437905c2016-04-26 19:01:05 +02001229func Test_read_in_close_cb()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001230 let g:Ch_received = ''
Bram Moolenaar437905c2016-04-26 19:01:05 +02001231 func! CloseHandler(chan)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001232 let g:Ch_received = ch_read(a:chan)
Bram Moolenaar437905c2016-04-26 19:01:05 +02001233 endfunc
1234 let job = job_start(s:python . " test_channel_pipe.py quit now",
1235 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001236 " the job may be done quickly, also accept "dead"
1237 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar437905c2016-04-26 19:01:05 +02001238 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001239 call WaitForAssert({-> assert_equal('quit', g:Ch_received)})
Bram Moolenaar437905c2016-04-26 19:01:05 +02001240 finally
1241 call job_stop(job)
1242 delfunc CloseHandler
1243 endtry
1244endfunc
1245
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001246" Use channel in NL mode but received text does not end in NL.
1247func Test_read_in_close_cb_incomplete()
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001248 let g:Ch_received = ''
1249 func! CloseHandler(chan)
1250 while ch_status(a:chan, {'part': 'out'}) == 'buffered'
1251 let g:Ch_received .= ch_read(a:chan)
1252 endwhile
1253 endfunc
1254 let job = job_start(s:python . " test_channel_pipe.py incomplete",
1255 \ {'close_cb': 'CloseHandler'})
Bram Moolenaare2956092019-01-25 21:01:17 +01001256 " the job may be done quickly, also accept "dead"
1257 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001258 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001259 call WaitForAssert({-> assert_equal('incomplete', g:Ch_received)})
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01001260 finally
1261 call job_stop(job)
1262 delfunc CloseHandler
1263 endtry
1264endfunc
1265
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001266func Test_out_cb_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001267 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001268 \ {'out_cb': {ch, msg -> execute("let g:Ch_outmsg = 'lambda: ' . msg")},
1269 \ 'out_mode': 'json',
1270 \ 'err_cb': {ch, msg -> execute(":let g:Ch_errmsg = 'lambda: ' . msg")},
1271 \ 'err_mode': 'json'})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001272 call assert_equal("run", job_status(job))
1273 try
1274 let g:Ch_outmsg = ''
1275 let g:Ch_errmsg = ''
1276 call ch_sendraw(job, "echo [0, \"hello\"]\n")
1277 call ch_sendraw(job, "echoerr [0, \"there\"]\n")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001278 call WaitForAssert({-> assert_equal("lambda: hello", g:Ch_outmsg)})
1279 call WaitForAssert({-> assert_equal("lambda: there", g:Ch_errmsg)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001280 finally
1281 call job_stop(job)
1282 endtry
1283endfunc
1284
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001285func Test_close_and_exit_cb()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001286 let g:retdict = {'ret': {}}
1287 func g:retdict.close_cb(ch) dict
Bram Moolenaar570497a2019-08-22 22:55:13 +02001288 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001289 endfunc
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001290 func g:retdict.exit_cb(job, status) dict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001291 let self.ret['exit_cb'] = job_status(a:job)
1292 endfunc
1293
Bram Moolenaare2956092019-01-25 21:01:17 +01001294 let job = job_start([&shell, &shellcmdflag, 'echo'],
1295 \ {'close_cb': g:retdict.close_cb,
1296 \ 'exit_cb': g:retdict.exit_cb})
1297 " the job may be done quickly, also accept "dead"
1298 call assert_match('^\%(dead\|run\)$', job_status(job))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001299 call WaitForAssert({-> assert_equal(2, len(g:retdict.ret))})
Bram Moolenaare2956092019-01-25 21:01:17 +01001300 call assert_match('^\%(dead\|run\)$', g:retdict.ret['close_cb'])
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001301 call assert_equal('dead', g:retdict.ret['exit_cb'])
1302 unlet g:retdict
Bram Moolenaar7df915d2016-11-17 17:25:32 +01001303endfunc
1304
Bram Moolenaard46ae142016-02-16 13:33:52 +01001305""""""""""
1306
Bram Moolenaar0b146882018-09-06 16:27:24 +02001307function ExitCbWipe(job, status)
1308 exe g:wipe_buf 'bw!'
1309endfunction
1310
1311" This caused a crash, because messages were handled while peeking for a
1312" character.
1313func Test_exit_cb_wipes_buf()
1314 if !has('timers')
1315 return
1316 endif
1317 set cursorline lazyredraw
1318 call test_override('redraw_flag', 1)
1319 new
1320 let g:wipe_buf = bufnr('')
1321
Bram Moolenaar453ce7c2018-10-12 22:15:12 +02001322 let job = job_start(has('win32') ? 'cmd /c echo:' : ['true'],
Bram Moolenaare2956092019-01-25 21:01:17 +01001323 \ {'exit_cb': 'ExitCbWipe'})
Bram Moolenaar0b146882018-09-06 16:27:24 +02001324 let timer = timer_start(300, {-> feedkeys("\<Esc>", 'nt')}, {'repeat': 5})
1325 call feedkeys(repeat('g', 1000) . 'o', 'ntx!')
1326 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1327 call timer_stop(timer)
1328
1329 set nocursorline nolazyredraw
1330 unlet g:wipe_buf
1331 call test_override('ALL', 0)
1332endfunc
1333
1334""""""""""
1335
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001336let g:Ch_unletResponse = ''
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001337func s:UnletHandler(handle, msg)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001338 let g:Ch_unletResponse = a:msg
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001339 unlet s:channelfd
1340endfunc
1341
1342" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001343func Ch_unlet_handle(port)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001344 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001345 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001346 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001347endfunc
1348
1349func Test_unlet_handle()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001350 call ch_log('Test_unlet_handle()')
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001351 call s:run_server('Ch_unlet_handle')
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01001352endfunc
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001353
Bram Moolenaard46ae142016-02-16 13:33:52 +01001354""""""""""
1355
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001356let g:Ch_unletResponse = ''
1357func Ch_CloseHandler(handle, msg)
1358 let g:Ch_unletResponse = a:msg
Bram Moolenaar570497a2019-08-22 22:55:13 +02001359 eval s:channelfd->ch_close()
Bram Moolenaard46ae142016-02-16 13:33:52 +01001360endfunc
1361
1362" Test that "unlet handle" in a handler doesn't crash Vim.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001363func Ch_close_handle(port)
Bram Moolenaard46ae142016-02-16 13:33:52 +01001364 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001365 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001366 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
Bram Moolenaard46ae142016-02-16 13:33:52 +01001367endfunc
1368
1369func Test_close_handle()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001370 call s:run_server('Ch_close_handle')
Bram Moolenaard46ae142016-02-16 13:33:52 +01001371endfunc
1372
1373""""""""""
1374
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001375func Test_open_fail()
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001376 call assert_fails("let ch = ch_open('noserver')", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001377 echo ch
1378 let d = ch
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001379 call assert_fails("let ch = ch_open('noserver', 10)", 'E474:')
1380 call assert_fails("let ch = ch_open('localhost:-1')", 'E475:')
1381 call assert_fails("let ch = ch_open('localhost:8765', {'timeout' : -1})",
1382 \ 'E474:')
1383 call assert_fails("let ch = ch_open('localhost:8765', {'axby' : 1})",
1384 \ 'E475:')
1385 call assert_fails("let ch = ch_open('localhost:8765', {'mode' : 'abc'})",
1386 \ 'E475:')
1387 call assert_fails("let ch = ch_open('localhost:8765', {'part' : 'out'})",
1388 \ 'E475:')
1389endfunc
1390
1391func Test_ch_info_fail()
1392 call assert_fails("let x = ch_info(10)", 'E475:')
Bram Moolenaar5cefd402016-02-16 12:44:26 +01001393endfunc
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001394
1395""""""""""
1396
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001397func Ch_open_delay(port)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001398 " Wait up to a second for the port to open.
1399 let s:chopt.waittime = 1000
1400 let channel = ch_open('localhost:' . a:port, s:chopt)
1401 unlet s:chopt.waittime
1402 if ch_status(channel) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001403 call assert_report("Can't open channel")
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001404 return
1405 endif
Bram Moolenaar570497a2019-08-22 22:55:13 +02001406 call assert_equal('got it', channel->ch_evalexpr('hello!'))
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001407 call ch_close(channel)
1408endfunc
1409
1410func Test_open_delay()
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001411 " The server will wait half a second before creating the port.
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001412 call s:run_server('Ch_open_delay', 'delay')
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001413endfunc
Bram Moolenaarece61b02016-02-20 21:39:05 +01001414
1415"""""""""
1416
1417function MyFunction(a,b,c)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001418 let g:Ch_call_ret = [a:a, a:b, a:c]
Bram Moolenaarece61b02016-02-20 21:39:05 +01001419endfunc
1420
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001421function Ch_test_call(port)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001422 let handle = ch_open('localhost:' . a:port, s:chopt)
1423 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001424 call assert_report("Can't open channel")
Bram Moolenaarece61b02016-02-20 21:39:05 +01001425 return
1426 endif
1427
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001428 let g:Ch_call_ret = []
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001429 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001430 call WaitForAssert({-> assert_equal([1, 2, 3], g:Ch_call_ret)})
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001431
1432 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'callback' : 'abc'})", 'E917:')
1433 call assert_fails("let i = ch_evalexpr(handle, '2 + 2', {'drop' : ''})", 'E475:')
1434 call assert_fails("let i = ch_evalexpr(test_null_job(), '2 + 2')", 'E906:')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001435endfunc
1436
1437func Test_call()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001438 call s:run_server('Ch_test_call')
Bram Moolenaarece61b02016-02-20 21:39:05 +01001439endfunc
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001440
1441"""""""""
1442
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001443let g:Ch_job_exit_ret = 'not yet'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001444function MyExitCb(job, status)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001445 let g:Ch_job_exit_ret = 'done'
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001446endfunc
1447
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001448function Ch_test_exit_callback(port)
Bram Moolenaar570497a2019-08-22 22:55:13 +02001449 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001450 let g:Ch_exit_job = g:currentJob
1451 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001452endfunc
1453
1454func Test_exit_callback()
Bram Moolenaarf386f082019-07-29 23:03:03 +02001455 call s:run_server('Ch_test_exit_callback')
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001456
Bram Moolenaarf386f082019-07-29 23:03:03 +02001457 " wait up to a second for the job to exit
1458 for i in range(100)
1459 if g:Ch_job_exit_ret == 'done'
1460 break
1461 endif
1462 sleep 10m
1463 " calling job_status() triggers the callback
1464 call job_status(g:Ch_exit_job)
1465 endfor
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001466
Bram Moolenaarf386f082019-07-29 23:03:03 +02001467 call assert_equal('done', g:Ch_job_exit_ret)
1468 call assert_equal('dead', job_info(g:Ch_exit_job).status)
1469 unlet g:Ch_exit_job
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001470endfunc
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001471
Bram Moolenaar97792de2016-10-15 18:36:49 +02001472function MyExitTimeCb(job, status)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001473 if job_info(a:job).process == g:exit_cb_val.process
1474 let g:exit_cb_val.end = reltime(g:exit_cb_val.start)
1475 endif
1476 call Resume()
Bram Moolenaar97792de2016-10-15 18:36:49 +02001477endfunction
1478
1479func Test_exit_callback_interval()
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001480 CheckFunction reltimefloat
1481
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001482 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
Bram Moolenaar570497a2019-08-22 22:55:13 +02001483 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001484 let g:exit_cb_val.process = job_info(job).process
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001485 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
Bram Moolenaar01688ad2016-10-27 20:00:07 +02001486 let elapsed = reltimefloat(g:exit_cb_val.end)
1487 call assert_true(elapsed > 0.5)
1488 call assert_true(elapsed < 1.0)
1489
1490 " case: unreferenced job, using timer
1491 if !has('timers')
1492 return
1493 endif
1494
1495 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1496 let g:job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'})
1497 let g:exit_cb_val.process = job_info(g:job).process
1498 unlet g:job
1499 call Standby(1000)
1500 if type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0
1501 let elapsed = reltimefloat(g:exit_cb_val.end)
1502 else
1503 let elapsed = 1.0
1504 endif
Bram Moolenaar772153f2019-03-04 12:09:49 +01001505 call assert_inrange(0.5, 1.0, elapsed)
Bram Moolenaar97792de2016-10-15 18:36:49 +02001506endfunc
1507
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001508"""""""""
1509
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001510let g:Ch_close_ret = 'alive'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001511function MyCloseCb(ch)
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001512 let g:Ch_close_ret = 'closed'
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001513endfunc
1514
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001515function Ch_test_close_callback(port)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001516 let handle = ch_open('localhost:' . a:port, s:chopt)
1517 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001518 call assert_report("Can't open channel")
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001519 return
1520 endif
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001521 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001522
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001523 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001524 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001525endfunc
1526
1527func Test_close_callback()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001528 call s:run_server('Ch_test_close_callback')
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001529endfunc
1530
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001531function Ch_test_close_partial(port)
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001532 let handle = ch_open('localhost:' . a:port, s:chopt)
1533 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001534 call assert_report("Can't open channel")
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001535 return
1536 endif
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001537 let g:Ch_d = {}
1538 func g:Ch_d.closeCb(ch) dict
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001539 let self.close_ret = 'closed'
1540 endfunc
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001541 call ch_setoptions(handle, {'close_cb': g:Ch_d.closeCb})
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001542
1543 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001544 call WaitForAssert({-> assert_equal('closed', g:Ch_d.close_ret)})
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001545 unlet g:Ch_d
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001546endfunc
1547
1548func Test_close_partial()
Bram Moolenaar321efdd2016-07-15 17:09:11 +02001549 call s:run_server('Ch_test_close_partial')
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001550endfunc
1551
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001552func Test_job_start_fails()
1553 " this was leaking memory
1554 call assert_fails("call job_start([''])", "E474:")
Bram Moolenaar80385682016-03-27 19:13:35 +02001555 call assert_fails('call job_start($x)', 'E474:')
1556 call assert_fails('call job_start("")', 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001557 call assert_fails('call job_start("ls", {"out_io" : "abc"})', 'E475:')
1558 call assert_fails('call job_start("ls", {"err_io" : "abc"})', 'E475:')
1559 call assert_fails('call job_start("ls", [])', 'E715:')
1560 call assert_fails("call job_start('ls', {'in_top' : -1})", 'E475:')
1561 call assert_fails("call job_start('ls', {'in_bot' : -1})", 'E475:')
1562 call assert_fails("call job_start('ls', {'channel' : -1})", 'E475:')
1563 call assert_fails("call job_start('ls', {'callback' : -1})", 'E475:')
1564 call assert_fails("call job_start('ls', {'out_cb' : -1})", 'E475:')
1565 call assert_fails("call job_start('ls', {'err_cb' : -1})", 'E475:')
1566 call assert_fails("call job_start('ls', {'close_cb' : -1})", 'E475:')
1567 call assert_fails("call job_start('ls', {'exit_cb' : -1})", 'E475:')
1568 call assert_fails("call job_start('ls', {'term_name' : []})", 'E475:')
1569 call assert_fails("call job_start('ls', {'term_finish' : 'run'})", 'E475:')
1570 call assert_fails("call job_start('ls', {'term_api' : []})", 'E475:')
1571 call assert_fails("call job_start('ls', {'stoponexit' : []})", 'E475:')
1572 call assert_fails("call job_start('ls', {'in_io' : 'file'})", 'E920:')
1573 call assert_fails("call job_start('ls', {'out_io' : 'file'})", 'E920:')
1574 call assert_fails("call job_start('ls', {'err_io' : 'file'})", 'E920:')
1575 call assert_fails("call job_start('ls', {'in_mode' : 'abc'})", 'E475:')
1576 call assert_fails("call job_start('ls', {'out_mode' : 'abc'})", 'E475:')
1577 call assert_fails("call job_start('ls', {'err_mode' : 'abc'})", 'E475:')
1578 call assert_fails("call job_start('ls',
1579 \ {'in_io' : 'buffer', 'in_buf' : 99999})", 'E86:')
1580 call assert_fails("call job_start('ls',
1581 \ {'out_io' : 'buffer', 'out_buf' : 99999})", 'E86:')
1582 call assert_fails("call job_start('ls',
1583 \ {'err_io' : 'buffer', 'err_buf' : 99999})", 'E86:')
1584
1585 call assert_fails("call job_start('ls',
1586 \ {'in_io' : 'buffer', 'in_buf' : -1})", 'E475:')
1587 call assert_fails("call job_start('ls',
1588 \ {'out_io' : 'buffer', 'out_buf' : -1})", 'E475:')
1589 call assert_fails("call job_start('ls',
1590 \ {'err_io' : 'buffer', 'err_buf' : -1})", 'E475:')
1591
1592 set nomodifiable
1593 call assert_fails("call job_start('cmd /c dir',
1594 \ {'out_io' : 'buffer', 'out_buf' :" .. bufnr() .. "})", 'E21:')
1595 call assert_fails("call job_start('cmd /c dir',
1596 \ {'err_io' : 'buffer', 'err_buf' :" .. bufnr() .. "})", 'E21:')
1597 set modifiable
1598
1599 call assert_fails("call job_start('ls', {'in_io' : 'buffer'})", 'E915:')
1600
1601 edit! XXX
1602 let bnum = bufnr()
1603 enew
1604 call assert_fails("call job_start('ls',
1605 \ {'in_io' : 'buffer', 'in_buf' : bnum})", 'E918:')
1606
1607 " Empty job tests
1608 " This was crashing on MS-Windows.
1609 call assert_fails('let job = job_start([""])', 'E474:')
1610 call assert_fails('let job = job_start([" "])', 'E474:')
1611 call assert_fails('let job = job_start("")', 'E474:')
1612 call assert_fails('let job = job_start(" ")', 'E474:')
1613 %bw!
Bram Moolenaar80385682016-03-27 19:13:35 +02001614endfunc
1615
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001616func Test_job_stop_immediately()
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001617 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001618 try
Bram Moolenaar570497a2019-08-22 22:55:13 +02001619 eval g:job->job_stop()
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001620 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001621 finally
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001622 call job_stop(g:job, 'kill')
1623 unlet g:job
Bram Moolenaarbb09ceb2016-10-18 16:27:23 +02001624 endtry
1625endfunc
1626
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001627" This was leaking memory.
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001628func Test_partial_in_channel_cycle()
1629 let d = {}
1630 let d.a = function('string', [d])
1631 try
1632 let d.b = ch_open('nowhere:123', {'close_cb': d.a})
1633 catch
1634 call assert_exception('E901:')
1635 endtry
1636 unlet d
1637endfunc
1638
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001639func Test_using_freed_memory()
1640 let g:a = job_start(['ls'])
1641 sleep 10m
Bram Moolenaar574860b2016-05-24 17:33:34 +02001642 call test_garbagecollect_now()
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001643endfunc
1644
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001645func Test_collapse_buffers()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001646 CheckExecutable cat
1647
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001648 sp test_channel.vim
1649 let g:linecount = line('$')
1650 close
1651 split testout
1652 1,$delete
1653 call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001654 call WaitForAssert({-> assert_inrange(g:linecount, g:linecount + 1, line('$'))})
Bram Moolenaarb8aefa42016-06-10 23:02:56 +02001655 bwipe!
1656endfunc
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001657
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001658func Test_write_to_deleted_buffer()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001659 CheckExecutable echo
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001660 CheckFeature quickfix
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001661
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001662 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001663 let bufnr = bufnr('test_buffer')
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001664 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001665 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1666 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001667
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001668 bdel test_buffer
1669 call assert_equal([], getbufline(bufnr, 1, '$'))
1670
1671 let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
Bram Moolenaarf780b8a2019-01-05 00:35:22 +01001672 call WaitForAssert({-> assert_equal(['hello'], getbufline(bufnr, 1, '$'))})
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001673 call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1674 call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1675
1676 bwipe! test_buffer
1677endfunc
1678
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001679func Test_cmd_parsing()
1680 if !has('unix')
1681 return
1682 endif
1683 call assert_false(filereadable("file with space"))
1684 let job = job_start('touch "file with space"')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001685 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001686 call delete("file with space")
1687
1688 let job = job_start('touch file\ with\ space')
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001689 call WaitForAssert({-> assert_true(filereadable("file with space"))})
Bram Moolenaard78f03f2017-10-06 01:07:41 +02001690 call delete("file with space")
1691endfunc
1692
Bram Moolenaar82117982016-08-27 19:21:48 +02001693func Test_raw_passes_nul()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001694 CheckExecutable cat
Bram Moolenaar82117982016-08-27 19:21:48 +02001695
1696 " Test lines from the job containing NUL are stored correctly in a buffer.
1697 new
1698 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1699 w! Xtestread
1700 bwipe!
1701 split testout
1702 1,$delete
1703 call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
1704 call WaitFor('line("$") > 2')
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001705 call assert_equal("asdf\nasdf", getline(1))
1706 call assert_equal("xxx\n", getline(2))
1707 call assert_equal("\nyyy", getline(3))
Bram Moolenaar82117982016-08-27 19:21:48 +02001708
1709 call delete('Xtestread')
1710 bwipe!
1711
1712 " Test lines from a buffer with NUL bytes are written correctly to the job.
1713 new mybuffer
1714 call setline(1, ["asdf\nasdf", "xxx\n", "\nyyy"])
1715 let g:Ch_job = job_start('cat', {'in_io': 'buffer', 'in_name': 'mybuffer', 'out_io': 'file', 'out_name': 'Xtestwrite'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001716 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
Bram Moolenaar82117982016-08-27 19:21:48 +02001717 bwipe!
1718 split Xtestwrite
1719 call assert_equal("asdf\nasdf", getline(1))
1720 call assert_equal("xxx\n", getline(2))
1721 call assert_equal("\nyyy", getline(3))
Bram Moolenaar819524702018-02-27 19:10:00 +01001722 call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xtestwrite$'))
Bram Moolenaar82117982016-08-27 19:21:48 +02001723
1724 call delete('Xtestwrite')
1725 bwipe!
1726endfunc
1727
Bram Moolenaarec68a992016-10-03 21:37:41 +02001728func Test_read_nonl_line()
Bram Moolenaarec68a992016-10-03 21:37:41 +02001729 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001730 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001731 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001732 call WaitForAssert({-> assert_equal(3, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001733 unlet g:linecount
1734endfunc
1735
1736func Test_read_nonl_in_close_cb()
Bram Moolenaar772153f2019-03-04 12:09:49 +01001737 func s:close_cb(ch)
1738 while ch_status(a:ch) == 'buffered'
1739 let g:out .= ch_read(a:ch)
1740 endwhile
1741 endfunc
1742
1743 let g:out = ''
1744 let arg = 'import sys;sys.stdout.write("1\n2\n3")'
1745 call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
1746 call WaitForAssert({-> assert_equal('123', g:out)})
1747 unlet g:out
1748 delfunc s:close_cb
Bram Moolenaarec68a992016-10-03 21:37:41 +02001749endfunc
1750
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001751func Test_read_from_terminated_job()
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001752 let g:linecount = 0
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001753 let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
Bram Moolenaar772153f2019-03-04 12:09:49 +01001754 call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001755 call WaitForAssert({-> assert_equal(1, g:linecount)})
Bram Moolenaar772153f2019-03-04 12:09:49 +01001756 unlet g:linecount
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001757endfunc
1758
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001759func Test_job_start_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001760 CheckMSWindows
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02001761
1762 " Check that backslash in $COMSPEC is handled properly.
1763 let g:echostr = ''
1764 let cmd = $COMSPEC . ' /c echo 123'
1765 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:echostr .= msg")}})
1766 let info = job_info(job)
1767 call assert_equal([$COMSPEC, '/c', 'echo', '123'], info.cmd)
1768
1769 call WaitForAssert({-> assert_equal("123", g:echostr)})
1770 unlet g:echostr
1771endfunc
1772
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001773func Test_env()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001774 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001775 if has('win32')
Bram Moolenaar22efba42018-04-07 13:22:21 +02001776 let cmd = ['cmd', '/c', 'echo %FOO%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001777 else
Bram Moolenaar22efba42018-04-07 13:22:21 +02001778 let cmd = [&shell, &shellcmdflag, 'echo $FOO']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001779 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001780 call assert_fails('call job_start(cmd, {"env": 1})', 'E475:')
1781 call job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'env': {'FOO': 'bar'}})
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001782 call WaitForAssert({-> assert_equal("bar", g:envstr)})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001783 unlet g:envstr
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001784endfunc
1785
1786func Test_cwd()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001787 let g:envstr = ''
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001788 if has('win32')
1789 let expect = $TEMP
Bram Moolenaar22efba42018-04-07 13:22:21 +02001790 let cmd = ['cmd', '/c', 'echo %CD%']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001791 else
1792 let expect = $HOME
Bram Moolenaar22efba42018-04-07 13:22:21 +02001793 let cmd = ['pwd']
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001794 endif
Bram Moolenaar22efba42018-04-07 13:22:21 +02001795 let job = job_start(cmd, {'callback': {ch,msg -> execute(":let g:envstr .= msg")}, 'cwd': expect})
Bram Moolenaar24820692017-12-02 16:38:12 +01001796 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001797 call WaitForAssert({-> assert_notequal("", g:envstr)})
Bram Moolenaar24820692017-12-02 16:38:12 +01001798 let expect = substitute(expect, '[/\\]$', '', '')
1799 let g:envstr = substitute(g:envstr, '[/\\]$', '', '')
1800 if $CI != '' && stridx(g:envstr, '/private/') == 0
1801 let g:envstr = g:envstr[8:]
1802 endif
1803 call assert_equal(expect, g:envstr)
1804 finally
1805 call job_stop(job)
1806 unlet g:envstr
1807 endtry
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001808endfunc
1809
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001810function Ch_test_close_lambda(port)
1811 let handle = ch_open('localhost:' . a:port, s:chopt)
1812 if ch_status(handle) == "fail"
Bram Moolenaar37175402017-03-18 20:18:45 +01001813 call assert_report("Can't open channel")
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001814 return
1815 endif
1816 let g:Ch_close_ret = ''
1817 call ch_setoptions(handle, {'close_cb': {ch -> execute("let g:Ch_close_ret = 'closed'")}})
1818
1819 call assert_equal('', ch_evalexpr(handle, 'close me'))
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001820 call WaitForAssert({-> assert_equal('closed', g:Ch_close_ret)})
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001821endfunc
1822
1823func Test_close_lambda()
Bram Moolenaar069c1e72016-07-15 21:25:08 +02001824 call s:run_server('Ch_test_close_lambda')
1825endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001826
1827func s:test_list_args(cmd, out, remove_lf)
1828 try
1829 let g:out = ''
Bram Moolenaar24820692017-12-02 16:38:12 +01001830 let job = job_start([s:python, '-c', a:cmd], {'callback': {ch, msg -> execute('let g:out .= msg')}, 'out_mode': 'raw'})
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001831 call WaitFor('"" != g:out')
1832 if has('win32')
1833 let g:out = substitute(g:out, '\r', '', 'g')
1834 endif
1835 if a:remove_lf
1836 let g:out = substitute(g:out, '\n$', '', 'g')
1837 endif
1838 call assert_equal(a:out, g:out)
1839 finally
Bram Moolenaar24820692017-12-02 16:38:12 +01001840 call job_stop(job)
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001841 unlet g:out
1842 endtry
1843endfunc
1844
1845func Test_list_args()
Bram Moolenaardcaa6132017-08-13 17:13:09 +02001846 call s:test_list_args('import sys;sys.stdout.write("hello world")', "hello world", 0)
1847 call s:test_list_args('import sys;sys.stdout.write("hello\nworld")', "hello\nworld", 0)
1848 call s:test_list_args('import sys;sys.stdout.write(''hello\nworld'')', "hello\nworld", 0)
1849 call s:test_list_args('import sys;sys.stdout.write(''hello"world'')', "hello\"world", 0)
1850 call s:test_list_args('import sys;sys.stdout.write(''hello^world'')', "hello^world", 0)
1851 call s:test_list_args('import sys;sys.stdout.write("hello&&world")', "hello&&world", 0)
1852 call s:test_list_args('import sys;sys.stdout.write(''hello\\world'')', "hello\\world", 0)
1853 call s:test_list_args('import sys;sys.stdout.write(''hello\\\\world'')', "hello\\\\world", 0)
1854 call s:test_list_args('import sys;sys.stdout.write("hello\"world\"")', 'hello"world"', 0)
1855 call s:test_list_args('import sys;sys.stdout.write("h\"ello worl\"d")', 'h"ello worl"d', 0)
1856 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo wor\\\"l\"d")', 'h"e\"llo wor\"l"d', 0)
1857 call s:test_list_args('import sys;sys.stdout.write("h\"e\\\"llo world")', 'h"e\"llo world', 0)
1858 call s:test_list_args('import sys;sys.stdout.write("hello\tworld")', "hello\tworld", 0)
1859
1860 " tests which not contain spaces in the argument
1861 call s:test_list_args('print("hello\nworld")', "hello\nworld", 1)
1862 call s:test_list_args('print(''hello\nworld'')', "hello\nworld", 1)
1863 call s:test_list_args('print(''hello"world'')', "hello\"world", 1)
1864 call s:test_list_args('print(''hello^world'')', "hello^world", 1)
1865 call s:test_list_args('print("hello&&world")', "hello&&world", 1)
1866 call s:test_list_args('print(''hello\\world'')', "hello\\world", 1)
1867 call s:test_list_args('print(''hello\\\\world'')', "hello\\\\world", 1)
1868 call s:test_list_args('print("hello\"world\"")', 'hello"world"', 1)
1869 call s:test_list_args('print("hello\tworld")', "hello\tworld", 1)
1870endfunc
Bram Moolenaard5359b22018-04-05 22:44:39 +02001871
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001872func Test_keep_pty_open()
1873 if !has('unix')
1874 return
1875 endif
1876
Bram Moolenaare2956092019-01-25 21:01:17 +01001877 let job = job_start(s:python . ' -c "import time;time.sleep(0.2)"',
1878 \ {'out_io': 'null', 'err_io': 'null', 'pty': 1})
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001879 let elapsed = WaitFor({-> job_status(job) ==# 'dead'})
1880 call assert_inrange(200, 1000, elapsed)
1881 call job_stop(job)
1882endfunc
Bram Moolenaarc46af532019-01-09 22:24:49 +01001883
1884func Test_job_start_in_timer()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001885 CheckFeature timers
Bram Moolenaar4f32f9c2020-03-15 14:53:35 +01001886 CheckFunction reltimefloat
Bram Moolenaarc46af532019-01-09 22:24:49 +01001887
1888 func OutCb(chan, msg)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001889 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001890 endfunc
1891
1892 func ExitCb(job, status)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001893 let g:val += 1
Bram Moolenaarc46af532019-01-09 22:24:49 +01001894 call Resume()
1895 endfunc
1896
1897 func TimerCb(timer)
1898 if has('win32')
1899 let cmd = ['cmd', '/c', 'echo.']
1900 else
1901 let cmd = ['echo']
1902 endif
1903 let g:job = job_start(cmd, {'out_cb': 'OutCb', 'exit_cb': 'ExitCb'})
1904 call substitute(repeat('a', 100000), '.', '', 'g')
1905 endfunc
1906
1907 " We should be interrupted before 'updatetime' elapsed.
1908 let g:val = 0
1909 call timer_start(1, 'TimerCb')
1910 let elapsed = Standby(&ut)
1911 call assert_inrange(1, &ut / 2, elapsed)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001912
1913 " Wait for both OutCb() and ExitCb() to have been called before deleting
1914 " them.
1915 call WaitForAssert({-> assert_equal(2, g:val)})
Bram Moolenaarc46af532019-01-09 22:24:49 +01001916 call job_stop(g:job)
1917
1918 delfunc OutCb
1919 delfunc ExitCb
1920 delfunc TimerCb
1921 unlet! g:val
1922 unlet! g:job
1923endfunc
Bram Moolenaar24058382019-01-24 23:11:49 +01001924
1925func Test_raw_large_data()
1926 try
1927 let g:out = ''
1928 let job = job_start(s:python . " test_channel_pipe.py",
Bram Moolenaare2956092019-01-25 21:01:17 +01001929 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1930 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
Bram Moolenaar24058382019-01-24 23:11:49 +01001931
Bram Moolenaare2956092019-01-25 21:01:17 +01001932 let outlen = 79999
1933 let want = repeat('X', outlen) . "\n"
Bram Moolenaar570497a2019-08-22 22:55:13 +02001934 eval job->ch_sendraw(want)
Bram Moolenaare2956092019-01-25 21:01:17 +01001935 call WaitFor({-> len(g:out) >= outlen}, 10000)
1936 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar24058382019-01-24 23:11:49 +01001937 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
1938 finally
1939 call job_stop(job)
1940 unlet g:out
1941 endtry
1942endfunc
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001943
Bram Moolenaar65240682019-02-10 22:23:26 +01001944func Test_no_hang_windows()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001945 CheckMSWindows
Bram Moolenaar65240682019-02-10 22:23:26 +01001946
1947 try
1948 let job = job_start(s:python . " test_channel_pipe.py busy",
1949 \ {'mode': 'raw', 'drop': 'never', 'noblock': 0})
1950 call assert_fails('call ch_sendraw(job, repeat("X", 80000))', 'E631:')
1951 finally
1952 call job_stop(job)
1953 endtry
1954endfunc
1955
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001956func Test_job_exitval_and_termsig()
1957 if !has('unix')
1958 return
1959 endif
1960
1961 " Terminate job normally
1962 let cmd = ['echo']
1963 let job = job_start(cmd)
1964 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1965 let info = job_info(job)
1966 call assert_equal(0, info.exitval)
1967 call assert_equal("", info.termsig)
1968
1969 " Terminate job by signal
1970 let cmd = ['sleep', '10']
1971 let job = job_start(cmd)
Bram Moolenaar08d2e792019-12-07 17:10:25 +01001972 " 10m usually works but 50m is needed when running Valgrind
1973 sleep 50m
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01001974 call job_stop(job)
1975 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1976 let info = job_info(job)
1977 call assert_equal(-1, info.exitval)
1978 call assert_equal("term", info.termsig)
1979endfunc
Bram Moolenaar59386482019-02-10 22:43:46 +01001980
1981func Test_job_tty_in_out()
Bram Moolenaar4641a122019-07-29 22:10:23 +02001982 CheckUnix
Bram Moolenaar59386482019-02-10 22:43:46 +01001983
1984 call writefile(['test'], 'Xtestin')
1985 let in_opts = [{},
1986 \ {'in_io': 'null'},
1987 \ {'in_io': 'file', 'in_name': 'Xtestin'}]
1988 let out_opts = [{},
1989 \ {'out_io': 'null'},
1990 \ {'out_io': 'file', 'out_name': 'Xtestout'}]
1991 let err_opts = [{},
1992 \ {'err_io': 'null'},
1993 \ {'err_io': 'file', 'err_name': 'Xtesterr'},
1994 \ {'err_io': 'out'}]
1995 let opts = []
1996
1997 for in_opt in in_opts
1998 let x = copy(in_opt)
1999 for out_opt in out_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002000 let x = extend(copy(x), out_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002001 for err_opt in err_opts
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002002 let x = extend(copy(x), err_opt)
Bram Moolenaar59386482019-02-10 22:43:46 +01002003 let opts += [extend({'pty': 1}, x)]
2004 endfor
2005 endfor
2006 endfor
2007
2008 for opt in opts
2009 let job = job_start('echo', opt)
2010 let info = job_info(job)
2011 let msg = printf('option={"in_io": "%s", "out_io": "%s", "err_io": "%s"}',
2012 \ get(opt, 'in_io', 'tty'),
2013 \ get(opt, 'out_io', 'tty'),
2014 \ get(opt, 'err_io', 'tty'))
2015
2016 if !has_key(opt, 'in_io') || !has_key(opt, 'out_io') || !has_key(opt, 'err_io')
2017 call assert_notequal('', info.tty_in, msg)
2018 else
2019 call assert_equal('', info.tty_in, msg)
2020 endif
2021 call assert_equal(info.tty_in, info.tty_out, msg)
2022
2023 call WaitForAssert({-> assert_equal('dead', job_status(job))})
2024 endfor
2025
2026 call delete('Xtestin')
2027 call delete('Xtestout')
2028 call delete('Xtesterr')
2029endfunc
Bram Moolenaarf386f082019-07-29 23:03:03 +02002030
2031" Do this last, it stops any channel log.
2032func Test_zz_nl_err_to_out_pipe()
Bram Moolenaar570497a2019-08-22 22:55:13 +02002033 eval 'Xlog'->ch_logfile()
Bram Moolenaarf386f082019-07-29 23:03:03 +02002034 call ch_log('Test_zz_nl_err_to_out_pipe()')
2035 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
2036 call assert_equal("run", job_status(job))
2037 try
2038 let handle = job_getchannel(job)
2039 call ch_sendraw(handle, "echo something\n")
2040 call assert_equal("something", ch_readraw(handle))
2041
2042 call ch_sendraw(handle, "echoerr wrong\n")
2043 call assert_equal("wrong", ch_readraw(handle))
2044 finally
2045 call job_stop(job)
2046 call ch_logfile('')
2047 let loglines = readfile('Xlog')
2048 call assert_true(len(loglines) > 10)
2049 let found_test = 0
2050 let found_send = 0
2051 let found_recv = 0
2052 let found_stop = 0
2053 for l in loglines
2054 if l =~ 'Test_zz_nl_err_to_out_pipe'
2055 let found_test = 1
2056 endif
2057 if l =~ 'SEND on.*echo something'
2058 let found_send = 1
2059 endif
2060 if l =~ 'RECV on.*something'
2061 let found_recv = 1
2062 endif
2063 if l =~ 'Stopping job with'
2064 let found_stop = 1
2065 endif
2066 endfor
2067 call assert_equal(1, found_test)
2068 call assert_equal(1, found_send)
2069 call assert_equal(1, found_recv)
2070 call assert_equal(1, found_stop)
2071 " On MS-Windows need to sleep for a moment to be able to delete the file.
2072 sleep 10m
2073 call delete('Xlog')
2074 endtry
2075endfunc
2076
2077" Do this last, it stops any channel log.
2078func Test_zz_ch_log()
2079 call ch_logfile('Xlog', 'w')
2080 call ch_log('hello there')
2081 call ch_log('%s%s')
2082 call ch_logfile('')
2083 let text = readfile('Xlog')
2084 call assert_match("hello there", text[1])
2085 call assert_match("%s%s", text[2])
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002086 call mkdir("Xdir1")
2087 call assert_fails("call ch_logfile('Xdir1')", 'E484:')
2088 cal delete("Xdir1", 'd')
Bram Moolenaarf386f082019-07-29 23:03:03 +02002089 call delete('Xlog')
2090endfunc
Bram Moolenaar538feb52020-01-20 21:59:39 +01002091
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002092func Test_issue_5150()
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002093 if has('win32')
2094 let cmd = 'cmd /c pause'
2095 else
2096 let cmd = 'grep foo'
2097 endif
2098 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002099 call job_stop(g:job)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002100 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002101 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002102 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002103 call job_stop(g:job, 'term')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002104 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002105 call assert_equal(-1, job_info(g:job).exitval)
Bram Moolenaard0d440f2020-03-07 17:24:59 +01002106 let g:job = job_start(cmd, {})
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002107 call job_stop(g:job, 'kill')
Bram Moolenaar353c3512020-03-15 14:19:26 +01002108 sleep 50m
Bram Moolenaarb3e195c2020-02-10 21:32:19 +01002109 call assert_equal(-1, job_info(g:job).exitval)
2110endfunc
Bram Moolenaar355757a2020-02-10 22:06:32 +01002111
2112func Test_issue_5485()
2113 let $VAR1 = 'global'
2114 let g:Ch_reply = ""
2115 let l:job = job_start([&shell, &shellcmdflag, has('win32') ? 'echo %VAR1% %VAR2%' : 'echo $VAR1 $VAR2'], {'env': {'VAR1': 'local', 'VAR2': 'local'}, 'callback': 'Ch_handler'})
2116 let g:Ch_job = l:job
2117 call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
2118 unlet $VAR1
2119endfunc
Bram Moolenaar8b633132020-03-20 18:20:51 +01002120
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002121func Test_job_trailing_space_unix()
2122 CheckUnix
2123 CheckExecutable cat
2124 let job = job_start("cat ", #{in_io: 'null'})
2125 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2126 call assert_equal(0, job_info(job).exitval)
2127endfunc
2128
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002129func Test_ch_getbufnr()
2130 let ch = test_null_channel()
2131 call assert_equal(-1, ch_getbufnr(ch, 'in'))
2132 call assert_equal(-1, ch_getbufnr(ch, 'out'))
2133 call assert_equal(-1, ch_getbufnr(ch, 'err'))
2134 call assert_equal(-1, ch_getbufnr(ch, ''))
2135endfunc
2136
2137" Test for unsupported options passed to ch_status()
2138func Test_invalid_job_chan_options()
2139 let ch = test_null_channel()
2140 let invalid_opts = [
2141 \ {'in_io' : 'null'},
2142 \ {'out_io' : 'null'},
2143 \ {'err_io' : 'null'},
2144 \ {'mode' : 'json'},
2145 \ {'out_mode' : 'json'},
2146 \ {'err_mode' : 'json'},
2147 \ {'noblock' : 1},
2148 \ {'in_name' : '/a/b'},
2149 \ {'pty' : 1},
2150 \ {'in_buf' : 1},
2151 \ {'out_buf' : 1},
2152 \ {'err_buf' : 1},
2153 \ {'out_modifiable' : 1},
2154 \ {'err_modifiable' : 1},
2155 \ {'out_msg' : 1},
2156 \ {'err_msg' : 1},
2157 \ {'in_top' : 1},
2158 \ {'in_bot' : 1},
2159 \ {'channel' : ch},
2160 \ {'callback' : ''},
2161 \ {'out_cb' : ''},
2162 \ {'err_cb' : ''},
2163 \ {'close_cb' : ''},
2164 \ {'exit_cb' : ''},
2165 \ {'term_opencmd' : ''},
2166 \ {'eof_chars' : ''},
2167 \ {'term_rows' : 10},
2168 \ {'term_cols' : 10},
2169 \ {'vertical' : 0},
2170 \ {'curwin' : 1},
2171 \ {'bufnr' : 1},
2172 \ {'hidden' : 0},
2173 \ {'norestore' : 0},
2174 \ {'term_kill' : 'kill'},
2175 \ {'tty_type' : ''},
2176 \ {'term_highlight' : ''},
2177 \ {'env' : {}},
2178 \ {'cwd' : ''},
2179 \ {'timeout' : 0},
2180 \ {'out_timeout' : 0},
2181 \ {'err_timeout' : 0},
2182 \ {'id' : 0},
2183 \ {'stoponexit' : ''},
2184 \ {'block_write' : 1}
2185 \ ]
2186 if has('gui')
2187 call add(invalid_opts, {'ansi_colors' : []})
2188 endif
2189
2190 for opt in invalid_opts
2191 call assert_fails("let x = ch_status(ch, opt)", 'E475:')
2192 endfor
2193endfunc
2194
2195" Test for passing the command and the arguments as List on MS-Windows
2196func Test_job_with_list_args()
2197 CheckMSWindows
2198
2199 enew!
2200 let bnum = bufnr()
2201 let job = job_start(['cmd', '/c', 'echo', 'Hello', 'World'], {'out_io' : 'buffer', 'out_buf' : bnum})
2202 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2203 call assert_equal('Hello World', getline(1))
2204 %bw!
2205endfunc
2206
Bram Moolenaar8b633132020-03-20 18:20:51 +01002207" vim: shiftwidth=2 sts=2 expandtab