use std::path::{Path, PathBuf};
use std::process::Command;
mod common;
fn tempdir(name: &str) -> common::TempDir {
common::tempdir("htl-cli-sample", name)
}
fn fixture_tree(root: &Path, sub: &str) -> PathBuf {
let from = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures")
.join(sub);
let to = root.join(sub);
copy_tree(&from, &to);
to
}
fn copy_tree(from: &Path, to: &Path) {
std::fs::create_dir_all(to).unwrap();
for e in std::fs::read_dir(from).unwrap() {
let e = e.unwrap();
let name = e.file_name();
let src = e.path();
let dst = to.join(&name);
if src.is_dir() {
copy_tree(&src, &dst);
} else {
std::fs::copy(&src, &dst).unwrap();
}
}
}
fn fixture(root: &Path, sub: &str, name: &str) -> String {
fixture_tree(root, sub)
.join(name)
.to_string_lossy()
.into_owned()
}
fn sample(root: &Path) -> Vec<String> {
let dir = fixture_tree(root, "sample");
["util.tl", "main.tl", "util_test.tl"]
.iter()
.map(|f| dir.join(f).to_string_lossy().into_owned())
.collect()
}
fn run(args: &[&str], cwd: &Path) -> (bool, String) {
let out = Command::new(common::htl_bin())
.args(args)
.current_dir(cwd)
.output()
.unwrap();
let mut text = String::from_utf8_lossy(&out.stdout).into_owned();
text.push_str(&String::from_utf8_lossy(&out.stderr));
(out.status.success(), text)
}
fn run_with(args: &[&str], extra: &[String], cwd: &Path) -> (bool, String) {
let mut all: Vec<&str> = args.to_vec();
all.extend(extra.iter().map(String::as_str));
run(&all, cwd)
}
#[test]
fn the_sample_modules_check_clean() {
let dir = tempdir("check");
let (ok, out) = run_with(&["check"], &sample(&dir), &dir);
assert!(ok, "the sample must check clean:\n{out}");
assert!(
out.contains("3 file(s), 0 error(s), 0 warning(s), 0 lint(s)"),
"all three modules were checked and nothing was reported:\n{out}"
);
}
#[test]
fn the_sample_suite_passes() {
let dir = tempdir("test");
let (ok, out) = run(&["test", &fixture(&dir, "sample", "util_test.tl")], &dir);
assert!(ok, "the sample suite must pass:\n{out}");
assert!(
out.contains("4 passed, 0 failed"),
"all four cases ran and passed:\n{out}"
);
}
#[test]
fn the_second_check_is_answered_out_of_the_store() {
let dir = tempdir("cache");
let files = sample(&dir);
let (ok, cold) = run_with(&["check", "--explain-cache"], &files, &dir);
assert!(ok, "{cold}");
assert!(
cold.contains("htl cache: 0 hit, 0 missed, 3 written,"),
"a cold store answers nothing and records all three modules:\n{cold}"
);
assert!(
!cold.contains("[cached]"),
"the first run has nothing to replay:\n{cold}"
);
let (ok, warm) = run_with(&["check", "--explain-cache"], &files, &dir);
assert!(ok, "{warm}");
assert!(
warm.contains("htl cache: 3 hit, 0 missed, 0 written,"),
"the second run reads all three back and writes nothing:\n{warm}"
);
assert!(
warm.lines().any(|l| l.ends_with(" [cached]")),
"and its summary says the whole walk was replayed:\n{warm}"
);
}
#[test]
fn cache_status_reports_what_the_check_wrote() {
let dir = tempdir("status");
let (ok, check) = run_with(&["check"], &sample(&dir), &dir);
assert!(ok, "{check}");
let (ok, status) = run(&["cache", "status"], &dir);
assert!(ok, "{status}");
assert!(
status.contains("htl cache: 3 entries,"),
"one entry per module checked:\n{status}"
);
}
#[test]
fn a_failing_suite_names_every_failure_and_exits_non_zero() {
let dir = tempdir("failing");
let (ok, out) = run(&["test", &fixture(&dir, "failing", "bad_test.tl")], &dir);
assert!(!ok, "a suite with failing cases must not exit zero:\n{out}");
assert!(
out.contains("fails on purpose: expected 3, got 2"),
"the wrong equality is named with both values:\n{out}"
);
assert!(
out.contains(
"expected an error but none came: expected an error, but the function returned"
),
"the assertion that expected a raise is named too:\n{out}"
);
assert!(
out.contains("1 passed, 2 failed"),
"the case that passes still passes; both failures are counted:\n{out}"
);
}
#[test]
fn a_suite_that_does_not_type_check_is_refused_before_it_runs() {
let dir = tempdir("typed");
let (ok, out) = run(&["test", &fixture(&dir, "failing", "typed_test.tl")], &dir);
assert!(!ok, "a suite that does not type check must fail:\n{out}");
assert!(
out.contains("got string \"2\", expected integer"),
"the type error is reported as a type error:\n{out}"
);
assert!(
out.contains("type check failed"),
"and the file is marked as one that never ran:\n{out}"
);
assert!(
out.contains("0 passed, 0 failed"),
"no case ran, so there is nothing to count either way:\n{out}"
);
}