2023-01-21 21:40:57 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
use anyhow::{bail, Result};
|
2023-01-21 03:01:02 +00:00
|
|
|
use mlua::Lua;
|
|
|
|
|
|
|
|
pub mod globals;
|
|
|
|
pub mod utils;
|
|
|
|
|
|
|
|
use crate::{
|
2023-01-21 20:30:22 +00:00
|
|
|
globals::{new_console, new_fs, new_net, new_process, new_task},
|
2023-01-21 21:40:57 +00:00
|
|
|
utils::formatting::{format_label, pretty_format_luau_error},
|
2023-01-21 03:01:02 +00:00
|
|
|
};
|
|
|
|
|
2023-01-21 21:40:57 +00:00
|
|
|
pub async fn run_lune(name: &str, chunk: &str, args: Vec<String>) -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let threads = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
lua.sandbox(true)?;
|
|
|
|
// Add in all globals
|
|
|
|
{
|
|
|
|
let globals = lua.globals();
|
|
|
|
globals.raw_set("console", new_console(&lua)?)?;
|
|
|
|
globals.raw_set("fs", new_fs(&lua)?)?;
|
|
|
|
globals.raw_set("net", new_net(&lua)?)?;
|
|
|
|
globals.raw_set("process", new_process(&lua, args.clone())?)?;
|
|
|
|
globals.raw_set("task", new_task(&lua, &threads)?)?;
|
|
|
|
globals.set_readonly(true);
|
2023-01-21 03:01:02 +00:00
|
|
|
}
|
2023-01-21 21:40:57 +00:00
|
|
|
// Run the requested chunk asynchronously
|
|
|
|
let result = lua.load(chunk).set_name(name)?.exec_async().await;
|
|
|
|
match result {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(e) => bail!(
|
|
|
|
"\n{}\n{}",
|
|
|
|
format_label("ERROR"),
|
|
|
|
pretty_format_luau_error(&e)
|
|
|
|
),
|
2023-01-21 03:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-21 05:03:16 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-01-21 21:40:57 +00:00
|
|
|
use crate::run_lune;
|
|
|
|
|
2023-01-21 05:03:16 +00:00
|
|
|
macro_rules! run_tests {
|
|
|
|
($($name:ident: $value:expr,)*) => {
|
|
|
|
$(
|
|
|
|
#[tokio::test]
|
|
|
|
async fn $name() {
|
|
|
|
let args = vec![
|
|
|
|
"Foo".to_owned(),
|
|
|
|
"Bar".to_owned()
|
|
|
|
];
|
|
|
|
let path = std::env::current_dir()
|
|
|
|
.unwrap()
|
|
|
|
.join(format!("src/tests/{}.luau", $value));
|
|
|
|
let script = tokio::fs::read_to_string(&path)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-01-21 21:40:57 +00:00
|
|
|
if let Err(e) = run_lune($value, &script, args).await {
|
2023-01-21 22:02:49 +00:00
|
|
|
panic!("\nTest '{}' failed!\n{}\n", $value, e.to_string())
|
2023-01-21 05:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run_tests! {
|
2023-01-21 06:37:31 +00:00
|
|
|
console_format: "console/format",
|
|
|
|
console_set_color: "console/set_color",
|
|
|
|
console_set_style: "console/set_style",
|
2023-01-21 07:01:46 +00:00
|
|
|
fs_files: "fs/files",
|
|
|
|
fs_dirs: "fs/dirs",
|
2023-01-21 05:03:16 +00:00
|
|
|
process_args: "process/args",
|
|
|
|
process_env: "process/env",
|
2023-01-21 07:07:17 +00:00
|
|
|
// NOTE: This test does not currently work, it will exit the entire
|
|
|
|
// process, meaning it will also exit our test runner and skip testing
|
|
|
|
// process_exit: "process/exit",
|
2023-01-21 05:03:16 +00:00
|
|
|
process_spawn: "process/spawn",
|
2023-01-21 06:10:19 +00:00
|
|
|
net_request_codes: "net/request/codes",
|
|
|
|
net_request_methods: "net/request/methods",
|
|
|
|
net_request_redirect: "net/request/redirect",
|
|
|
|
net_json_decode: "net/json/decode",
|
|
|
|
net_json_encode: "net/json/encode",
|
2023-01-21 20:07:18 +00:00
|
|
|
task_defer: "task/defer",
|
|
|
|
task_delay: "task/delay",
|
|
|
|
task_spawn: "task/spawn",
|
2023-01-21 18:33:33 +00:00
|
|
|
task_wait: "task/wait",
|
2023-01-21 05:03:16 +00:00
|
|
|
}
|
|
|
|
}
|