patch 8.1.0993: ch_read() may return garbage if terminating NL is missing
Problem: ch_read() may return garbage if terminating NL is missing.
Solution: Add terminating NUL. (Ozaki Kiichi, closes #4065)
diff --git a/src/testdir/test_channel.vim b/src/testdir/test_channel.vim
index 67163e6..2f2b559 100644
--- a/src/testdir/test_channel.vim
+++ b/src/testdir/test_channel.vim
@@ -203,8 +203,7 @@
let start = reltime()
call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
let elapsed = reltime(start)
- call assert_true(reltimefloat(elapsed) > 0.3)
- call assert_true(reltimefloat(elapsed) < 0.6)
+ call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
" Send without waiting for a response, then wait for a response.
call ch_sendexpr(handle, 'wait a bit')
@@ -434,9 +433,7 @@
else
" Failed connection should wait about 500 msec. Can be longer if the
" computer is busy with other things.
- let elapsed = reltime(start)
- call assert_true(reltimefloat(elapsed) > 0.3)
- call assert_true(reltimefloat(elapsed) < 1.5)
+ call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
endif
catch
if v:exception !~ 'Connection reset by peer'
@@ -1590,8 +1587,7 @@
else
let elapsed = 1.0
endif
- call assert_true(elapsed > 0.5)
- call assert_true(elapsed < 1.0)
+ call assert_inrange(0.5, 1.0, elapsed)
endfunc
"""""""""
@@ -1764,10 +1760,6 @@
bwipe!
endfunc
-func MyLineCountCb(ch, msg)
- let g:linecount += 1
-endfunc
-
func Test_read_nonl_line()
if !has('job')
return
@@ -1775,8 +1767,28 @@
let g:linecount = 0
let arg = 'import sys;sys.stdout.write("1\n2\n3")'
- call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
+ call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
call WaitForAssert({-> assert_equal(3, g:linecount)})
+ unlet g:linecount
+endfunc
+
+func Test_read_nonl_in_close_cb()
+ if !has('job')
+ return
+ endif
+
+ func s:close_cb(ch)
+ while ch_status(a:ch) == 'buffered'
+ let g:out .= ch_read(a:ch)
+ endwhile
+ endfunc
+
+ let g:out = ''
+ let arg = 'import sys;sys.stdout.write("1\n2\n3")'
+ call job_start([s:python, '-c', arg], {'close_cb': function('s:close_cb')})
+ call WaitForAssert({-> assert_equal('123', g:out)})
+ unlet g:out
+ delfunc s:close_cb
endfunc
func Test_read_from_terminated_job()
@@ -1786,8 +1798,9 @@
let g:linecount = 0
let arg = 'import os,sys;os.close(1);sys.stderr.write("test\n")'
- call job_start([s:python, '-c', arg], {'callback': 'MyLineCountCb'})
+ call job_start([s:python, '-c', arg], {'callback': {-> execute('let g:linecount += 1')}})
call WaitForAssert({-> assert_equal(1, g:linecount)})
+ unlet g:linecount
endfunc
func Test_job_start_windows()