Fails when the committed Cargo manifests differ from what tools/cargo-gen writes from the current BUCK files. Fix with buck2 run //tools/cargo-gen:update.

5use std::collections::BTreeMap;
6use std::path::Path;
8fn files(base: &Path, dir: &Path, out: &mut BTreeMap<String, String>) {
9    for entry in std::fs::read_dir(dir).unwrap() {
10        let path = entry.unwrap().path();
11        if path.is_dir() {
12            files(base, &path, out);
13        } else {
14            let rel = path.strip_prefix(base).unwrap().display().to_string();
15            out.insert(rel, std::fs::read_to_string(&path).unwrap());
16        }
17    }
18}
19
20#[test]
21fn committed_manifests_match_the_buck_graph() {
22    let (mut generated, mut committed) = (BTreeMap::new(), BTreeMap::new());
23    let g = std::env::var("GENERATED").unwrap();
24    let c = std::env::var("COMMITTED").unwrap();
25    files(Path::new(&g), Path::new(&g), &mut generated);
26    files(Path::new(&c), Path::new(&c), &mut committed);
27    let mut drift = Vec::new();
28    for (path, text) in &generated {
29        match committed.get(path) {
30            None => drift.push(format!("missing: {path}")),
31            Some(t) if t != text => drift.push(format!("stale: {path}\n--- expected ---\n{text}")),
32            Some(_) => {}
33        }
34    }
35    for path in committed.keys().filter(|p| !generated.contains_key(*p)) {
36        drift.push(format!("not generated (remove it): {path}"));
37    }
38    assert!(
39        drift.is_empty(),
40        "Cargo manifests are out of date; run `buck2 run //tools/cargo-gen:update`.\n{}",
41        drift.join("\n")
42    );
43}