Skip to main content

max / makenotwork

8.5 KB · 222 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 original 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 ///
37 /// # 2 is the floor, and it is not a to-do
38 ///
39 /// The two files left are `routes/stripe/mod.rs` and `db/synckit/mod.rs`. Both
40 /// are declaration-only: `mod` and `pub use`, plus a route table. Neither holds
41 /// behaviour a test could observe that is not already observed by testing the
42 /// thing it declares, and a test written to move this number would be theatre
43 /// inside a seal whose only value is that it is dumb and honest.
44 ///
45 /// Excluding them by rule was considered and declined. The rule is one sentence
46 /// long ("how many files in these areas contain no test"), and a second concept
47 /// bolted on to spare two files costs more than it saves; a floor of 2 that says
48 /// why is more honest than a rule with a carve-out in it. So: do not drive this
49 /// to 0, and do not treat 2 as work outstanding.
50 const UNTESTED_HIGH_WATER: usize = 2;
51
52 /// Anything that moves money or decides what someone is entitled to.
53 const MONEY: &[&str] = &[
54 "src/payments/",
55 "src/routes/stripe/",
56 "src/pricing",
57 "src/tier_prices.rs",
58 "src/synckit_billing.rs",
59 "src/db/transactions/",
60 "src/db/subscriptions.rs",
61 "src/db/promo_codes.rs",
62 "src/db/fan_plus.rs",
63 "src/db/creator_tiers/",
64 "src/helpers/billing.rs",
65 ];
66
67 /// Anything that stores, serves or synchronises a creator's own data.
68 const USER_DATA: &[&str] = &[
69 "src/storage.rs",
70 "src/routes/storage/",
71 "src/db/items/",
72 "src/db/users.rs",
73 "src/db/synckit/",
74 "src/routes/synckit/",
75 "src/import/",
76 "src/db/pending_",
77 ];
78
79 #[test]
80 fn untested_money_and_data_files_do_not_increase() {
81 let mut money = Vec::new();
82 let mut data = Vec::new();
83 let subjects = declared_contract_subjects();
84
85 for path in rs_files(Path::new("src")) {
86 let rel = path.to_string_lossy().replace('\\', "/");
87 let in_money = MONEY.iter().any(|p| rel.starts_with(p));
88 let in_data = USER_DATA.iter().any(|p| rel.starts_with(p));
89 if !in_money && !in_data {
90 continue;
91 }
92 if has_test(&path) || sibling_tests_file(&path) || subjects.contains(&module_path_of(&rel))
93 {
94 continue;
95 }
96 if in_money { &mut money } else { &mut data }.push(rel);
97 }
98
99 money.sort();
100 data.sort();
101 let total = money.len() + data.len();
102
103 assert!(
104 total <= UNTESTED_HIGH_WATER,
105 "untested money/data files rose from {UNTESTED_HIGH_WATER} to {total} \
106 (money {}, data {}).\n\
107 A new file on these paths needs a test before it lands. If the logic is \
108 async and database-bound, that is a contract test in \
109 `tests/workflows/db_*.rs` whose header reads \"contract tests for \
110 `your::module`\", not a unit test.\n\
111 Money: {money:#?}\nUser data: {data:#?}",
112 money.len(),
113 data.len(),
114 );
115
116 assert_eq!(
117 total, UNTESTED_HIGH_WATER,
118 "untested money/data files fell to {total}. Lower UNTESTED_HIGH_WATER to {total}.",
119 );
120 }
121
122 /// Whether the file carries any test of its own.
123 ///
124 /// Matches the attribute rather than `#[cfg(test)]`, because a `#[cfg(test)]`
125 /// block is not a test. `db/subscriptions.rs` held 811 lines of subscription
126 /// logic behind a `#[cfg(test)]` that contained one test-only constructor and
127 /// no test, and counting modules instead of tests is what hid it.
128 fn has_test(path: &Path) -> bool {
129 let Ok(text) = fs::read_to_string(path) else {
130 return false;
131 };
132 text.contains("#[test]") || text.contains("#[tokio::test") || text.contains("#[sqlx::test")
133 }
134
135 /// Whether the file's own directory has a `tests.rs` covering it.
136 ///
137 /// `db/creator_tiers/` keeps its 25 tests in a sibling file, which is the only
138 /// place in the crate that does. Without this, the seal would report three
139 /// well-covered files as untested and the number would stop meaning anything.
140 fn sibling_tests_file(path: &Path) -> bool {
141 path.parent()
142 .is_some_and(|dir| dir.join("tests.rs").exists())
143 }
144
145 /// Every module named as the subject of a contract-test file under `tests/`.
146 ///
147 /// The convention is a doc header reading "contract tests for `db::foo::bar`",
148 /// which twenty-odd files in `tests/workflows/` already follow. Crediting it
149 /// closes a hole that made this seal contradict its own advice: the failure
150 /// message tells you a database-bound module wants a contract test in `tests/`
151 /// rather than a unit test, and then the count refused to see the file you
152 /// wrote.
153 ///
154 /// Deliberately as dumb as the rest of the seal: a declared subject, not an
155 /// inferred one. A test file that does not say what it covers is not credited,
156 /// which keeps the rule cheap to state and hard to satisfy by accident.
157 fn declared_contract_subjects() -> HashSet<String> {
158 let mut subjects = HashSet::new();
159 for path in rs_files(Path::new("tests")) {
160 let Ok(text) = fs::read_to_string(&path) else {
161 continue;
162 };
163 let header: String = text
164 .lines()
165 .take_while(|l| l.starts_with("//!") || l.trim().is_empty())
166 .collect::<Vec<_>>()
167 .join(" ");
168 if !header.contains("contract tests for") {
169 continue;
170 }
171 // Every backticked path in the header, so a file covering several
172 // modules ("`db::tips`, `db::license_keys`, `db::pending_refunds`")
173 // credits all of them.
174 //
175 // Underscores are part of a module name, not a separator: without them
176 // `db::pending_refunds` reads as prose and goes uncredited, which is
177 // how the first cut of this seal landed on 35 instead of 34.
178 for chunk in header.split('`').skip(1).step_by(2) {
179 let path_shaped = chunk.contains("::")
180 && !chunk.ends_with(':')
181 && chunk
182 .chars()
183 .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == ':');
184 if path_shaped {
185 subjects.insert(chunk.to_string());
186 }
187 }
188 }
189 subjects
190 }
191
192 /// The module path a source file defines, as a contract-test header would spell
193 /// it: `src/db/synckit/invitations.rs` -> `db::synckit::invitations`, and a
194 /// `mod.rs` names its directory rather than itself.
195 fn module_path_of(rel: &str) -> String {
196 rel.trim_start_matches("src/")
197 .trim_end_matches(".rs")
198 .replace('/', "::")
199 .trim_end_matches("::mod")
200 .to_string()
201 }
202
203 fn rs_files(dir: &Path) -> Vec<PathBuf> {
204 let mut out = Vec::new();
205 let mut stack = vec![dir.to_path_buf()];
206 while let Some(d) = stack.pop() {
207 let Ok(entries) = fs::read_dir(&d) else {
208 continue;
209 };
210 for entry in entries.flatten() {
211 let p = entry.path();
212 if p.is_dir() {
213 stack.push(p);
214 } else if p.extension().is_some_and(|e| e == "rs") {
215 out.push(p);
216 }
217 }
218 }
219 out.sort();
220 out
221 }
222