From 94393419c2420b02e5757076d77d280336713327 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Fri, 20 Jan 2023 23:40:31 -0500 Subject: [PATCH] Start work on proper tests suite --- .lune/tests/process/args.luau | 17 +++++++++++++++++ .lune/tests/process/env.luau | 17 +++++++++++++++++ .lune/tests/process/spawn.luau | 11 +++++++++++ src/cli/main.rs | 31 ++++++++++++++++++++++++------- src/lib/globals/process.rs | 20 +++++++++++++------- src/lib/lib.rs | 2 +- 6 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 .lune/tests/process/args.luau create mode 100644 .lune/tests/process/env.luau create mode 100644 .lune/tests/process/spawn.luau diff --git a/.lune/tests/process/args.luau b/.lune/tests/process/args.luau new file mode 100644 index 0000000..a0c1dba --- /dev/null +++ b/.lune/tests/process/args.luau @@ -0,0 +1,17 @@ +assert(#process.args > 0, "No process arguments found") + +assert(process.args[1] == "Foo", "Invalid first argument to process") +assert(process.args[2] == "Bar", "Invalid second argument to process") + +local success = pcall(function() + process.args[1] = "abc" +end) +assert(not success, "Trying to set process arguments should throw an error") + +local foundValue = false +for _, _ in process.args do + foundValue = true + break +end + +assert(foundValue, "Iterating using generalized iteration") diff --git a/.lune/tests/process/env.luau b/.lune/tests/process/env.luau new file mode 100644 index 0000000..1e1bd43 --- /dev/null +++ b/.lune/tests/process/env.luau @@ -0,0 +1,17 @@ +local randomKey = string.format("LUNE_TEST_%d", math.random(1, 999_999)) + +assert(process.env[randomKey] == nil, "Unset variable returned a non-nil value") + +process.env[randomKey] = "abc" +assert(process.env[randomKey] == "abc", "Failed to set environment variable") + +process.env[randomKey] = nil +assert(process.env[randomKey] == nil, "Failed to set environment variable") + +local foundValue = false +for _, _ in process.env do + foundValue = true + break +end + +assert(foundValue, "Iterating using generalized iteration") diff --git a/.lune/tests/process/spawn.luau b/.lune/tests/process/spawn.luau new file mode 100644 index 0000000..cd6bda8 --- /dev/null +++ b/.lune/tests/process/spawn.luau @@ -0,0 +1,11 @@ +local result = process.spawn("ls", { + "-a", +}) + +assert(result.ok, "Failed to spawn child process") + +assert(result.stderr == "", "Stderr was not empty") +assert(result.stdout ~= "", "Stdout was empty") + +assert(string.find(result.stdout, "Cargo.toml") ~= nil, "Missing Cargo.toml in output") +assert(string.find(result.stdout, ".gitignore") ~= nil, "Missing .gitignore in output") diff --git a/src/cli/main.rs b/src/cli/main.rs index 53f83c8..02f6640 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -1,4 +1,5 @@ -#![deny(clippy::all, clippy::cargo, clippy::pedantic)] +#![deny(clippy::all)] +#![warn(clippy::cargo, clippy::pedantic)] #![allow(clippy::needless_pass_by_value, clippy::match_bool)] use anyhow::Result; @@ -16,10 +17,26 @@ async fn main() -> Result<()> { Ok(()) } -#[tokio::test] -async fn hello_lune() { - let args = vec!["Hello, test! ✅".to_owned()]; - let cli = Cli::from_path_with_args("hello_lune", args); - let result = cli.run().await; - assert!(result.is_ok()); +#[cfg(test)] +mod tests { + macro_rules! tests { + ($($name:ident: $value:expr,)*) => { + $( + #[tokio::test] + async fn $name() { + let args = vec!["Foo".to_owned(), "Bar".to_owned()]; + let cli = crate::Cli::from_path_with_args($value, args); + if let Err(e) = cli.run().await { + panic!("{}", e.to_string()) + } + } + )* + } + } + + tests! { + process_args: "tests/process/args", + process_env: "tests/process/env", + process_spawn: "tests/process/spawn", + } } diff --git a/src/lib/globals/process.rs b/src/lib/globals/process.rs index ec23d06..984eeae 100644 --- a/src/lib/globals/process.rs +++ b/src/lib/globals/process.rs @@ -35,6 +35,7 @@ impl UserData for Process { for arg in &this.args { tab.push(arg.clone())?; } + tab.set_readonly(true); Ok(tab) }); fields.add_field_method_get("env", |lua, _| { @@ -74,7 +75,7 @@ fn process_env_get<'lua>(lua: &'lua Lua, (_, key): (Value<'lua>, String)) -> Res } } -fn process_env_set(_: &Lua, (_, key, value): (Value, String, String)) -> Result<()> { +fn process_env_set(_: &Lua, (_, key, value): (Value, String, Option)) -> Result<()> { // Make sure key is valid, otherwise set_var will panic if key.is_empty() { return Err(Error::RuntimeError("Key must not be empty".to_string())); @@ -87,13 +88,18 @@ fn process_env_set(_: &Lua, (_, key, value): (Value, String, String)) -> Result< "Key must not contain the NUL character".to_string(), )); } - // Make sure value is valid, otherwise set_var will panic - if value.contains('\0') { - return Err(Error::RuntimeError( - "Value must not contain the NUL character".to_string(), - )); + match value { + Some(value) => { + // Make sure value is valid, otherwise set_var will panic + if value.contains('\0') { + return Err(Error::RuntimeError( + "Value must not contain the NUL character".to_string(), + )); + } + env::set_var(&key, &value); + } + None => env::remove_var(&key), } - env::set_var(&key, &value); Ok(()) } diff --git a/src/lib/lib.rs b/src/lib/lib.rs index 81f547b..3153ca9 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -53,7 +53,7 @@ impl Lune { print_label("ERROR").unwrap(); eprintln!(); pretty_print_luau_error(&e); - std::process::exit(1); + Err(e.into()) } } }