diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/approve.rs | 2 | ||||
| -rw-r--r-- | tests/common/mod.rs | 4 | ||||
| -rw-r--r-- | tests/db.rs | 4 | ||||
| -rw-r--r-- | tests/invariants.rs | 4 | ||||
| -rw-r--r-- | tests/schema_ownership.rs | 88 |
5 files changed, 95 insertions, 7 deletions
diff --git a/tests/approve.rs b/tests/approve.rs index fb894f1..5a515eb 100644 --- a/tests/approve.rs +++ b/tests/approve.rs @@ -45,7 +45,7 @@ fn approve_writes_an_immutable_row_carrying_both_hashes() { let err = env .db .writer - .tx(move |tx| { + .tx_for_tests(move |tx| { tx.execute("UPDATE approval SET note='rewritten' WHERE id=1", [])?; Ok(()) }) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 67c5592..de5f87e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -64,7 +64,7 @@ impl Env { let params: Vec<P> = params.to_vec(); self.db .writer - .tx(move |tx| { + .tx_for_tests(move |tx| { let refs: Vec<&dyn rusqlite::ToSql> = params .iter() .map(|p| match p { @@ -84,7 +84,7 @@ impl Env { let at = "2026-01-01T00:00:00Z"; self.db .writer - .tx(move |tx| { + .tx_for_tests(move |tx| { tx.execute( "INSERT INTO approval (candidate_id, base_release_id, decided_at, decision, risk_level, diff_digest, identity_hash) diff --git a/tests/db.rs b/tests/db.rs index e2608a1..75977d1 100644 --- a/tests/db.rs +++ b/tests/db.rs @@ -125,7 +125,7 @@ fn accepted_release_pointer_is_compared_and_swapped() { let rel = env .db .writer - .tx(move |tx| { + .tx_for_tests(move |tx| { tx.execute( "INSERT INTO release (pkgbase_id, aur_commit, recipe_hash, pkgver, pkgrel, state, created_at) VALUES (?1,'aaaa','rh','1.0','1','published','2026-01-01T00:00:00Z')", @@ -222,7 +222,7 @@ fn i1_query_flags_index_entries_without_approval() { let rel = env .db .writer - .tx(move |tx| { + .tx_for_tests(move |tx| { tx.execute( "INSERT INTO release (pkgbase_id, aur_commit, recipe_hash, pkgver, pkgrel, state, created_at) VALUES (?1,'aaaa','rh','1.0','1','published','2026-01-01T00:00:00Z')", diff --git a/tests/invariants.rs b/tests/invariants.rs index 8234817..a083a89 100644 --- a/tests/invariants.rs +++ b/tests/invariants.rs @@ -208,7 +208,7 @@ fn i7_the_schema_refuses_a_second_open_review() { let err = env .db .writer - .tx(move |tx| { + .tx_for_tests(move |tx| { tx.execute( "INSERT INTO candidate (pkgbase_id, kind, snapshot_id, epoch, pkgver, pkgrel, upstream_pins_json, source_digests_json, identity_hash, @@ -316,7 +316,7 @@ fn i10_audit_is_append_only_and_records_the_decisions() { let s = sql.to_string(); assert!( db.writer - .tx(move |tx| { + .tx_for_tests(move |tx| { tx.execute(&s, [])?; Ok(()) }) diff --git a/tests/schema_ownership.rs b/tests/schema_ownership.rs new file mode 100644 index 0000000..e2904f4 --- /dev/null +++ b/tests/schema_ownership.rs @@ -0,0 +1,88 @@ +//! The schema has one owner (§6 M-1). +//! +//! `db/` is a boundary, not a directory, only if SQL cannot land anywhere else. +//! Part of that is a compile-time fact: `Writer::call`, `tx` and `send` are +//! `pub(crate)`, and the raw door the test harness uses exists only under the +//! `test-support` feature. What the compiler cannot express is the other half — +//! that no module *inside* this crate writes a statement of its own. This does. +//! +//! The rule it enforces: a SQL statement, or a rusqlite statement call, appears +//! in `src/db/` and nowhere else. A module that needs the database asks `db/` +//! for a typed query; if the query does not exist, that is the change to make. + +use std::path::{Path, PathBuf}; + +/// String literals that begin a statement. Matched on the quote so a prose +/// comment that mentions a `SELECT` cannot trip it, and comment lines are +/// skipped anyway. +const SQL_LITERALS: &[&str] = &[ + "\"SELECT", "\"select", "\"INSERT", "\"insert", "\"UPDATE", "\"update", "\"DELETE", "\"delete", + "\"PRAGMA", "\"WITH", +]; + +/// rusqlite entry points. Outside `db/` there is nothing legitimate to hand +/// them, since every statement string is supposed to live in `db/`. +const STATEMENT_CALLS: &[&str] = &[ + ".query_row(", ".query_map(", ".execute(", ".execute_batch(", ".prepare(", ".pragma_query_value(", +]; + +fn rust_files(dir: &Path, out: &mut Vec<PathBuf>) { + for entry in std::fs::read_dir(dir).expect("read_dir") { + let path = entry.expect("entry").path(); + if path.is_dir() { + rust_files(&path, out); + } else if path.extension().is_some_and(|e| e == "rs") { + out.push(path); + } + } +} + +#[test] +fn no_sql_statements_outside_the_db_module() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); + let mut files = Vec::new(); + rust_files(&root, &mut files); + assert!(files.len() > 20, "found only {} source files — the walk is wrong", files.len()); + + let mut offenders: Vec<String> = Vec::new(); + for file in files { + let rel = file.strip_path(&root); + // The owner of the schema is allowed to contain SQL. + if rel.starts_with("db/") || rel.starts_with("db\\") { + continue; + } + let text = std::fs::read_to_string(&file).expect("read source"); + for (no, line) in text.lines().enumerate() { + let t = line.trim_start(); + if t.starts_with("//") { + continue; + } + for needle in SQL_LITERALS.iter().chain(STATEMENT_CALLS.iter()) { + if line.contains(needle) { + offenders.push(format!("{}:{}: {t}", rel, no + 1)); + break; + } + } + } + } + assert!( + offenders.is_empty(), + "SQL belongs to `db/` (§6 M-1). Move these into a typed query:\n{}", + offenders.join("\n") + ); +} + +/// `src/db/queries.rs` → `db/queries.rs`, so the failure message points at the +/// module rather than an absolute path. +trait StripPath { + fn strip_path(&self, root: &Path) -> String; +} + +impl StripPath for PathBuf { + fn strip_path(&self, root: &Path) -> String { + self.strip_prefix(root) + .unwrap_or(self.as_path()) + .to_string_lossy() + .replace('\\', "/") + } +} |
