cli: test ordinary extract, split and render through the atomic writer

On the revision integrated with main's shared atomic writer
(craft::atomic), ordinary extract, split and render write the requested
pages, parts and image size, leave the source byte-for-byte unchanged,
and leave nothing else in the folder. The missing-value regression also
checks that a refused command writes nothing and keeps the source.
This commit is contained in:
jweaver3 committed 2026-11-28 22:53:30 -05:00
1 parent 0c8d342f1c
commit 820f2eb32c
1 file changed
+60 -1
+60 -1
View File
@@ -44,6 +44,62 @@ fn workdir(test: &str) -> (PathBuf, String) {
(d, name)
}
/// The page count and first page size `info` reports for a PDF the CLI wrote.
fn pages_of(path: &std::path::Path) -> (u64, serde_json::Value) {
let run = Command::new(BIN).args(["info", path.to_str().unwrap()]).output().unwrap();
assert!(run.status.success(), "info {}: {}", path.display(), String::from_utf8_lossy(&run.stderr));
let info: serde_json::Value = serde_json::from_slice(&run.stdout).unwrap();
(info["pages"].as_u64().unwrap(), info["first_page_pt"].clone())
}
/// The names in `dir`, sorted, so a test sees a stray staging file or a missing part.
fn listing(dir: &std::path::Path) -> Vec<String> {
let mut names: Vec<String> = std::fs::read_dir(dir).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect();
names.sort();
names
}
// Ordinary command lines through the shared atomic writer (`craft::atomic`): the lookup still
// finds each command's real options, writes what they ask for, and leaves the source alone.
#[test]
fn ordinary_extract_writes_the_chosen_pages_and_keeps_its_source() {
let (dir, input) = workdir("plain-extract");
let out = dir.join("out.pdf");
let run = Command::new(BIN).args(["extract", &input, "--pages", "2-3", "--out", out.to_str().unwrap()]).output().unwrap();
assert!(run.status.success(), "{}", String::from_utf8_lossy(&run.stderr));
assert_eq!(pages_of(&out), (2, serde_json::json!([200.0, 300.0])));
assert_eq!(std::fs::read(&input).unwrap(), fixture(3), "the source file changed");
assert_eq!(listing(&dir), ["in.pdf", "out.pdf"], "only the output was written");
}
#[test]
fn ordinary_split_writes_one_part_per_page_and_keeps_its_source() {
let (dir, input) = workdir("plain-split");
let parts = dir.join("parts");
let run = Command::new(BIN).args(["split", &input, "--every", "1", "--out-dir", parts.to_str().unwrap()]).output().unwrap();
assert!(run.status.success(), "{}", String::from_utf8_lossy(&run.stderr));
assert_eq!(listing(&parts), ["in-p1.pdf", "in-p2.pdf", "in-p3.pdf"]);
for n in 1..=3 {
assert_eq!(pages_of(&parts.join(format!("in-p{n}.pdf"))).0, 1, "part {n}");
}
assert_eq!(std::fs::read(&input).unwrap(), fixture(3), "the source file changed");
assert_eq!(listing(&dir), ["in.pdf", "parts"]);
}
#[test]
fn ordinary_render_writes_the_chosen_page_at_the_chosen_dpi_and_keeps_its_source() {
let (dir, input) = workdir("plain-render");
let png = dir.join("page.png");
let run = Command::new(BIN).args(["render", &input, "--page", "2", "--dpi", "36", "--out", png.to_str().unwrap()]).output().unwrap();
assert!(run.status.success(), "{}", String::from_utf8_lossy(&run.stderr));
let image = craft_image_codecs::decode(&std::fs::read(&png).unwrap()).unwrap();
// 200×300 pt at 36 dpi.
assert_eq!((image.width(), image.height()), (100, 150));
assert_eq!(std::fs::read(&input).unwrap(), fixture(3), "the source file changed");
assert_eq!(listing(&dir), ["in.pdf", "page.png"], "only the image was written");
}
#[test]
fn extract_does_not_write_over_its_source_when_the_password_is_out() {
let (dir, input) = workdir("extract");
@@ -61,7 +117,8 @@ fn extract_reads_the_real_pages_when_the_password_is_pages() {
// `--password --pages` then the file: the old lookup read the file's name as the page list.
let run = Command::new(BIN).args(["extract", "--password", "--pages", &input, "--pages", "2", "--out", out.to_str().unwrap()]).output().unwrap();
assert!(run.status.success(), "{}", String::from_utf8_lossy(&run.stderr));
assert!(out.is_file());
assert_eq!(pages_of(&out).0, 1, "the real --pages chose one page");
assert_eq!(std::fs::read(&input).unwrap(), fixture(3), "the source file changed");
}
#[test]
@@ -105,4 +162,6 @@ fn an_option_with_no_operand_is_named() {
assert!(stderr.contains(&format!("{option} needs a value")), "{args:?}: {stderr}");
}
assert!(!png.exists(), "nothing was rendered");
assert_eq!(listing(&dir), ["in.pdf"], "a refused command left a file behind");
assert_eq!(std::fs::read(&input).unwrap(), fixture(3), "the source file changed");
}