Use mlua for Lua tests

This commit is contained in:
Ottatop 2024-04-20 19:17:43 -05:00
parent 978eb14ec9
commit 78eb41ef51
3 changed files with 114 additions and 70 deletions

127
Cargo.lock generated
View file

@ -187,7 +187,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -198,7 +198,7 @@ checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -344,7 +344,7 @@ checksum = "4da9a32f3fed317401fa3c862968128267c3106685286e15d5aaa3d7389c2f60"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -482,7 +482,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -844,7 +844,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -909,7 +909,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -1496,6 +1496,46 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "mlua"
version = "0.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d9bed6bce296397a9d6a86f995dd10a547a4e6949825d45225906bdcbfe7367"
dependencies = [
"bstr",
"mlua-sys",
"mlua_derive",
"num-traits",
"once_cell",
"rustc-hash",
]
[[package]]
name = "mlua-sys"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d16a9ba1dd2c6ac971b204262d434c24d65067038598f0638b64e5dca28d52b8"
dependencies = [
"cc",
"cfg-if",
"pkg-config",
]
[[package]]
name = "mlua_derive"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aaade5f94e5829db58791664ba98f35fea6a3ffebc783becb51dc97c7a21abee"
dependencies = [
"itertools",
"once_cell",
"proc-macro-error",
"proc-macro2",
"quote",
"regex",
"syn 2.0.58",
]
[[package]]
name = "multimap"
version = "0.10.0"
@ -1606,7 +1646,7 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -1738,7 +1778,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -1765,6 +1805,7 @@ dependencies = [
"cliclack",
"dircpy",
"image",
"mlua",
"nix",
"pinnacle",
"pinnacle-api",
@ -1826,7 +1867,7 @@ version = "0.0.2"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -1875,7 +1916,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7"
dependencies = [
"proc-macro2",
"syn",
"syn 2.0.58",
]
[[package]]
@ -1887,6 +1928,30 @@ dependencies = [
"toml_edit 0.21.1",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
@ -1912,7 +1977,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8021cf59c8ec9c432cfc2526ac6b8aa508ecaf29cd415f271b8406c1b851c3fd"
dependencies = [
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -1942,7 +2007,7 @@ dependencies = [
"prost",
"prost-types",
"regex",
"syn",
"syn 2.0.58",
"tempfile",
]
@ -1956,7 +2021,7 @@ dependencies = [
"itertools",
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -2121,6 +2186,12 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustix"
version = "0.38.32"
@ -2184,7 +2255,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -2355,6 +2426,16 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.58"
@ -2426,7 +2507,7 @@ checksum = "c8f546451eaa38373f549093fe9fd05e7d2bade739e2ddf834b9968621d60107"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -2457,7 +2538,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -2537,7 +2618,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -2647,7 +2728,7 @@ dependencies = [
"proc-macro2",
"prost-build",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -2727,7 +2808,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -2881,7 +2962,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
"wasm-bindgen-shared",
]
@ -2915,7 +2996,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -3506,7 +3587,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]
[[package]]
@ -3526,5 +3607,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.58",
]

View file

@ -111,6 +111,7 @@ tempfile = "3.10.1"
test-log = { version = "0.2.15", default-features = false, features = ["trace"] }
pinnacle = { path = ".", features = ["testing"] }
pinnacle-api = { path = "./api/rust" }
mlua = { version = "0.9.7", features = ["lua54", "send", "macros"] }
[features]
testing = [

View file

@ -1,12 +1,8 @@
mod common;
use std::{
io::Write,
process::{Command, Stdio},
};
use crate::common::{output_for_name, sleep_secs, test_api, with_state};
use mlua::Lua;
use pinnacle::state::WithState;
use test_log::test;
@ -21,39 +17,16 @@ fn run_lua(ident: &str, code: &str) {
local success, err = pcall(run, {ident})
if not success then
print(err)
os.exit(1)
error(err)
end
end)
"#);
let mut child = Command::new("lua").stdin(Stdio::piped()).spawn().unwrap();
let mut stdin = child.stdin.take().unwrap();
stdin.write_all(code.as_bytes()).unwrap();
drop(stdin);
let exit_status = child.wait().unwrap();
if exit_status.code().is_some_and(|code| code != 0) {
panic!("lua code panicked");
}
let lua = unsafe { Lua::unsafe_new() };
lua.load(code).exec().unwrap();
}
struct SetupLuaGuard {
child: std::process::Child,
}
impl Drop for SetupLuaGuard {
fn drop(&mut self) {
let _ = self.child.kill();
}
}
#[must_use]
fn setup_lua(ident: &str, code: &str) -> SetupLuaGuard {
fn setup_lua(ident: &str, code: &str) {
#[rustfmt::skip]
let code = format!(r#"
require("pinnacle").setup(function({ident})
@ -70,21 +43,10 @@ fn setup_lua(ident: &str, code: &str) -> SetupLuaGuard {
end)
"#);
let mut child = Command::new("lua").stdin(Stdio::piped()).spawn().unwrap();
let mut stdin = child.stdin.take().unwrap();
stdin.write_all(code.as_bytes()).unwrap();
drop(stdin);
SetupLuaGuard { child }
// let exit_status = child.wait().unwrap();
//
// if exit_status.code().is_some_and(|code| code != 0) {
// panic!("lua code panicked");
// }
std::thread::spawn(|| {
let lua = unsafe { Lua::unsafe_new() };
lua.load(code).exec().unwrap();
});
}
macro_rules! run_lua {