use assert_cmd::prelude::*;
use std::process::Command;
const README: &str = include_str!("../README.md");
const BIN: &str = "net-mesh";
fn documented_commands() -> Vec<(usize, Vec<String>)> {
let mut out = Vec::new();
let mut in_shell_fence = false;
for (idx, raw) in README.lines().enumerate() {
let line = raw.trim();
if let Some(info) = line.strip_prefix("```") {
in_shell_fence = if in_shell_fence {
false
} else {
matches!(info, "sh" | "bash" | "shell" | "console")
};
continue;
}
if !in_shell_fence || !line.starts_with(BIN) {
continue;
}
if line.ends_with('\\') {
continue;
}
let argv: Vec<String> = line
.split_whitespace()
.skip(1) .take_while(|tok| !matches!(*tok, "#" | ">" | ">>" | "|" | "2>" | "&&" | ";"))
.map(str::to_owned)
.collect();
if !argv.is_empty() {
out.push((idx + 1, argv));
}
}
out
}
#[test]
fn readme_publishes_commands_at_all() {
let found = documented_commands();
assert!(
found.len() >= 5,
"extracted only {} commands from the README — the fence parser is \
probably broken, and a checker that finds nothing passes forever",
found.len()
);
}
#[test]
fn every_readme_command_resolves_against_the_real_binary() {
let mut failures = Vec::new();
for (line, argv) in documented_commands() {
let output = Command::cargo_bin(BIN)
.unwrap()
.args(&argv)
.arg("--help")
.output()
.unwrap();
if !output.status.success() {
failures.push(format!(
"README.md:{line}: `{BIN} {}` does not resolve\n {}",
argv.join(" "),
String::from_utf8_lossy(&output.stderr)
.lines()
.next()
.unwrap_or("(no stderr)"),
));
}
}
assert!(
failures.is_empty(),
"the README documents {} command(s) the CLI does not accept:\n{}",
failures.len(),
failures.join("\n"),
);
}
#[test]
fn the_probe_rejects_a_subcommand_that_does_not_exist() {
let output = Command::cargo_bin(BIN)
.unwrap()
.args(["snapshot", "show", "--help"])
.output()
.unwrap();
assert!(
!output.status.success(),
"`{BIN} snapshot show --help` succeeded — `--help` is short-circuiting \
ahead of subcommand resolution, so the README check above proves nothing"
);
}