use std::path::{Path, PathBuf};
const REMAINING: [&str; 17] = [
"headwater-capture",
"headwater-check",
"headwater-completions",
"headwater-conformance",
"headwater-explain",
"headwater-export",
"headwater-gate",
"headwater-generate",
"headwater-import",
"headwater-infer",
"headwater-init",
"headwater-mcp",
"headwater-new",
"headwater-probe",
"headwater-query",
"headwater-route",
"headwater-taxonomy",
];
const PROSE_ONLY: [&str; 2] = ["headwater-probe", "headwater-query"];
fn interfaces_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../docs/interfaces")
}
fn read(slug: &str) -> String {
let path = interfaces_dir().join(format!("{slug}.md"));
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
}
fn no_color_row(text: &str) -> Option<&str> {
text.lines()
.find_map(|line| line.strip_prefix("| `--no-color` | "))
.map(|rest| rest.trim_end_matches(" |"))
}
fn canonical_sentence() -> String {
let help = read("headwater-help");
no_color_row(&help)
.expect("headwater-help.md carries the --no-color row this file reads the sentence from")
.to_string()
}
#[test]
fn every_rewritten_options_table_states_the_sentence_headwater_help_states() {
let canonical = canonical_sentence();
let mut wrong = Vec::new();
for slug in REMAINING.iter().filter(|slug| !PROSE_ONLY.contains(slug)) {
let text = read(slug);
match no_color_row(&text) {
Some(row) if row.contains(&canonical) => {}
Some(row) => wrong.push(format!("{slug}.md: {row}")),
None => wrong.push(format!(
"{slug}.md: no `--no-color` row in its Options table"
)),
}
}
assert!(
wrong.is_empty(),
"these documents do not restate the sentence headwater-help.md carries:\n{}",
wrong.join("\n")
);
}
#[test]
fn no_document_still_claims_no_run_of_this_binary_ever_writes_color() {
let retired = [
"changes no byte",
"Confirms color-free output",
"Confirm the binary's color-free output",
"Disable color output",
"color-free output",
];
let mut offenders = Vec::new();
for slug in REMAINING {
let text = read(slug);
for phrase in retired {
if text.contains(phrase) {
offenders.push(format!("{slug}.md: {phrase:?}"));
}
}
}
assert!(
offenders.is_empty(),
"these documents still state the sentence this decision made false:\n{}",
offenders.join("\n")
);
}
#[test]
fn every_document_gains_a_no_banner_mention() {
let mut missing = Vec::new();
for slug in REMAINING {
let text = read(slug);
let carries = match PROSE_ONLY.contains(&slug) {
true => text.contains("--no-banner"),
false => text
.lines()
.any(|line| line.starts_with("| `--no-banner` |")),
};
if !carries {
missing.push(slug);
}
}
assert!(
missing.is_empty(),
"these documents name no `--no-banner`: {missing:?}"
);
}
#[test]
fn the_two_prose_only_documents_carry_no_banner_beside_no_color() {
for slug in PROSE_ONLY {
let text = read(slug);
assert!(
text.contains("`--no-color`") && text.contains("`--no-banner`"),
"{slug}.md names --no-color without --no-banner beside it"
);
}
}
const REVERSED: [&str; 5] = [
"changes no byte",
"writes no color on either stream",
"reaches either stream",
"under any terminal",
"is read by nothing",
];
fn hw_dr_0033_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join(
"../../../docs/decisions/\
0033-q33-whether-the-command-line-is-derived-and-who-a-flag-belongs-to.md",
)
}
fn hw_dr_0033_no_color_paragraph() -> String {
let path = hw_dr_0033_path();
let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
text.lines()
.find(|line| line.starts_with("**`--no-color`"))
.unwrap_or_else(|| {
panic!(
"{} carries no paragraph opening in bold on `--no-color`",
path.display()
)
})
.to_string()
}
#[test]
fn hw_dr_0033_cites_the_ruling_that_moved_it_and_drops_the_reversed_claim() {
let paragraph = hw_dr_0033_no_color_paragraph();
let link = "0045-coloring-the-cli-and-where-the-banner-goes.md";
assert!(
paragraph.contains(link),
"the paragraph links nothing to the ruling that reversed it:\n{paragraph}"
);
let ruling = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../docs/decisions")
.join(link);
let ruling = std::fs::read_to_string(&ruling)
.unwrap_or_else(|e| panic!("the paragraph links {}: {e}", ruling.display()));
assert!(
ruling.contains("id: HW-DR-0045"),
"the link the paragraph carries reaches a document that is not HW-DR-0045"
);
let still: Vec<&str> = REVERSED
.into_iter()
.filter(|phrase| paragraph.contains(phrase))
.collect();
assert!(
still.is_empty(),
"the paragraph still states what HW-DR-0045 reversed: {still:?}\n{paragraph}"
);
}
#[test]
fn nothing_in_hw_dr_0033_still_states_the_behavior_hw_dr_0045_reversed() {
let _anchor = hw_dr_0033_no_color_paragraph();
let path = hw_dr_0033_path();
let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
let mut offenders = Vec::new();
for (number, line) in text.lines().enumerate() {
for phrase in REVERSED {
if line.contains(phrase) {
offenders.push(format!("line {}: {phrase:?}", number + 1));
}
}
}
assert!(
offenders.is_empty(),
"HW-DR-0033 still states what HW-DR-0045 reversed:\n{}",
offenders.join("\n")
);
}
#[test]
fn hw_dr_0033_states_the_number_of_escape_byte_cases_width_actually_holds() {
let width = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/width.rs");
let text =
std::fs::read_to_string(&width).unwrap_or_else(|e| panic!("{}: {e}", width.display()));
let held = text
.lines()
.filter(|line| line.starts_with("fn no_escape_byte_"))
.count();
let spelled = ["no", "one", "two", "three", "four", "five", "six", "seven"]
.get(held)
.copied()
.unwrap_or_else(|| panic!("{held} cases is past the range this case spells"));
let paragraph = hw_dr_0033_no_color_paragraph();
assert!(
paragraph.contains(&format!("in {spelled} places")),
"`width.rs` holds {held} `no_escape_byte_*` cases, and the paragraph does not say \
\"in {spelled} places\":\n{paragraph}"
);
}