use std::path::{Path, PathBuf};
use std::process::Command;
mod common;
fn scratch(name: &str) -> PathBuf {
common::scratch("htl-cli-one-path", name)
}
fn write(path: &Path, text: &str) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}
fn htl(args: &[&str], cwd: &Path) -> (String, String) {
let out = Command::new(common::htl_bin())
.args(args)
.current_dir(cwd)
.output()
.unwrap();
(
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
}
fn project() -> PathBuf {
let root = scratch("mixed");
write(&root.join("htl.toml"), "[[contract]]\ndir = \"mods\"\n");
write(
&root.join("mlua-pkg.toml"),
"[package]\nname = \"game\"\nversion = \"0.1.0\"\n",
);
write(
&root.join("src/bad.tl"),
"local x: integer = \"no\"\nprint(x)\n",
);
write(
&root.join("src/a.tl"),
"local b = require(\"b\")\nreturn { b = b }\n",
);
write(
&root.join("src/b.tl"),
"local a = require(\"a\")\nreturn { a = a }\n",
);
write(
&root.join("defs.tl"),
"local record defs\n record Mod ---@contract\n name: string\n end\nend\nreturn defs\n",
);
root
}
#[test]
fn a_report_spells_every_path_one_way_in_text_and_in_json() {
let root = project();
let (_, err) = htl(&["check", "--no-cache", "."], &root);
for line in err.lines().filter(|l| {
l.starts_with("error: ") || l.starts_with("warning: ") || l.starts_with("lint: ")
}) {
let path = line.split_once(": ").unwrap().1;
assert!(
!path.starts_with("./") && !path.starts_with('/'),
"every file in the project reads relative, with no ./: {line}"
);
}
assert!(err.contains("error: src/bad.tl:1:"), "{err}");
assert!(err.contains("lint: src/a.tl:1:"), "{err}");
assert!(err.contains("lint: defs.tl:2:1:"), "{err}");
let (out, _) = htl(&["check", "--no-cache", "--format", "json", "."], &root);
let v: serde_json::Value = serde_json::from_str(&out).expect(&out);
let files: Vec<&str> = v["diagnostics"]
.as_array()
.unwrap()
.iter()
.map(|d| d["file"].as_str().unwrap())
.collect();
assert!(files.contains(&"src/bad.tl"), "{files:?}");
assert!(files.contains(&"src/a.tl"), "{files:?}");
assert!(files.contains(&"defs.tl"), "{files:?}");
}
#[test]
fn from_a_subdirectory_paths_read_against_it() {
let root = project();
let (_, err) = htl(&["check", "--no-cache", ".."], &root.join("src"));
assert!(err.contains("error: bad.tl:1:"), "{err}");
assert!(err.contains("lint: a.tl:1:"), "{err}");
let defs = std::fs::canonicalize(root.join("defs.tl")).unwrap();
assert!(
err.contains(&format!("lint: {}:2:1:", defs.display())),
"outside the working directory: absolute: {err}"
);
assert!(
err.contains(&format!("were not checked: {};", defs.display())),
"{err}"
);
}
#[test]
fn build_spells_the_linkers_errors_as_the_checks() {
let root = scratch("build");
write(&root.join("htl.toml"), "[build]\nextra = [\"ghost\"]\n");
write(
&root.join("mlua-pkg.toml"),
"[package]\nname = \"game\"\nversion = \"0.1.0\"\n",
);
write(
&root.join("src/main.tl"),
"local gone = require(\"nothere\")\nprint(gone)\n",
);
let (_, err) = htl(
&["build", "--no-cache", "./src/main.tl", "-o", "out.hb"],
&root,
);
assert!(
err.lines().any(|l| l.starts_with("error: src/main.tl:1:")
&& l.contains("require(\"nothere\") is not on the search path")),
"{err}"
);
assert!(!err.contains("./src/main.tl"), "{err}");
assert!(!err.contains("module not found"), "{err}");
assert_eq!(err.matches("src/main.tl:1:").count(), 1, "{err}");
assert!(err.contains("htl build: 2 error(s)"), "{err}");
assert_eq!(
err.matches("error: extra module 'ghost' not found on the search path")
.count(),
1,
"{err}"
);
}
#[test]
fn junit_spells_a_failed_check_as_the_report_does() {
let root = scratch("junit");
write(
&root.join("mlua-pkg.toml"),
"[package]\nname = \"game\"\nversion = \"0.1.0\"\n",
);
write(
&root.join("tests/x_test.tl"),
"local t = require(\"htl.test\")\nlocal x: integer = \"no\"\nt.it(\"a\", function() t.expect(x):to_equal(1) end)\n",
);
htl(&["test", "--junit", "report.xml", "."], &root);
let out = std::fs::read_to_string(root.join("report.xml")).unwrap();
let body = out.split("type=\"check\">").nth(1).expect(&out);
assert!(body.starts_with("tests/x_test.tl:2:"), "{out}");
assert!(!out.contains("./tests/x_test.tl:"), "{out}");
}
#[test]
fn htl_test_names_its_files_as_check_does() {
let root = scratch("test-files");
write(
&root.join("mlua-pkg.toml"),
"[package]\nname = \"game\"\nversion = \"0.1.0\"\n",
);
write(
&root.join("tests/boom_test.tl"),
"local t = require(\"htl.test\")\nerror(\"boom\")\nt.it(\"never\", function() end)\n",
);
write(
&root.join("tests/snap_test.tl"),
"local t = require(\"htl.test\")\nt.it(\"snap\", function() t.expect(1):to_match_snapshot(\"first\") end)\n",
);
let (_, err) = htl(&["test", "--junit", "r.xml", "./tests"], &root);
assert!(
err.contains("FAIL tests/boom_test.tl (error: runtime error: tests/boom_test.tl:2: boom"),
"{err}"
);
assert!(err.contains("ok tests/snap_test.tl "), "{err}");
assert!(
err.contains("snapshot written: tests/__snapshots__/snap_test/first.snap"),
"{err}"
);
assert!(!err.contains("./tests"), "{err}");
let junit = std::fs::read_to_string(root.join("r.xml")).unwrap();
assert!(
junit.contains("<testsuite name=\"tests/boom_test.tl\""),
"{junit}"
);
assert!(!junit.contains("./tests"), "{junit}");
let (out, _) = htl(&["test", "--format", "json", "./tests"], &root);
let v: serde_json::Value = serde_json::from_str(&out).expect(&out);
let paths: Vec<&str> = v["files"]
.as_array()
.unwrap()
.iter()
.map(|f| f["path"].as_str().unwrap())
.collect();
assert_eq!(paths, ["tests/boom_test.tl", "tests/snap_test.tl"]);
}