test_clientserver.vim 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. " Tests for the +clientserver feature.
  2. source check.vim
  3. CheckFeature job
  4. if !has('clientserver')
  5. call assert_fails('call remote_startserver("local")', 'E942:')
  6. endif
  7. CheckFeature clientserver
  8. source shared.vim
  9. func Check_X11_Connection()
  10. if has('x11')
  11. CheckEnv DISPLAY
  12. try
  13. call remote_send('xxx', '')
  14. catch
  15. if v:exception =~ 'E240:'
  16. throw 'Skipped: no connection to the X server'
  17. endif
  18. " ignore other errors
  19. endtry
  20. endif
  21. endfunc
  22. func Test_client_server()
  23. let g:test_is_flaky = 1
  24. let cmd = GetVimCommand()
  25. if cmd == ''
  26. throw 'GetVimCommand() failed'
  27. endif
  28. call Check_X11_Connection()
  29. let name = 'XVIMTEST'
  30. let cmd .= ' --servername ' . name
  31. let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
  32. call WaitForAssert({-> assert_equal("run", job_status(job))})
  33. " Takes a short while for the server to be active.
  34. " When using valgrind it takes much longer.
  35. call WaitForAssert({-> assert_match(name, serverlist())})
  36. if !has('win32')
  37. if RunVim([], [], '--serverlist >Xtest_serverlist')
  38. let lines = readfile('Xtest_serverlist')
  39. call assert_true(index(lines, 'XVIMTEST') >= 0)
  40. endif
  41. call delete('Xtest_serverlist')
  42. endif
  43. eval name->remote_foreground()
  44. call remote_send(name, ":let testvar = 'yes'\<CR>")
  45. call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
  46. call assert_equal('yes', remote_expr(name, "testvar", "", 2))
  47. call assert_fails("let x=remote_expr(name, '2+x')", 'E449:')
  48. call assert_fails("let x=remote_expr('[], '2+2')", 'E116:')
  49. if has('unix') && has('gui') && !has('gui_running')
  50. " Running in a terminal and the GUI is available: Tell the server to open
  51. " the GUI and check that the remote command still works.
  52. " Need to wait for the GUI to start up, otherwise the send hangs in trying
  53. " to send to the terminal window.
  54. if has('gui_motif')
  55. " For this GUI ignore the 'failed to create input context' error.
  56. call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>")
  57. else
  58. call remote_send(name, ":gui -f\<CR>")
  59. endif
  60. " Wait for the server to be up and answering requests.
  61. " When using valgrind this can be very, very slow.
  62. sleep 1
  63. call WaitForAssert({-> assert_match('\d', name->remote_expr("v:version", "", 1))}, 10000)
  64. call remote_send(name, ":let testvar = 'maybe'\<CR>")
  65. call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))})
  66. endif
  67. call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241')
  68. call writefile(['one'], 'Xclientfile')
  69. let cmd = GetVimProg() .. ' --servername ' .. name .. ' --remote Xclientfile'
  70. call system(cmd)
  71. call WaitForAssert({-> assert_equal('Xclientfile', remote_expr(name, "bufname()", "", 2))})
  72. call WaitForAssert({-> assert_equal('one', remote_expr(name, "getline(1)", "", 2))})
  73. call writefile(['one', 'two'], 'Xclientfile')
  74. call system(cmd)
  75. call WaitForAssert({-> assert_equal('two', remote_expr(name, "getline(2)", "", 2))})
  76. call delete('Xclientfile')
  77. " Expression evaluated locally.
  78. if v:servername == ''
  79. eval 'MYSELF'->remote_startserver()
  80. " May get MYSELF1 when running the test again.
  81. call assert_match('MYSELF', v:servername)
  82. call assert_fails("call remote_startserver('MYSELF')", 'E941:')
  83. endif
  84. let g:testvar = 'myself'
  85. call assert_equal('myself', remote_expr(v:servername, 'testvar'))
  86. call remote_send(v:servername, ":let g:testvar2 = 75\<CR>")
  87. call feedkeys('', 'x')
  88. call assert_equal(75, g:testvar2)
  89. call assert_fails('let v = remote_expr(v:servername, "/2")', ['E15:.*/2'])
  90. call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
  91. call assert_equal('got it', g:myserverid->remote_read(2))
  92. call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid')
  93. let peek_result = 'nothing'
  94. let r = g:myserverid->remote_peek('peek_result')
  95. " unpredictable whether the result is already available.
  96. if r > 0
  97. call assert_equal('another', peek_result)
  98. elseif r == 0
  99. call assert_equal('nothing', peek_result)
  100. else
  101. call assert_report('remote_peek() failed')
  102. endif
  103. let g:peek_result = 'empty'
  104. call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
  105. call assert_equal('another', g:peek_result)
  106. call assert_equal('another', remote_read(g:myserverid, 2))
  107. if !has('gui_running')
  108. " In GUI vim, the following tests display a dialog box
  109. let cmd = GetVimProg() .. ' --servername ' .. name
  110. " Run a separate instance to send a command to the server
  111. call remote_expr(name, 'execute("only")')
  112. call system(cmd .. ' --remote-send ":new Xclientfile<CR>"')
  113. call assert_equal('2', remote_expr(name, 'winnr("$")'))
  114. call assert_equal('Xclientfile', remote_expr(name, 'winbufnr(1)->bufname()'))
  115. call remote_expr(name, 'execute("only")')
  116. " Invoke a remote-expr. On MS-Windows, the returned value has a carriage
  117. " return.
  118. let l = system(cmd .. ' --remote-expr "2 + 2"')
  119. call assert_equal(['4'], split(l, "\n"))
  120. " Edit multiple files using --remote
  121. call system(cmd .. ' --remote Xclientfile1 Xclientfile2 Xclientfile3')
  122. call assert_match(".*Xclientfile1\n.*Xclientfile2\n.*Xclientfile3\n", remote_expr(name, 'argv()'))
  123. eval name->remote_send(":%bw!\<CR>")
  124. " Edit files in separate tab pages
  125. call system(cmd .. ' --remote-tab Xclientfile1 Xclientfile2 Xclientfile3')
  126. call WaitForAssert({-> assert_equal('3', remote_expr(name, 'tabpagenr("$")'))})
  127. call assert_match('.*\<Xclientfile2', remote_expr(name, 'bufname(tabpagebuflist(2)[0])'))
  128. eval name->remote_send(":%bw!\<CR>")
  129. " Edit a file using --remote-wait
  130. eval name->remote_send(":source $VIMRUNTIME/plugin/rrhelper.vim\<CR>")
  131. call system(cmd .. ' --remote-wait +enew Xclientfile1')
  132. call assert_match('.*\<Xclientfile1', remote_expr(name, 'bufname("#")'))
  133. eval name->remote_send(":%bw!\<CR>")
  134. " Edit files using --remote-tab-wait
  135. call system(cmd .. ' --remote-tabwait +tabonly\|enew Xclientfile1 Xclientfile2')
  136. call assert_equal('1', remote_expr(name, 'tabpagenr("$")'))
  137. eval name->remote_send(":%bw!\<CR>")
  138. " Error cases
  139. if v:lang == "C" || v:lang =~ '^[Ee]n'
  140. let l = split(system(cmd .. ' --remote +pwd'), "\n")
  141. call assert_equal("Argument missing after: \"+pwd\"", l[1])
  142. endif
  143. let l = system(cmd .. ' --remote-expr "abcd"')
  144. call assert_match('^E449: ', l)
  145. endif
  146. eval name->remote_send(":%bw!\<CR>")
  147. eval name->remote_send(":qa!\<CR>")
  148. try
  149. call WaitForAssert({-> assert_equal("dead", job_status(job))})
  150. finally
  151. if job_status(job) != 'dead'
  152. call assert_report('Server did not exit')
  153. call job_stop(job, 'kill')
  154. endif
  155. endtry
  156. call assert_fails('call remote_startserver([])', 'E730:')
  157. call assert_fails("let x = remote_peek([])", 'E730:')
  158. call assert_fails("let x = remote_read('vim10')",
  159. \ has('unix') ? ['E573:.*vim10'] : 'E277:')
  160. call assert_fails("call server2client('abc', 'xyz')",
  161. \ has('unix') ? ['E573:.*abc'] : 'E258:')
  162. endfunc
  163. " Uncomment this line to get a debugging log
  164. " call ch_logfile('channellog', 'w')
  165. " vim: shiftwidth=2 sts=2 expandtab