use std::path::Path;
use pasta_lua::loader::{CacheManager, PastaLoader};
use pasta_lua::{RuntimeConfig, SourceMode};
fn make_base_dir(temp: &Path, pasta_toml: &str) {
std::fs::create_dir_all(temp.join("dic/test")).unwrap();
std::fs::write(
temp.join("dic/test/hello.pasta"),
"\
*あいさつ
さくら:「おはよう!」
さくら:「げんき?」
",
)
.unwrap();
std::fs::write(temp.join("pasta.toml"), pasta_toml).unwrap();
let crate_root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
for sub in ["pasta_scripts", "scriptlibs"] {
let src = crate_root.join(sub);
let dst = temp.join(sub);
if src.exists() {
std::fs::create_dir_all(&dst).unwrap();
copy_dir(&src, &dst).unwrap();
}
}
}
fn copy_dir(src: &Path, dst: &Path) -> std::io::Result<()> {
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let path = entry.path();
let dest = dst.join(entry.file_name());
if path.is_dir() {
if entry.file_name() == "profile" {
continue;
}
std::fs::create_dir_all(&dest)?;
copy_dir(&path, &dest)?;
} else {
std::fs::copy(&path, &dest)?;
}
}
Ok(())
}
#[test]
fn enabled_debug_runtime_holds_aggregated_source_map_in_order() {
let temp = tempfile::TempDir::new().unwrap();
make_base_dir(
temp.path(),
"\
[loader]
debug_mode = true
[debug]
enabled = true
port = 0
",
);
let runtime = PastaLoader::load_with_config(temp.path(), RuntimeConfig::new())
.expect("loader must build an enabled-debug runtime");
assert!(
runtime.debug_enabled(),
"3.1/4.2: enabled [debug] must install the backend (handle held)"
);
let map = runtime
.debug_source_map()
.expect("3.1: enabled debug runtime must hold the aggregated Arc<SourceMap>");
let pasta_file = temp.path().join("dic/test/hello.pasta");
let cache_manager = CacheManager::new(temp.path().to_path_buf(), "profile/pasta/cache/lua");
let chunk = cache_manager
.source_to_cache_path(&pasta_file)
.to_string_lossy()
.to_string();
let resolved_any = (1u32..=200)
.filter_map(|lua_line| map.resolve_lua_to_pasta(&chunk, lua_line))
.next();
assert!(
resolved_any.is_some(),
"3.1: held map must contain the transpiled chunk's mappings (chunk={chunk}, file={})",
pasta_file.display()
);
}
#[test]
fn disabled_debug_runtime_holds_no_source_map() {
let temp = tempfile::TempDir::new().unwrap();
make_base_dir(
temp.path(),
"\
[loader]
debug_mode = true
",
);
let runtime = PastaLoader::load_with_config(temp.path(), RuntimeConfig::new())
.expect("loader must build a disabled-debug runtime");
assert!(
!runtime.debug_enabled(),
"7.1: no [debug] => debug disabled (zero-cost)"
);
assert!(
runtime.debug_source_map().is_none(),
"3.1/7.1: disabled debug must NOT build/hold a source map"
);
}
#[test]
fn file_present_as_lua_is_applied_to_resolved_debug_config() {
let temp = tempfile::TempDir::new().unwrap();
make_base_dir(
temp.path(),
"\
[loader]
debug_mode = true
[debug]
enabled = true
port = 0
present_as = \"lua\"
",
);
let runtime = PastaLoader::load_with_config(temp.path(), RuntimeConfig::new())
.expect("loader must build runtime with present_as=lua");
assert!(runtime.debug_enabled(), "enabled debug runtime");
assert_eq!(
runtime.debug_source_mode(),
Some(SourceMode::Lua),
"6.3: pasta.toml [debug] present_as=\"lua\" must be applied to the session"
);
}