use crate::common::*;
use std::path::Path;
#[test]
fn a_temp_directory_name_is_not_reproducible_from_the_pid_and_a_counter() {
let dir = tmp_dir();
assert_eq!(
std::fs::read_dir(&dir).unwrap().count(),
0,
"a fresh temp directory must be empty — inheriting one with contents \
in it is the whole failure this guards against"
);
let name = dir.file_name().unwrap().to_string_lossy().into_owned();
let derivable = format!("stuffr-cli-archive-{}-", std::process::id());
let tail = name
.strip_prefix(&derivable)
.unwrap_or_else(|| panic!("unexpected temp directory name: {name}"));
assert!(
tail.contains('-') && !tail.chars().all(|c| c.is_ascii_digit()),
"everything after the pid was the counter alone, so a second run on a \
recycled pid reproduced this name exactly: {name}"
);
let other = tmp("token-check");
let other_name = other.file_name().unwrap().to_string_lossy().into_owned();
assert_ne!(
other_name,
format!("stuffr-cli-{}-token-check", std::process::id()),
"tmp() must carry the run token too"
);
assert!(
other_name.ends_with("-token-check"),
"…and must still end in the caller's own name: {other_name}"
);
}
#[test]
fn documented_install_commands_name_features_this_crate_declares() {
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let manifest = std::fs::read_to_string(manifest_dir.join("Cargo.toml")).unwrap();
let features: Vec<String> = manifest
.split("\n[")
.find(|section| section.starts_with("features]"))
.expect(
"stuffr-cli must declare a [features] section: without one, every \
documented `cargo install stuffr-cli --features …` fails",
)
.lines()
.filter_map(|line| line.split_once('='))
.filter(|(_, rhs)| rhs.trim_start().starts_with('['))
.map(|(name, _)| name.trim().to_string())
.collect();
assert!(
features.contains(&"legacy".to_string()),
"the parse found {features:?}; if `legacy` is missing the scan itself is broken, \
not the manifest"
);
let docs = user_facing_documents();
let mut commands_checked = 0usize;
for doc in docs {
let text = std::fs::read_to_string(&doc).unwrap();
for line in text.lines() {
let Some(rest) = line
.split("cargo install")
.nth(1)
.and_then(|r| r.split("stuffr-cli").nth(1))
else {
continue;
};
let Some(after) = rest.split("--features").nth(1) else {
continue;
};
let name = after.split_whitespace().next().unwrap_or("");
commands_checked += 1;
assert!(
features.contains(&name.to_string()),
"{} prints `cargo install stuffr-cli --features {name}`, but stuffr-cli \
declares only {features:?} — that command fails with \"the package \
'stuffr-cli' does not contain this feature\"",
doc.display()
);
}
}
assert!(
commands_checked >= 2,
"expected the READMEs to print at least the c-backed and legacy install \
commands; found {commands_checked} — the scan has stopped finding them"
);
}
#[test]
fn no_document_carries_a_stale_unqualified_codec_or_container_count() {
const LEGACY_ONLY_CODECS: &[&str] = &["compress"];
const LEGACY_ONLY_CONTAINERS: &[&str] = &["lha", "arj", "arc", "zoo"];
const CUES: &[&str] = &[
"pure",
"pure-tier",
"writable",
"round-trip",
"read-only",
"legacy",
"no-default-features",
];
const QUALIFIERS: &[&str] = &["round-trip", "read-only", "pure-tier"];
const CUE_WINDOW: usize = 8;
let registry = stuffr::registry();
let rows = registry.matrix();
let is_container = |id| registry.container(id).is_some();
let count = |container: bool, pred: &dyn Fn(&stuffr::FormatRow) -> bool| {
rows.iter()
.filter(|r| is_container(r.id) == container && pred(r))
.count()
};
let legacy = |container: bool, id: &str| {
if container {
LEGACY_ONLY_CONTAINERS.contains(&id)
} else {
LEGACY_ONLY_CODECS.contains(&id)
}
};
let full = |container: bool| count(container, &|_| true);
let narrowed = |container: bool| {
[
count(container, &|r| r.write),
count(container, &|r| !r.write),
count(container, &|r| !legacy(container, r.id.as_str())),
]
};
const NUMBERS: &[(&str, usize)] = &[
("one", 1),
("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
("ten", 10),
("eleven", 11),
("twelve", 12),
("thirteen", 13),
("fourteen", 14),
("fifteen", 15),
("sixteen", 16),
("seventeen", 17),
("eighteen", 18),
("nineteen", 19),
("twenty", 20),
];
let mut claims_checked = 0usize;
for doc in user_facing_documents() {
let text = std::fs::read_to_string(&doc).unwrap();
let flat = text.split_whitespace().collect::<Vec<_>>().join(" ");
for clause in flat.split(". ").flat_map(|s| s.split('|')) {
let words: Vec<String> = clause
.split(' ')
.map(|w| {
w.trim_matches(|c: char| !c.is_alphanumeric() && c != '-')
.to_lowercase()
})
.collect();
for i in 0..words.len() {
let Some((_, value)) = NUMBERS.iter().find(|(w, _)| *w == words[i]) else {
continue;
};
let Some(noun) = words.get(i + 1) else {
continue;
};
if QUALIFIERS.contains(&noun.as_str()) {
continue;
}
let container = match noun.as_str() {
"containers" => true,
"codecs" => false,
_ => continue,
};
claims_checked += 1;
let from = i.saturating_sub(CUE_WINDOW);
let cued = words[from..=i + 1]
.iter()
.any(|w| CUES.contains(&w.as_str()));
let want_full = full(container);
let ok = if cued {
*value == want_full || narrowed(container).contains(value)
} else {
*value == want_full
};
assert!(
ok,
"{} says `{} {noun}`, but this build registers {want_full} {noun} \
(narrowed counts, allowed only in a clause naming a narrowing — \
{CUES:?}, within {CUE_WINDOW} words — are {:?}). The clause is \
{clause:?}",
doc.display(),
words[i],
narrowed(container),
);
}
}
}
assert!(
claims_checked >= 5,
"the scan found only {claims_checked} unqualified counts — it is matching nothing \
and proving nothing"
);
}
#[test]
fn no_document_carries_a_stale_round_trip_or_read_only_count() {
const LEGACY_ONLY_CODECS: &[&str] = &["compress"];
const LEGACY_ONLY_CONTAINERS: &[&str] = &["lha", "arj", "arc", "zoo"];
let registry = stuffr::registry();
let rows = registry.matrix();
let is_container = |id| registry.container(id).is_some();
let count = |container: bool, pred: &dyn Fn(&stuffr::FormatRow) -> bool| {
rows.iter()
.filter(|r| is_container(r.id) == container && pred(r))
.count()
};
let legacy = |container: bool, id: &str| {
if container {
LEGACY_ONLY_CONTAINERS.contains(&id)
} else {
LEGACY_ONLY_CODECS.contains(&id)
}
};
let expected = |qualifier: &str, container: bool| -> usize {
match qualifier {
"round-trip" => count(container, &|r| r.write),
"read-only" => count(container, &|r| !r.write),
"pure-tier" => count(container, &|r| !legacy(container, r.id.as_str())),
other => unreachable!("unhandled qualifier {other}"),
}
};
const NUMBERS: &[(&str, usize)] = &[
("one", 1),
("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
("ten", 10),
("eleven", 11),
("twelve", 12),
("thirteen", 13),
("fourteen", 14),
("fifteen", 15),
("sixteen", 16),
("seventeen", 17),
("eighteen", 18),
("nineteen", 19),
("twenty", 20),
];
let docs = user_facing_documents();
let mut claims_checked = 0usize;
for doc in &docs {
let text = std::fs::read_to_string(doc).unwrap();
let flat = text.split_whitespace().collect::<Vec<_>>().join(" ");
let words: Vec<String> = flat
.split(' ')
.map(|w| {
w.trim_matches(|c: char| !c.is_alphanumeric() && c != '-')
.to_lowercase()
})
.collect();
for i in 0..words.len() {
let Some((_, value)) = NUMBERS.iter().find(|(w, _)| *w == words[i]) else {
continue;
};
let Some(qualifier) = words.get(i + 1) else {
continue;
};
if !["round-trip", "read-only", "pure-tier"].contains(&qualifier.as_str()) {
continue;
}
let noun = match words.get(i + 2).map(String::as_str) {
Some("legacy") => words.get(i + 3),
other => other.and_then(|_| words.get(i + 2)),
};
let container = match noun.map(String::as_str) {
Some("containers") => true,
Some("codecs") => false,
_ => continue,
};
claims_checked += 1;
let want = expected(qualifier, container);
assert_eq!(
*value,
want,
"{} says `{} {qualifier}{} {}`, but this build has {want}. This is the \
check that closes the class three stale write-capability claims were found \
by hand in: the row-count guard cannot see the WRITE column, so nothing \
else would say so",
doc.display(),
words[i],
if words.get(i + 2).map(String::as_str) == Some("legacy") {
" legacy"
} else {
""
},
if container { "containers" } else { "codecs" },
);
}
}
assert!(
claims_checked >= 8,
"the scan found only {claims_checked} qualified counts across the {} documents — \
it is matching nothing and proving nothing",
docs.len()
);
}