Start work on proper tests suite

This commit is contained in:
Filip Tibell 2023-01-20 23:40:31 -05:00
parent 25b1dcb472
commit 94393419c2
No known key found for this signature in database
6 changed files with 83 additions and 15 deletions

View file

@ -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")

View file

@ -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")

View file

@ -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")

View file

@ -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)] #![allow(clippy::needless_pass_by_value, clippy::match_bool)]
use anyhow::Result; use anyhow::Result;
@ -16,10 +17,26 @@ async fn main() -> Result<()> {
Ok(()) Ok(())
} }
#[tokio::test] #[cfg(test)]
async fn hello_lune() { mod tests {
let args = vec!["Hello, test! ✅".to_owned()]; macro_rules! tests {
let cli = Cli::from_path_with_args("hello_lune", args); ($($name:ident: $value:expr,)*) => {
let result = cli.run().await; $(
assert!(result.is_ok()); #[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",
}
} }

View file

@ -35,6 +35,7 @@ impl UserData for Process {
for arg in &this.args { for arg in &this.args {
tab.push(arg.clone())?; tab.push(arg.clone())?;
} }
tab.set_readonly(true);
Ok(tab) Ok(tab)
}); });
fields.add_field_method_get("env", |lua, _| { 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<String>)) -> Result<()> {
// Make sure key is valid, otherwise set_var will panic // Make sure key is valid, otherwise set_var will panic
if key.is_empty() { if key.is_empty() {
return Err(Error::RuntimeError("Key must not be empty".to_string())); 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(), "Key must not contain the NUL character".to_string(),
)); ));
} }
// Make sure value is valid, otherwise set_var will panic match value {
if value.contains('\0') { Some(value) => {
return Err(Error::RuntimeError( // Make sure value is valid, otherwise set_var will panic
"Value must not contain the NUL character".to_string(), 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(()) Ok(())
} }

View file

@ -53,7 +53,7 @@ impl Lune {
print_label("ERROR").unwrap(); print_label("ERROR").unwrap();
eprintln!(); eprintln!();
pretty_print_luau_error(&e); pretty_print_luau_error(&e);
std::process::exit(1); Err(e.into())
} }
} }
} }