//! NO FILE HERE CLAIMS A GENERATOR THAT IS NOT IN THE TREE. //! //! `scripts/rust-gen.ts` wrote nine files in this workspace and stamped each of //! them "GENERATED by `scripts/rust-gen.ts` … do not edit by hand". Step 5i //! deleted the generator (design R2) and those files became ordinary source. //! //! A header that still names a script nobody can run is worse than no header: //! it tells the next reader their edit will be overwritten, so they do not make //! it, and the file quietly stops being maintained by anyone. This is the guard //! R2 asks for — it does not forbid the words "generated by", it requires that //! the thing they name still exists. //! //! It walks from `crates/`, which is this crate's manifest directory's parent, //! and the repo root is one above that. Both reads are asserted before any //! comparison, so a move fails HERE, loudly, rather than sweeping an empty tree. use std::fs; use std::path::{Path, PathBuf}; /// `crates/` — the workspace this sweeps. fn crates_root() -> PathBuf { cotton_store::manifest_dir().join("..") } fn walk(dir: &Path, out: &mut Vec) { let Ok(entries) = fs::read_dir(dir) else { return }; for entry in entries.flatten() { let path = entry.path(); let name = entry.file_name(); let name = name.to_string_lossy(); if name == "target" || name == "node_modules" || name.starts_with('.') { continue; } if path.is_dir() { walk(&path, out); } else if name.ends_with(".rs") { out.push(path); } } } /// The SCRIPT a "GENERATED by `…`" claim names, if the line names one. /// /// A claim has to look like a file for this to judge it: a repo-relative path /// with an extension. "GENERATED by `cargo test -p cotton-core --lib /// language::emit`" names a COMMAND, and `cotton-gen`'s own renderer writes a /// `{GENERATED_BY}` placeholder — neither is a path, and asking whether either /// exists on disk is a question with no answer. fn claimed_generator(line: &str) -> Option<&str> { let rest = line.split_once("GENERATED by ")?.1; let rest = rest.strip_prefix('`')?; let (path, _) = rest.split_once('`')?; let looks_like_a_file = path.contains('/') && !path.contains(' ') && matches!( path.rsplit_once('.').map(|(_, ext)| ext), Some("ts" | "tsx" | "js" | "mjs" | "sh" | "rs") ); looks_like_a_file.then_some(path) } #[test] fn no_file_claims_a_generator_that_does_not_exist() { let crates = crates_root(); assert!( crates.join("cotton-store").is_dir(), "the sweep's root is not the workspace — {} has no cotton-store in it", crates.display() ); let repo_root = crates.join(".."); let mut files = Vec::new(); walk(&crates, &mut files); assert!( files.len() > 200, "the sweep found only {} Rust files — it is not reaching the workspace, and an empty \ sweep passes for the wrong reason", files.len() ); let mut offenders = Vec::new(); for file in &files { // THIS FILE. It names `scripts/rust-gen.ts` twice — once in its header // explaining what a dead generator claim looks like, once in the // reader's own unit test below — and those are the guard's SUBJECT, not // its provenance. Sweeping itself, it reports itself, every time, for // ever. (Found on the proof run, by this guard failing on its first // green tree.) if file.ends_with("no_dead_generator_header.rs") { continue; } let Ok(text) = fs::read_to_string(file) else { continue }; for (number, line) in text.lines().enumerate() { let Some(claim) = claimed_generator(line) else { continue }; if !repo_root.join(claim).exists() { offenders.push(format!( "{}:{} names `{claim}`, which is not in the tree", file.display(), number + 1 )); } } } assert!( offenders.is_empty(), "these files say they are generated by something that does not exist — a directory \ called `generated` with no generator is a lie that outlives the person who knew \ better, and git history is where provenance lives:\n {}", offenders.join("\n ") ); } #[test] fn the_claim_reader_reads_a_claim() { // A sweep whose parser stopped parsing passes over everything, so the shape // it is looking for is pinned here rather than assumed. assert_eq!( claimed_generator("//! GENERATED by `scripts/rust-gen.ts` from `@cottondb/store` — do not edit."), Some("scripts/rust-gen.ts") ); assert_eq!(claimed_generator("//! The schema, as one CREATE per table."), None); assert_eq!(claimed_generator("// GENERATED by hand"), None); // A COMMAND is not a path, and neither is a renderer's own placeholder. assert_eq!( claimed_generator("//! GENERATED by `cargo test -p cotton-core --lib language::emit` from x"), None ); assert_eq!(claimed_generator("// GENERATED by `{GENERATED_BY}` from `{source}`"), None); }