mirror of
https://github.com/lune-org/lune.git
synced 2025-04-04 10:30:54 +01:00
chore(types + tests): update types and tests for exec
This commit is contained in:
parent
50b1bcbd64
commit
6a2f5061d5
7 changed files with 32 additions and 26 deletions
|
@ -129,7 +129,7 @@ end
|
|||
]]
|
||||
|
||||
print("Sending 4 pings to google 🌏")
|
||||
local result = process.spawn("ping", {
|
||||
local result = process.exec("ping", {
|
||||
"google.com",
|
||||
"-c 4",
|
||||
})
|
||||
|
|
|
@ -11,18 +11,16 @@ pub struct ChildProcessReader<R: AsyncRead>(pub R);
|
|||
pub struct ChildProcessWriter<W: AsyncWrite>(pub W);
|
||||
|
||||
impl<R: AsyncRead + Unpin> ChildProcessReader<R> {
|
||||
pub async fn read(&mut self) -> LuaResult<Vec<u8>> {
|
||||
let mut buf = BytesMut::with_capacity(CHUNK_SIZE);
|
||||
pub async fn read(&mut self, chunk_size: Option<usize>) -> LuaResult<Vec<u8>> {
|
||||
let mut buf = BytesMut::with_capacity(chunk_size.unwrap_or(CHUNK_SIZE));
|
||||
self.0.read_buf(&mut buf).await?;
|
||||
|
||||
Ok(buf.to_vec())
|
||||
}
|
||||
|
||||
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![];
|
||||
|
||||
self.0.read_to_end(&mut buf).await?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
@ -30,8 +28,8 @@ impl<R: AsyncRead + Unpin> ChildProcessReader<R> {
|
|||
|
||||
impl<R: AsyncRead + Unpin + 'static> LuaUserData for ChildProcessReader<R> {
|
||||
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_async_method_mut("read", |lua, this, ()| async {
|
||||
Ok(lua.create_buffer(this.read().await?))
|
||||
methods.add_async_method_mut("read", |lua, this, chunk_size: Option<usize>| async move {
|
||||
Ok(lua.create_buffer(this.read(chunk_size).await?))
|
||||
});
|
||||
|
||||
methods.add_async_method_mut("readToEnd", |lua, this, ()| async {
|
||||
|
|
|
@ -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 }?)
|
||||
print("Checking if", program, "is installed")
|
||||
local result = process.spawn(program, args)
|
||||
local result = process.exec(program, args)
|
||||
if not result.ok then
|
||||
stdio.ewrite(string.format("Program '%s' is not installed\n", program))
|
||||
process.exit(1)
|
||||
|
@ -123,7 +123,7 @@ checkInstalled(BIN_ZLIB, { "--version" })
|
|||
-- Run them to generate files
|
||||
|
||||
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
|
||||
stdio.ewrite(string.format("Command '%s' failed\n", program))
|
||||
if #result.stdout > 0 then
|
||||
|
|
26
test.lua
26
test.lua
|
@ -1,16 +1,22 @@
|
|||
local process = require("@lune/process")
|
||||
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
|
||||
child.stdin:write("hello world")
|
||||
local buf = child.stdout:read()
|
||||
stdio.write(buffer.tostring(child.stdout:readToEnd()))
|
||||
stdio.write(buffer.tostring(child.stdout:readToEnd()))
|
||||
stdio.write(buffer.tostring(child.stdout:readToEnd()))
|
||||
|
||||
if buffer.len(buf) == 0 then
|
||||
break
|
||||
end
|
||||
-- while true do
|
||||
-- child.stdin:write("hello world")
|
||||
-- local buf = child.stdout:read()
|
||||
|
||||
stdio.write(buffer.tostring(buf) .. "\n")
|
||||
-- stdio.write(buffer.tostring(child.stderr:read() .. child.stderr:read() .. child.stderr:read() .. child.stderr:read()))
|
||||
end
|
||||
-- if buffer.len(buf) == 0 then
|
||||
-- break
|
||||
-- end
|
||||
|
||||
-- stdio.write(buffer.tostring(buf) .. "\n")
|
||||
-- -- stdio.write(buffer.tostring(child.stderr:read() .. child.stderr:read() .. child.stderr:read() .. child.stderr:read()))
|
||||
-- end
|
|
@ -31,7 +31,7 @@ if not runLocaleTests then
|
|||
return
|
||||
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 = {
|
||||
LC_ALL = "fr_FR.UTF-8 ",
|
||||
},
|
||||
|
|
|
@ -10,7 +10,7 @@ local echoResult = process.exec("echo", {
|
|||
}, {
|
||||
env = { TEST_VAR = echoMessage },
|
||||
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)
|
||||
|
|
|
@ -41,7 +41,7 @@ export type SpawnOptions = {
|
|||
* `stdin` - Optional standard input to pass to executed child process
|
||||
]=]
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -56,12 +56,13 @@ local ChildProcessReader = {}
|
|||
--[=[
|
||||
@within ChildProcessReader
|
||||
|
||||
Reads a chunk of data (8 bytes at a time) from the reader into a buffer.
|
||||
Returns a buffer of size 0 if there is no more data to read.
|
||||
Reads a chunk of data (specified length or a default of 8 bytes at a time) from
|
||||
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
|
||||
]=]
|
||||
function ChildProcessReader:read(): buffer
|
||||
function ChildProcessReader:read(chunkSize: number?): buffer
|
||||
return nil :: any
|
||||
end
|
||||
|
||||
|
@ -69,6 +70,7 @@ end
|
|||
@within ChildProcessReader
|
||||
|
||||
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
|
||||
]=]
|
||||
|
|
Loading…
Add table
Reference in a new issue