//! A LISP routine in a `run` script runs to its end before the script goes on and the drawing is //! saved, however many slices it takes (#3635). use std::process::{Command, Output}; fn cli(args: &[&str]) -> Output { Command::new(env!("CARGO_BIN_EXE_cadcraft-cli")).args(args).output().unwrap() } /// A fresh scratch directory that is removed on drop, also when an assertion fails. struct Scratch(std::path::PathBuf); impl Scratch { fn new(tag: &str) -> Self { let dir = std::env::temp_dir().join(format!("cc-cli-{tag}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir(&dir).expect("create scratch dir"); Scratch(dir) } } impl Drop for Scratch { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } } /// The loop takes many slices: the circle drawn after it is in the saved drawing, and the next /// line runs once the routine has ended. #[test] fn a_long_routine_in_a_script_draws_before_the_save() { let scratch = Scratch::new("lisp-script"); let saved = scratch.0.join("out.dxf"); let saved = saved.to_str().unwrap(); let script = r#"(progn (repeat 1000000 (setq x 1)) (command "circle" "0,0" "5") (princ "done"))\n(princ (+ x 1))"#; let out = cli(&["run", "--metric", "--script", script, "--save", saved]); let err = String::from_utf8_lossy(&out.stderr); assert!(out.status.success(), "{:?}: {err}", out.status); assert!(err.lines().any(|l| l == "done") && err.lines().any(|l| l == "2"), "{err}"); let info = cli(&["info", saved]); let v: serde_json::Value = serde_json::from_slice(&info.stdout).unwrap(); assert_eq!(v["counts"]["Circle"], 1, "{v}"); }