
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1209 · main · 10 min 03 s
1Étoiles
0Forks
0Issues
| 1 | //! UI tests: `tests/ui/*.lse` files with inline expectations. |
| 2 | //! |
| 3 | //! - `//~ ERROR E0202 fragment` expects a diagnostic with that code (and a |
| 4 | //! message or a note containing `fragment`, if given) whose primary span |
| 5 | //! starts on this line. `//~^ ERROR ...` points one line up (`^^` two lines, ...). |
| 6 | //! - `//~ WARN W0101` is the same for warnings. |
| 7 | //! - `//~ TYPE name: scheme` expects the top-level item `name` to have |
| 8 | //! this type, as printed in hovers. |
| 9 | //! |
| 10 | //! Every diagnostic must be expected, and every expectation met. The file |
| 11 | //! is checked as module `main` with the real standard library, through |
| 12 | //! the same pipeline as `louise check` and the language server. |
| 13 | |
| 14 | use std::path::{Path, PathBuf}; |
| 15 | |
| 16 | use louise_db::Db; |
| 17 | use louise_diag::{Diagnostic, Severity}; |
| 18 | use louise_types::Printer; |
| 19 | |
| 20 | struct Expectation { |
| 21 | line: u32, |
| 22 | severity: Severity, |
| 23 | code: String, |
| 24 | fragment: String, |
| 25 | } |
| 26 | |
| 27 | fn parse_expectations(text: &str) -> (Vec<Expectation>, Vec<(String, String)>) { |
| 28 | let mut diags = Vec::new(); |
| 29 | let mut types = Vec::new(); |
| 30 | for (i, line) in text.lines().enumerate() { |
| 31 | let Some((_, rest)) = line.split_once("//~") else { |
| 32 | continue; |
| 33 | }; |
| 34 | let carets = rest.chars().take_while(|c| *c == '^').count(); |
| 35 | let rest = rest[carets..].trim(); |
| 36 | if let Some(t) = rest.strip_prefix("TYPE ") { |
| 37 | let (name, ty) = t.split_once(':').expect("`//~ TYPE name: type`"); |
| 38 | types.push((name.trim().to_owned(), ty.trim().to_owned())); |
| 39 | continue; |
| 40 | } |
| 41 | let (severity, rest) = if let Some(r) = rest.strip_prefix("ERROR ") { |
| 42 | (Severity::Error, r) |
| 43 | } else if let Some(r) = rest.strip_prefix("WARN ") { |
| 44 | (Severity::Warning, r) |
| 45 | } else { |
| 46 | panic!("line {}: unknown expectation `{rest}`", i + 1); |
| 47 | }; |
| 48 | let (code, fragment) = rest.split_once(' ').unwrap_or((rest, "")); |
| 49 | diags.push(Expectation { |
| 50 | line: (i - carets) as u32, |
| 51 | severity, |
| 52 | code: code.to_owned(), |
| 53 | fragment: fragment.trim().to_owned(), |
| 54 | }); |
| 55 | } |
| 56 | (diags, types) |
| 57 | } |
| 58 | |
| 59 | fn run(path: &Path) -> Vec<String> { |
| 60 | let text = std::fs::read_to_string(path).unwrap(); |
| 61 | let mut db = Db::new(); |
| 62 | let main_file = db.add_module("main", &path.display().to_string(), text.clone()); |
| 63 | let mut analysis = db.analyze(); |
| 64 | if !analysis.diagnostics.iter().any(Diagnostic::is_error) { |
| 65 | let semantic = louise_db::semantic_diagnostics(&db, &analysis); |
| 66 | analysis.diagnostics.extend(semantic); |
| 67 | } |
| 68 | let (hir, typeck, diags) = (&analysis.hir, &analysis.typeck, &analysis.diagnostics); |
| 69 | |
| 70 | let (mut expected, types) = parse_expectations(&text); |
| 71 | let mut failures = Vec::new(); |
| 72 | for d in diags { |
| 73 | let file = db.sources().get(d.primary.span.file); |
| 74 | let line = file.line_col(d.primary.span.range.start()).line; |
| 75 | if d.primary.span.file != main_file { |
| 76 | failures.push(format!( |
| 77 | "diagnostic in {}:{}: [{}] {}", |
| 78 | file.path(), |
| 79 | line + 1, |
| 80 | d.code, |
| 81 | d.message |
| 82 | )); |
| 83 | continue; |
| 84 | } |
| 85 | let hit = expected.iter().position(|e| { |
| 86 | e.line == line |
| 87 | && e.severity == d.severity |
| 88 | && e.code == d.code |
| 89 | && mentions(&d.message, &d.notes, &e.fragment) |
| 90 | }); |
| 91 | match hit { |
| 92 | Some(i) => { |
| 93 | expected.remove(i); |
| 94 | } |
| 95 | None => failures.push(format!( |
| 96 | "unexpected at line {}: [{}] {}", |
| 97 | line + 1, |
| 98 | d.code, |
| 99 | d.message |
| 100 | )), |
| 101 | } |
| 102 | } |
| 103 | for e in expected { |
| 104 | failures.push(format!( |
| 105 | "missing at line {}: [{}] containing {:?}", |
| 106 | e.line + 1, |
| 107 | e.code, |
| 108 | e.fragment |
| 109 | )); |
| 110 | } |
| 111 | let main = hir.module_by_path("main").unwrap(); |
| 112 | for (name, want) in types { |
| 113 | let Some(def) = hir.lookup_value(main, &name) else { |
| 114 | failures.push(format!("TYPE: no item `{name}`")); |
| 115 | continue; |
| 116 | }; |
| 117 | let printer = Printer { |
| 118 | hir, |
| 119 | ctx: None, |
| 120 | gen_names: &[], |
| 121 | rigid_names: &[], |
| 122 | }; |
| 123 | let got = typeck |
| 124 | .schemes |
| 125 | .get(&def) |
| 126 | .map(|s| printer.scheme(s)) |
| 127 | .unwrap_or_else(|| "<none>".into()); |
| 128 | if got != want { |
| 129 | failures.push(format!("TYPE {name}: expected `{want}`, got `{got}`")); |
| 130 | } |
| 131 | } |
| 132 | failures |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn ui() { |
| 137 | let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/ui"); |
| 138 | let mut files: Vec<PathBuf> = std::fs::read_dir(&dir) |
| 139 | .unwrap() |
| 140 | .map(|e| e.unwrap().path()) |
| 141 | .filter(|p| p.extension().is_some_and(|e| e == "lse")) |
| 142 | .collect(); |
| 143 | files.sort(); |
| 144 | assert!(!files.is_empty()); |
| 145 | // LOUISE_UI_BLESS=1 rewrites the `//~ ERROR`/`//~ WARN` markers to the |
| 146 | // lines diagnostics actually point at (keeping `//~ TYPE` markers and |
| 147 | // reusing message fragments by diagnostic code), for after formatting |
| 148 | // or other line-shifting changes. The file is reformatted on the way, |
| 149 | // so `louise fmt --check tests/ui` stays clean. |
| 150 | if std::env::var("LOUISE_UI_BLESS").is_ok() { |
| 151 | for file in &files { |
| 152 | bless(file); |
| 153 | } |
| 154 | return; |
| 155 | } |
| 156 | let mut report = String::new(); |
| 157 | for file in &files { |
| 158 | for failure in run(file) { |
| 159 | report.push_str(&format!( |
| 160 | "{}: {failure}\n", |
| 161 | file.file_name().unwrap().to_string_lossy() |
| 162 | )); |
| 163 | } |
| 164 | } |
| 165 | assert!(report.is_empty(), "UI test failures:\n{report}"); |
| 166 | } |
| 167 | |
| 168 | /// Whether a diagnostic's message or one of its notes contains `fragment`. |
| 169 | fn mentions(message: &str, notes: &[String], fragment: &str) -> bool { |
| 170 | message.contains(fragment) || notes.iter().any(|n| n.contains(fragment)) |
| 171 | } |
| 172 | |
| 173 | /// A diagnostic as the harness sees it: severity, code, line (1-based), |
| 174 | /// message and notes. |
| 175 | struct Actual { |
| 176 | line: u32, |
| 177 | severity: Severity, |
| 178 | code: String, |
| 179 | message: String, |
| 180 | notes: Vec<String>, |
| 181 | } |
| 182 | |
| 183 | fn diagnostics(path: &Path, text: &str) -> Vec<Actual> { |
| 184 | let mut db = Db::new(); |
| 185 | let main_file = db.add_module("main", &path.display().to_string(), text.to_owned()); |
| 186 | let mut analysis = db.analyze(); |
| 187 | if !analysis.diagnostics.iter().any(Diagnostic::is_error) { |
| 188 | let semantic = louise_db::semantic_diagnostics(&db, &analysis); |
| 189 | analysis.diagnostics.extend(semantic); |
| 190 | } |
| 191 | let mut out = Vec::new(); |
| 192 | for d in &analysis.diagnostics { |
| 193 | if d.primary.span.file != main_file { |
| 194 | continue; |
| 195 | } |
| 196 | let file = db.sources().get(d.primary.span.file); |
| 197 | let line = file.line_col(d.primary.span.range.start()).line + 1; |
| 198 | out.push(Actual { |
| 199 | line, |
| 200 | severity: d.severity, |
| 201 | code: d.code.to_owned(), |
| 202 | message: d.message.clone(), |
| 203 | notes: d.notes.clone(), |
| 204 | }); |
| 205 | } |
| 206 | out |
| 207 | } |
| 208 | |
| 209 | /// Rewrite a UI file's `//~ ERROR`/`//~ WARN` markers at the lines of its |
| 210 | /// current diagnostics: trailing on the target line when it fits in 80 |
| 211 | /// columns, else on `//~^` lines right below. `//~ TYPE` markers stay. |
| 212 | /// Fragments are reused from the old markers by diagnostic code. |
| 213 | fn bless(path: &Path) { |
| 214 | let text = std::fs::read_to_string(path).unwrap(); |
| 215 | let (old, _) = parse_expectations(&text); |
| 216 | let mut lines: Vec<String> = text.lines().map(str::to_owned).collect(); |
| 217 | // Strip ERROR/WARN markers: standalone marker lines are removed, |
| 218 | // trailing markers trimmed off. |
| 219 | let mut i = lines.len(); |
| 220 | while i > 0 { |
| 221 | i -= 1; |
| 222 | let Some(at) = lines[i].find("//~") else { |
| 223 | continue; |
| 224 | }; |
| 225 | let before = &lines[i][..at]; |
| 226 | let rest = lines[i][at + 3..].trim_start_matches('^').trim_start(); |
| 227 | if rest.starts_with("ERROR ") || rest.starts_with("WARN ") { |
| 228 | if before.trim().is_empty() { |
| 229 | // Standalone marker line: drop it. |
| 230 | lines.remove(i); |
| 231 | } else { |
| 232 | // Trailing marker: keep the code. |
| 233 | lines[i] = before.trim_end().to_owned(); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | let mut text = lines.join("\n") + "\n"; |
| 238 | let mut diags = diagnostics(path, &text); |
| 239 | diags.sort_by_key(|d| d.line); |
| 240 | // Bottom-up, so insertions below do not move earlier targets. |
| 241 | for d in diags.iter().rev() { |
| 242 | let fragment = old |
| 243 | .iter() |
| 244 | .filter(|e| e.code == d.code && mentions(&d.message, &d.notes, &e.fragment)) |
| 245 | .map(|e| e.fragment.clone()) |
| 246 | .next() |
| 247 | .unwrap_or_default(); |
| 248 | let marker = format!( |
| 249 | "//~ {} {}{}", |
| 250 | match d.severity { |
| 251 | Severity::Error => "ERROR", |
| 252 | Severity::Warning => "WARN", |
| 253 | }, |
| 254 | d.code, |
| 255 | if fragment.is_empty() { |
| 256 | String::new() |
| 257 | } else { |
| 258 | format!(" {fragment}") |
| 259 | }, |
| 260 | ); |
| 261 | let mut lines: Vec<String> = text.lines().map(str::to_owned).collect(); |
| 262 | let target = (d.line - 1) as usize; |
| 263 | let trailing = format!("{} {}", lines[target], marker); |
| 264 | if trailing.chars().count() <= 80 && !lines[target].contains("//~") { |
| 265 | lines[target] = trailing; |
| 266 | } else { |
| 267 | // Standalone `//~^` lines right below, indented as the |
| 268 | // formatter will indent them: with the following code line. |
| 269 | let mut carets = 1; |
| 270 | let mut at = target + 1; |
| 271 | while at < lines.len() && lines[at].trim_start().starts_with("//~") { |
| 272 | carets += 1; |
| 273 | at += 1; |
| 274 | } |
| 275 | let indent: String = lines |
| 276 | .get(at) |
| 277 | .filter(|l| !l.trim().is_empty()) |
| 278 | .map(|l| l.chars().take_while(|c| *c == ' ' || *c == '\t').collect()) |
| 279 | .unwrap_or_else(|| { |
| 280 | lines[target] |
| 281 | .chars() |
| 282 | .take_while(|c| *c == ' ' || *c == '\t') |
| 283 | .collect() |
| 284 | }); |
| 285 | let marker = format!( |
| 286 | "{indent}//~{} {}", |
| 287 | "^".repeat(carets), |
| 288 | marker.trim_start_matches("//~") |
| 289 | ); |
| 290 | lines.insert(at, marker); |
| 291 | } |
| 292 | text = lines.join("\n") + "\n"; |
| 293 | } |
| 294 | // Indentation of the marker lines is the formatter's call; formatting |
| 295 | // here keeps `louise fmt --check tests/ui` clean. |
| 296 | if let Ok(formatted) = louise_syntax::format::format(&text) { |
| 297 | text = formatted; |
| 298 | } |
| 299 | std::fs::write(path, text).unwrap(); |
| 300 | } |