Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/holger

holger-recipe/tests/bench_examples_build.rs

//! **Every bench example builds, because the gate builds every one.**
//!
//! `art_probe`, `pip_probe` and `retention_probe` sat in
//! `honesty::EXAMPLES_THAT_DO_NOT_BUILD` from 2026-09-20 to 2026-10-23: they
//! declared no `required-features`, so cargo built them with whatever features
//! happened to be on and they failed (`E0432 holger_bench_scenarios::retention`,
//! measured on builder-64 2026-10-23). Nothing built them, so nothing noticed.
//!
//! Two rules close that:
//! 1. every file in `bench-scenarios/examples/` has an `[[example]]` entry with
//!    `required-features`, so it builds when its features are on and is skipped
//!    cleanly when they are not;
//! 2. every such example is in [`honesty::BENCH_DRIVERS`] with exactly those
//!    features, so `cargo xtask gate` builds it.

use holger_recipe::honesty::{self, BENCH_DRIVERS};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

fn root() -> PathBuf {
    honesty::find_workspace_root(Path::new(env!("CARGO_MANIFEST_DIR")))
        .expect("holger's [workspace] Cargo.toml above holger-recipe")
}

fn declared_examples() -> BTreeMap<String, Vec<String>> {
    let text = std::fs::read_to_string(root().join("bench-scenarios/Cargo.toml")).unwrap();
    let doc: toml::Value = toml::from_str(&text).unwrap();
    doc.get("example")
        .and_then(|e| e.as_array())
        .into_iter()
        .flatten()
        .map(|e| {
            let name = e["name"].as_str().unwrap().to_string();
            let feats = e
                .get("required-features")
                .and_then(|f| f.as_array())
                .map(|a| a.iter().map(|f| f.as_str().unwrap().to_string()).collect())
                .unwrap_or_default();
            (name, feats)
        })
        .collect()
}

#[test]
fn every_bench_scenarios_example_declares_its_features() {
    let declared = declared_examples();
    let mut files: Vec<String> = std::fs::read_dir(root().join("bench-scenarios/examples"))
        .unwrap()
        .filter_map(|e| e.ok())
        .filter_map(|e| {
            e.path()
                .file_stem()
                .map(|s| s.to_string_lossy().into_owned())
        })
        .collect();
    files.sort();
    for f in &files {
        let feats = declared
            .get(f)
            .unwrap_or_else(|| panic!("bench-scenarios/examples/{f}.rs has no [[example]] entry"));
        assert!(
            !feats.is_empty(),
            "the example {f} declares no required-features"
        );
    }
}

#[test]
fn every_bench_scenarios_example_is_a_gated_bench_driver() {
    for (name, feats) in declared_examples() {
        let row = BENCH_DRIVERS
            .iter()
            .find(|(pkg, ex, _)| *pkg == "holger-bench-scenarios" && *ex == name)
            .unwrap_or_else(|| panic!("the example {name} is not in honesty::BENCH_DRIVERS, so the gate never builds it"));
        let mut want: Vec<&str> = feats.iter().map(String::as_str).collect();
        let mut got: Vec<&str> = row.2.to_vec();
        want.sort();
        got.sort();
        assert_eq!(got, want, "BENCH_DRIVERS names other features for {name}");
    }
    assert!(
        honesty::EXAMPLES_THAT_DO_NOT_BUILD.is_empty(),
        "an example that does not build is a red gate, not a list entry"
    );
}