Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_cli_paths.rs

3.0 KiB, 2 runs

created by r1870400018:63574, 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` resolves the source and the root as `typst compile` does: both are made canonical, so
2//! a source named relative to the working directory under an absolute `--root`, or the reverse, still lies
3//! within the root and the files it reaches for are found.
4//!
5//! Each case compiles a synthetic project with the built binary run from inside it.
6
7use std::path::{
8 Path,
9 PathBuf,
10};
11use std::process::Command;
12
13// A project: `main.typ` includes and imports files below it. Written fresh for each case.
14fn project(name: &str) -> PathBuf {
15 let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_cli_paths").join(name);
16 let _ = std::fs::remove_dir_all(&dir);
17 std::fs::create_dir_all(dir.join("sub")).expect("the project's directory");
18 std::fs::write(dir.join("main.typ"), "#import \"sub/lib.typ\": word\n#include \"sub/part.typ\"\n#word\n").expect("main");
19 std::fs::write(dir.join("sub/lib.typ"), "#let word = [Imported.]\n").expect("lib");
20 std::fs::write(dir.join("sub/part.typ"), "Included.\n").expect("part");
21 dir
22}
23
24// Compiles `src` with `--root root`, run from `cwd`; the exit status's success and the standard error.
25fn compile(cwd: &Path, root: &str, src: &str) -> (bool, String) {
26 let out_dir = cwd.join("out");
27 let out = Command::new(env!("CARGO_BIN_EXE_austenite"))
28 .current_dir(cwd)
29 .args(["--eval", "--diag-summary", "--root", root, src])
30 .arg(&out_dir)
31 .output()
32 .expect("the built austenite binary");
33 (out.status.success() && out_dir.join("document.pdf").is_file(), String::from_utf8_lossy(&out.stderr).to_string())
34}
35
36#[test]
37fn a_source_named_relative_to_the_working_directory_under_an_absolute_root_is_within_it() {
38 let dir = project("relative_source");
39 let (ok, err) = compile(&dir, dir.to_str().expect("a UTF-8 path"), "main.typ");
40 assert!(ok, "the relative source compiles under the absolute root:\n{}", err);
41 assert!(!err.contains("diag-error"), "no error is named:\n{}", err);
42}
43
44#[test]
45fn a_root_named_relative_to_the_working_directory_holds_an_absolute_source() {
46 let dir = project("relative_root");
47 let main = dir.join("main.typ");
48 let (ok, err) = compile(&dir, ".", main.to_str().expect("a UTF-8 path"));
49 assert!(ok, "the absolute source compiles under the relative root:\n{}", err);
50}
51
52#[test]
53fn a_root_and_a_source_that_climb_with_dotdot_agree_where_the_source_is() {
54 let dir = project("dotdot");
55 let (ok, err) = compile(&dir.join("sub"), "..", "../main.typ");
56 assert!(ok, "the source reached through `..` compiles under the root reached through `..`:\n{}", err);
57}
58
59#[test]
60fn a_bare_source_name_with_no_root_takes_the_working_directory_as_its_root() {
61 let dir = project("bare_default");
62 let out_dir = dir.join("out");
63 let out = Command::new(env!("CARGO_BIN_EXE_austenite"))
64 .current_dir(&dir)
65 .args(["--eval", "--diag-summary", "main.typ"])
66 .arg(&out_dir)
67 .output()
68 .expect("the built austenite binary");
69 let err = String::from_utf8_lossy(&out.stderr).to_string();
70 assert!(out.status.success() && out_dir.join("document.pdf").is_file(),
71 "`main.typ` alone compiles, its root the directory it is in:\n{}", err);
72}