oxedyne/fe2o3/fe2o3_austenite/tests/diag_summary.rs
12.0 KiB, 11 runs
created by r1870400018:63572, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `austenite --eval --diag-summary`: one stderr line for each severity, kind and construct, counts only. |
| 2 | //! |
| 3 | //! The report names what a compile passed over and never the document it passed over in, so it can be read |
| 4 | //! where the document may not be. Each case compiles a synthetic file with the built binary and holds the |
| 5 | //! lines to their exact text. |
| 6 | |
| 7 | use oxedyne_fe2o3_austenite::diag::{ |
| 8 | error_lines, |
| 9 | summary_lines, |
| 10 | Diagnostic, |
| 11 | DiagnosticKind, |
| 12 | }; |
| 13 | use oxedyne_fe2o3_austenite::syntax::{ |
| 14 | FileId, |
| 15 | Source, |
| 16 | Span, |
| 17 | }; |
| 18 | |
| 19 | use std::path::PathBuf; |
| 20 | use std::process::Command; |
| 21 | |
| 22 | // One file's compile: the exit status's success, the report's lines and the error lines. |
| 23 | struct Run { |
| 24 | ok: bool, |
| 25 | lines: Vec<String>, |
| 26 | errors: Vec<String>, |
| 27 | stderr: String, |
| 28 | } |
| 29 | |
| 30 | fn run(name: &str, src: &str, flags: &[&str]) -> Run { |
| 31 | run_with(name, src, &[], flags) |
| 32 | } |
| 33 | |
| 34 | // As `run`, with the extra files (path below the case's directory, text) written beside `main.typ`. |
| 35 | fn run_with(name: &str, src: &str, files: &[(&str, &str)], flags: &[&str]) -> Run { |
| 36 | let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("diag_summary").join(name); |
| 37 | let _ = std::fs::remove_dir_all(&dir); |
| 38 | std::fs::create_dir_all(&dir).expect("the case's directory"); |
| 39 | let main = dir.join("main.typ"); |
| 40 | std::fs::write(&main, src).expect("the case's source"); |
| 41 | for (path, text) in files { |
| 42 | let at = dir.join(path); |
| 43 | std::fs::create_dir_all(at.parent().expect("a parent")).expect("the file's directory"); |
| 44 | std::fs::write(&at, text).expect("the case's file"); |
| 45 | } |
| 46 | let out = Command::new(env!("CARGO_BIN_EXE_austenite")) |
| 47 | .args(flags) |
| 48 | .arg(&main) |
| 49 | .arg(dir.join("out")) |
| 50 | .output() |
| 51 | .expect("the built austenite binary"); |
| 52 | let stderr = String::from_utf8_lossy(&out.stderr).to_string(); |
| 53 | let lines = stderr.lines() |
| 54 | .filter(|l| l.starts_with("diag-summary ")) |
| 55 | .map(|l| l.to_string()) |
| 56 | .collect(); |
| 57 | let errors = stderr.lines() |
| 58 | .filter(|l| l.starts_with("diag-error ")) |
| 59 | .map(|l| l.to_string()) |
| 60 | .collect(); |
| 61 | Run { ok: out.status.success(), lines, errors, stderr } |
| 62 | } |
| 63 | |
| 64 | // A page bleed is passed over and a gradient drawn flat, both of kind `unsupported` (the second message |
| 65 | // has a colon, so its head is not the whole message); a label that no element carries is Typst's own |
| 66 | // remark, of kind `lint`. |
| 67 | const TWO_UNSUPPORTED_ONE_LINT: &str = "\ |
| 68 | <nothing> |
| 69 | #set page(bleed: 3pt) |
| 70 | #rect(fill: gradient.linear(red, blue)) |
| 71 | "; |
| 72 | |
| 73 | #[test] |
| 74 | fn two_unsupported_constructs_and_a_lint_are_counted_and_nothing_else_is_printed() { |
| 75 | let r = run("counts", TWO_UNSUPPORTED_ONE_LINT, &["--eval", "--diag-summary"]); |
| 76 | assert!(r.ok, "the compile succeeds:\n{}", r.stderr); |
| 77 | assert_eq!(r.lines, vec![ |
| 78 | "diag-summary warning lint - 1", |
| 79 | "diag-summary warning unsupported gradients_are_drawn_in_one_flat_colour 1", |
| 80 | "diag-summary warning unsupported page_bleed_is_not_drawn 1", |
| 81 | ], "the summary of:\n{}", r.stderr); |
| 82 | // Nothing a message, a path or a position would add: the summary lines and the terse skip line only. |
| 83 | for l in r.stderr.lines() { |
| 84 | assert!(l.starts_with("diag-summary ") || l.starts_with("[austenite] skipped: "), |
| 85 | "a line that is neither the summary nor the skip line: {:?}", l); |
| 86 | } |
| 87 | assert!(r.errors.is_empty(), "warnings are not named as errors:\n{}", r.stderr); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn no_report_is_printed_unless_the_flag_is_given() { |
| 92 | let r = run("off", TWO_UNSUPPORTED_ONE_LINT, &["--eval"]); |
| 93 | assert!(r.ok, "the compile succeeds:\n{}", r.stderr); |
| 94 | assert!(r.lines.is_empty(), "no report is printed unasked:\n{}", r.stderr); |
| 95 | let r = run("off_error", "#text(size: \"a\")[x]\n", &["--eval"]); |
| 96 | assert!(!r.ok, "the compile fails"); |
| 97 | assert!(r.errors.is_empty(), "no error is named unasked:\n{}", r.stderr); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn a_compile_that_fails_prints_the_report_before_the_error() { |
| 102 | // A show rule's error is the final pass's own, so the compile fails after the layout that raised the |
| 103 | // bleed's warning. |
| 104 | let src = "\ |
| 105 | #set page(bleed: 3pt) |
| 106 | #show heading: it => it.nonesuch |
| 107 | = Title |
| 108 | "; |
| 109 | let r = run("fails", src, &["--eval", "--diag-summary"]); |
| 110 | assert!(!r.ok, "the compile fails:\n{}", r.stderr); |
| 111 | let at = |needle: &str| r.stderr.lines().position(|l| l.contains(needle)); |
| 112 | assert_eq!(r.lines, vec![ |
| 113 | "diag-summary error type - 1", |
| 114 | "diag-summary warning unsupported page_bleed_is_not_drawn 1", |
| 115 | ], "the summary of:\n{}", r.stderr); |
| 116 | let summary = at("diag-summary ").expect("a summary line"); |
| 117 | let error = at(": error: ").expect("the rendered error"); |
| 118 | assert!(summary < error, "the summary comes before the error:\n{}", r.stderr); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn the_flag_on_the_curated_reader_is_refused() { |
| 123 | let r = run("no_eval", "Hello.\n", &["--engine", "curated", "--diag-summary"]); |
| 124 | assert!(!r.ok, "the flag on the curated reader is refused"); |
| 125 | assert!(r.stderr.contains("--diag-summary is not read on the curated reader path"), "{}", r.stderr); |
| 126 | assert!(r.lines.is_empty(), "no report is printed:\n{}", r.stderr); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn a_construct_is_the_message_head_with_its_spaces_written_as_underscores() { |
| 131 | let at = Span::detached(); |
| 132 | let w = |kind, msg: &str| Diagnostic::warning(kind, at, msg); |
| 133 | let diags = vec![ |
| 134 | w(DiagnosticKind::Unsupported, "unknown paper size: a5x"), |
| 135 | w(DiagnosticKind::Unsupported, "unknown paper size: b9"), |
| 136 | w(DiagnosticKind::Unsupported, "page bleed is not drawn"), |
| 137 | w(DiagnosticKind::Lint, "label `<a>` is not attached to anything"), |
| 138 | w(DiagnosticKind::Lint, "label `<b>` is not attached to anything"), |
| 139 | w(DiagnosticKind::MissingFont, "unknown font family: Nonesuch"), |
| 140 | Diagnostic::error(DiagnosticKind::Unsupported, at, "plugins are not supported: here"), |
| 141 | Diagnostic::error(DiagnosticKind::Syntax, at, "unclosed delimiter"), |
| 142 | ]; |
| 143 | assert_eq!(summary_lines(&diags), vec![ |
| 144 | "diag-summary error syntax - 1", |
| 145 | "diag-summary error unsupported plugins_are_not_supported 1", |
| 146 | "diag-summary warning lint - 2", |
| 147 | "diag-summary warning missing_font - 1", |
| 148 | "diag-summary warning unsupported page_bleed_is_not_drawn 1", |
| 149 | "diag-summary warning unsupported unknown_paper_size 2", |
| 150 | ]); |
| 151 | assert!(summary_lines(&[]).is_empty(), "a clean compile has no lines"); |
| 152 | } |
| 153 | |
| 154 | // Each error line has the shape the probe lets through, `diag-error <kind> callee:<name> expected:<type> |
| 155 | // found:<type> file:<class>`, with each token in its closed vocabulary. |
| 156 | fn shape_ok(line: &str) -> bool { |
| 157 | let t: Vec<&str> = line.split(' ').collect(); |
| 158 | if t.len() != 6 || t[0] != "diag-error" { |
| 159 | return false; |
| 160 | } |
| 161 | let word = |s: &str, extra: &str| !s.is_empty() && s.chars().all(|c| c.is_ascii_lowercase() || extra.contains(c)); |
| 162 | word(t[1], "_") |
| 163 | && t[2].strip_prefix("callee:").map_or(false, |v| word(v, "0123456789.-")) |
| 164 | && t[3].strip_prefix("expected:").map_or(false, |v| word(v, "-")) |
| 165 | && t[4].strip_prefix("found:").map_or(false, |v| word(v, "-")) |
| 166 | && matches!(t[5], "file:main" | "file:sibling" | "file:other") |
| 167 | } |
| 168 | |
| 169 | // One failing source, the error line it gives, and that the line has the probe's shape. |
| 170 | fn named(name: &str, src: &str, files: &[(&str, &str)]) -> Vec<String> { |
| 171 | let r = run_with(name, src, files, &["--eval", "--diag-summary"]); |
| 172 | assert!(!r.ok, "the compile fails:\n{}", r.stderr); |
| 173 | for l in &r.errors { |
| 174 | assert!(shape_ok(l), "an error line outside the probe's shape: {:?}", l); |
| 175 | } |
| 176 | r.errors |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn a_standard_call_with_a_type_pair_is_named_with_both_types() { |
| 181 | // `text(size: "a")`: Typst's "expected length, found string", at the argument inside the call. |
| 182 | assert_eq!(named("pair", "#text(size: \"a\")[x]\n", &[]), |
| 183 | vec!["diag-error type callee:text expected:length found:str file:main"]); |
| 184 | // A type spelled differently in the message and in `repr` is one registry type: "relative length". |
| 185 | assert_eq!(named("pair_rel", "#rect(width: \"a\")\n", &[]), |
| 186 | vec!["diag-error type callee:rect expected:relative found:str file:main"]); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn a_dotted_standard_path_is_named_and_a_message_offering_several_types_has_no_pair() { |
| 191 | assert_eq!(named("dotted", "#calc.abs(\"a\")\n", &[]), |
| 192 | vec!["diag-error type callee:calc.abs expected:- found:- file:main"]); |
| 193 | // A standard name outside the probe's lower-case alphabet (`sym.Alpha`) is `-`, as is any other. Typst |
| 194 | // refuses the call of a letter with "symbol Α is not callable", which offers no pair of types. |
| 195 | assert_eq!(named("upper", "#sym.Alpha(1)\n", &[]), |
| 196 | vec!["diag-error type callee:- expected:- found:- file:main"]); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn a_document_s_own_function_or_name_is_never_named() { |
| 201 | // A user function called with an argument it does not take: the error is at its call, and `f` is not |
| 202 | // the standard library's. |
| 203 | assert_eq!(named("user_fn", "#let f(x) = x\n#f(1, 2)\n", &[]), |
| 204 | vec!["diag-error type callee:- expected:- found:- file:main"]); |
| 205 | // A name bound nowhere is no standard name either. |
| 206 | assert_eq!(named("unbound", "#nonesuch(1)\n", &[]), |
| 207 | vec!["diag-error unknown_variable callee:- expected:- found:- file:main"]); |
| 208 | // A path whose first segment is standard and whose last is not is hidden: `nonesuch` is not in `calc`. |
| 209 | let lines = named("user_member", "#calc.nonesuch(1)\n", &[]); |
| 210 | assert_eq!(lines.len(), 1, "one error: {:?}", lines); |
| 211 | assert!(lines[0].contains(" callee:- "), "a member the module lacks is not named: {:?}", lines); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn the_file_of_an_error_is_main_a_sibling_or_other() { |
| 216 | let bad = "#text(size: \"a\")[x]\n"; |
| 217 | assert_eq!(named("f_main", bad, &[]), |
| 218 | vec!["diag-error type callee:text expected:length found:str file:main"]); |
| 219 | assert_eq!(named("f_sibling", "#include \"sib.typ\"\n", &[("sib.typ", bad)]), |
| 220 | vec!["diag-error type callee:text expected:length found:str file:sibling"]); |
| 221 | assert_eq!(named("f_other", "#include \"sub/deep.typ\"\n", &[("sub/deep.typ", bad)]), |
| 222 | vec!["diag-error type callee:text expected:length found:str file:other"]); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn the_error_lines_are_built_from_the_registry_and_the_standard_scope_alone() { |
| 227 | let src = |id: u16, path: &str, text: &str| Source::new(FileId(id), PathBuf::from(path), text.to_string()); |
| 228 | let sources = vec![ |
| 229 | src(0, "/p/main.typ", "#text(size: val)[a]\n"), |
| 230 | src(1, "/p/sib.typ", "#pad(secret_name)[a]\n"), |
| 231 | src(2, "/p/sub/deep.typ", "#secretfn(1)\n"), |
| 232 | ]; |
| 233 | let at = |id: u16, key: &str| { |
| 234 | let start = sources[id as usize].text.find(key).expect("the text"); |
| 235 | Span::new(FileId(id), start as u32, (start + key.len()) as u32) |
| 236 | }; |
| 237 | let err = |span, msg: &str| Diagnostic::error(DiagnosticKind::Type, span, msg); |
| 238 | let diags = vec![ |
| 239 | // The argument of a standard call: the call's name, the types of a registry pair. |
| 240 | err(at(0, "val"), "expected length, found string"), |
| 241 | // A user name inside a standard call is not named but the call is. |
| 242 | err(at(1, "secret_name"), "expected integer, found content"), |
| 243 | // A user function's own call is hidden, and `foo` is no type. |
| 244 | err(at(2, "secretfn"), "expected foo, found bar"), |
| 245 | // A span in no file at all, and a message offering several types. |
| 246 | err(Span::detached(), "expected length or auto, found string"), |
| 247 | // A warning is not an error. |
| 248 | Diagnostic::warning(DiagnosticKind::Lint, at(0, "val"), "expected length, found string"), |
| 249 | ]; |
| 250 | assert_eq!(error_lines(&diags, &sources), vec![ |
| 251 | "diag-error type callee:text expected:length found:str file:main", |
| 252 | "diag-error type callee:pad expected:int found:content file:sibling", |
| 253 | "diag-error type callee:- expected:- found:- file:other", |
| 254 | "diag-error type callee:- expected:- found:- file:other", |
| 255 | ]); |
| 256 | assert!(error_lines(&[], &sources).is_empty(), "no error, no line"); |
| 257 | // A message of the shape whose sides are not both types has no pair, whatever it says. |
| 258 | for m in ["expected length, found", "expected , found string", "expected length, found string, x", |
| 259 | "expected Length, found string", "expected length found string", "expecting length, found string"] { |
| 260 | let l = error_lines(&[err(Span::detached(), m)], &[]); |
| 261 | assert_eq!(l, vec!["diag-error type callee:- expected:- found:- file:other"], "the message {:?}", m); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn a_pdf_attachment_is_passed_over_by_name_and_an_artifact_is_not() { |
| 267 | // Austenite embeds no file, so the attachment is named as passed over; an artifact only tags a PDF. |
| 268 | let r = run("pdf", "#pdf.attach(\"a.txt\", bytes(\"hi\"))\n#pdf.artifact(kind: \"header\")[x]\n", &["--eval", "--diag-summary"]); |
| 269 | assert!(r.ok, "the compile succeeds:\n{}", r.stderr); |
| 270 | assert_eq!(r.lines, vec!["diag-summary warning unsupported pdf_attachments_are_not_embedded 1"], |
| 271 | "the summary of:\n{}", r.stderr); |
| 272 | } |