Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_watch.rs

14.1 KiB, 2 runs

created by r1870400018:66903, 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 --watch` rebuilds when a file the evaluation read changes, and for no other cause.
2//!
3//! The watched set is the evaluator's own record of what it asked the vfs for ([`Evaluated::files_read`]),
4//! so these cases hold it to the files a synthetic project reads: its main, an import, an include, an image,
5//! a data file, a package's files and the fonts under `--font-path`, and to nothing beside them. The
6//! rebuild cases run the built binary over such a project, edit a file, and count the status lines it
7//! prints: one for the edit of a file it read, none for the edit of one it did not.
8
9use oxedyne_fe2o3_austenite::compile;
10use oxedyne_fe2o3_austenite::emit::sinks::PdfSink;
11use oxedyne_fe2o3_austenite::eval::package::{
12 self,
13 PackageSpec,
14};
15use oxedyne_fe2o3_austenite::flow::text::FontStore;
16
17use std::io::BufRead;
18use std::path::{
19 Path,
20 PathBuf,
21};
22use std::process::{
23 Child,
24 Command,
25 Stdio,
26};
27use std::sync::mpsc::{
28 self,
29 Receiver,
30};
31use std::time::Duration;
32
33const SVG: &str = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"10\" height=\"10\"><rect width=\"10\" height=\"10\" fill=\"red\"/></svg>";
34
35// A project beneath the target's temporary directory, written fresh for each case: a main that imports a
36// library, includes a part, shows an image and reads a data file, an unrelated file beside them, and a font
37// directory holding a file no parser accepts (the watch keys on what was found, not on what parsed).
38fn project(name: &str) -> PathBuf {
39 let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_watch").join(name);
40 let _ = std::fs::remove_dir_all(&dir);
41 std::fs::create_dir_all(dir.join("fonts")).expect("the project's directories");
42 let dir = std::fs::canonicalize(&dir).expect("the project's canonical path");
43 write(&dir.join("main.typ"), concat!(
44 "#import \"lib.typ\": word\n",
45 "#include \"part.typ\"\n",
46 "#let d = json(\"data.json\")\n",
47 "#image(\"pic.svg\", width: 1cm)\n",
48 "#word #d.n\n",
49 ));
50 write(&dir.join("lib.typ"), "#let word = [Imported.]\n");
51 write(&dir.join("part.typ"), "Included.\n");
52 write(&dir.join("data.json"), "{\"n\": 1}\n");
53 write(&dir.join("pic.svg"), SVG);
54 write(&dir.join("unrelated.typ"), "Nothing reads this.\n");
55 write(&dir.join("fonts/fake.ttf"), "not a font");
56 dir
57}
58
59fn write(path: &Path, text: &str) {
60 std::fs::write(path, text).expect("a project file");
61}
62
63// A package at `<dir>/local/<name>/0.1.0`, whose entrypoint `lib.typ` defines `greet`.
64fn package_dir(dir: &Path, name: &str) -> PathBuf {
65 let pkg = dir.join("packages");
66 let own = pkg.join("local").join(name).join("0.1.0");
67 std::fs::create_dir_all(&own).expect("the package's directory");
68 write(&own.join("typst.toml"), &format!("[package]\nname = \"{}\"\nversion = \"0.1.0\"\nentrypoint = \"lib.typ\"\n", name));
69 write(&own.join("lib.typ"), "#let greet = [Packaged.]\n");
70 pkg
71}
72
73// What the evaluator read compiling `main` under `root`, with each of `fonts` a font directory.
74fn read(main: &Path, root: &Path, fonts: &[&Path]) -> Vec<PathBuf> {
75 let mut store = FontStore::default();
76 for f in fonts {
77 store.add_dir(f.to_path_buf());
78 }
79 let mut sink = PdfSink::new().expect("a PDF sink");
80 let done = compile::assemble_eval(main, root, store, &mut sink).expect("the main is readable");
81 done.files_read()
82}
83
84#[test]
85fn the_watch_set_is_the_files_the_evaluation_read() {
86 let dir = project("set");
87 let pkgs = package_dir(&dir, "wdemo");
88 package::add_dir(pkgs.clone()).expect("the package directory is added");
89 write(&dir.join("main.typ"), concat!(
90 "#import \"lib.typ\": word\n",
91 "#import \"@local/wdemo:0.1.0\": greet\n",
92 "#include \"part.typ\"\n",
93 "#let d = json(\"data.json\")\n",
94 "#image(\"pic.svg\", width: 1cm)\n",
95 "#word #greet #d.n\n",
96 ));
97 let found = read(&dir.join("main.typ"), &dir, &[&dir.join("fonts")]);
98 let own = pkgs.join("local/wdemo/0.1.0");
99 let mut want = vec![
100 dir.join("main.typ"),
101 dir.join("lib.typ"),
102 dir.join("part.typ"),
103 dir.join("data.json"),
104 dir.join("pic.svg"),
105 own.join("typst.toml"),
106 own.join("lib.typ"),
107 dir.join("fonts"),
108 dir.join("fonts/fake.ttf"),
109 ];
110 want.sort();
111 assert_eq!(found, want, "the files read, the package's on disc beside the project's, and nothing else");
112 assert!(!found.contains(&dir.join("unrelated.typ")), "a file nothing reads is not watched");
113}
114
115#[test]
116fn a_file_asked_for_and_not_there_is_watched_so_that_its_arrival_is_seen() {
117 let dir = project("missing");
118 write(&dir.join("main.typ"), "#import \"later.typ\": x\n#x\n");
119 let found = read(&dir.join("main.typ"), &dir, &[]);
120 assert!(found.contains(&dir.join("main.typ")), "the main is watched: {:?}", found);
121 assert!(found.contains(&dir.join("later.typ")), "the import that failed is watched: {:?}", found);
122}
123
124#[test]
125fn a_package_supplied_in_memory_has_no_file_to_watch() {
126 let dir = project("memory");
127 let spec = PackageSpec { namespace: "local".to_string(), name: "memdemo".to_string(), version: (0, 1, 0) };
128 package::supply(&spec, vec![
129 ("typst.toml".to_string(), b"[package]\nname = \"memdemo\"\nversion = \"0.1.0\"\nentrypoint = \"lib.typ\"\n".to_vec()),
130 ("lib.typ".to_string(), b"#let greet = [Held.]\n".to_vec()),
131 ]).expect("the package is supplied");
132 write(&dir.join("main.typ"), "#import \"@local/memdemo:0.1.0\": greet\n#greet\n");
133 let found = read(&dir.join("main.typ"), &dir, &[]);
134 assert_eq!(found, vec![dir.join("main.typ")], "only the main stands on disc");
135 let _ = package::withdraw(&spec);
136}
137
138// A running `austenite --eval --watch`, its standard output and error read line by line on channels. It is
139// killed when it goes out of scope, whatever the case did.
140struct Watch {
141 child: Child,
142 out: Receiver<String>,
143 err: Receiver<String>,
144}
145
146impl Watch {
147 fn start(dir: &Path, extra: &[&str], envs: &[(&str, &Path)]) -> Self {
148 Self::run(dir, extra, "main.typ", envs)
149 }
150
151 fn run(dir: &Path, extra: &[&str], main: &str, envs: &[(&str, &Path)]) -> Self {
152 // The project was written an instant ago. A file saved within a tick of a build's start is built
153 // again, once, since its stamp cannot be told from one made during the build; no real start is so
154 // close behind a save, so the case waits it out and the quiet it asserts is the watch's own.
155 std::thread::sleep(Duration::from_millis(150));
156 let mut cmd = Command::new(env!("CARGO_BIN_EXE_austenite"));
157 cmd.current_dir(dir)
158 .args(["--eval", "--watch"])
159 .args(extra)
160 .args([main, "out"])
161 .stdout(Stdio::piped())
162 .stderr(Stdio::piped());
163 for (k, v) in envs {
164 cmd.env(k, v);
165 }
166 let mut child = cmd.spawn().expect("the built austenite binary");
167 let out = Self::lines(child.stdout.take().expect("a piped standard output"));
168 let err = Self::lines(child.stderr.take().expect("a piped standard error"));
169 Self { child, out, err }
170 }
171
172 fn lines<R: std::io::Read + Send + 'static>(from: R) -> Receiver<String> {
173 let (tx, rx) = mpsc::channel();
174 std::thread::spawn(move || {
175 for line in std::io::BufReader::new(from).lines().map_while(Result::ok) {
176 if tx.send(line).is_err() {
177 return;
178 }
179 }
180 });
181 rx
182 }
183
184 // The next status line of a rebuild that worked, within `wait`.
185 fn status(&self, wait: Duration) -> Option<String> {
186 let end = std::time::Instant::now() + wait;
187 loop {
188 let left = end.checked_duration_since(std::time::Instant::now())?;
189 match self.out.recv_timeout(left) {
190 Ok(l) if l.contains(" page(s), ") => return Some(l),
191 Ok(_) => continue,
192 Err(_) => return None,
193 }
194 }
195 }
196
197 // Is there no status line for `wait`?
198 fn quiet(&self, wait: Duration) -> bool {
199 self.status(wait).is_none()
200 }
201
202 // The next line of standard error that holds `needle`, within `wait`.
203 fn complaint(&self, needle: &str, wait: Duration) -> Option<String> {
204 let end = std::time::Instant::now() + wait;
205 loop {
206 let left = end.checked_duration_since(std::time::Instant::now())?;
207 match self.err.recv_timeout(left) {
208 Ok(l) if l.contains(needle) => return Some(l),
209 Ok(_) => continue,
210 Err(_) => return None,
211 }
212 }
213 }
214}
215
216impl Drop for Watch {
217 fn drop(&mut self) {
218 let _ = self.child.kill();
219 let _ = self.child.wait();
220 }
221}
222
223const FIRST: Duration = Duration::from_secs(40); // the first build
224const REBUILD: Duration = Duration::from_secs(30);
225const SETTLE: Duration = Duration::from_millis(2500); // six polls
226
227fn pdf(dir: &Path) -> Vec<u8> {
228 std::fs::read(dir.join("out/document.pdf")).expect("the PDF the watch wrote")
229}
230
231#[test]
232fn a_change_to_an_imported_file_rebuilds_once() {
233 let dir = project("import");
234 let w = Watch::start(&dir, &[], &[]);
235 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
236 assert!(w.quiet(SETTLE), "nothing rebuilds while nothing changes");
237 write(&dir.join("lib.typ"), "#let word = [Imported again, and longer.]\n");
238 assert!(w.status(REBUILD).is_some(), "an edit of the imported file rebuilds");
239 assert!(w.quiet(SETTLE), "and rebuilds once");
240}
241
242#[test]
243fn a_change_to_a_file_nothing_reads_rebuilds_nothing() {
244 let dir = project("unrelated");
245 let w = Watch::start(&dir, &[], &[]);
246 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
247 write(&dir.join("unrelated.typ"), "Still read by nothing.\n");
248 write(&dir.join("notes.txt"), "a new file beside the main\n");
249 assert!(w.quiet(SETTLE), "an edit of a file the evaluation did not read does not rebuild");
250 write(&dir.join("part.typ"), "Included, and edited.\n");
251 assert!(w.status(REBUILD).is_some(), "an edit of an included file still does");
252}
253
254#[test]
255fn a_change_to_an_image_or_a_data_file_rebuilds() {
256 let dir = project("assets");
257 let w = Watch::start(&dir, &[], &[]);
258 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
259 write(&dir.join("pic.svg"), &SVG.replace("red", "blue"));
260 assert!(w.status(REBUILD).is_some(), "an edit of the image rebuilds");
261 assert!(w.quiet(SETTLE), "once");
262 write(&dir.join("data.json"), "{\"n\": 2}\n");
263 assert!(w.status(REBUILD).is_some(), "an edit of the data file rebuilds");
264}
265
266#[test]
267fn a_font_under_the_font_path_is_watched() {
268 let dir = project("fonts");
269 let w = Watch::start(&dir, &["--font-path", "fonts"], &[]);
270 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
271 write(&dir.join("fonts/fake.ttf"), "still not a font, but changed");
272 assert!(w.status(REBUILD).is_some(), "an edit of a font file rebuilds");
273 assert!(w.quiet(SETTLE), "once");
274 write(&dir.join("fonts/another.otf"), "a font added");
275 assert!(w.status(REBUILD).is_some(), "a font added to the directory rebuilds");
276}
277
278#[test]
279fn a_change_to_a_package_file_rebuilds() {
280 let dir = project("package");
281 let pkgs = package_dir(&dir, "wwatch");
282 write(&dir.join("main.typ"), "#import \"@local/wwatch:0.1.0\": greet\n#greet\n");
283 let w = Watch::start(&dir, &[], &[("TYPST_PACKAGE_CACHE_PATH", &pkgs)]);
284 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
285 write(&pkgs.join("local/wwatch/0.1.0/lib.typ"), "#let greet = [Packaged, then edited.]\n");
286 assert!(w.status(REBUILD).is_some(), "an edit of a package's file rebuilds");
287}
288
289#[test]
290fn a_file_first_read_after_an_edit_is_watched_from_then_on() {
291 let dir = project("grows");
292 write(&dir.join("extra.typ"), "#let more = [Extra.]\n");
293 let w = Watch::start(&dir, &[], &[]);
294 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
295 write(&dir.join("extra.typ"), "#let more = [Extra, before anything imports it.]\n");
296 assert!(w.quiet(SETTLE), "extra.typ is not read yet, so an edit of it rebuilds nothing");
297 write(&dir.join("main.typ"), "#import \"extra.typ\": more\n#more\n");
298 assert!(w.status(REBUILD).is_some(), "the edit of the main rebuilds");
299 assert!(w.quiet(SETTLE), "once");
300 write(&dir.join("extra.typ"), "#let more = [Extra, now imported.]\n");
301 assert!(w.status(REBUILD).is_some(), "the file the rebuild read for the first time is now watched");
302}
303
304#[test]
305fn a_failed_rebuild_keeps_the_last_good_pdf_says_why_and_is_mended_by_the_file_it_stopped_at() {
306 let dir = project("failure");
307 let w = Watch::start(&dir, &[], &[]);
308 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
309 let good = pdf(&dir);
310 write(&dir.join("lib.typ"), "#let word = (\n");
311 assert!(w.complaint("error", REBUILD).is_some(), "a rebuild that fails says why");
312 assert!(w.quiet(SETTLE), "and prints no status line for it");
313 assert_eq!(pdf(&dir), good, "the last good PDF is as it was");
314 write(&dir.join("lib.typ"), "#let word = [Mended.]\n");
315 assert!(w.status(REBUILD).is_some(), "mending the file the failed build stopped at rebuilds");
316 assert!(w.quiet(SETTLE), "once");
317}
318
319#[test]
320fn strict_and_the_root_are_honoured_in_the_watch() {
321 let dir = project("strict");
322 std::fs::create_dir_all(dir.join("sub")).expect("a subdirectory");
323 write(&dir.join("sub/main.typ"), "#import \"/lib.typ\": word\n#word\n");
324 // Under the root the main sits in `sub` and reaches `lib.typ` above it by a root-absolute path.
325 let w = Watch::run(&dir, &["--strict", "--root", "."], "sub/main.typ", &[]);
326 assert!(w.status(FIRST).is_some(), "a main that reaches a root-absolute path builds under --root");
327 write(&dir.join("lib.typ"), "#set text(font: \"No Such Family Anywhere\")\n#let word = [Strict.]\n");
328 assert!(w.complaint("strict:", REBUILD).is_some(),
329 "under --strict a warning that refuses the compile fails the rebuild, and says so");
330 assert!(w.quiet(SETTLE), "with no status line");
331}
332
333#[test]
334fn the_alias_writes_where_it_is_told_and_reads_no_settings_file() {
335 let dir = project("alias");
336 std::fs::create_dir_all(dir.join("sub")).expect("a subdirectory");
337 write(&dir.join("sub/main.typ"), "#import \"/lib.typ\": word\n#word\n");
338 // A settings file `austenite watch` would obey: another place for the PDF, and its colour space.
339 write(&dir.join("austenite.jdat"), "{\"output\": \"elsewhere.pdf\", \"colour\": {\"space\": \"cmyk\"}}");
340 let w = Watch::run(&dir, &["--root", "."], "sub/main.typ", &[]);
341 assert!(w.status(FIRST).is_some(), "the first build prints a status line");
342 let bytes = pdf(&dir);
343 assert!(bytes.starts_with(b"%PDF-1.7"), "the PDF is in the output directory named from the working directory");
344 assert!(!bytes.windows(10).any(|w| w == b"DeviceCMYK"), "the settings file is not read");
345 assert!(!dir.join("sub/out").exists() && !dir.join("sub/elsewhere.pdf").exists() && !dir.join("elsewhere.pdf").exists(),
346 "nothing is written beside the source or where the file says");
347}