1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071openCoreopenAsyncopenImportopenVcamlopenVcaml_test_helpers(* [Child] clients are used by oneshot plugins. *)let%expect_test"Simple test of [Child] client"=Expect_test_helpers_async.with_temp_dir(funtmp_dir->letsocket=tmp_dir^/"socket"inlet%bindnvim=(* There is some undocumented internal limit for the socket length (it doesn't
appear in `:h limits`) so to ensure we create a socket we set the working dir
to [tmp_dir] and create the socket with a relative path. *)Process.create_exn()~working_dir:tmp_dir~prog:neovim_path~args:["--headless";"-n";"--clean";"--listen";"./socket"]~env:(`Extend["NVIM_RPLUGIN_MANIFEST","rplugin.vim"])inlet%bind()=with_process_cleanup~name:"nvim"(Process.pidnvim)~f:(fun()->match%bindspin_until_nvim_creates_socket_file(Process.pidnvim)~socketwith|`Nvim_crashedexit_or_signal->return(`Already_reapedexit_or_signal)|`Socket_missing->raise_s[%message"Socket was not created"]|`Socket_created->letsaved_stdin=Core_unix.dupCore_unix.stdininletsaved_stdout=Core_unix.dupCore_unix.stdoutinletsocketfd=Core_unix.socket~domain:PF_UNIX~kind:SOCK_STREAM~protocol:0()inCore_unix.connectsocketfd~addr:(ADDR_UNIXsocket);Core_unix.dup2~src:socketfd~dst:Core_unix.stdin();Core_unix.dup2~src:socketfd~dst:Core_unix.stdout();lettest()=letopenDeferred.Or_error.Let_syntaxinlet%bindclient=letclient=Client.create~on_error:`Raisein(* We don't close the reader and writer on disconnect because we want
[Reader.stdin] and [Writer.stdout] to continue to work after restoring
the original stdin and stdout. *)Client.attach~close_reader_and_writer_on_disconnect:false~time_source:(Time_source.read_only(Time_source.create~now:Time_ns.epoch()))clientStdioinlet%bindresult=run_join[%here]client(Nvim.eval"'Hello, world!'"~result_type:String)inlet%map()=attempt_to_quit~tmp_dir~client|>Deferred.okinresultinlet%bindresult=Deferred.Or_error.try_withtest>>|Or_error.joininCore_unix.dup2~src:saved_stdin~dst:Core_unix.stdin();Core_unix.dup2~src:saved_stdout~dst:Core_unix.stdout();Core_unix.closesaved_stdin;Core_unix.closesaved_stdout;Core_unix.closesocketfd;print_s[%sexp(result:stringOr_error.t)];return(`Need_to_reap`Patient))in[%expect{|
(Ok "Hello, world!")
("nvim exited" (exit_or_signal (Ok ()))) |}];return());;