Skip to main content

max / makenotwork

7.7 KB · 208 lines History Blame Raw
1 //! Coverage ratchet for the two areas where being wrong is most expensive:
2 //! anything that moves money, and anything that holds a creator's data.
3 //!
4 //! The same idea as `test_hygiene.rs`, turned on the source rather than on the
5 //! tests. It counts files in those areas that contain no test of any kind and
6 //! fails when the number goes up. It cannot make anyone write a good test; it
7 //! can stop a new payment handler or sync table from landing with none at all,
8 //! which is how the current 34 accumulated, one reasonable-looking file at a
9 //! time.
10 //!
11 //! Deliberately dumb. A file counts as covered if it holds a test attribute, if
12 //! its directory has a sibling `tests.rs`, or if a file under `tests/` names it
13 //! as the subject of a contract test. No coverage instrumentation, no AST walk:
14 //! a rule cheap enough to state as
15 //! "how many files look like this" is a rule that stays honest, and mutation
16 //! testing (`.cargo/mutants.toml`) is where the harder question of whether the
17 //! tests are any *good* gets asked.
18 //!
19 //! Method, tiers and the full file list: wiki `testing-posture`.
20 //!
21 //! Run with: cargo test --test untested_money_paths
22
23 use std::collections::HashSet;
24 use std::fs;
25 use std::path::{Path, PathBuf};
26
27 /// Money and user-data files with no test at all.
28 ///
29 /// A contract file's header must name the file it covers, not only its
30 /// directory module, or the subject credit stops at the module and the file
31 /// reads as untested.
32 ///
33 /// Lower it when you cover one. Never raise it: a new untested file in these
34 /// areas is the thing this seal exists to refuse. If you genuinely need to add
35 /// one, the honest move is to write the test, not to bump the constant.
36 const UNTESTED_HIGH_WATER: usize = 32;
37
38 /// Anything that moves money or decides what someone is entitled to.
39 const MONEY: &[&str] = &[
40 "src/payments/",
41 "src/routes/stripe/",
42 "src/pricing",
43 "src/tier_prices.rs",
44 "src/synckit_billing.rs",
45 "src/db/transactions/",
46 "src/db/subscriptions.rs",
47 "src/db/promo_codes.rs",
48 "src/db/fan_plus.rs",
49 "src/db/creator_tiers/",
50 "src/helpers/billing.rs",
51 ];
52
53 /// Anything that stores, serves or synchronises a creator's own data.
54 const USER_DATA: &[&str] = &[
55 "src/storage.rs",
56 "src/routes/storage/",
57 "src/db/items/",
58 "src/db/users.rs",
59 "src/db/synckit/",
60 "src/routes/synckit/",
61 "src/import/",
62 "src/db/pending_",
63 ];
64
65 #[test]
66 fn untested_money_and_data_files_do_not_increase() {
67 let mut money = Vec::new();
68 let mut data = Vec::new();
69 let subjects = declared_contract_subjects();
70
71 for path in rs_files(Path::new("src")) {
72 let rel = path.to_string_lossy().replace('\\', "/");
73 let in_money = MONEY.iter().any(|p| rel.starts_with(p));
74 let in_data = USER_DATA.iter().any(|p| rel.starts_with(p));
75 if !in_money && !in_data {
76 continue;
77 }
78 if has_test(&path) || sibling_tests_file(&path) || subjects.contains(&module_path_of(&rel))
79 {
80 continue;
81 }
82 if in_money { &mut money } else { &mut data }.push(rel);
83 }
84
85 money.sort();
86 data.sort();
87 let total = money.len() + data.len();
88
89 assert!(
90 total <= UNTESTED_HIGH_WATER,
91 "untested money/data files rose from {UNTESTED_HIGH_WATER} to {total} \
92 (money {}, data {}).\n\
93 A new file on these paths needs a test before it lands. If the logic is \
94 async and database-bound, that is a contract test in \
95 `tests/workflows/db_*.rs` whose header reads \"contract tests for \
96 `your::module`\", not a unit test.\n\
97 Money: {money:#?}\nUser data: {data:#?}",
98 money.len(),
99 data.len(),
100 );
101
102 assert_eq!(
103 total, UNTESTED_HIGH_WATER,
104 "untested money/data files fell to {total}. Lower UNTESTED_HIGH_WATER to {total}.",
105 );
106 }
107
108 /// Whether the file carries any test of its own.
109 ///
110 /// Matches the attribute rather than `#[cfg(test)]`, because a `#[cfg(test)]`
111 /// block is not a test. `db/subscriptions.rs` held 811 lines of subscription
112 /// logic behind a `#[cfg(test)]` that contained one test-only constructor and
113 /// no test, and counting modules instead of tests is what hid it.
114 fn has_test(path: &Path) -> bool {
115 let Ok(text) = fs::read_to_string(path) else {
116 return false;
117 };
118 text.contains("#[test]") || text.contains("#[tokio::test") || text.contains("#[sqlx::test")
119 }
120
121 /// Whether the file's own directory has a `tests.rs` covering it.
122 ///
123 /// `db/creator_tiers/` keeps its 25 tests in a sibling file, which is the only
124 /// place in the crate that does. Without this, the seal would report three
125 /// well-covered files as untested and the number would stop meaning anything.
126 fn sibling_tests_file(path: &Path) -> bool {
127 path.parent()
128 .is_some_and(|dir| dir.join("tests.rs").exists())
129 }
130
131 /// Every module named as the subject of a contract-test file under `tests/`.
132 ///
133 /// The convention is a doc header reading "contract tests for `db::foo::bar`",
134 /// which twenty-odd files in `tests/workflows/` already follow. Crediting it
135 /// closes a hole that made this seal contradict its own advice: the failure
136 /// message tells you a database-bound module wants a contract test in `tests/`
137 /// rather than a unit test, and then the count refused to see the file you
138 /// wrote.
139 ///
140 /// Deliberately as dumb as the rest of the seal: a declared subject, not an
141 /// inferred one. A test file that does not say what it covers is not credited,
142 /// which keeps the rule cheap to state and hard to satisfy by accident.
143 fn declared_contract_subjects() -> HashSet<String> {
144 let mut subjects = HashSet::new();
145 for path in rs_files(Path::new("tests")) {
146 let Ok(text) = fs::read_to_string(&path) else {
147 continue;
148 };
149 let header: String = text
150 .lines()
151 .take_while(|l| l.starts_with("//!") || l.trim().is_empty())
152 .collect::<Vec<_>>()
153 .join(" ");
154 if !header.contains("contract tests for") {
155 continue;
156 }
157 // Every backticked path in the header, so a file covering several
158 // modules ("`db::tips`, `db::license_keys`, `db::pending_refunds`")
159 // credits all of them.
160 //
161 // Underscores are part of a module name, not a separator: without them
162 // `db::pending_refunds` reads as prose and goes uncredited, which is
163 // how the first cut of this seal landed on 35 instead of 34.
164 for chunk in header.split('`').skip(1).step_by(2) {
165 let path_shaped = chunk.contains("::")
166 && !chunk.ends_with(':')
167 && chunk
168 .chars()
169 .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == ':');
170 if path_shaped {
171 subjects.insert(chunk.to_string());
172 }
173 }
174 }
175 subjects
176 }
177
178 /// The module path a source file defines, as a contract-test header would spell
179 /// it: `src/db/synckit/invitations.rs` -> `db::synckit::invitations`, and a
180 /// `mod.rs` names its directory rather than itself.
181 fn module_path_of(rel: &str) -> String {
182 rel.trim_start_matches("src/")
183 .trim_end_matches(".rs")
184 .replace('/', "::")
185 .trim_end_matches("::mod")
186 .to_string()
187 }
188
189 fn rs_files(dir: &Path) -> Vec<PathBuf> {
190 let mut out = Vec::new();
191 let mut stack = vec![dir.to_path_buf()];
192 while let Some(d) = stack.pop() {
193 let Ok(entries) = fs::read_dir(&d) else {
194 continue;
195 };
196 for entry in entries.flatten() {
197 let p = entry.path();
198 if p.is_dir() {
199 stack.push(p);
200 } else if p.extension().is_some_and(|e| e == "rs") {
201 out.push(p);
202 }
203 }
204 }
205 out.sort();
206 out
207 }
208