oxedyne/fe2o3/fe2o3_austenite/tests/eval_package.rs
3.1 KiB, 1 run
created by r1870400018:63630, 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 | //! Package specifications (`@namespace/name:1.2.3`) parsed by `package::parse_spec` against the |
| 2 | //! `typst` 0.15 oracle: every malformed form must fail with Typst's own message, and a well-formed one |
| 3 | //! must yield its parts. `EVAL_ORACLE_SKIP=1` skips explicitly. |
| 4 | |
| 5 | use oxedyne_fe2o3_austenite::diag::message_of; |
| 6 | use oxedyne_fe2o3_austenite::eval::package::parse_spec; |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | |
| 10 | use std::path::PathBuf; |
| 11 | use std::process::Command; |
| 12 | |
| 13 | // Malformed specifications, each failing a different check, and well-formed ones Typst accepts as far |
| 14 | // as looking for the package (which is absent). |
| 15 | const SPECS: &[&str] = &[ |
| 16 | "@preview/x:1.2", |
| 17 | "@preview/x:1.2.3.4", |
| 18 | "@/x:1.2.3", |
| 19 | "@preview/:1.2.3", |
| 20 | "@preview/x", |
| 21 | "@preview/x:", |
| 22 | "@preview/x:a.2.3", |
| 23 | "@preview/x:1..3", |
| 24 | "@pre view/x:1.2.3", |
| 25 | "@preview/x y:1.2.3", |
| 26 | "@preview/x:1.2.b", |
| 27 | "@pre-view/x:1.2.3", |
| 28 | "@preview/x:01.2.3", |
| 29 | "@local/tidy_thing:10.20.30", |
| 30 | ]; |
| 31 | |
| 32 | fn skip() -> bool { std::env::var("EVAL_ORACLE_SKIP").map(|v| v == "1").unwrap_or(false) } |
| 33 | |
| 34 | // The first error typst gives for `#import "<spec>"`. |
| 35 | fn oracle(spec: &str, n: usize) -> Outcome<String> { |
| 36 | let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_package"); |
| 37 | res!(std::fs::create_dir_all(&dir).map_err(|e| err!("Cannot create {}: {}", dir.display(), e; IO, File, Write))); |
| 38 | let src = dir.join(fmt!("p{}.typ", n)); |
| 39 | res!(std::fs::write(&src, fmt!("#import \"{}\"\n", spec)).map_err(|e| err!( |
| 40 | "Cannot write {}: {}", src.display(), e; IO, File, Write))); |
| 41 | let out = res!(Command::new("systemd-run") |
| 42 | .args(["--user", "--scope", "--quiet", "-p", "MemoryMax=3G", "--slice=claude-rc.slice", "typst", "compile", |
| 43 | "-f", "pdf"]) |
| 44 | .arg(&src).arg(dir.join(fmt!("p{}.pdf", n))).output() |
| 45 | .map_err(|e| err!("The typst oracle could not run ({}); set EVAL_ORACLE_SKIP=1 to skip.", e; IO, Missing))); |
| 46 | let stderr = String::from_utf8_lossy(&out.stderr).to_string(); |
| 47 | match stderr.lines().find_map(|l| l.strip_prefix("error: ")) { |
| 48 | Some(m) => Ok(m.to_string()), |
| 49 | None => Err(err!("typst accepted `{}`, which no package here satisfies", spec; Invalid)), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn package_specifications_match_the_typst_oracle() -> Outcome<()> { |
| 55 | if skip() { |
| 56 | println!("EVAL_ORACLE_SKIP=1: oracle comparison skipped"); |
| 57 | return Ok(()); |
| 58 | } |
| 59 | let mut failures = Vec::new(); |
| 60 | let mut parsed = 0; |
| 61 | for (n, spec) in SPECS.iter().enumerate() { |
| 62 | let want = res!(oracle(spec, n)); |
| 63 | match parse_spec(spec) { |
| 64 | // Typst got past the specification and looked for the package. |
| 65 | Ok(p) => { |
| 66 | parsed += 1; |
| 67 | let shown = fmt!("@{}/{}:{}.{}.{}", p.namespace, p.name, p.version.0, p.version.1, p.version.2); |
| 68 | if want != fmt!("package not found (searched for {})", shown) { |
| 69 | failures.push(fmt!("{}: parsed as {}, typst: {}", spec, shown, want)); |
| 70 | } |
| 71 | } |
| 72 | Err(e) => { |
| 73 | let got = message_of(&e); |
| 74 | if got != want { |
| 75 | failures.push(fmt!("{}: austenite `{}`, typst `{}`", spec, got, want)); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | assert!(parsed >= 2 && parsed < SPECS.len(), "{} of {} parsed: the corpus must hold both kinds", parsed, SPECS.len()); |
| 81 | assert!(failures.is_empty(), "{} of {} specifications differ:\n{}", failures.len(), SPECS.len(), failures.join("\n")); |
| 82 | Ok(()) |
| 83 | } |