oxedyne/fe2o3/fe2o3_austenite/tests/eval_import.rs
21.1 KiB, 1 run
created by r1870400018:63588, 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 | //! U10a: imports, includes and packages, against the host's `typst` 0.15.x. |
| 2 | //! |
| 3 | //! - The `import` corpus (`tests/fixtures/eval/import/`) holds named, renamed, wildcard and module imports, |
| 4 | //! a bare import, a path that climbs from a nested file, a root-absolute path, `include`, the template |
| 5 | //! pattern (`#show: doc.with(..)` from a file), data read by a relative path and strings given to `eval`. |
| 6 | //! Each accepted fixture is held to Typst's values, and the template and include fixtures to the structure |
| 7 | //! of Typst's realisation. Its rejected fixtures (a missing file, a cycle, a path out of the root, a |
| 8 | //! backslash, an unresolved name, a file that is not UTF-8, a directory) are held to Typst's first error: |
| 9 | //! message, file, line and column. |
| 10 | //! - The `package` corpus holds packages the area supplies itself (`packages/local/<name>/<version>`, |
| 11 | //! laid out as Typst's package path is), with the entrypoints, manifests and paths of Typst's own rules. |
| 12 | //! - A module is evaluated once per canonical path; a diagnostic carries the kind of what failed, so a |
| 13 | //! caller never switches on wording; a package is supplied by the host and never fetched. |
| 14 | //! - The real packages the book will import (cetz, fletcher, tablex, oxifmt) evaluate without a panic or a |
| 15 | //! run that does not end. |
| 16 | |
| 17 | #![allow(dead_code)] |
| 18 | |
| 19 | #[path = "eval_oracle/mod.rs"] |
| 20 | mod harness; |
| 21 | |
| 22 | use harness::austenite; |
| 23 | use harness::corpus::{ |
| 24 | discover, |
| 25 | Expect, |
| 26 | Filter, |
| 27 | Fixture, |
| 28 | }; |
| 29 | use harness::oracle::Oracle; |
| 30 | use harness::{ |
| 31 | check, |
| 32 | FixtureReport, |
| 33 | Verdict, |
| 34 | }; |
| 35 | |
| 36 | use oxedyne_fe2o3_austenite::diag::{ |
| 37 | Diagnostic, |
| 38 | DiagnosticKind, |
| 39 | }; |
| 40 | use oxedyne_fe2o3_austenite::eval::eval::{ |
| 41 | eval_string, |
| 42 | EvalMode, |
| 43 | }; |
| 44 | use oxedyne_fe2o3_austenite::eval::package; |
| 45 | use oxedyne_fe2o3_austenite::eval::scope::Scope; |
| 46 | use oxedyne_fe2o3_austenite::eval::{ |
| 47 | eval_source, |
| 48 | Engine, |
| 49 | World, |
| 50 | }; |
| 51 | use oxedyne_fe2o3_austenite::syntax::Span; |
| 52 | use oxedyne_fe2o3_austenite::vfs; |
| 53 | |
| 54 | use oxedyne_fe2o3_core::prelude::*; |
| 55 | |
| 56 | use std::path::{ |
| 57 | Path, |
| 58 | PathBuf, |
| 59 | }; |
| 60 | |
| 61 | fn oracle() -> Outcome<Option<Oracle>> { |
| 62 | let o = res!(Oracle::find()); |
| 63 | if o.is_none() { |
| 64 | println!("[eval-import] SKIPPED: EVAL_ORACLE_SKIP=1 and no typst 0.15.x -- nothing was compared"); |
| 65 | } |
| 66 | Ok(o) |
| 67 | } |
| 68 | |
| 69 | fn fixtures(area: &str) -> Outcome<Vec<Fixture>> { |
| 70 | discover(&Filter::only(area)) |
| 71 | } |
| 72 | |
| 73 | fn named(area: &str, name: &str) -> Outcome<Fixture> { |
| 74 | let all = res!(fixtures(area)); |
| 75 | match all.into_iter().find(|f| f.name == name) { |
| 76 | Some(f) => Ok(f), |
| 77 | None => Err(err!("The fixture {}/{} is missing from tests/fixtures/eval/{}.", area, name, area; Missing)), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Every level a fixture asked for, and its rejection, must pass: the two corpora here are held strictly. The |
| 82 | // numbers compared come back so a test can insist the corpus compared something. |
| 83 | fn hold(area: &str, o: &Oracle, floor: usize) -> Outcome<(usize, usize)> { |
| 84 | let all = res!(fixtures(area)); |
| 85 | assert!(all.len() >= floor, "{} fixtures under tests/fixtures/eval/{}, {} expected", all.len(), area, floor); |
| 86 | let limit = harness::timeout(); |
| 87 | let mut bad = Vec::new(); |
| 88 | let (mut structure, mut errors) = (0, 0); |
| 89 | for fx in &all { |
| 90 | let rep: FixtureReport = res!(check(fx, o, limit)); |
| 91 | if let Some(f) = &rep.fault { |
| 92 | bad.push(fmt!("{}: {}", rep.id, f)); |
| 93 | } |
| 94 | for (i, v) in rep.levels.iter().enumerate() { |
| 95 | if let Verdict::Fail(d) = v { |
| 96 | bad.push(fmt!("{} L{}: {}", rep.id, i + 1, d.join("; "))); |
| 97 | } |
| 98 | } |
| 99 | if let Verdict::Fail(d) = &rep.rejection { |
| 100 | bad.push(fmt!("{} Err: {}", rep.id, d.join("; "))); |
| 101 | } |
| 102 | structure += rep.levels[2].is_pass() as usize; |
| 103 | errors += rep.rejection.is_pass() as usize; |
| 104 | } |
| 105 | assert!(bad.is_empty(), "{} disagreement(s) with typst in {}:\n{}", bad.len(), area, bad.join("\n")); |
| 106 | Ok((structure, errors)) |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn imports_and_includes_agree_with_the_oracle() -> Outcome<()> { |
| 111 | let o = match res!(oracle()) { |
| 112 | Some(o) => o, |
| 113 | None => return Ok(()), |
| 114 | }; |
| 115 | let (structure, errors) = res!(hold("import", &o, 28)); |
| 116 | // Agreement is only worth something when the corpus compared things of each kind. |
| 117 | assert!(structure >= 12, "{} fixtures agreed on realised structure, 12 expected", structure); |
| 118 | assert!(errors >= 16, "{} rejected fixtures agreed on the first error, 16 expected", errors); |
| 119 | Ok(()) |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn packages_agree_with_the_oracle() -> Outcome<()> { |
| 124 | let o = match res!(oracle()) { |
| 125 | Some(o) => o, |
| 126 | None => return Ok(()), |
| 127 | }; |
| 128 | let (structure, errors) = res!(hold("package", &o, 47)); |
| 129 | assert!(structure >= 14, "{} package fixtures agreed on realised structure, 14 expected", structure); |
| 130 | assert!(errors >= 33, "{} rejected package fixtures agreed on the first error, 33 expected", errors); |
| 131 | Ok(()) |
| 132 | } |
| 133 | |
| 134 | // Values through imports, as `typst eval` serialises them: each expression carries its own `import`s, so it |
| 135 | // reaches the area's files the way a document does, and Austenite evaluates the same string. |
| 136 | const VALUES: [(&str, &str); 14] = [ |
| 137 | ("names", r#"import "_lib.typ": a, b as other, g; (a, other, g(1))"#), |
| 138 | ("star", r#"import "_lib.typ": *; (a, b, c, f(1), g(1))"#), |
| 139 | ("module_alias", r#"import "_lib.typ" as m; (m.a, m.b, m.c, m.f(2), m.g(2), m.hidden)"#), |
| 140 | ("bare", r#"import "_lib.typ"; (_lib.a, _lib.c)"#), |
| 141 | ("nested", r#"import "sub/_inner.typ" as inner; inner.sees"#), |
| 142 | ("nested", r#"import "sub/deeper/_leaf.typ": leaf; leaf"#), |
| 143 | ("root_abs", r#"import "/_lib.typ": a, c; import "/sub/peer.typ": p; (a, c, p)"#), |
| 144 | ("dotted", r#"import "_lib.typ": a; import "./_lib.typ": b; import "sub/_up.typ": c; (a, b, c)"#), |
| 145 | ("include", r#"import "_part.typ": hidden; hidden"#), |
| 146 | ("read_ok", r#"import "sub/_reader.typ": from_sub; (read("_data.txt"), from_sub)"#), |
| 147 | ("eval_paths", r#"(eval("read(\"_data.txt\")"), eval("import \"_lib.typ\": a, c; (a, c)"))"#), |
| 148 | ("once", r#"import "_warn.typ": w; import "./_warn.typ": w as w2; import "sub/_again.typ": w as w3; (w, w2, w3)"#), |
| 149 | ("names", r#"import "_lib.typ" as m; import m: a, c as d; (a, d)"#), |
| 150 | // An escape a string does not define keeps its backslash, so a package's regex pattern reads as written. |
| 151 | ("names", r#"("\*", "\$", "a\qb", "\u{41}\t|", "\\")"#), |
| 152 | ]; |
| 153 | |
| 154 | // What Austenite makes of an expression, evaluated from a source in the fixture's area. |
| 155 | fn austenite_value(fx: &Fixture, code: &str) -> Outcome<std::result::Result<harness::json::J, String>> { |
| 156 | let path = fx.path.clone(); |
| 157 | let root = fx.root.clone(); |
| 158 | let code = code.to_string(); |
| 159 | let handle = res!(std::thread::Builder::new().stack_size(256 << 20).spawn(move || -> Outcome<std::result::Result<harness::json::J, String>> { |
| 160 | austenite::supply_packages(&root); |
| 161 | let mut world = World::new(root); |
| 162 | let id = res!(world.load(&path)); |
| 163 | let mut engine = Engine::new(world); |
| 164 | let span = Span::new(id, 0, 0); |
| 165 | Ok(match eval_string(&mut engine, &code, EvalMode::Code, Scope::new(), span) { |
| 166 | Ok(v) => Ok(austenite::to_json(&v)), |
| 167 | Err(_) => Err(engine.diags.iter().find(|d| d.is_error()).map(|d| d.message.clone()).unwrap_or_default()), |
| 168 | }) |
| 169 | }).map_err(|e| err!("Cannot start the evaluation thread: {}", e; IO))); |
| 170 | match handle.join() { |
| 171 | Ok(r) => r, |
| 172 | Err(_) => Err(err!("The evaluation panicked."; Bug)), |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn values_through_imports_agree_with_typst_eval() -> Outcome<()> { |
| 178 | let o = match res!(oracle()) { |
| 179 | Some(o) => o, |
| 180 | None => return Ok(()), |
| 181 | }; |
| 182 | let mut bad = Vec::new(); |
| 183 | for (name, code) in VALUES { |
| 184 | let fx = res!(named("import", name)); |
| 185 | let want = match res!(o.value(&fx, code)) { |
| 186 | Ok(v) => v, |
| 187 | Err(e) => { |
| 188 | bad.push(fmt!("{}: typst rejects `{}`: {}", name, code, e)); |
| 189 | continue; |
| 190 | } |
| 191 | }; |
| 192 | match res!(austenite_value(&fx, code)) { |
| 193 | Ok(got) => { |
| 194 | let mut d = Vec::new(); |
| 195 | harness::json::diff(name, &want, &got, &mut d); |
| 196 | if !d.is_empty() { |
| 197 | bad.push(fmt!("{}: `{}`: {}", name, code, d.join("; "))); |
| 198 | } |
| 199 | } |
| 200 | Err(e) => bad.push(fmt!("{}: `{}`: austenite failed: {}", name, code, e)), |
| 201 | } |
| 202 | } |
| 203 | assert!(bad.is_empty(), "{} of {} expressions differ:\n{}", bad.len(), VALUES.len(), bad.join("\n")); |
| 204 | Ok(()) |
| 205 | } |
| 206 | |
| 207 | // What evaluating one fixture left behind. |
| 208 | struct Run { |
| 209 | failed: bool, |
| 210 | diags: Vec<Diagnostic>, |
| 211 | modules: usize, |
| 212 | root: PathBuf, |
| 213 | sources: Vec<PathBuf>, // by file id |
| 214 | } |
| 215 | |
| 216 | impl Run { |
| 217 | // The file a diagnostic is placed in, relative to the project root. |
| 218 | fn file_of(&self, d: &Diagnostic) -> Option<String> { |
| 219 | let path = self.sources.get(d.span.file.0 as usize)?; |
| 220 | Some(path.strip_prefix(&self.root).unwrap_or(path).display().to_string()) |
| 221 | } |
| 222 | |
| 223 | fn first_error(&self) -> Option<&Diagnostic> { self.diags.iter().find(|d| d.is_error()) } |
| 224 | |
| 225 | fn warnings(&self, containing: &str) -> usize { |
| 226 | self.diags.iter().filter(|d| !d.is_error() && d.message.contains(containing)).count() |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Evaluates a fixture on a thread with room for the evaluator's recursion. |
| 231 | fn evaluate(fx: &Fixture) -> Outcome<Run> { |
| 232 | let path = fx.path.clone(); |
| 233 | let root = fx.root.clone(); |
| 234 | let handle = res!(std::thread::Builder::new().stack_size(256 << 20).spawn(move || -> Outcome<Run> { |
| 235 | austenite::supply_packages(&root); |
| 236 | let mut world = World::new(root.clone()); |
| 237 | let id = res!(world.load(&path)); |
| 238 | let mut engine = Engine::new(world); |
| 239 | let failed = eval_source(&mut engine, id).is_err(); |
| 240 | let sources = engine.world.sources.iter().map(|s| s.path.clone()).collect(); |
| 241 | Ok(Run { failed, diags: engine.diags.clone(), modules: engine.world.modules.len(), root, sources }) |
| 242 | }).map_err(|e| err!("Cannot start the evaluation thread: {}", e; IO))); |
| 243 | match handle.join() { |
| 244 | Ok(r) => r, |
| 245 | Err(_) => Err(err!("The evaluation of {} panicked.", fx.id(); Bug)), |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn a_module_is_evaluated_once_per_canonical_path() -> Outcome<()> { |
| 251 | // `once.typ` imports `_warn.typ` four times, under three spellings of one path and from a nested file, |
| 252 | // and the module warns each time it is evaluated. |
| 253 | let run = res!(evaluate(&res!(named("import", "once")))); |
| 254 | assert!(!run.failed, "once.typ failed: {:?}", run.first_error()); |
| 255 | assert_eq!(run.warnings("unnecessary import rename"), 1, "the module's warning must be raised once: {:?}", run.diags); |
| 256 | // `_warn.typ` and `sub/_again.typ`, the only modules the fixture reaches. |
| 257 | assert_eq!(run.modules, 2, "one cache entry per module file"); |
| 258 | Ok(()) |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn a_diagnostic_carries_the_kind_of_what_failed() -> Outcome<()> { |
| 263 | use DiagnosticKind as K; |
| 264 | let cases = [ |
| 265 | ("import", "missing", K::MissingFile, "file not found (searched at "), |
| 266 | ("import", "missing_read", K::MissingFile, "file not found (searched at "), |
| 267 | ("import", "directory", K::MissingFile, "failed to load file (is a directory)"), |
| 268 | ("import", "not_utf8", K::Encoding, "file is not valid UTF-8"), |
| 269 | ("import", "unresolved", K::UnknownVariable, "unresolved import"), |
| 270 | ("import", "escape", K::Type, "would escape the project root"), |
| 271 | ("import", "backslash", K::Type, "path must not contain a backslash"), |
| 272 | ("package", "missing", K::Package, "package not found (searched for @local/nope:0.1.0)"), |
| 273 | ("package", "multi_missing", K::Package, "package not found (searched for @local/multi:0.3.0)"), |
| 274 | ("package", "badc", K::Package, "package manifest is malformed"), |
| 275 | ("package", "nomanifest", K::Package, "file not found"), |
| 276 | ("package", "namemm", K::Package, "package manifest contains mismatched name"), |
| 277 | ("package", "newc", K::Package, "package requires Typst 0.99.0 or newer"), |
| 278 | ("package", "noentfile", K::MissingFile, "file not found"), |
| 279 | ("package", "esc_read_missing", K::MissingFile, "file not found (searched at "), |
| 280 | ]; |
| 281 | let mut bad = Vec::new(); |
| 282 | for (area, name, kind, text) in cases { |
| 283 | let run = res!(evaluate(&res!(named(area, name)))); |
| 284 | match run.first_error() { |
| 285 | Some(d) if d.kind == kind && d.message.contains(text) => (), |
| 286 | Some(d) => bad.push(fmt!("{}/{}: kind {} ({}), wanted {} ({})", area, name, d.kind, d.message, kind, text)), |
| 287 | None => bad.push(fmt!("{}/{}: no error", area, name)), |
| 288 | } |
| 289 | } |
| 290 | assert!(bad.is_empty(), "{} case(s) differ:\n{}", bad.len(), bad.join("\n")); |
| 291 | Ok(()) |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn a_cycle_is_reported_where_it_closes_and_names_its_files() -> Outcome<()> { |
| 296 | let want = [ |
| 297 | ("cycle", "_cyc_b.typ", "_cyc_a.typ -> _cyc_b.typ -> _cyc_a.typ"), |
| 298 | ("self_cycle", "self_cycle.typ", "self_cycle.typ -> self_cycle.typ"), |
| 299 | ("main_cycle", "_back.typ", "main_cycle.typ -> _back.typ -> main_cycle.typ"), |
| 300 | ]; |
| 301 | for (name, file, chain) in want { |
| 302 | let run = res!(evaluate(&res!(named("import", name)))); |
| 303 | let d = match run.first_error() { |
| 304 | Some(d) => d, |
| 305 | None => return Err(err!("{} raised no error", name; Invalid)), |
| 306 | }; |
| 307 | assert_eq!(d.message, "cyclic import", "{}", name); |
| 308 | assert_eq!(d.kind, DiagnosticKind::Type, "{}", name); |
| 309 | assert!(d.hints.iter().any(|h| h.ends_with(chain)), "{}: the hint must name the chain {}: {:?}", name, chain, d.hints); |
| 310 | // The error is placed in the file whose import closes the cycle. |
| 311 | assert_eq!(run.file_of(d).as_deref(), Some(file), "{}", name); |
| 312 | } |
| 313 | Ok(()) |
| 314 | } |
| 315 | |
| 316 | #[test] |
| 317 | fn a_chain_of_imports_is_bounded_and_a_shorter_one_is_not() -> Outcome<()> { |
| 318 | let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_import_chain"); |
| 319 | res!(std::fs::create_dir_all(&root).map_err(|e| err!("Cannot create {}: {}", root.display(), e; IO, File, Write))); |
| 320 | // `m0.typ` imports `m1.typ`, which imports `m2.typ`, and so on; the last file ends the chain. |
| 321 | let n = 120; |
| 322 | for i in 0..n { |
| 323 | let text = if i + 1 < n { fmt!("#import \"m{}.typ\"\n#let top = {}\n", i + 1, i) } else { "#let top = 0\n".to_string() }; |
| 324 | res!(std::fs::write(root.join(fmt!("m{}.typ", i)), text).map_err(|e| err!("Cannot write m{}.typ: {}", i, e; IO, File, Write))); |
| 325 | } |
| 326 | let long = res!(run_text(&root, "main_long.typ", "#import \"m0.typ\": top\n")); |
| 327 | let d = res!(long.first_error().ok_or_else(|| err!("A chain of {} imports was not bounded.", n; Invalid))); |
| 328 | assert_eq!(d.kind, DiagnosticKind::Limit, "{}", d.message); |
| 329 | // The same files, entered sixty files from the end, are within the bound. |
| 330 | let short = res!(run_text(&root, "main_short.typ", "#import \"m60.typ\": top\n")); |
| 331 | assert!(!short.failed, "a chain of {} imports failed: {:?}", n - 60, short.first_error().map(|d| d.message.clone())); |
| 332 | Ok(()) |
| 333 | } |
| 334 | |
| 335 | // A package the host supplies in memory, as Daimond's origin does through the wasm surface. |
| 336 | fn supply_demo(version: &str, value: i64) -> Outcome<package::PackageSpec> { |
| 337 | let spec = res!(package::parse_spec(&fmt!("@local/memdemo:{}", version))); |
| 338 | let manifest = fmt!("[package]\nname = \"memdemo\"\nversion = \"{}\"\nentrypoint = \"src/lib.typ\"\n", version); |
| 339 | let lib = fmt!("#import \"util.typ\": twice\n#let v = {}\n#let doubled = twice(v)\n", value); |
| 340 | let files = vec![ |
| 341 | ("typst.toml".to_string(), manifest.into_bytes()), |
| 342 | ("/src/lib.typ".to_string(), lib.into_bytes()), |
| 343 | ("src/util.typ".to_string(), b"#let twice(x) = x * 2\n".to_vec()), |
| 344 | ("data/d.txt".to_string(), b"memory data".to_vec()), |
| 345 | ]; |
| 346 | res!(package::supply(&spec, files)); |
| 347 | Ok(spec) |
| 348 | } |
| 349 | |
| 350 | fn diag_message(e: &Error<ErrTag>) -> String { oxedyne_fe2o3_austenite::diag::message_of(e) } |
| 351 | |
| 352 | fn run_text(root: &Path, name: &str, text: &str) -> Outcome<Run> { |
| 353 | let root = root.to_path_buf(); |
| 354 | let name = name.to_string(); |
| 355 | let text = text.to_string(); |
| 356 | let handle = res!(std::thread::Builder::new().stack_size(256 << 20).spawn(move || -> Outcome<Run> { |
| 357 | let mut world = World::new(root.clone()); |
| 358 | let id = res!(world.add_source(root.join(&name), text)); |
| 359 | let mut engine = Engine::new(world); |
| 360 | let failed = eval_source(&mut engine, id).is_err(); |
| 361 | let sources = engine.world.sources.iter().map(|s| s.path.clone()).collect(); |
| 362 | Ok(Run { failed, diags: engine.diags.clone(), modules: engine.world.modules.len(), root, sources }) |
| 363 | }).map_err(|e| err!("Cannot start the evaluation thread: {}", e; IO))); |
| 364 | match handle.join() { |
| 365 | Ok(r) => r, |
| 366 | Err(_) => Err(err!("The evaluation of the text panicked."; Bug)), |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn a_package_is_supplied_by_the_host_and_withdrawn_again() -> Outcome<()> { |
| 372 | let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_import_memory"); |
| 373 | res!(std::fs::create_dir_all(&root).map_err(|e| err!("Cannot create {}: {}", root.display(), e; IO, File, Write))); |
| 374 | let code = "#import \"@local/memdemo:1.2.3\": v, doubled\n#let r = (v, doubled)\n"; |
| 375 | // Nothing is supplied: the import fails with a package error and no download is attempted. |
| 376 | let before = res!(run_text(&root, "before.typ", code)); |
| 377 | let d = res!(before.first_error().ok_or_else(|| err!("The import of an absent package succeeded."; Invalid))); |
| 378 | assert_eq!(d.kind, DiagnosticKind::Package); |
| 379 | assert_eq!(d.message, "package not found (searched for @local/memdemo:1.2.3)"); |
| 380 | assert!(d.hints.iter().any(|h| h.contains("never downloads")), "{:?}", d.hints); |
| 381 | // Supplied, the entrypoint's own imports resolve inside the package, and its data reads by package path. |
| 382 | let spec = res!(supply_demo("1.2.3", 21)); |
| 383 | assert!(res!(package::supplied()).contains(&spec)); |
| 384 | // A version is immutable: the same files again change nothing, different files are refused. |
| 385 | res!(supply_demo("1.2.3", 21)); |
| 386 | let changed = supply_demo("1.2.3", 22); |
| 387 | let refused = res!(changed.err().ok_or_else(|| err!("A supplied version was replaced by different files."; Invalid))); |
| 388 | assert!(diag_message(&refused).contains("immutable"), "{}", diag_message(&refused)); |
| 389 | let after = res!(run_text(&root, "after.typ", &fmt!("{}#let d = read(\"/x\")\n", code))); |
| 390 | // `read("/x")` is the project's file, absent here: the package supply must not have shadowed the project. |
| 391 | let e = res!(after.first_error().ok_or_else(|| err!("The read of an absent project file succeeded."; Invalid))); |
| 392 | assert_eq!(e.kind, DiagnosticKind::MissingFile, "{}", e.message); |
| 393 | let ok = res!(run_text(&root, "ok.typ", code)); |
| 394 | assert!(!ok.failed, "the supplied package did not import: {:?}", ok.first_error()); |
| 395 | assert_eq!(ok.modules, 3, "the package, its entrypoint and the file the entrypoint imports are cached once each"); |
| 396 | // Its files are in the vfs under the package's own root, and read-only. |
| 397 | let file = spec.root().join("data/d.txt"); |
| 398 | assert_eq!(res!(vfs::read(&file).map_err(|e| err!("{}", e; IO))), b"memory data".to_vec()); |
| 399 | assert!(vfs::is_file(&file) && !vfs::is_dir(&file) && vfs::is_dir(&spec.root().join("data"))); |
| 400 | assert_eq!(vfs::write(&file, b"x").map_err(|e| e.kind()), Err(std::io::ErrorKind::PermissionDenied), |
| 401 | "a package file must be refused as read-only, not merely fail to be written"); |
| 402 | assert!(vfs::read(&spec.root().join("data/absent.txt")).is_err()); |
| 403 | // Another version is a different package. |
| 404 | let other = res!(run_text(&root, "other.typ", "#import \"@local/memdemo:1.2.4\"\n")); |
| 405 | let o = res!(other.first_error().ok_or_else(|| err!("An unsupplied version imported."; Invalid))); |
| 406 | assert_eq!(o.kind, DiagnosticKind::Package); |
| 407 | assert!(o.hints.iter().any(|h| h.contains("1.2.3")), "the hint names the version the host did supply: {:?}", o.hints); |
| 408 | // Withdrawn, it is gone. |
| 409 | assert!(res!(package::withdraw(&spec))); |
| 410 | assert!(!res!(package::withdraw(&spec)), "a package withdrawn twice"); |
| 411 | let gone = res!(run_text(&root, "gone.typ", code)); |
| 412 | assert!(gone.failed); |
| 413 | assert!(vfs::read(&file).is_err()); |
| 414 | // Once withdrawn, the version may be supplied afresh, with other files. |
| 415 | let again = res!(supply_demo("1.2.3", 22)); |
| 416 | let fresh = res!(run_text(&root, "fresh.typ", "#import \"@local/memdemo:1.2.3\": v\n#assert.eq(v, 22)\n")); |
| 417 | assert!(!fresh.failed, "{:?}", fresh.first_error().map(|d| d.message.clone())); |
| 418 | assert!(res!(package::withdraw(&again))); |
| 419 | Ok(()) |
| 420 | } |
| 421 | |
| 422 | // The examples below import a real package from Typst's own cache and use it. Austenite's library is not |
| 423 | // complete, so an evaluation error is reported and not failed; a panic, a run that does not end, and a |
| 424 | // failure to find the package or one of its own files are failures. |
| 425 | const REAL: [&str; 4] = ["real_oxifmt", "real_tablex", "real_fletcher", "real_cetz"]; |
| 426 | |
| 427 | #[test] |
| 428 | fn real_packages_import_without_a_panic_or_a_runaway() -> Outcome<()> { |
| 429 | let cache = match std::env::var("HOME") { |
| 430 | Ok(h) => PathBuf::from(h).join(".cache/typst/packages/preview"), |
| 431 | Err(_) => PathBuf::new(), |
| 432 | }; |
| 433 | if !cache.is_dir() { |
| 434 | let skip = std::env::var("EVAL_ORACLE_SKIP").map(|v| v == "1").unwrap_or(false); |
| 435 | assert!(skip, "no package cache at {} (set EVAL_ORACLE_SKIP=1 to skip explicitly)", cache.display()); |
| 436 | println!("[eval-import] SKIPPED: no package cache at {}", cache.display()); |
| 437 | return Ok(()); |
| 438 | } |
| 439 | let area = corpus_dir().join("import"); |
| 440 | let limit = harness::timeout(); |
| 441 | let mut faults = Vec::new(); |
| 442 | for name in REAL { |
| 443 | let path = area.join(fmt!("{}.typ", name)); |
| 444 | assert!(path.is_file(), "{} is missing", path.display()); |
| 445 | match austenite::eval_only(&path, &area, limit) { |
| 446 | harness::austenite::RunEnd::Done(out) => match &out.eval { |
| 447 | None => println!("[eval-import] {}: evaluated without error", name), |
| 448 | Some(e) => { |
| 449 | println!("[eval-import] {}: {}", name, e); |
| 450 | for l in ["package not found", "file not found", "cyclic import", "would escape", "package manifest"] { |
| 451 | if e.contains(l) { |
| 452 | faults.push(fmt!("{}: the package did not load ({})", name, e)); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | }, |
| 457 | harness::austenite::RunEnd::Panic(m) => faults.push(fmt!("{}: evaluation panicked: {}", name, m)), |
| 458 | harness::austenite::RunEnd::Timeout(s) => faults.push(fmt!("{}: evaluation ran past {}s", name, s)), |
| 459 | } |
| 460 | } |
| 461 | assert!(faults.is_empty(), "{} fault(s):\n{}", faults.len(), faults.join("\n")); |
| 462 | Ok(()) |
| 463 | } |
| 464 | |
| 465 | fn corpus_dir() -> PathBuf { harness::corpus::fixtures_dir() } |
| 466 | |
| 467 | #[test] |
| 468 | fn the_import_corpus_stays_synthetic_and_inside_its_area() -> Outcome<()> { |
| 469 | for area in ["import", "package"] { |
| 470 | for fx in res!(fixtures(area)) { |
| 471 | if let Some(f) = harness::corpus::provenance_fault(&fx) { |
| 472 | return Err(err!("{}", f; Invalid)); |
| 473 | } |
| 474 | assert!(fx.expect == Expect::Accepts || fx.expect == Expect::Rejects); |
| 475 | } |
| 476 | } |
| 477 | Ok(()) |
| 478 | } |