robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Lewis Russelltest: run Lua harness with nvim -l9432e6c1e2

master
7.8 KiB301 linesraw
1---
2--- Basic stream types.
3--- See `rpc_stream.lua` for the msgpack layer.
4---
5
6local uv = vim.uv
7
8local function close_handle(handle)
9  if handle and not handle:is_closing() then
10    handle:close()
11  end
12end
13
14local function read_stop(handle)
15  if handle and not handle:is_closing() then
16    handle:read_stop()
17  end
18end
19
20--- @class test.Stream
21--- @field write fun(self, data: string|string[])
22--- @field read_start fun(self, cb: fun(chunk: string))
23--- @field read_stop fun(self)
24--- @field close fun(self, signal?: string, noblock?: boolean)
25
26--- Stream over given pipes.
27---
28--- @class vim.StdioStream : test.Stream
29--- @field private _in uv.uv_pipe_t
30--- @field private _out uv.uv_pipe_t
31local StdioStream = {}
32StdioStream.__index = StdioStream
33
34function StdioStream.open()
35  local self = setmetatable({
36    _in = assert(uv.new_pipe(false)),
37    _out = assert(uv.new_pipe(false)),
38  }, StdioStream)
39  self._in:open(0)
40  self._out:open(1)
41  return self
42end
43
44--- @param data string|string[]
45function StdioStream:write(data)
46  self._out:write(data)
47end
48
49function StdioStream:read_start(cb)
50  self._in:read_start(function(err, chunk)
51    if err then
52      error(err)
53    end
54    cb(chunk)
55  end)
56end
57
58function StdioStream:read_stop()
59  read_stop(self._in)
60end
61
62function StdioStream:close()
63  close_handle(self._in)
64  close_handle(self._out)
65end
66
67--- Stream over a named pipe or TCP socket.
68---
69--- @class test.SocketStream : test.Stream
70--- @field package _stream_error? string
71--- @field package _socket uv.uv_pipe_t
72local SocketStream = {}
73SocketStream.__index = SocketStream
74
75function SocketStream.open(file)
76  local socket = assert(uv.new_pipe(false))
77  local self = setmetatable({
78    _socket = socket,
79    _stream_error = nil,
80  }, SocketStream)
81  uv.pipe_connect(socket, file, function(err)
82    uv.stop()
83    self._stream_error = self._stream_error or err
84  end)
85  -- On Windows, writing to the pipe doesn't work if it's not connected yet,
86  -- so wait for the connect callback to be called.
87  uv.run()
88  if self._stream_error then
89    close_handle(socket)
90    error(self._stream_error)
91  end
92  return self
93end
94
95function SocketStream.connect(host, port)
96  local socket = assert(uv.new_tcp())
97  local self = setmetatable({
98    _socket = socket,
99    _stream_error = nil,
100  }, SocketStream)
101  uv.tcp_connect(socket, host, port, function(err)
102    self._stream_error = self._stream_error or err
103  end)
104  return self
105end
106
107function SocketStream:write(data)
108  if self._stream_error then
109    error(self._stream_error)
110  end
111  uv.write(self._socket, data, function(err)
112    if err then
113      self._stream_error = self._stream_error or err
114    end
115  end)
116end
117
118function SocketStream:read_start(cb)
119  if self._stream_error then
120    error(self._stream_error)
121  end
122  uv.read_start(self._socket, function(err, chunk)
123    if err then
124      self._stream_error = self._stream_error or err
125      cb(nil) -- Signal EOF so the session layer stops.
126      return
127    end
128    cb(chunk)
129  end)
130end
131
132function SocketStream:read_stop()
133  if self._stream_error then
134    error(self._stream_error)
135  end
136  read_stop(self._socket)
137end
138
139function SocketStream:close()
140  close_handle(self._socket)
141end
142
143--- Stream over child process stdio.
144---
145--- @class test.ProcStream : test.Stream
146--- @field private _proc uv.uv_process_t
147--- @field private _pid integer
148--- @field private _child_stdin uv.uv_pipe_t
149--- @field private _child_stdout uv.uv_pipe_t
150--- @field private _child_stderr uv.uv_pipe_t
151--- @field package _closed integer
152--- @field package _on_exit fun(closed: integer?)
153--- Collects stdout (if `collect_text=true`). Treats data as text (CRLF converted to LF).
154--- @field stdout string
155--- Collects stderr as raw data.
156--- @field stderr string
157--- Gets stderr+stdout as text (CRLF converted to LF).
158--- @field output fun(): string
159--- @field stdout_eof boolean
160--- @field stderr_eof boolean
161--- Collects text into the `stdout` field.
162--- @field collect_text boolean
163--- Exit code
164--- @field status integer
165--- @field signal integer
166local ProcStream = {}
167ProcStream.__index = ProcStream
168
169--- Starts child process specified by `argv`.
170---
171--- @param argv string[]
172--- @param env string[]?
173--- @param io_extra uv.uv_pipe_t?
174--- @param on_exit fun(closed: integer?)? Called after the child process exits.
175--- `closed` is the timestamp (uv.now()) when close() was called, or nil if it wasn't.
176--- @param forward_stderr boolean? Forward child process stderr, otherwise collect it.
177--- @return test.ProcStream
178function ProcStream.spawn(argv, env, io_extra, on_exit, forward_stderr)
179  local self = setmetatable({
180    collect_text = false,
181    output = function(self)
182      if not self.collect_text then
183        error('set collect_text=true')
184      end
185      return (self.stderr .. self.stdout):gsub('\r\n', '\n')
186    end,
187    stdout = '',
188    stderr = '',
189    stdout_eof = false,
190    stderr_eof = false,
191    _child_stdin = assert(uv.new_pipe(false)),
192    _child_stdout = assert(uv.new_pipe(false)),
193    _child_stderr = assert(uv.new_pipe(false)),
194    _exiting = false,
195    _on_exit = on_exit,
196  }, ProcStream)
197  local prog = argv[1]
198  local args = {} --- @type string[]
199  for i = 2, #argv do
200    args[#args + 1] = argv[i]
201  end
202  local stderr = forward_stderr and 1 or self._child_stderr
203  --- @diagnostic disable-next-line:missing-fields
204  self._proc, self._pid = uv.spawn(prog, {
205    stdio = { self._child_stdin, self._child_stdout, stderr, io_extra },
206    args = args,
207    --- @diagnostic disable-next-line:assign-type-mismatch
208    env = env,
209  }, function(status, signal)
210    self.signal = signal
211    -- "Abort" exit may not set status; force to nonzero in that case.
212    self.status = (0 ~= (status or 0) or 0 == (signal or 0)) and status or (128 + (signal or 0))
213    close_handle(self._child_stdin)
214    close_handle(self._proc)
215    if self._on_exit then
216      self._on_exit(self._closed)
217    end
218  end)
219
220  if not self._proc then
221    local err = self._pid
222    error(err)
223  end
224
225  return self
226end
227
228function ProcStream:write(data)
229  self._child_stdin:write(data)
230end
231
232function ProcStream:on_read(stream, cb, err, chunk)
233  if err then
234    error(err) -- stream read failed?
235  elseif chunk then
236    -- Always collect stderr, in case it gives useful info on failure.
237    if stream == 'stderr' then
238      self.stderr = self.stderr .. chunk --[[@as string]]
239    elseif stream == 'stdout' and self.collect_text then
240      -- Set `stdout` and convert CRLF => LF.
241      self.stdout = (self.stdout .. chunk):gsub('\r\n', '\n')
242    end
243  else
244    -- stderr_eof/stdout_eof
245    self[stream .. '_eof'] = true ---@type boolean
246    -- EOF is the stream's lifecycle end even if the caller never closes it.
247    close_handle(stream == 'stdout' and self._child_stdout or self._child_stderr)
248  end
249
250  -- Handler provided by the caller.
251  if cb then
252    cb(chunk)
253  end
254end
255
256--- Collects output until the process exits.
257function ProcStream:wait()
258  while not (self.stdout_eof and self.stderr_eof and (self.status or self.signal)) do
259    uv.run('once')
260  end
261end
262
263function ProcStream:read_start(on_stdout, on_stderr)
264  self._child_stdout:read_start(function(err, chunk)
265    self:on_read('stdout', on_stdout, err, chunk)
266  end)
267  self._child_stderr:read_start(function(err, chunk)
268    self:on_read('stderr', on_stderr, err, chunk)
269  end)
270end
271
272function ProcStream:read_stop()
273  read_stop(self._child_stdout)
274  read_stop(self._child_stderr)
275end
276
277function ProcStream:close(signal, noblock)
278  if self._closed then
279    return
280  end
281  self._closed = uv.now()
282  self:read_stop()
283  close_handle(self._child_stdin)
284  close_handle(self._child_stdout)
285  close_handle(self._child_stderr)
286  if type(signal) == 'string' and self._proc and not self._proc:is_closing() then
287    self._proc:kill('sig' .. signal)
288  end
289  if not noblock then
290    while self.status == nil do
291      uv.run 'once'
292    end
293    return self.status, self.signal
294  end
295end
296
297return {
298  StdioStream = StdioStream,
299  ProcStream = ProcStream,
300  SocketStream = SocketStream,
301}