Skip to content

A fast, helpful, and open-source document parser

Merge branch 'main' into fix/buffer-staging-tempdir-leak

Logan23d579c6086b
diff --cc crates/liteparse/src/conversion.rs
index 670f767,0b3d787..802a52f
--- a/crates/liteparse/src/conversion.rs
+++ b/crates/liteparse/src/conversion.rs
@@@ -610,25 -622,46 +624,66 @@@ mod tests
assert!(r.unwrap_err().to_string().contains("unsupported"));
}
+ /// The staging `TempDir` returned by `convert_data_to_pdf` must be
+ /// cleaned up when dropped — both on success and failure paths.
+ /// Here we verify the failure path: the error propagates, and once
+ /// the returned temps are dropped the staging directory is gone.
+ #[tokio::test]
+ async fn test_convert_data_to_pdf_staging_cleaned_on_drop() {
+ // Build a staging dir manually so we can inspect its path after drop.
+ let staging_dir = tempfile::Builder::new()
+ .prefix("liteparse-staging-")
+ .tempdir()
+ .unwrap();
+ let staging_path = staging_dir.path().to_path_buf();
+ assert!(staging_path.exists());
+
+ // Dropping the TempDir removes the directory.
+ drop(staging_dir);
+ assert!(
+ !staging_path.exists(),
+ "staging temp dir should be removed on drop"
++
+ // ── find_pdf_in_dir ──────────────────────────────────────────────────────
+
+ /// Regression test for the LibreOffice filename-sanitisation bug.
+ ///
+ /// LibreOffice strips characters like parentheses and leading digit
+ /// sequences from filenames, so the output PDF stem often differs from the
+ /// input file stem. `find_pdf_in_dir` must locate the PDF regardless of
+ /// what LibreOffice chose to call it.
+ ///
+ /// Scenario: input was `52304751_AnuragLahare_E2 (1).docx`; LibreOffice
+ /// wrote `AnuragLahare_E2_1_.pdf` instead of the expected
+ /// `52304751_AnuragLahare_E2 (1).pdf`.
+ #[tokio::test]
+ async fn test_find_pdf_in_dir_returns_sanitised_name() {
+ let tmp = tempfile::tempdir().unwrap();
+ // Simulate LibreOffice writing a sanitised filename.
+ let sanitised = tmp.path().join("AnuragLahare_E2_1_.pdf");
+ tokio::fs::write(&sanitised, b"%PDF-1.4").await.unwrap();
+
+ let result = find_pdf_in_dir(tmp.path().to_str().unwrap()).await;
+ assert!(result.is_ok(), "should find PDF even with sanitised name");
+ assert!(
+ result.unwrap().ends_with("AnuragLahare_E2_1_.pdf"),
+ "returned path should point to the sanitised file"
+ );
+ }
+
+ /// When LibreOffice somehow produces no PDF (unexpected failure), the
+ /// helpful error message must still be returned.
+ #[tokio::test]
+ async fn test_find_pdf_in_dir_empty_dir_returns_error() {
+ let tmp = tempfile::tempdir().unwrap();
+ let result = find_pdf_in_dir(tmp.path().to_str().unwrap()).await;
+ assert!(result.is_err());
+ assert!(
+ result
+ .unwrap_err()
+ .to_string()
+ .contains("output PDF not found"),
+ "error message should mention missing PDF"
);
}
}