use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
const TARGETS: &[(&str, &str)] = &[
("js", "js"),
("ts", "ts"),
("python", "py"),
("rust", "rs"),
("go", "go"),
];
fn bock_bin() -> Command {
Command::new(env!("CARGO_BIN_EXE_bock"))
}
fn make_project(tag: &str) -> PathBuf {
let root =
PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("stdlib_error_targets_{tag}"));
let _ = fs::remove_dir_all(&root);
fs::create_dir_all(root.join("src")).unwrap();
fs::write(
root.join("bock.project"),
"name = \"tgtdemo\"\nversion = \"0.1.0\"\n",
)
.unwrap();
fs::write(
root.join("src/main.bock"),
"module main\n\
\n\
use core.error.{error}\n\
\n\
public fn main() {\n\
\x20\x20println(error(\"boom\").message())\n\
}\n",
)
.unwrap();
root
}
fn read_entry(build_dir: &Path, target: &str, ext: &str) -> Option<String> {
let target_dir = build_dir.join(target);
let entry = if target == "rust" {
target_dir.join("src").join("main.rs")
} else {
target_dir.join(format!("main.{ext}"))
};
fs::read_to_string(entry).ok()
}
fn module_file_path(build_dir: &Path, target: &str, module: &str, ext: &str) -> PathBuf {
let target_dir = build_dir.join(target);
match target {
"rust" => {
let mut p = target_dir.join("src");
for seg in module.split('.') {
p = p.join(seg);
}
p.with_extension(ext)
}
"go" => target_dir.join(format!("{module}.{ext}")),
_ => {
let mut p = target_dir;
let segs: Vec<&str> = module.split('.').collect();
for seg in &segs[..segs.len() - 1] {
p = p.join(seg);
}
p.join(format!("{}.{ext}", segs[segs.len() - 1]))
}
}
}
fn assert_entry_imports_module(entry: &str, target: &str, module: &str) {
match target {
"python" => assert!(
entry.contains(&format!("from {module} import")),
"target {target}: entry must import from the {module} module file",
),
"js" | "ts" => {
let spec_ext = if target == "ts" { "ts" } else { "js" };
let rel = format!("./{}.{spec_ext}", module.replace('.', "/"));
assert!(
entry.contains("import ") && entry.contains(&rel),
"target {target}: entry must `import … from \"{rel}\"`",
);
}
"rust" => {
let crate_path = format!("crate::{}::", module.replace('.', "::"));
assert!(
entry.contains(&format!("use {crate_path}")),
"target {target}: entry must `use {crate_path}…;`",
);
}
"go" => {}
other => panic!("assert_entry_imports_module: unexpected per-module target {other}"),
}
}
#[test]
fn core_error_compiles_on_every_target() {
for (target, ext) in TARGETS {
let root = make_project(target);
let output = bock_bin()
.current_dir(&root)
.arg("build")
.arg("-t")
.arg(target)
.arg("--source-only")
.output()
.unwrap();
assert!(
output.status.success(),
"build --source-only failed for target {target}\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
let build_dir = root.join("build");
let module_file = module_file_path(&build_dir, target, "core.error", ext);
let module_src = fs::read_to_string(&module_file).unwrap_or_else(|_| {
panic!(
"target {target}: no per-module file {}",
module_file.display()
)
});
assert!(
module_src.contains("SimpleError"),
"target {target}: core.error module file lacks `SimpleError`",
);
let entry = read_entry(&build_dir, target, ext)
.unwrap_or_else(|| panic!("target {target}: no entry file"));
assert_entry_imports_module(&entry, target, "core.error");
}
}