//! NO TEST STAMPS A FILE AT OR ABOVE THIS BUILD'S VERSION WITH A FIXED NUMBER. //! //! "A file from a newer build" is written as `SCHEMA_VERSION + 1`. The client's //! bootstrap and replica tests once stamped the fixed number 45 when the //! ladder stood at 44; the ladder reached 45 and the "newer" file became this //! build's own, so the tests kept passing while testing nothing they claimed — //! and at 46 they would have quietly become tests of an OLDER file. //! //! A literal below the top rung names a real past version and stays true. A //! literal at or above it is a claim about the future that the next rung //! breaks, so this sweep refuses it anywhere under `crates/`. use std::fs; use std::path::{Path, PathBuf}; use cotton_store::schema::ddl::SCHEMA_VERSION; /// `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); } } } /// Every number a line stamps with `user_version = N`, in any spelling SQLite /// takes (`PRAGMA user_version = 12`, `user_version=12`). fn stamped_numbers(line: &str) -> Vec { let mut found = Vec::new(); let mut rest = line; while let Some((_, after)) = rest.split_once("user_version") { let tail = after.trim_start(); if let Some(value) = tail.strip_prefix('=') { let digits: String = value .trim_start() .chars() .take_while(char::is_ascii_digit) .collect(); if let Ok(n) = digits.parse() { found.push(n); } } rest = after; } found } /// RULE: A test stamps a file from a newer build as SCHEMA_VERSION + 1, never a fixed number the next rung makes stale. #[test] fn no_file_is_stamped_at_or_above_this_build_with_a_fixed_number() { let crates = crates_root(); let mut files = Vec::new(); walk(&crates, &mut files); assert!( files.len() > 100, "the sweep found {} Rust files under {} — it is not reading the workspace", files.len(), crates.display() ); let mut stale = Vec::new(); for file in &files { let text = fs::read_to_string(file).expect("a source file"); for (index, line) in text.lines().enumerate() { for n in stamped_numbers(line) { if n >= SCHEMA_VERSION { stale.push(format!("{}:{}: {}", file.display(), index + 1, line.trim())); } } } } assert!( stale.is_empty(), "these stamp a fixed version at or above this build's {SCHEMA_VERSION}; write \ `SCHEMA_VERSION + 1` for a newer build's file, or a real past version:\n{}", stale.join("\n") ); } #[test] fn the_stamp_reader_reads_every_spelling() { // A sweep whose parser stopped parsing passes over everything, so the // shapes it looks for are pinned here rather than assumed. assert_eq!( stamped_numbers(r#"db.exec("PRAGMA user_version = 12")"#), vec![12] ); assert_eq!(stamped_numbers("PRAGMA user_version=7;"), vec![7]); assert_eq!( stamped_numbers(r#"format!("PRAGMA user_version = {}", SCHEMA_VERSION + 1)"#), Vec::::new() ); assert_eq!( stamped_numbers("let user_version = read();"), Vec::::new() ); }