use std::path::Path;
use std::process::{Command, Output};
use std::time::{Duration, SystemTime};
mod common;
mod generated {
include!("fixtures/rs/teal_types.rs");
}
fn write(path: &Path, text: &str) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}
fn read(path: &Path) -> String {
std::fs::read_to_string(path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
}
fn htl(root: &Path, args: &[&str]) -> Output {
Command::new(common::htl_bin())
.args(args)
.current_dir(root)
.output()
.unwrap()
}
fn stderr(out: &Output) -> String {
String::from_utf8_lossy(&out.stderr).into_owned()
}
const EV: &str = "local record M
record Event
kind: Kind
at: integer
note: string
end
enum Kind
\"open\"
\"close\"
end
end
return M
";
const MODULES_EV: &str = "[rust]\nmodules = [\"ev\"]\n";
fn project(name: &str) -> common::TempDir {
project_with(name, MODULES_EV, EV)
}
fn project_with(name: &str, toml: &str, ev: &str) -> common::TempDir {
let root = common::tempdir("htl-cli-rs", name);
write(&root.join("htl.toml"), toml);
write(&root.join("src/ev/init.tl"), ev);
root
}
#[test]
fn a_record_and_an_enum_are_written_as_a_struct_and_an_enum() {
let root = project("acceptance-1");
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(stderr(&out), "htl rs: wrote src/teal_types.rs\n");
let file = read(&root.join("src/teal_types.rs"));
assert!(file.starts_with("// Generated by htl rs"), "{file}");
assert!(file.contains("pub mod ev {"), "{file}");
assert!(
file.contains(
" #[derive(Debug, Clone, PartialEq, ::htl::TealRecord)]
#[teal(name = \"Event\")]
pub struct Event {
pub kind: ::core::option::Option<Kind>,
pub at: ::core::option::Option<i64>,
pub note: ::core::option::Option<::std::string::String>,
}"
),
"{file}"
);
assert!(
file.contains(
" pub enum Kind {
#[teal(name = \"close\")]
Close,
#[teal(name = \"open\")]
Open,
}"
),
"{file}"
);
}
#[test]
fn an_exported_module_is_written_as_the_client_the_macro_expands_to() {
let root = common::tempdir("htl-cli-rs", "client");
write(
&root.join("htl.toml"),
include_str!("../../htl-core/tests/fixtures/client/htl.toml"),
);
write(
&root.join("src/shop/types.tl"),
include_str!("../../htl-core/tests/fixtures/client/src/shop/types.tl"),
);
write(
&root.join("src/shop/app.tl"),
include_str!("../../htl-core/tests/fixtures/client/src/shop/app.tl"),
);
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(stderr(&out), "htl rs: wrote src/teal_types.rs\n");
assert_eq!(
read(&root.join("src/teal_types.rs")),
include_str!("../../htl-core/tests/fixtures/client/teal_types.rs")
);
}
#[test]
fn exports_alone_are_written_and_a_function_outside_the_body_is_a_note() {
let app = "local record app\n add: function(integer, integer): integer\nend\n\
function app.add(a: integer, b: integer): integer\n return a + b\nend\n\
function app.helper()\nend\nreturn app\n";
let root = project_with("exports-alone", "[rust]\nexports = [\"app\"]\n", EV);
write(&root.join("src/app.tl"), app);
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
assert!(
stderr(&out).contains(
"src/app.tl:7:1: `app.helper` is defined outside the body of the record, so it is not in the client; declare `helper: function(..)` in the body to add it"
),
"{}",
stderr(&out)
);
let file = read(&root.join("src/teal_types.rs"));
assert!(file.contains("pub struct App {"), "{file}");
assert!(!file.contains("helper"), "{file}");
}
#[test]
fn a_struct_record_requires_the_fields_that_are_not_optional() {
let marked = EV
.replace("record Event\n", "record Event ---@struct\n")
.replace("note: string\n", "note: string ---@optional\n");
let root = project_with("acceptance-2", MODULES_EV, &marked);
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
let file = read(&root.join("src/teal_types.rs"));
assert!(
file.contains(
" pub struct Event {
pub kind: Kind,
pub at: i64,
pub note: ::core::option::Option<::std::string::String>,
}"
),
"{file}"
);
}
#[test]
fn a_types_entry_sets_a_field_and_a_key_that_matches_nothing_is_refused() {
let toml = format!("{MODULES_EV}[rust.types]\n\"ev.Event.at\" = \"u32\"\n");
let root = project_with("acceptance-3", &toml, EV);
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
let file = read(&root.join("src/teal_types.rs"));
assert!(file.contains(" pub at: u32,\n"), "{file}");
let bad = format!("{MODULES_EV}[rust.types]\n\"ev.Event.nope\" = \"u32\"\n");
let root = project_with("acceptance-3-nope", &bad, EV);
let out = htl(&root, &["rs"]);
assert_eq!(out.status.code(), Some(1), "{}", stderr(&out));
let err = stderr(&out);
assert!(err.contains("\"ev.Event.nope\" matches no field"), "{err}");
assert!(
err.ends_with("htl rs: src/teal_types.rs not written\n"),
"{err}"
);
assert!(!root.join("src/teal_types.rs").exists());
}
#[test]
fn a_second_run_is_current_and_leaves_the_file_alone() {
let root = project("acceptance-4");
assert!(htl(&root, &["rs"]).status.success());
let path = root.join("src/teal_types.rs");
let long_ago = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
std::fs::File::options()
.write(true)
.open(&path)
.unwrap()
.set_modified(long_ago)
.unwrap();
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(stderr(&out), "htl rs: src/teal_types.rs is current\n");
assert_eq!(
std::fs::metadata(&path).unwrap().modified().unwrap(),
long_ago
);
}
#[test]
fn a_function_field_is_refused_and_nothing_is_written() {
let with_fn = EV.replace(
"note: string\n",
"note: string\n f: function(): integer\n",
);
let root = project_with("acceptance-5", MODULES_EV, &with_fn);
let out = htl(&root, &["rs"]);
assert_eq!(out.status.code(), Some(1), "{}", stderr(&out));
assert_eq!(
stderr(&out),
"error: src/ev/init.tl:6:7: `M.Event.f`: a function or method field has no Rust type\n\
htl rs: src/teal_types.rs not written\n"
);
assert!(!root.join("src/teal_types.rs").exists());
}
#[test]
fn an_empty_modules_removes_the_file_it_wrote() {
let root = project("acceptance-6");
assert!(htl(&root, &["rs"]).status.success());
assert!(root.join("src/teal_types.rs").exists());
write(&root.join("htl.toml"), "[rust]\nmodules = []\n");
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(
stderr(&out),
"htl rs: nothing to write ([rust] modules and exports are empty)\n\
htl rs: removed src/teal_types.rs\n"
);
assert!(!root.join("src/teal_types.rs").exists());
}
#[test]
fn a_file_that_htl_rs_did_not_write_is_left_alone() {
let root = project("acceptance-7");
write(
&root.join("src/teal_types.rs"),
"// mine\npub struct Mine;\n",
);
let out = htl(&root, &["rs"]);
assert_eq!(out.status.code(), Some(1), "{}", stderr(&out));
assert_eq!(
stderr(&out),
"htl rs: src/teal_types.rs is not written by htl rs; move it, or set [rust] out\n"
);
assert_eq!(
read(&root.join("src/teal_types.rs")),
"// mine\npub struct Mine;\n"
);
let out = htl(&root, &["rs", "--check"]);
assert_eq!(out.status.code(), Some(1), "{}", stderr(&out));
assert!(stderr(&out).contains("is not written by htl rs"));
assert_eq!(
read(&root.join("src/teal_types.rs")),
"// mine\npub struct Mine;\n"
);
}
#[test]
fn check_finds_a_file_that_has_fallen_behind() {
let root = project("acceptance-8");
let out = htl(&root, &["rs", "--check"]);
assert_eq!(out.status.code(), Some(1), "no file yet: {}", stderr(&out));
assert_eq!(
stderr(&out),
"htl rs: src/teal_types.rs is stale; run htl rs\n"
);
assert!(
!root.join("src/teal_types.rs").exists(),
"--check wrote the file"
);
assert!(htl(&root, &["rs"]).status.success());
let out = htl(&root, &["rs", "--check"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(stderr(&out), "htl rs: src/teal_types.rs is current\n");
write(
&root.join("src/ev/init.tl"),
&EV.replace("note: string\n", "note: string\n by: string\n"),
);
let out = htl(&root, &["rs", "--check"]);
assert_eq!(out.status.code(), Some(1), "{}", stderr(&out));
assert_eq!(
stderr(&out),
"htl rs: src/teal_types.rs is stale; run htl rs\n"
);
assert!(htl(&root, &["rs"]).status.success());
let out = htl(&root, &["rs", "--check"]);
assert!(out.status.success(), "{}", stderr(&out));
}
#[test]
fn check_writes_no_declaration_either() {
let root = project_with(
"check-writes-nothing",
&format!("{MODULES_EV}\n[[contract]]\ndir = \"mods\"\n"),
EV,
);
write(
&root.join("src/defs.tl"),
"local record defs\n record Mod ---@contract\n name: string ---@required\n \
end\nend\nreturn defs\n",
);
write(&root.join("mods/good.tl"), "return { name = \"g\" }\n");
let out = htl(&root, &["rs", "--check"]);
assert_eq!(out.status.code(), Some(1), "{}", stderr(&out));
assert!(
!root.join("types/defs.d.tl").exists(),
"--check published a declaration: {}",
stderr(&out)
);
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
assert!(
root.join("types/defs.d.tl").exists(),
"htl rs without --check publishes it, as every command does: {}",
stderr(&out)
);
}
#[test]
fn a_type_error_in_any_own_file_stops_the_run() {
let root = project("type-error");
write(
&root.join("src/other.tl"),
"local x: integer = \"not an integer\"\nreturn x\n",
);
let out = htl(&root, &["rs"]);
assert_eq!(out.status.code(), Some(1), "{}", stderr(&out));
let err = stderr(&out);
assert!(err.contains("src/other.tl:1:"), "{err}");
assert!(
err.ends_with("htl rs: src/teal_types.rs not written\n"),
"{err}"
);
assert!(!root.join("src/teal_types.rs").exists());
}
#[test]
fn out_sets_the_file() {
let toml = "[rust]\nmodules = [\"ev\"]\nout = \"gen/types.rs\"\n";
let root = project_with("out", toml, EV);
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
assert_eq!(stderr(&out), "htl rs: wrote gen/types.rs\n");
assert!(root.join("gen/types.rs").exists());
assert!(!root.join("src/teal_types.rs").exists());
}
#[test]
fn the_generated_file_carries_a_table_to_a_struct_and_back() {
use generated::ev::{Event, Kind};
use htl::mlua::{FromLua, IntoLua};
let h = htl::Htl::new().unwrap();
let lua = h.lua();
let table: htl::mlua::Value = lua
.load(r#"return { kind = "open", at = 3 }"#)
.eval()
.unwrap();
let event = Event::from_lua(table, lua).unwrap();
assert_eq!(
event,
Event {
kind: Some(Kind::Open),
at: Some(3),
note: None,
}
);
let back = event.into_lua(lua).unwrap();
lua.globals().set("back", back).unwrap();
let same: bool = lua
.load(
r#"
local n = 0
for _ in pairs(back) do n = n + 1 end
return n == 2 and back.kind == "open" and back.at == 3 and back.note == nil
"#,
)
.eval()
.unwrap();
assert!(same, "the table that came back is not the one that went in");
}
#[test]
fn the_binary_writes_the_committed_file() {
let root = project("fixture");
assert!(htl(&root, &["rs"]).status.success());
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/rs/teal_types.rs");
assert_eq!(read(&root.join("src/teal_types.rs")), read(&fixture));
}
#[test]
fn an_alias_in_a_listed_module_with_no_record_or_enum_is_lowered() {
let root = project_with(
"alias-only-module",
"[rust]\nmodules = [\"ev\", \"geom\"]\n",
"local geom = require(\"geom\")
local record M
record Event
pts: geom.Pts
end
end
return M
",
);
write(
&root.join("src/geom/init.tl"),
"local record M
type Pts = {integer}
end
return M
",
);
let out = htl(&root, &["rs"]);
assert!(out.status.success(), "{}", stderr(&out));
let file = read(&root.join("src/teal_types.rs"));
assert!(
file.contains(" pub pts: ::core::option::Option<::std::vec::Vec<i64>>,\n"),
"{file}"
);
}