From 4cb5ba3b983582e520aa999a0f1118611cf56df1 Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Sat, 9 Oct 2021 19:24:59 -0500 Subject: [PATCH] Fix tests --- distant-core/src/client/process.rs | 4 +- .../tests/lua/async/spawn_wait.rs | 4 +- distant-lua-tests/tests/lua/sync/mod.rs | 1 + .../tests/lua/sync/spawn_wait.rs | 131 ++++++++++++++++++ distant-lua/src/session.rs | 2 +- distant-lua/src/session/api.rs | 2 +- distant-lua/src/session/proc.rs | 6 +- tests/cli/action/proc_run.rs | 2 +- 8 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 distant-lua-tests/tests/lua/sync/spawn_wait.rs diff --git a/distant-core/src/client/process.rs b/distant-core/src/client/process.rs index 2ebcf17..f089d92 100644 --- a/distant-core/src/client/process.rs +++ b/distant-core/src/client/process.rs @@ -416,7 +416,7 @@ mod tests { "test-tenant", req.id, vec![ResponseData::Error(Error { - kind: ErrorKind::Other, + kind: ErrorKind::BrokenPipe, description: String::from("some error"), })], )) @@ -429,7 +429,7 @@ mod tests { matches!( &result, Err(RemoteProcessError::TransportError(TransportError::IoError(x))) - if x.kind() == io::ErrorKind::InvalidData + if x.kind() == io::ErrorKind::BrokenPipe ), "Unexpected result: {:?}", result diff --git a/distant-lua-tests/tests/lua/async/spawn_wait.rs b/distant-lua-tests/tests/lua/async/spawn_wait.rs index fc07ff1..617fdc2 100644 --- a/distant-lua-tests/tests/lua/async/spawn_wait.rs +++ b/distant-lua-tests/tests/lua/async/spawn_wait.rs @@ -100,7 +100,7 @@ fn should_return_back_status_on_success(ctx: &'_ DistantServerCtx) { // with / but thinks it's on windows and is providing \ #[rstest] #[cfg_attr(windows, ignore)] -fn should_return_process_that_can_retrieve_stdout(ctx: &'_ DistantServerCtx) { +fn should_capture_all_stdout(ctx: &'_ DistantServerCtx) { let lua = lua::make().unwrap(); let new_session = session::make_function(&lua, ctx).unwrap(); let schedule_fn = poll::make_function(&lua).unwrap(); @@ -138,7 +138,7 @@ fn should_return_process_that_can_retrieve_stdout(ctx: &'_ DistantServerCtx) { // with / but thinks it's on windows and is providing \ #[rstest] #[cfg_attr(windows, ignore)] -fn should_return_process_that_can_retrieve_stderr(ctx: &'_ DistantServerCtx) { +fn should_capture_all_stderr(ctx: &'_ DistantServerCtx) { let lua = lua::make().unwrap(); let new_session = session::make_function(&lua, ctx).unwrap(); let schedule_fn = poll::make_function(&lua).unwrap(); diff --git a/distant-lua-tests/tests/lua/sync/mod.rs b/distant-lua-tests/tests/lua/sync/mod.rs index 074847b..94f07df 100644 --- a/distant-lua-tests/tests/lua/sync/mod.rs +++ b/distant-lua-tests/tests/lua/sync/mod.rs @@ -10,6 +10,7 @@ mod read_file_text; mod remove; mod rename; mod spawn; +mod spawn_wait; mod system_info; mod write_file; mod write_file_text; diff --git a/distant-lua-tests/tests/lua/sync/spawn_wait.rs b/distant-lua-tests/tests/lua/sync/spawn_wait.rs new file mode 100644 index 0000000..79f898d --- /dev/null +++ b/distant-lua-tests/tests/lua/sync/spawn_wait.rs @@ -0,0 +1,131 @@ +use crate::common::{fixtures::*, lua, session}; +use assert_fs::prelude::*; +use mlua::chunk; +use once_cell::sync::Lazy; +use rstest::*; + +static TEMP_SCRIPT_DIR: Lazy = Lazy::new(|| assert_fs::TempDir::new().unwrap()); +static SCRIPT_RUNNER: Lazy = Lazy::new(|| String::from("bash")); + +static ECHO_ARGS_TO_STDOUT_SH: Lazy = Lazy::new(|| { + let script = TEMP_SCRIPT_DIR.child("echo_args_to_stdout.sh"); + script + .write_str(indoc::indoc!( + r#" + #/usr/bin/env bash + printf "%s" "$*" + "# + )) + .unwrap(); + script +}); + +static ECHO_ARGS_TO_STDERR_SH: Lazy = Lazy::new(|| { + let script = TEMP_SCRIPT_DIR.child("echo_args_to_stderr.sh"); + script + .write_str(indoc::indoc!( + r#" + #/usr/bin/env bash + printf "%s" "$*" 1>&2 + "# + )) + .unwrap(); + script +}); + +static DOES_NOT_EXIST_BIN: Lazy = + Lazy::new(|| TEMP_SCRIPT_DIR.child("does_not_exist_bin")); + +#[rstest] +fn should_return_error_on_failure(ctx: &'_ DistantServerCtx) { + let lua = lua::make().unwrap(); + let new_session = session::make_function(&lua, ctx).unwrap(); + + let cmd = DOES_NOT_EXIST_BIN.to_str().unwrap().to_string(); + let args: Vec = Vec::new(); + + let result = lua + .load(chunk! { + local session = $new_session() + local status, _ = pcall(session.spawn_wait, session, { + cmd = $cmd, + args = $args + }) + assert(not status, "Unexpectedly succeeded!") + }) + .exec(); + assert!(result.is_ok(), "Failed: {}", result.unwrap_err()); +} + +#[rstest] +fn should_return_back_process_on_success(ctx: &'_ DistantServerCtx) { + let lua = lua::make().unwrap(); + let new_session = session::make_function(&lua, ctx).unwrap(); + + let cmd = SCRIPT_RUNNER.to_string(); + let args = vec![ECHO_ARGS_TO_STDOUT_SH.to_str().unwrap().to_string()]; + + let result = lua + .load(chunk! { + local session = $new_session() + local output = session:spawn_wait({ cmd = $cmd, args = $args }) + assert(output, "Missing process output") + assert(output.success, "Process unexpectedly failed") + }) + .exec(); + assert!(result.is_ok(), "Failed: {}", result.unwrap_err()); +} + +// NOTE: Ignoring on windows because it's using WSL which wants a Linux path +// with / but thinks it's on windows and is providing \ +#[rstest] +#[cfg_attr(windows, ignore)] +fn should_capture_all_stdout(ctx: &'_ DistantServerCtx) { + let lua = lua::make().unwrap(); + let new_session = session::make_function(&lua, ctx).unwrap(); + + let cmd = SCRIPT_RUNNER.to_string(); + let args = vec![ + ECHO_ARGS_TO_STDOUT_SH.to_str().unwrap().to_string(), + String::from("some stdout"), + ]; + + let result = lua + .load(chunk! { + local session = $new_session() + local output = session:spawn_wait({ cmd = $cmd, args = $args }) + assert(output, "Missing process output") + assert(output.success, "Process unexpectedly failed") + assert(output.stdout == "some stdout", "Unexpected stdout: " .. output.stdout) + assert(output.stderr == "", "Unexpected stderr: " .. output.stderr) + }) + .exec(); + assert!(result.is_ok(), "Failed: {}", result.unwrap_err()); +} + +// NOTE: Ignoring on windows because it's using WSL which wants a Linux path +// with / but thinks it's on windows and is providing \ +#[rstest] +#[cfg_attr(windows, ignore)] +fn should_capture_all_stderr(ctx: &'_ DistantServerCtx) { + let lua = lua::make().unwrap(); + let new_session = session::make_function(&lua, ctx).unwrap(); + + let cmd = SCRIPT_RUNNER.to_string(); + let args = vec![ + ECHO_ARGS_TO_STDERR_SH.to_str().unwrap().to_string(), + String::from("some stderr"), + ]; + + let result = lua + .load(chunk! { + local session = $new_session() + local output = session:spawn_wait({ cmd = $cmd, args = $args }) + assert(output, "Missing process output") + assert(output.success, "Process unexpectedly failed") + assert(output.stdout == "", "Unexpected stdout: " .. output.stdout) + assert(output.stderr == "some stderr", "Unexpected stderr: " .. output.stderr) + }) + .exec(); + assert!(result.is_ok(), "Failed: {}", result.unwrap_err()); +} diff --git a/distant-lua/src/session.rs b/distant-lua/src/session.rs index 318c416..d478ea4 100644 --- a/distant-lua/src/session.rs +++ b/distant-lua/src/session.rs @@ -323,7 +323,7 @@ impl UserData for Session { Ok(RemoteLspProcess::from_distant(proc)) }); impl_methods!(methods, system_info, |lua, info| { - Ok(lua.to_value(&info)?) + lua.to_value(&info) }); impl_methods!(methods, write_file); impl_methods!(methods, write_file_text); diff --git a/distant-lua/src/session/api.rs b/distant-lua/src/session/api.rs index 7970b34..1edf374 100644 --- a/distant-lua/src/session/api.rs +++ b/distant-lua/src/session/api.rs @@ -174,7 +174,7 @@ make_api!( { cmd: String, args: Vec }, |channel, tenant, params| { let proc = channel.spawn(tenant, params.cmd, params.args).await.to_lua_err()?; - let id = LuaRemoteProcess::from_distant(proc)?.id; + let id = LuaRemoteProcess::from_distant_async(proc).await?.id; LuaRemoteProcess::output_async(id).await } ); diff --git a/distant-lua/src/session/proc.rs b/distant-lua/src/session/proc.rs index 283c40a..0d6a888 100644 --- a/distant-lua/src/session/proc.rs +++ b/distant-lua/src/session/proc.rs @@ -58,8 +58,12 @@ macro_rules! impl_process { } pub fn from_distant(proc: $type) -> LuaResult { + runtime::get_runtime()?.block_on(Self::from_distant_async(proc)) + } + + pub async fn from_distant_async(proc: $type) -> LuaResult { let id = proc.id(); - runtime::get_runtime()?.block_on($map_name.write()).insert(id, proc); + $map_name.write().await.insert(id, proc); Ok(Self::new(id)) } diff --git a/tests/cli/action/proc_run.rs b/tests/cli/action/proc_run.rs index 40776cb..c55b445 100644 --- a/tests/cli/action/proc_run.rs +++ b/tests/cli/action/proc_run.rs @@ -158,7 +158,7 @@ fn yield_an_error_when_fails(mut action_cmd: Command) { .args(&["proc-run", "--"]) .arg(DOES_NOT_EXIST_BIN.to_str().unwrap()) .assert() - .code(ExitCode::DataErr.to_i32()) + .code(ExitCode::IoError.to_i32()) .stdout("") .stderr(""); }