oxedyne/fe2o3/fe2o3_austenite/tests/sys_inputs_cli.rs
4.0 KiB, 1 run
created by r1870400018:74539, 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 | //! `--input key=value` on the command line reaches the document as `sys.inputs`, as `typst compile --input` |
| 2 | //! does (D-20261018-03): on a plain compile, under `--explain`, and on `austenite build`. Each source asserts |
| 3 | //! what it should see, so a run that succeeds is a run whose document saw it; a failed assertion fails the |
| 4 | //! compile and the run. |
| 5 | |
| 6 | use std::path::{ |
| 7 | Path, |
| 8 | PathBuf, |
| 9 | }; |
| 10 | use std::process::{ |
| 11 | Command, |
| 12 | Output, |
| 13 | }; |
| 14 | |
| 15 | // A fresh case directory holding `main.typ` with `src`. |
| 16 | fn case(name: &str, src: &str) -> PathBuf { |
| 17 | let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("sys_inputs_cli").join(name); |
| 18 | let _ = std::fs::remove_dir_all(&dir); |
| 19 | std::fs::create_dir_all(&dir).expect("the case's directory"); |
| 20 | std::fs::write(dir.join("main.typ"), src).expect("the case's source"); |
| 21 | dir |
| 22 | } |
| 23 | |
| 24 | fn run(dir: &Path, args: &[&str]) -> Output { |
| 25 | Command::new(env!("CARGO_BIN_EXE_austenite")) |
| 26 | .current_dir(dir) |
| 27 | .args(args) |
| 28 | .output() |
| 29 | .expect("the built austenite binary") |
| 30 | } |
| 31 | |
| 32 | fn said(out: &Output) -> String { |
| 33 | format!("stdout: {}\nstderr: {}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)) |
| 34 | } |
| 35 | |
| 36 | const ENGINE: &str = "#assert.eq(sys.inputs.at(\"engine\", default: none), \"austenite\")\n\ |
| 37 | #assert.eq(sys.inputs, (engine: \"austenite\", note: \"a=b\"))\nCreated using Austenite.\n"; |
| 38 | |
| 39 | const NONE: &str = "#assert.eq(sys.inputs, (:))\n#assert.eq(sys.inputs.at(\"engine\", default: none), none)\nPlain.\n"; |
| 40 | |
| 41 | #[test] |
| 42 | fn a_plain_compile_passes_each_input_to_the_document() { |
| 43 | let d = case("plain", ENGINE); |
| 44 | let out = run(&d, &["--input", "engine=austenite", "--input", "note=a=b", "main.typ", "out"]); |
| 45 | assert!(out.status.success(), "the document saw its inputs\n{}", said(&out)); |
| 46 | assert!(d.join("out/document.pdf").is_file()); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn explain_passes_each_input_to_the_document() { |
| 51 | let d = case("explain", ENGINE); |
| 52 | let out = run(&d, &["--explain", "main.typ", "out", "--input", "engine=austenite", "--input", "note=a=b"]); |
| 53 | assert!(out.status.success(), "the document saw its inputs under --explain\n{}", said(&out)); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn build_passes_each_input_to_the_document() { |
| 58 | let d = case("build", ENGINE); |
| 59 | let out = run(&d, &["build", "main.typ", "--set", "view.open=false", "--input", "engine=austenite", "--input", "note=a=b"]); |
| 60 | assert!(out.status.success(), "the document saw its inputs under build\n{}", said(&out)); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn no_input_gives_the_document_an_empty_dictionary() { |
| 65 | let d = case("none", NONE); |
| 66 | let out = run(&d, &["main.typ", "out"]); |
| 67 | assert!(out.status.success(), "sys.inputs is empty with no --input\n{}", said(&out)); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn a_wrong_expectation_fails_the_run() { |
| 72 | // The control: the assertions above are what make a success mean the document saw its inputs. |
| 73 | let d = case("control", ENGINE); |
| 74 | let out = run(&d, &["--input", "engine=typst", "--input", "note=a=b", "main.typ", "out"]); |
| 75 | assert!(!out.status.success(), "a document that sees another engine fails its assertion\n{}", said(&out)); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn a_malformed_input_is_refused_by_name() { |
| 80 | let d = case("malformed", NONE); |
| 81 | for (arg, why) in [("=x", "key"), (" =x", "key"), ("noeq", "=")] { |
| 82 | let out = run(&d, &["--input", arg, "main.typ", "out"]); |
| 83 | let s = said(&out); |
| 84 | assert!(!out.status.success(), "--input {:?} is refused\n{}", arg, s); |
| 85 | assert!(s.contains("--input") && s.contains(arg.trim()) && s.contains(why), "the refusal names {:?}\n{}", arg, s); |
| 86 | } |
| 87 | let out = run(&d, &["main.typ", "out", "--input"]); |
| 88 | let s = said(&out); |
| 89 | assert!(!out.status.success() && s.contains("--input needs key=value"), "a missing value is refused\n{}", s); |
| 90 | let out = run(&d, &["build", "main.typ", "--input", "=x"]); |
| 91 | let s = said(&out); |
| 92 | assert!(!out.status.success() && s.contains("--input"), "build refuses it too\n{}", s); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn version_names_the_engine_and_its_commit() { |
| 97 | let d = case("version", NONE); |
| 98 | let out = run(&d, &["--version"]); |
| 99 | let o = String::from_utf8_lossy(&out.stdout).to_string(); |
| 100 | assert!(out.status.success() && o.starts_with("austenite ") && o.contains('('), "{}", said(&out)); |
| 101 | } |