use std::path::{Path, PathBuf};
use std::process::Command;
mod common;
fn scratch(name: &str) -> PathBuf {
common::scratch("htl-cli-sample", name)
}
fn fixture(rel: &str) -> String {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures")
.join(rel)
.to_string_lossy()
.into_owned()
}
fn sample() -> Vec<String> {
["sample/util.tl", "sample/main.tl", "sample/util_test.tl"]
.iter()
.map(|f| fixture(f))
.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 = scratch("check");
let (ok, out) = run_with(&["check"], &sample(), &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 = scratch("test");
let (ok, out) = run(&["test", &fixture("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 = scratch("cache");
let files = sample();
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 = scratch("status");
let (ok, check) = run_with(&["check"], &sample(), &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 = scratch("failing");
let (ok, out) = run(&["test", &fixture("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 = scratch("typed");
let (ok, out) = run(&["test", &fixture("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}"
);
}