chore(types + tests): update types and tests for exec

This commit is contained in:
Erica Marigold 2024-06-09 21:54:56 +05:30
parent 50b1bcbd64
commit 6a2f5061d5
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
7 changed files with 32 additions and 26 deletions

View file

@ -129,7 +129,7 @@ end
]] ]]
print("Sending 4 pings to google 🌏") print("Sending 4 pings to google 🌏")
local result = process.spawn("ping", { local result = process.exec("ping", {
"google.com", "google.com",
"-c 4", "-c 4",
}) })

View file

@ -11,18 +11,16 @@ pub struct ChildProcessReader<R: AsyncRead>(pub R);
pub struct ChildProcessWriter<W: AsyncWrite>(pub W); pub struct ChildProcessWriter<W: AsyncWrite>(pub W);
impl<R: AsyncRead + Unpin> ChildProcessReader<R> { impl<R: AsyncRead + Unpin> ChildProcessReader<R> {
pub async fn read(&mut self) -> LuaResult<Vec<u8>> { pub async fn read(&mut self, chunk_size: Option<usize>) -> LuaResult<Vec<u8>> {
let mut buf = BytesMut::with_capacity(CHUNK_SIZE); let mut buf = BytesMut::with_capacity(chunk_size.unwrap_or(CHUNK_SIZE));
self.0.read_buf(&mut buf).await?; self.0.read_buf(&mut buf).await?;
Ok(buf.to_vec()) Ok(buf.to_vec())
} }
pub async fn read_to_end(&mut self) -> LuaResult<Vec<u8>> { pub async fn read_to_end(&mut self) -> LuaResult<Vec<u8>> {
// FIXME: This yields, but should rather only return the stdout
// till present moment instead, so we should have our own logic
// instead of using read_to_end
let mut buf = vec![]; let mut buf = vec![];
self.0.read_to_end(&mut buf).await?; self.0.read_to_end(&mut buf).await?;
Ok(buf) Ok(buf)
} }
@ -30,8 +28,8 @@ impl<R: AsyncRead + Unpin> ChildProcessReader<R> {
impl<R: AsyncRead + Unpin + 'static> LuaUserData for ChildProcessReader<R> { impl<R: AsyncRead + Unpin + 'static> LuaUserData for ChildProcessReader<R> {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method_mut("read", |lua, this, ()| async { methods.add_async_method_mut("read", |lua, this, chunk_size: Option<usize>| async move {
Ok(lua.create_buffer(this.read().await?)) Ok(lua.create_buffer(this.read(chunk_size).await?))
}); });
methods.add_async_method_mut("readToEnd", |lua, this, ()| async { methods.add_async_method_mut("readToEnd", |lua, this, ()| async {

View file

@ -108,7 +108,7 @@ local BIN_ZLIB = if process.os == "macos" then "/opt/homebrew/bin/pigz" else "pi
local function checkInstalled(program: string, args: { string }?) local function checkInstalled(program: string, args: { string }?)
print("Checking if", program, "is installed") print("Checking if", program, "is installed")
local result = process.spawn(program, args) local result = process.exec(program, args)
if not result.ok then if not result.ok then
stdio.ewrite(string.format("Program '%s' is not installed\n", program)) stdio.ewrite(string.format("Program '%s' is not installed\n", program))
process.exit(1) process.exit(1)
@ -123,7 +123,7 @@ checkInstalled(BIN_ZLIB, { "--version" })
-- Run them to generate files -- Run them to generate files
local function run(program: string, args: { string }): string local function run(program: string, args: { string }): string
local result = process.spawn(program, args) local result = process.exec(program, args)
if not result.ok then if not result.ok then
stdio.ewrite(string.format("Command '%s' failed\n", program)) stdio.ewrite(string.format("Command '%s' failed\n", program))
if #result.stdout > 0 then if #result.stdout > 0 then

View file

@ -1,16 +1,22 @@
local process = require("@lune/process") local process = require("@lune/process")
local stdio = require("@lune/stdio") local stdio = require("@lune/stdio")
local task = require("@lune/task")
local child = process.spawn("echo", { "lsp" })
task.wait(1)
local child = process.spawn("luau-lsp", { "lsp" })
while true do stdio.write(buffer.tostring(child.stdout:readToEnd()))
child.stdin:write("hello world") stdio.write(buffer.tostring(child.stdout:readToEnd()))
local buf = child.stdout:read() stdio.write(buffer.tostring(child.stdout:readToEnd()))
if buffer.len(buf) == 0 then -- while true do
break -- child.stdin:write("hello world")
end -- local buf = child.stdout:read()
stdio.write(buffer.tostring(buf) .. "\n") -- if buffer.len(buf) == 0 then
-- stdio.write(buffer.tostring(child.stderr:read() .. child.stderr:read() .. child.stderr:read() .. child.stderr:read())) -- break
end -- end
-- stdio.write(buffer.tostring(buf) .. "\n")
-- -- stdio.write(buffer.tostring(child.stderr:read() .. child.stderr:read() .. child.stderr:read() .. child.stderr:read()))
-- end

View file

@ -31,7 +31,7 @@ if not runLocaleTests then
return return
end end
local dateCmd = process.spawn("bash", { "-c", "date +\"%A, %d %B %Y\" --date='@1693068988'" }, { local dateCmd = process.exec("bash", { "-c", "date +\"%A, %d %B %Y\" --date='@1693068988'" }, {
env = { env = {
LC_ALL = "fr_FR.UTF-8 ", LC_ALL = "fr_FR.UTF-8 ",
}, },

View file

@ -10,7 +10,7 @@ local echoResult = process.exec("echo", {
}, { }, {
env = { TEST_VAR = echoMessage }, env = { TEST_VAR = echoMessage },
shell = if IS_WINDOWS then "powershell" else "bash", shell = if IS_WINDOWS then "powershell" else "bash",
stdio = "inherit", stdio = "inherit" :: process.SpawnOptionsStdioKind, -- FIXME: This should just work without a cast?
}) })
-- Windows uses \r\n (CRLF) and unix uses \n (LF) -- Windows uses \r\n (CRLF) and unix uses \n (LF)

View file

@ -41,7 +41,7 @@ export type SpawnOptions = {
* `stdin` - Optional standard input to pass to executed child process * `stdin` - Optional standard input to pass to executed child process
]=] ]=]
export type ExecuteOptions = SpawnOptions & { export type ExecuteOptions = SpawnOptions & {
stdio: (SpawnOptionsStdio | SpawnOptionsStdioKind)?, stdio: (SpawnOptionsStdioKind | SpawnOptionsStdio)?,
stdin: string?, -- TODO: Remove this since it is now available in stdio above, breaking change stdin: string?, -- TODO: Remove this since it is now available in stdio above, breaking change
} }
@ -56,12 +56,13 @@ local ChildProcessReader = {}
--[=[ --[=[
@within ChildProcessReader @within ChildProcessReader
Reads a chunk of data (8 bytes at a time) from the reader into a buffer. Reads a chunk of data (specified length or a default of 8 bytes at a time) from
Returns a buffer of size 0 if there is no more data to read. the reader into a buffer. Returns a buffer of size 0 if there is no more data to
read.
@return The buffer containing the data read from the reader @return The buffer containing the data read from the reader
]=] ]=]
function ChildProcessReader:read(): buffer function ChildProcessReader:read(chunkSize: number?): buffer
return nil :: any return nil :: any
end end
@ -69,6 +70,7 @@ end
@within ChildProcessReader @within ChildProcessReader
Reads all the data currently present in the reader into a buffer. Reads all the data currently present in the reader into a buffer.
This function will yield until the process exits.
@return The buffer containing the data read from the reader @return The buffer containing the data read from the reader
]=] ]=]