#![allow(clippy::expect_used, clippy::unwrap_used)]
mod support;
use std::collections::BTreeSet;
use std::fs;
use tempfile::TempDir;
use voom::catalog::CATALOG;
use voom::run::{RunOptions, run};
fn readme_ecosystems() -> Vec<String> {
let readme = include_str!("../README.md");
readme
.lines()
.skip_while(|line| !line.starts_with("| Ecosystem | Marker | Artifacts |"))
.skip(2)
.take_while(|line| line.starts_with("| "))
.filter_map(|line| line.split('|').nth(1))
.map(|cell| cell.trim().to_owned())
.collect()
}
#[test]
fn the_readme_table_lists_exactly_the_catalog() {
let documented: BTreeSet<String> = readme_ecosystems().into_iter().collect();
let shipped: BTreeSet<String> = CATALOG.iter().map(|ecosystem| ecosystem.name.to_owned()).collect();
assert!(
!documented.is_empty(),
"the README's ecosystem table could not be found"
);
let missing: Vec<_> = shipped.difference(&documented).collect();
assert!(
missing.is_empty(),
"in the catalog but not in the README's table: {missing:?}"
);
let extra: Vec<_> = documented.difference(&shipped).collect();
assert!(
extra.is_empty(),
"in the README's table but not in the catalog: {extra:?}"
);
}
#[test]
fn the_readme_table_has_one_row_per_ecosystem() {
assert_eq!(
readme_ecosystems().len(),
CATALOG.len(),
"the README's ecosystem table and the catalog disagree on how many there are"
);
}
#[test]
fn the_readme_table_omits_nothing_the_catalog_ships() {
let readme = include_str!("../README.md");
let rows: Vec<&str> = readme
.lines()
.skip_while(|line| !line.starts_with("| Ecosystem | Marker | Artifacts |"))
.skip(2)
.take_while(|line| line.starts_with("| "))
.collect();
assert_eq!(rows.len(), CATALOG.len(), "one row per ecosystem");
let mut omissions = Vec::new();
for ecosystem in CATALOG {
let row = rows
.iter()
.find(|row| row.split('|').nth(1).is_some_and(|cell| cell.trim() == ecosystem.name))
.unwrap_or_else(|| panic!("no README row for `{}`", ecosystem.name));
let advertised: Vec<&str> = row
.split('|')
.nth(2)
.unwrap_or_default()
.split(',')
.map(|token| token.trim().trim_matches('`'))
.filter(|token| !token.is_empty())
.collect();
for marker in ecosystem.markers {
let named = advertised
.iter()
.any(|token| *token == *marker || token.strip_suffix('*').is_some_and(|stem| marker.starts_with(stem)));
if !named {
omissions.push(format!("{}: marker `{marker}`", ecosystem.name));
}
}
for artifact in ecosystem.artifacts {
if !row.contains(artifact.path) {
omissions.push(format!("{}: artifact `{}`", ecosystem.name, artifact.path));
}
}
}
assert!(
omissions.is_empty(),
"the catalog matches on these, and the README's table does not say so:\n {}",
omissions.join("\n ")
);
}
#[test]
fn every_marker_the_readme_advertises_is_a_real_marker() {
let readme = include_str!("../README.md");
let rows: Vec<&str> = readme
.lines()
.skip_while(|line| !line.starts_with("| Ecosystem | Marker | Artifacts |"))
.skip(2)
.take_while(|line| line.starts_with("| "))
.collect();
for row in rows {
let mut cells = row.split('|').skip(1);
let name = cells.next().unwrap_or_default().trim();
let markers = cells.next().unwrap_or_default();
let ecosystem = CATALOG
.iter()
.find(|ecosystem| ecosystem.name == name)
.unwrap_or_else(|| panic!("`{name}` is in the README but not the catalog"));
for documented in markers.split(',') {
let documented = documented.trim().trim_matches('`');
if documented.is_empty() {
continue;
}
assert!(
ecosystem.markers.contains(&documented),
"`{name}`: the README advertises the marker `{documented}`, which the catalog \
does not declare (it has {:?})",
ecosystem.markers
);
}
}
}
fn readme_config_examples() -> Vec<String> {
let readme = include_str!("../README.md");
let mut blocks = Vec::new();
let mut current: Option<String> = None;
for line in readme.lines() {
match (&mut current, line.trim()) {
(None, "```toml") => current = Some(String::new()),
(Some(_), "```") => blocks.push(current.take().unwrap_or_default()),
(Some(block), _) => {
block.push_str(line);
block.push('\n');
}
(None, _) => {}
}
}
blocks.retain(|block| !block.contains("[[hooks."));
blocks
}
#[test]
fn every_readme_configuration_example_loads() {
let examples = readme_config_examples();
assert!(!examples.is_empty(), "the README documents configuration somewhere");
for example in examples {
let fixture = TempDir::new().expect("a temp dir");
fs::write(fixture.path().join("voom.toml"), &example).expect("a config file");
let options = RunOptions {
dry_run: true,
..support::options(fixture.path())
};
if let Err(error) = run(&options) {
panic!("a README example does not load: {error}\n\n{example}");
}
}
}
#[test]
fn every_top_level_key_the_readme_documents_is_accepted() {
for key in ["exclude", "include"] {
let fixture = TempDir::new().expect("a temp dir");
fs::write(fixture.path().join("voom.toml"), format!("{key} = []\n")).expect("a config file");
let options = RunOptions {
dry_run: true,
..support::options(fixture.path())
};
assert!(run(&options).is_ok(), "`{key}` is documented but not accepted");
}
}
fn source_files(dir: &std::path::Path, found: &mut Vec<(std::path::PathBuf, usize)>) {
let entries = fs::read_dir(dir).expect("src/ is readable");
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
source_files(&path, found);
} else if path.extension().is_some_and(|extension| extension == "rs") {
let lines = fs::read_to_string(&path).expect("a source file").lines().count();
found.push((path, lines));
}
}
}
#[test]
fn no_source_file_exceeds_the_module_size_cap() {
const CAP: usize = 1000;
let mut files = Vec::new();
source_files(std::path::Path::new("src"), &mut files);
assert!(files.len() > 10, "the walk found almost nothing — is the path right?");
let over: Vec<String> = files
.iter()
.filter(|(_, lines)| *lines > CAP)
.map(|(path, lines)| format!("{} is {lines} lines", path.display()))
.collect();
assert!(
over.is_empty(),
"over the {CAP}-line cap:\n {}\n\nSplit along a seam that already exists — the catalog \
splits by ecosystem group, the reporter by output format, config by load/merge/resolve. \
If no seam presents itself, that is the signal the module is doing two things.",
over.join("\n ")
);
}
fn states_an_ecosystem_count(line: &str) -> bool {
line.match_indices("ecosystem").any(|(index, _)| {
let before = line[..index].trim_end();
let before = before.strip_suffix("language").map_or(before, str::trim_end);
let subject = before.trim_end_matches(|character: char| character.is_ascii_digit());
subject.len() != before.len() && !subject.trim_end().ends_with("holding")
})
}
fn hard_coded_ecosystem_counts() -> (Vec<(std::path::PathBuf, String)>, usize) {
const SKIP: [&str; 4] = ["target", ".git", "node_modules", "__pycache__"];
const EXEMPT: [&str; 3] = ["./CHANGELOG.md", "./adrs", "./tests/docs.rs"];
fn walk(dir: &std::path::Path, found: &mut Vec<(std::path::PathBuf, String)>, scanned: &mut usize) {
let Ok(entries) = fs::read_dir(dir) else { return };
for entry in entries.flatten() {
let path = entry.path();
let name = entry.file_name();
if SKIP.iter().any(|skip| name == *skip) || EXEMPT.iter().any(|exempt| path == std::path::Path::new(exempt))
{
continue;
}
if path.is_dir() {
walk(&path, found, scanned);
continue;
}
let Ok(text) = fs::read_to_string(&path) else { continue };
*scanned += 1;
for line in text.lines() {
if states_an_ecosystem_count(line) {
found.push((path.clone(), line.trim().to_owned()));
}
}
}
}
let mut found = Vec::new();
let mut scanned = 0;
walk(std::path::Path::new("."), &mut found, &mut scanned);
(found, scanned)
}
#[test]
fn no_surface_hard_codes_an_ecosystem_count() {
assert!(
states_an_ecosystem_count("build-artifact pruning across 24 language ecosystems"),
"the matcher no longer recognises the phrasing it exists to forbid"
);
assert!(
states_an_ecosystem_count("- **24 ecosystems** — Rust, Node, Python"),
"the matcher no longer recognises a bare count before the noun"
);
assert!(
!states_an_ecosystem_count("build-artifact pruning across every major language ecosystem"),
"the matcher flags countless prose, so it would fail no matter what anyone wrote"
);
assert!(
!states_an_ecosystem_count("a 130 GB `~/workspace` holding 16 ecosystems, and a `$HOME` above it"),
"the matcher flags the benchmark corpus, which counts a fixture tree and not what voom supports"
);
let (found, scanned) = hard_coded_ecosystem_counts();
assert!(
scanned > 50,
"the walk read only {scanned} text files — is the path right?"
);
let stated: Vec<String> = found
.iter()
.map(|(path, line)| format!("{}: {line}", path.display()))
.collect();
assert!(
stated.is_empty(),
"the number of ecosystems is written out by hand here:\n {}\n\nSay it without a count. \
`voom catalog` prints the catalog and the README's table is checked against it, so those \
two are the only places the number belongs.",
stated.join("\n ")
);
}
#[test]
fn every_flag_the_cli_accepts_is_documented() {
use clap::CommandFactory;
let readme = include_str!("../README.md");
let command = voom::cli::Cli::command();
let mut missing = Vec::new();
collect_undocumented_flags(&command, readme, &mut missing);
assert!(
missing.is_empty(),
"the CLI accepts these and the README never mentions them:\n {}",
missing.join("\n ")
);
}
#[test]
fn every_subcommand_is_documented() {
use clap::CommandFactory;
let readme = include_str!("../README.md");
let missing: Vec<String> = voom::cli::Cli::command()
.get_subcommands()
.map(clap::Command::get_name)
.filter(|name| !readme.contains(&format!("voom {name}")))
.map(str::to_owned)
.collect();
assert!(missing.is_empty(), "subcommands absent from the README: {missing:?}");
}
fn collect_undocumented_flags(command: &clap::Command, readme: &str, missing: &mut Vec<String>) {
let scope = command.get_name().to_owned();
for argument in command.get_arguments() {
if matches!(argument.get_id().as_str(), "help" | "version") {
continue;
}
let Some(long) = argument.get_long() else {
continue;
};
if !readme.contains(&format!("--{long}")) {
missing.push(format!("{scope}: --{long}"));
}
}
for subcommand in command.get_subcommands() {
collect_undocumented_flags(subcommand, readme, missing);
}
}
#[test]
fn every_cache_id_is_documented() {
let readme = include_str!("../README.md");
let missing: Vec<&str> = voom::caches::CACHES
.iter()
.map(|cache| cache.id)
.filter(|id| !readme.contains(*id))
.collect();
assert!(missing.is_empty(), "cache ids absent from the README: {missing:?}");
}