diff options
Diffstat (limited to 'test/client/uv_stream.lua')
-rw-r--r-- | test/client/uv_stream.lua | 63 |
1 files changed, 44 insertions, 19 deletions
diff --git a/test/client/uv_stream.lua b/test/client/uv_stream.lua index cea77f0dbd..0540c44eb2 100644 --- a/test/client/uv_stream.lua +++ b/test/client/uv_stream.lua @@ -1,18 +1,28 @@ -local uv = require('luv') +local uv = vim.uv +--- @class test.Stream +--- @field write fun(self, data: string|string[]) +--- @field read_start fun(self, cb: fun(chunk: string)) +--- @field read_stop fun(self) +--- @field close fun(self, signal?: string) + +--- @class vim.StdioStream : test.Stream +--- @field private _in uv.uv_pipe_t +--- @field private _out uv.uv_pipe_t local StdioStream = {} StdioStream.__index = StdioStream function StdioStream.open() local self = setmetatable({ - _in = uv.new_pipe(false), - _out = uv.new_pipe(false) + _in = assert(uv.new_pipe(false)), + _out = assert(uv.new_pipe(false)), }, StdioStream) self._in:open(0) self._out:open(1) return self end +--- @param data string|string[] function StdioStream:write(data) self._out:write(data) end @@ -35,34 +45,36 @@ function StdioStream:close() self._out:close() end +--- @class test.SocketStream : test.Stream +--- @field package _stream_error? string +--- @field package _socket uv.uv_pipe_t local SocketStream = {} SocketStream.__index = SocketStream function SocketStream.open(file) - local socket = uv.new_pipe(false) + local socket = assert(uv.new_pipe(false)) local self = setmetatable({ _socket = socket, - _stream_error = nil + _stream_error = nil, }, SocketStream) - uv.pipe_connect(socket, file, function (err) + uv.pipe_connect(socket, file, function(err) self._stream_error = self._stream_error or err end) return self end function SocketStream.connect(host, port) - local socket = uv.new_tcp() + local socket = assert(uv.new_tcp()) local self = setmetatable({ _socket = socket, - _stream_error = nil + _stream_error = nil, }, SocketStream) - uv.tcp_connect(socket, host, port, function (err) + uv.tcp_connect(socket, host, port, function(err) self._stream_error = self._stream_error or err end) return self end - function SocketStream:write(data) if self._stream_error then error(self._stream_error) @@ -97,23 +109,36 @@ function SocketStream:close() uv.close(self._socket) end +--- @class test.ChildProcessStream : test.Stream +--- @field private _proc uv.uv_process_t +--- @field private _pid integer +--- @field private _child_stdin uv.uv_pipe_t +--- @field private _child_stdout uv.uv_pipe_t +--- @field status integer +--- @field signal integer local ChildProcessStream = {} ChildProcessStream.__index = ChildProcessStream +--- @param argv string[] +--- @param env string[]? +--- @param io_extra uv.uv_pipe_t? +--- @return test.ChildProcessStream function ChildProcessStream.spawn(argv, env, io_extra) local self = setmetatable({ - _child_stdin = uv.new_pipe(false); - _child_stdout = uv.new_pipe(false); - _exiting = false; + _child_stdin = uv.new_pipe(false), + _child_stdout = uv.new_pipe(false), + _exiting = false, }, ChildProcessStream) local prog = argv[1] - local args = {} + local args = {} --- @type string[] for i = 2, #argv do args[#args + 1] = argv[i] end + --- @diagnostic disable-next-line:missing-fields self._proc, self._pid = uv.spawn(prog, { - stdio = {self._child_stdin, self._child_stdout, 2, io_extra}, + stdio = { self._child_stdin, self._child_stdout, 2, io_extra }, args = args, + --- @diagnostic disable-next-line:assign-type-mismatch env = env, }, function(status, signal) self.status = status @@ -154,7 +179,7 @@ function ChildProcessStream:close(signal) self._child_stdin:close() self._child_stdout:close() if type(signal) == 'string' then - self._proc:kill('sig'..signal) + self._proc:kill('sig' .. signal) end while self.status == nil do uv.run 'once' @@ -163,7 +188,7 @@ function ChildProcessStream:close(signal) end return { - StdioStream = StdioStream; - ChildProcessStream = ChildProcessStream; - SocketStream = SocketStream; + StdioStream = StdioStream, + ChildProcessStream = ChildProcessStream, + SocketStream = SocketStream, } |