#![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 ecosystem_count_claims() -> Vec<(std::path::PathBuf, String)> {
const PHRASE: &str = " language ecosystems";
const SKIP: [&str; 4] = ["target", ".git", "node_modules", "__pycache__"];
fn walk(dir: &std::path::Path, found: &mut Vec<(std::path::PathBuf, String)>) {
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) {
continue;
}
if path.is_dir() {
walk(&path, found);
continue;
}
let Ok(text) = fs::read_to_string(&path) else { continue };
for line in text.lines() {
for (index, _) in line.match_indices(PHRASE) {
let before = &line[..index];
if let Some(claim) = before.rsplit(' ').next()
&& before.trim_end().ends_with(&format!("across {claim}"))
{
found.push((path.clone(), claim.to_owned()));
}
}
}
}
}
let mut found = Vec::new();
walk(std::path::Path::new("."), &mut found);
found
}
#[test]
fn every_stated_ecosystem_count_matches_the_catalog() {
let claims = ecosystem_count_claims();
assert!(
claims.len() >= 8,
"the description is stated in many places; the walk found only {} — is the phrase right?",
claims.len()
);
let expected = CATALOG.len().to_string();
let stale: Vec<String> = claims
.iter()
.filter(|(_, claimed)| *claimed != expected)
.map(|(path, claimed)| format!("{} says {claimed}", path.display()))
.collect();
assert!(
stale.is_empty(),
"the catalog ships {expected} ecosystems, and these disagree:\n {}",
stale.join("\n ")
);
}