flpdf-cli 0.5.1

Command-line tools for flpdf, mirroring qpdf inspection and rewrite subcommands.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! qpdf 11.9.0 progress-reporting CLI contracts.

use assert_cmd::Command;
use std::path::{Path, PathBuf};

#[path = "support/eol.rs"]
mod eol;
use eol::EOL;

fn fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../tests/fixtures/compat")
        .join(name)
}

fn progress_lines(output_name: &str, percentages: &[u8]) -> String {
    percentages
        .iter()
        .map(|percent| format!("flpdf: {output_name}: write progress: {percent}%{EOL}"))
        .collect()
}

#[test]
fn progress_reports_file_output_on_info_stream() {
    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let output_path = tempdir.path().join("out.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id"])
        .arg(&input)
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        String::from_utf8(output.stdout).expect("progress output is UTF-8"),
        progress_lines(
            &output_path.display().to_string(),
            &[0, 29, 43, 58, 72, 86, 99, 100]
        )
    );
    assert!(output.stderr.is_empty());
    assert!(output_path.exists(), "progress write must create the PDF");
}

#[test]
fn progress_reaches_the_attachment_writer() {
    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let attachment_path = tempdir.path().join("payload.txt");
    std::fs::write(&attachment_path, b"attachment payload").expect("attachment");
    let output_path = tempdir.path().join("attachment-out.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id", "--add-attachment"])
        .arg(&attachment_path)
        .arg("--")
        .arg(&input)
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("progress output is UTF-8");
    assert!(
        stdout.starts_with(&format!(
            "flpdf: {}: write progress: 0%{EOL}",
            output_path.display()
        )),
        "{stdout}"
    );
    assert!(
        stdout.contains(&format!("write progress: 100%{EOL}")),
        "{stdout}"
    );
    assert!(stdout
        .lines()
        .all(|line| line.contains(&output_path.display().to_string())));
    assert!(output.stderr.is_empty());
    assert!(
        output_path.exists(),
        "attachment rewrite must create the PDF"
    );
}

#[test]
fn progress_keeps_pdf_on_stdout_and_reports_on_stderr() {
    let input = fixture("one-page.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id"])
        .arg(&input)
        .arg("-")
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(output.stdout.starts_with(b"%PDF-1.3\n"));
    assert_eq!(
        String::from_utf8(output.stderr).expect("progress output is UTF-8"),
        progress_lines("standard output", &[0, 29, 43, 58, 72, 86, 99, 100])
    );
}

#[test]
fn native_rewrite_progress_uses_the_same_reporter_route() {
    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let output_path = tempdir.path().join("rewrite-out.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["rewrite", "--progress", "--deterministic-id"])
        .arg(&input)
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        String::from_utf8(output.stdout).expect("progress output is UTF-8"),
        progress_lines(
            &output_path.display().to_string(),
            &[0, 29, 43, 58, 72, 86, 99, 100]
        )
    );
    assert!(output.stderr.is_empty());
    assert!(output_path.exists(), "progress rewrite must create the PDF");
}

#[test]
fn progress_warning_keeps_exit_three_and_completed_output() {
    let input = fixture_from_path("../../tests/fixtures/test_driver/repairable_input.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let output_path = tempdir.path().join("warning-out.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id"])
        .arg(&input)
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert_eq!(output.status.code(), Some(3));
    assert_eq!(
        String::from_utf8(output.stdout).expect("progress output is UTF-8"),
        progress_lines(&output_path.display().to_string(), &[0, 99, 100])
    );
    let stderr = String::from_utf8(output.stderr).expect("diagnostics are UTF-8");
    assert!(stderr.contains("file is damaged"), "stderr: {stderr}");
    assert!(
        stderr.contains("operation succeeded with warnings"),
        "stderr: {stderr}"
    );
    assert!(output_path.exists(), "warning exit must retain the output");
}

#[test]
fn progress_error_keeps_exit_two_without_progress_or_output() {
    let input = fixture_from_path("../../tests/fixtures/test_driver/open_repair_failure.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let output_path = tempdir.path().join("error-out.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id"])
        .arg(&input)
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert_eq!(output.status.code(), Some(2));
    assert!(output.stdout.is_empty());
    let stderr = String::from_utf8(output.stderr).expect("diagnostics are UTF-8");
    assert!(
        stderr.contains("unable to find trailer dictionary"),
        "stderr: {stderr}"
    );
    assert!(!output_path.exists(), "fatal input must not create output");
}

#[test]
fn progress_reports_each_split_writer_once() {
    let input = fixture("two-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let output_path = tempdir.path().join("split.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id", "--split-pages=1"])
        .arg(&input)
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("progress output is UTF-8");
    assert_eq!(
        stdout.matches(&format!("write progress: 0%{EOL}")).count(),
        2,
        "{stdout}"
    );
    assert_eq!(
        stdout
            .matches(&format!("write progress: 100%{EOL}"))
            .count(),
        2,
        "{stdout}"
    );
    assert!(stdout
        .lines()
        .all(|line| line.contains(&output_path.display().to_string())));
    assert!(tempdir.path().join("split-1.pdf").exists());
    assert!(tempdir.path().join("split-2.pdf").exists());
    assert!(output.stderr.is_empty());
}

#[test]
fn progress_reports_each_pages_split_writer_once() {
    let input = fixture("two-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let output_path = tempdir.path().join("pages-split.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id"])
        .arg(&input)
        .args(["--pages", ".", "1-2", "--", "--split-pages=1"])
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("progress output is UTF-8");
    assert_eq!(
        stdout.matches(&format!("write progress: 0%{EOL}")).count(),
        2,
        "{stdout}"
    );
    assert_eq!(
        stdout
            .matches(&format!("write progress: 100%{EOL}"))
            .count(),
        2,
        "{stdout}"
    );
    assert!(stdout
        .lines()
        .all(|line| line.contains(&output_path.display().to_string())));
    assert!(tempdir.path().join("pages-split-1.pdf").exists());
    assert!(tempdir.path().join("pages-split-2.pdf").exists());
    assert!(output.stderr.is_empty());
}

#[test]
fn remove_attachment_keeps_progress_off_stdout() {
    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let attachment_path = tempdir.path().join("payload.txt");
    std::fs::write(&attachment_path, b"attachment payload").expect("attachment");
    let with_attachment = tempdir.path().join("with-attachment.pdf");

    let add = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--deterministic-id", "--add-attachment"])
        .arg(&attachment_path)
        .arg("--key=rtkey")
        .arg("--")
        .arg(&input)
        .arg(&with_attachment)
        .output()
        .expect("flpdf invocation");
    assert!(
        add.status.success(),
        "add-attachment failed: {}",
        String::from_utf8_lossy(&add.stderr)
    );

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args([
            "--progress",
            "--deterministic-id",
            "--remove-attachment=rtkey",
        ])
        .arg(&with_attachment)
        .arg("-")
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // Before this fix, `run_remove_attachment` never prepared standard-output
    // routing, so a literal "-" output path both fell through to being
    // treated as a real filename and left progress notifications free to
    // interleave with PDF bytes on stdout instead of stderr.
    assert!(output.stdout.starts_with(b"%PDF-1.3\n"));
    assert!(
        !output.stdout.windows(8).any(|window| window == b"progress"),
        "progress text leaked into PDF stdout bytes"
    );
    assert_eq!(
        String::from_utf8(output.stderr).expect("progress output is UTF-8"),
        progress_lines("standard output", &[0, 21, 31, 41, 51, 61, 71, 81, 100])
    );
    assert!(
        !tempdir.path().join("-").exists(),
        "stdout output must not create a file literally named \"-\""
    );
}

/// Build a classic-xref (no object streams) PDF with an embedded attachment
/// and then corrupt its `startxref` offset the same way
/// `tests/fixtures/test_driver/repairable_input.pdf` is corrupted, so
/// opening it triggers qpdf-shaped xref reconstruction warnings while still
/// carrying an attachment to remove.
fn damaged_pdf_with_attachment(dir: &Path, key: &str) -> PathBuf {
    let input = fixture("one-page.pdf");
    let attachment_path = dir.join("payload.txt");
    std::fs::write(&attachment_path, b"attachment payload").expect("attachment");
    let clean_path = dir.join("clean-with-attachment.pdf");

    let add = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--object-streams=disable", "--add-attachment"])
        .arg(&attachment_path)
        .arg(format!("--key={key}"))
        .arg("--")
        .arg(&input)
        .arg(&clean_path)
        .output()
        .expect("flpdf invocation");
    assert!(
        add.status.success(),
        "add-attachment failed: {}",
        String::from_utf8_lossy(&add.stderr)
    );

    let bytes = std::fs::read(&clean_path).expect("read clean pdf");
    let marker_start = rfind_bytes(&bytes, b"startxref\n").expect("startxref present");
    let eof_offset = rfind_bytes(&bytes, b"%%EOF").expect("%%EOF present");
    let mut damaged = bytes[..marker_start].to_vec();
    damaged.extend_from_slice(b"startxref\n0\n");
    damaged.extend_from_slice(&bytes[eof_offset..]);

    let damaged_path = dir.join("damaged-with-attachment.pdf");
    std::fs::write(&damaged_path, damaged).expect("write damaged pdf");
    damaged_path
}

fn rfind_bytes(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack
        .windows(needle.len())
        .rposition(|window| window == needle)
}

#[test]
fn remove_attachment_stdout_output_omits_the_resulting_file_warning_suffix() {
    let tempdir = tempfile::tempdir().expect("tempdir");
    let damaged = damaged_pdf_with_attachment(tempdir.path(), "repkey");

    let file_output_path = tempdir.path().join("removed-file.pdf");
    let file_output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .arg("--remove-attachment=repkey")
        .arg(&damaged)
        .arg(&file_output_path)
        .output()
        .expect("flpdf invocation");
    assert_eq!(file_output.status.code(), Some(3));
    let file_stderr = String::from_utf8(file_output.stderr).expect("diagnostics are UTF-8");
    assert!(
        file_stderr
            .contains("operation succeeded with warnings; resulting file may have some problems"),
        "stderr: {file_stderr}"
    );

    let stdout_output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .arg("--remove-attachment=repkey")
        .arg(&damaged)
        .arg("-")
        .output()
        .expect("flpdf invocation");
    assert_eq!(stdout_output.status.code(), Some(3));
    assert!(stdout_output.stdout.starts_with(b"%PDF-1.3\n"));
    let stdout_stderr = String::from_utf8(stdout_output.stderr).expect("diagnostics are UTF-8");
    assert!(
        stdout_stderr.contains(&format!("operation succeeded with warnings{EOL}")),
        "stderr: {stdout_stderr}"
    );
    assert!(
        !stdout_stderr.contains("resulting file may have some problems"),
        "stdout output must not report a file-creation warning suffix: {stdout_stderr}"
    );
}

#[test]
fn progress_rotate_to_a_valid_destination_still_reports_progress() {
    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let output_path = tempdir.path().join("rotate-out.pdf");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id", "--rotate=90:1"])
        .arg(&input)
        .arg(&output_path)
        .output()
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        String::from_utf8(output.stdout).expect("progress output is UTF-8"),
        progress_lines(
            &output_path.display().to_string(),
            &[0, 29, 43, 58, 72, 86, 99, 100]
        )
    );
    assert!(output.stderr.is_empty());
    assert!(
        output_path.exists(),
        "the canonical writer must create the valid destination and let the rotate write succeed"
    );
}

#[test]
fn progress_rewrite_to_unusable_destination_fails_before_any_progress_output() {
    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    // A directory is never a writable regular-file destination, matching the
    // qpdf oracle probe (`qpdf --progress --rotate=90:1 in.pdf a-directory`),
    // which fails immediately with no progress line printed at all.
    let bad_destination = tempdir.path().join("not-a-file");
    std::fs::create_dir(&bad_destination).expect("directory destination");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id", "--rotate=90:1"])
        .arg(&input)
        .arg(&bad_destination)
        .output()
        .expect("flpdf invocation");

    assert!(!output.status.success());
    assert!(
        output.stdout.is_empty(),
        "no progress output must be printed before the writer opens the destination: {:?}",
        String::from_utf8_lossy(&output.stdout)
    );
    let stderr = String::from_utf8(output.stderr).expect("diagnostics are UTF-8");
    // The OS error text for opening a directory for writing is
    // platform-specific ("Is a directory" on Linux/macOS, "Access is denied"
    // on Windows); only the fail-fast-before-progress behavior above is the
    // qpdf-parity contract under test.
    assert!(!stderr.is_empty(), "expected a diagnostic on stderr");
}

// qpdf opens its real output destination exactly once in `wb+` mode before
// progress starts. An O_RDWR FIFO open does not wait for a reader, so the
// canonical file sink must retain that mode instead of reopening it with
// O_WRONLY after progress (Unix-only: named pipes are not exercised on
// Windows).
#[cfg(unix)]
#[test]
fn progress_rotate_to_a_fifo_destination_does_not_hang() {
    use std::io::Read;
    use std::process::Stdio;
    use std::sync::mpsc;
    use std::time::Duration;

    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let fifo_path = tempdir.path().join("out.fifo");
    assert!(
        std::process::Command::new("mkfifo")
            .arg(&fifo_path)
            .status()
            .expect("mkfifo invocation")
            .success(),
        "mkfifo must succeed"
    );

    let reader_fifo = fifo_path.clone();
    let reader = std::thread::spawn(move || {
        let mut file = std::fs::File::open(&reader_fifo).expect("open fifo for reading");
        let mut buf = Vec::new();
        file.read_to_end(&mut buf).expect("read fifo");
        buf
    });

    let child = std::process::Command::new(assert_cmd::cargo::cargo_bin("flpdf"))
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id", "--rotate=90:1"])
        .arg(&input)
        .arg(&fifo_path)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn flpdf");

    let (tx, rx) = mpsc::channel();
    std::thread::spawn(move || {
        let _ = tx.send(child.wait_with_output());
    });

    let output = rx
        .recv_timeout(Duration::from_secs(10))
        .expect("flpdf must not hang writing to a FIFO destination")
        .expect("flpdf invocation");

    assert!(
        output.status.success(),
        "flpdf failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let bytes = reader.join().expect("fifo reader thread");
    assert!(
        bytes.starts_with(b"%PDF-1.3\n"),
        "fifo reader did not receive the rewritten PDF"
    );
}

// Unlike a FIFO, opening a Unix domain socket path as a plain file fails
// immediately (ENXIO), so the canonical writer must still fail before any
// progress here (Unix-only: this socket kind is not exercised on Windows).
#[cfg(unix)]
#[test]
fn progress_rotate_to_a_socket_destination_fails_before_any_progress_output() {
    use std::os::unix::net::UnixListener;

    let input = fixture("one-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let socket_path = tempdir.path().join("out.sock");
    let _listener = UnixListener::bind(&socket_path).expect("bind unix socket");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id", "--rotate=90:1"])
        .arg(&input)
        .arg(&socket_path)
        .output()
        .expect("flpdf invocation");

    assert!(!output.status.success());
    assert!(
        output.stdout.is_empty(),
        "no progress output must be printed before the writer opens the destination: {:?}",
        String::from_utf8_lossy(&output.stdout)
    );
    let stderr = String::from_utf8(output.stderr).expect("diagnostics are UTF-8");
    assert!(!stderr.is_empty(), "expected a diagnostic on stderr");
}

#[test]
fn progress_pages_extraction_to_unusable_destination_fails_before_any_progress_output() {
    let input = fixture("two-page.pdf");
    let tempdir = tempfile::tempdir().expect("tempdir");
    let bad_destination = tempdir.path().join("not-a-file");
    std::fs::create_dir(&bad_destination).expect("directory destination");

    let output = Command::cargo_bin("flpdf")
        .expect("flpdf binary")
        .env("FLPDF_STATIC_ID_QUIET", "1")
        .args(["--progress", "--deterministic-id"])
        .arg(&input)
        .args(["--pages", ".", "1", "--"])
        .arg(&bad_destination)
        .output()
        .expect("flpdf invocation");

    assert!(!output.status.success());
    assert!(
        output.stdout.is_empty(),
        "no progress output must be printed before the writer opens the destination: {:?}",
        String::from_utf8_lossy(&output.stdout)
    );
    let stderr = String::from_utf8(output.stderr).expect("diagnostics are UTF-8");
    // The OS error text for opening a directory for writing is
    // platform-specific ("Is a directory" on Linux/macOS, "Access is denied"
    // on Windows); only the fail-fast-before-progress behavior above is the
    // qpdf-parity contract under test.
    assert!(!stderr.is_empty(), "expected a diagnostic on stderr");
}

fn fixture_from_path(path: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join(path)
}