| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use std::collections::HashSet; |
| 24 |
use std::fs; |
| 25 |
use std::path::{Path, PathBuf}; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
const UNTESTED_HIGH_WATER: usize = 33; |
| 44 |
|
| 45 |
|
| 46 |
const MONEY: &[&str] = &[ |
| 47 |
"src/payments/", |
| 48 |
"src/routes/stripe/", |
| 49 |
"src/pricing", |
| 50 |
"src/tier_prices.rs", |
| 51 |
"src/synckit_billing.rs", |
| 52 |
"src/db/transactions/", |
| 53 |
"src/db/subscriptions.rs", |
| 54 |
"src/db/promo_codes.rs", |
| 55 |
"src/db/fan_plus.rs", |
| 56 |
"src/db/creator_tiers/", |
| 57 |
"src/helpers/billing.rs", |
| 58 |
]; |
| 59 |
|
| 60 |
|
| 61 |
const USER_DATA: &[&str] = &[ |
| 62 |
"src/storage.rs", |
| 63 |
"src/routes/storage/", |
| 64 |
"src/db/items/", |
| 65 |
"src/db/users.rs", |
| 66 |
"src/db/synckit/", |
| 67 |
"src/routes/synckit/", |
| 68 |
"src/import/", |
| 69 |
"src/db/pending_", |
| 70 |
]; |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn untested_money_and_data_files_do_not_increase() { |
| 74 |
let mut money = Vec::new(); |
| 75 |
let mut data = Vec::new(); |
| 76 |
let subjects = declared_contract_subjects(); |
| 77 |
|
| 78 |
for path in rs_files(Path::new("src")) { |
| 79 |
let rel = path.to_string_lossy().replace('\\', "/"); |
| 80 |
let in_money = MONEY.iter().any(|p| rel.starts_with(p)); |
| 81 |
let in_data = USER_DATA.iter().any(|p| rel.starts_with(p)); |
| 82 |
if !in_money && !in_data { |
| 83 |
continue; |
| 84 |
} |
| 85 |
if has_test(&path) || sibling_tests_file(&path) || subjects.contains(&module_path_of(&rel)) |
| 86 |
{ |
| 87 |
continue; |
| 88 |
} |
| 89 |
if in_money { &mut money } else { &mut data }.push(rel); |
| 90 |
} |
| 91 |
|
| 92 |
money.sort(); |
| 93 |
data.sort(); |
| 94 |
let total = money.len() + data.len(); |
| 95 |
|
| 96 |
assert!( |
| 97 |
total <= UNTESTED_HIGH_WATER, |
| 98 |
"untested money/data files rose from {UNTESTED_HIGH_WATER} to {total} \ |
| 99 |
(money {}, data {}).\n\ |
| 100 |
A new file on these paths needs a test before it lands. If the logic is \ |
| 101 |
async and database-bound, that is a contract test in \ |
| 102 |
`tests/workflows/db_*.rs` whose header reads \"contract tests for \ |
| 103 |
`your::module`\", not a unit test.\n\ |
| 104 |
Money: {money:#?}\nUser data: {data:#?}", |
| 105 |
money.len(), |
| 106 |
data.len(), |
| 107 |
); |
| 108 |
|
| 109 |
assert_eq!( |
| 110 |
total, UNTESTED_HIGH_WATER, |
| 111 |
"untested money/data files fell to {total}. Lower UNTESTED_HIGH_WATER to {total}.", |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
fn has_test(path: &Path) -> bool { |
| 122 |
let Ok(text) = fs::read_to_string(path) else { |
| 123 |
return false; |
| 124 |
}; |
| 125 |
text.contains("#[test]") || text.contains("#[tokio::test") || text.contains("#[sqlx::test") |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
fn sibling_tests_file(path: &Path) -> bool { |
| 134 |
path.parent() |
| 135 |
.is_some_and(|dir| dir.join("tests.rs").exists()) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
fn declared_contract_subjects() -> HashSet<String> { |
| 152 |
let mut subjects = HashSet::new(); |
| 153 |
for path in rs_files(Path::new("tests")) { |
| 154 |
let Ok(text) = fs::read_to_string(&path) else { |
| 155 |
continue; |
| 156 |
}; |
| 157 |
let header: String = text |
| 158 |
.lines() |
| 159 |
.take_while(|l| l.starts_with("//!") || l.trim().is_empty()) |
| 160 |
.collect::<Vec<_>>() |
| 161 |
.join(" "); |
| 162 |
if !header.contains("contract tests for") { |
| 163 |
continue; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
for chunk in header.split('`').skip(1).step_by(2) { |
| 173 |
let path_shaped = chunk.contains("::") |
| 174 |
&& !chunk.ends_with(':') |
| 175 |
&& chunk |
| 176 |
.chars() |
| 177 |
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == ':'); |
| 178 |
if path_shaped { |
| 179 |
subjects.insert(chunk.to_string()); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
subjects |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
fn module_path_of(rel: &str) -> String { |
| 190 |
rel.trim_start_matches("src/") |
| 191 |
.trim_end_matches(".rs") |
| 192 |
.replace('/', "::") |
| 193 |
.trim_end_matches("::mod") |
| 194 |
.to_string() |
| 195 |
} |
| 196 |
|
| 197 |
fn rs_files(dir: &Path) -> Vec<PathBuf> { |
| 198 |
let mut out = Vec::new(); |
| 199 |
let mut stack = vec![dir.to_path_buf()]; |
| 200 |
while let Some(d) = stack.pop() { |
| 201 |
let Ok(entries) = fs::read_dir(&d) else { |
| 202 |
continue; |
| 203 |
}; |
| 204 |
for entry in entries.flatten() { |
| 205 |
let p = entry.path(); |
| 206 |
if p.is_dir() { |
| 207 |
stack.push(p); |
| 208 |
} else if p.extension().is_some_and(|e| e == "rs") { |
| 209 |
out.push(p); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
out.sort(); |
| 214 |
out |
| 215 |
} |
| 216 |
|