#![allow(dead_code)]
pub mod e2e_helpers;
use pasta_lua::{PastaLuaRuntime, TranspileContext};
use std::path::PathBuf;
pub fn normalize_line_endings(s: &str) -> String {
s.replace("\r\n", "\n").replace("\r", "\n")
}
pub fn create_empty_context() -> TranspileContext {
TranspileContext::new()
}
pub fn get_scripts_dir() -> String {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("pasta_scripts")
.to_string_lossy()
.replace('\\', "/")
}
pub fn create_runtime_with_pasta_path() -> PastaLuaRuntime {
let ctx = create_empty_context();
let runtime = PastaLuaRuntime::new(ctx).unwrap();
let scripts_dir = get_scripts_dir();
runtime
.exec(&format!(
r#"package.path = "{scripts_dir}/?.lua;{scripts_dir}/?/init.lua;" .. package.path"#
))
.expect("Failed to configure package.path");
runtime
.exec(
r#"
package.loaded["@pasta_persistence"] = {
load = function() return {} end,
save = function(data) return true end
}
"#,
)
.expect("Failed to mock @pasta_persistence");
runtime
.exec(
r#"
package.loaded["@pasta_search"] = setmetatable({}, {
__index = function() return function() return nil end end
})
"#,
)
.expect("Failed to mock @pasta_search");
runtime
.exec(
r#"
package.loaded["@pasta_sakura_script"] = {
talk_to_script = function(actor, text) return text or "" end
}
"#,
)
.expect("Failed to mock @pasta_sakura_script");
runtime
}
pub fn loader_fixtures_path(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/loader")
.join(name)
}
pub fn copy_dir_recursive(src: &std::path::Path, dst: &std::path::Path) -> std::io::Result<()> {
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let path = entry.path();
let dest_path = dst.join(entry.file_name());
if path.is_dir() {
if entry.file_name() == "profile" {
continue;
}
std::fs::create_dir_all(&dest_path)?;
copy_dir_recursive(&path, &dest_path)?;
} else {
std::fs::copy(&path, &dest_path)?;
}
}
Ok(())
}
pub fn copy_fixture_to_temp(name: &str) -> tempfile::TempDir {
let src = loader_fixtures_path(name);
let temp = tempfile::TempDir::new().unwrap();
copy_dir_recursive(&src, temp.path()).unwrap();
let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let scripts_src = crate_root.join("pasta_scripts");
let scripts_dst = temp.path().join("pasta_scripts");
if scripts_src.exists() {
std::fs::create_dir_all(&scripts_dst).unwrap();
copy_dir_recursive(&scripts_src, &scripts_dst).unwrap();
}
let scriptlibs_src = crate_root.join("scriptlibs");
let scriptlibs_dst = temp.path().join("scriptlibs");
if scriptlibs_src.exists() {
std::fs::create_dir_all(&scriptlibs_dst).unwrap();
copy_dir_recursive(&scriptlibs_src, &scriptlibs_dst).unwrap();
}
temp
}
pub fn create_temp_with_pasta(pasta_content: &str) -> tempfile::TempDir {
let temp = tempfile::TempDir::new().unwrap();
let base_dir = temp.path();
std::fs::create_dir_all(base_dir.join("dic/test")).unwrap();
std::fs::write(base_dir.join("dic/test/hello.pasta"), pasta_content).unwrap();
std::fs::write(base_dir.join("pasta.toml"), "[loader]\ndebug_mode = true\n").unwrap();
let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let scripts_src = crate_root.join("pasta_scripts");
let scripts_dst = base_dir.join("pasta_scripts");
if scripts_src.exists() {
std::fs::create_dir_all(&scripts_dst).unwrap();
copy_dir_recursive(&scripts_src, &scripts_dst).unwrap();
}
let scriptlibs_src = crate_root.join("scriptlibs");
let scriptlibs_dst = base_dir.join("scriptlibs");
if scriptlibs_src.exists() {
std::fs::create_dir_all(&scriptlibs_dst).unwrap();
copy_dir_recursive(&scriptlibs_src, &scriptlibs_dst).unwrap();
}
temp
}
pub fn value_as_str(value: &mlua::Value) -> Option<String> {
value
.as_string()
.and_then(|s| s.to_str().ok())
.map(|s| s.to_string())
}
use pasta_lua::loader::TalkConfig;
use pasta_lua::sakura_script;
pub fn create_sakura_test_runtime() -> mlua::Lua {
let lua = mlua::Lua::new();
let config = TalkConfig::default();
let module = sakura_script::register(&lua, Some(&config)).unwrap();
let package: mlua::Table = lua.globals().get("package").unwrap();
let loaded: mlua::Table = package.get("loaded").unwrap();
loaded.set("@pasta_sakura_script", module).unwrap();
lua
}
pub fn create_sakura_test_runtime_with_config(config: &TalkConfig) -> mlua::Lua {
let lua = mlua::Lua::new();
let module = sakura_script::register(&lua, Some(config)).unwrap();
let package: mlua::Table = lua.globals().get("package").unwrap();
let loaded: mlua::Table = package.get("loaded").unwrap();
loaded.set("@pasta_sakura_script", module).unwrap();
lua
}