Skip to main content

create_thumbnail: use more impl AsRef<Path>; tidy up tmp dirs in tests

ID
a44eee3
date
2026-09-26 15:18:34+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
024e4fe
message
create_thumbnail: use more impl AsRef<Path>; tidy up tmp dirs in tests
changed files
2 files, 46 additions, 41 deletions

Changed files

src/create_thumbnail.rs (16108 → 16492)

diff --git a/src/create_thumbnail.rs b/src/create_thumbnail.rs
index cd5c3bc..bc616f3 100644
--- a/src/create_thumbnail.rs
+++ b/src/create_thumbnail.rs
@@ -16,7 +16,7 @@ use crate::is_animated_gif::is_animated_gif;
 /// the thumbnail within the output directory.
 pub fn create_thumbnail(
     path: impl AsRef<Path>,
-    out_dir: &PathBuf,
+    out_dir: impl AsRef<Path>,
     page: Option<usize>,
     target: TargetDimension,
 ) -> Result<PathBuf, ThumbnailError> {
@@ -26,7 +26,7 @@ pub fn create_thumbnail(
         .and_then(|e| e.to_str())
         .map(|e| e.to_ascii_lowercase());
 
-    fs::create_dir_all(out_dir)?;
+    fs::create_dir_all(&out_dir)?;
 
     match (ext.as_deref(), page) {
         (Some("pdf"), page_num) => {
@@ -49,7 +49,7 @@ pub fn create_thumbnail(
 /// Create a thumbnail for a PDF.
 fn create_pdf_thumbnail(
     pdf_path: impl AsRef<Path>,
-    out_dir: &PathBuf,
+    out_dir: impl AsRef<Path>,
     page: usize,
     target: TargetDimension,
 ) -> Result<PathBuf, ThumbnailError> {
@@ -120,7 +120,7 @@ fn create_pdf_thumbnail(
 ///
 fn create_animated_gif_thumbnail(
     path: impl AsRef<Path>,
-    out_dir: &PathBuf,
+    out_dir: impl AsRef<Path>,
     target: TargetDimension,
 ) -> Result<PathBuf, ThumbnailError> {
     let (width, height) = thumbnail_dimensions(&path, target)?;
@@ -129,6 +129,7 @@ fn create_animated_gif_thumbnail(
         .file_name()
         .ok_or(ThumbnailError::MissingFileName)?;
 
+    let out_dir = out_dir.as_ref();
     let thumbnail_path = out_dir.join(file_name).with_extension("mp4");
 
     let gif_path_str = gif_path
@@ -186,7 +187,7 @@ fn ensure_even(x: u32) -> u32 {
 ///
 fn create_static_thumbnail(
     image_path: impl AsRef<Path>,
-    out_dir: &PathBuf,
+    out_dir: impl AsRef<Path>,
     target: TargetDimension,
 ) -> Result<PathBuf, ThumbnailError> {
     let image_path = image_path.as_ref();
@@ -194,6 +195,7 @@ fn create_static_thumbnail(
         .file_name()
         .ok_or(ThumbnailError::MissingFileName)?;
 
+    let out_dir = out_dir.as_ref();
     let thumbnail_path = out_dir.join(file_name);
 
     // Make sure we don't overwrite the original image with a thumbnail
@@ -217,9 +219,11 @@ fn create_static_thumbnail(
 
 #[cfg(test)]
 mod test_create_image_thumbnail {
+    use anyhow::Result;
+
     use super::create_thumbnail;
     use crate::dimensions::TargetDimension;
-    use crate::test_utils::{get_dimensions, test_dir};
+    use crate::test_utils::get_dimensions;
 
     macro_rules! image_thumbnail_tests {
         ($($name:ident: {
@@ -232,13 +236,15 @@ mod test_create_image_thumbnail {
         {
             $(
                 #[test]
-                fn $name() {
-                    let out_dir = test_dir();
-                    let thumbnail_path = create_thumbnail($img_path, &out_dir, None, $target).unwrap();
+                fn $name() -> Result<()> {
+                    let out_dir = tempfile::tempdir()?;
+                    let thumbnail_path = create_thumbnail($img_path, &out_dir, None, $target)?;
 
-                    assert_eq!(thumbnail_path, out_dir.join($expected_filename));
+                    assert_eq!(thumbnail_path, out_dir.as_ref().join($expected_filename));
                     assert!(thumbnail_path.exists());
                     assert_eq!(get_dimensions(&thumbnail_path), $expected_dims);
+
+                    Ok(())
                 }
             )*
         }
@@ -309,13 +315,15 @@ mod test_create_image_thumbnail {
 
     // Passing a page number is an error if the file isn't a PDF.
     #[test]
-    fn errors_if_page_for_images() {
-        let out_dir = test_dir();
+    fn errors_if_page_for_images() -> Result<()> {
+        let out_dir = tempfile::tempdir()?;
         let page = Some(1);
         let target = TargetDimension::MaxWidth(50);
 
         let result = create_thumbnail("src/tests/noise.jpg", &out_dir, page, target);
         assert!(result.is_err());
+
+        Ok(())
     }
 }
 
@@ -327,7 +335,7 @@ mod test_create_pdf_thumbnail {
     use super::create_thumbnail;
     use crate::dimensions::TargetDimension;
     use crate::errors::ThumbnailError;
-    use crate::test_utils::{get_dimensions, test_dir};
+    use crate::test_utils::get_dimensions;
 
     macro_rules! pdf_thumbnail_tests {
         ($($name: ident: {
@@ -341,11 +349,11 @@ mod test_create_pdf_thumbnail {
         {
             $(
                 #[test]
-                fn $name() {
-                    let out_dir = test_dir();
-                    let thumbnail_path = create_thumbnail($pdf_path, &out_dir, Some($page), $target).unwrap();
+                fn $name() -> Result<()> {
+                    let out_dir = tempfile::tempdir()?;
+                    let thumbnail_path = create_thumbnail($pdf_path, &out_dir, Some($page), $target)?;
 
-                    assert_eq!(thumbnail_path, out_dir.join($expected_filename));
+                    assert_eq!(thumbnail_path, out_dir.as_ref().join($expected_filename));
                     assert!(thumbnail_path.exists());
                     assert_eq!(get_dimensions(&thumbnail_path), $expected_dims);
 
@@ -353,7 +361,9 @@ mod test_create_pdf_thumbnail {
                     // If the image format doesn't match the extension,
                     // (for example, if this is a PNG saved with a .jpeg
                     // suffix) this will error.
-                    ImageReader::open(&thumbnail_path).unwrap();
+                    ImageReader::open(&thumbnail_path)?;
+
+                    Ok(())
                 }
             )*
         }
@@ -398,12 +408,14 @@ mod test_create_pdf_thumbnail {
     }
 
     #[test]
-    fn errors_if_nonexistent_pdf() {
-        let out_dir = test_dir();
+    fn errors_if_nonexistent_pdf() -> Result<()> {
+        let out_dir = tempfile::tempdir()?;
         let target = TargetDimension::MaxWidth(50);
 
         let result = create_thumbnail("does_not_exist.pdf", &out_dir, None, target);
         assert!(matches!(result, Err(ThumbnailError::PdfOpenError(_))));
+
+        Ok(())
     }
 
     #[test]
@@ -413,7 +425,7 @@ mod test_create_pdf_thumbnail {
             .rand_bytes(5)
             .tempfile()?;
 
-        let out_dir = test_dir();
+        let out_dir = tempfile::tempdir()?;
         let target = TargetDimension::MaxWidth(50);
 
         let result = create_thumbnail(pdf_file.path(), &out_dir, None, target);
@@ -434,7 +446,7 @@ mod test_create_pdf_thumbnail {
         ];
         std::fs::write(&pdf_path, png_header).unwrap();
 
-        let out_dir = test_dir();
+        let out_dir = tempfile::tempdir()?;
         let target = TargetDimension::MaxWidth(50);
 
         let result = create_thumbnail(&pdf_path, &out_dir, None, target);
@@ -444,10 +456,10 @@ mod test_create_pdf_thumbnail {
     }
 
     #[test]
-    fn errors_if_zero_pdf_page() {
+    fn errors_if_zero_pdf_page() -> Result<()> {
         let page = Some(0);
 
-        let out_dir = test_dir();
+        let out_dir = tempfile::tempdir()?;
         let target = TargetDimension::MaxWidth(50);
 
         let result = create_thumbnail("src/tests/rgb.pdf", &out_dir, page, target);
@@ -458,13 +470,15 @@ mod test_create_pdf_thumbnail {
                 total_pages: 3
             })
         ));
+
+        Ok(())
     }
 
     #[test]
-    fn errors_if_too_large_pdf_page() {
+    fn errors_if_too_large_pdf_page() -> Result<()> {
         let page = Some(5);
 
-        let out_dir = test_dir();
+        let out_dir = tempfile::tempdir()?;
         let target = TargetDimension::MaxWidth(50);
 
         let result = create_thumbnail("src/tests/rgb.pdf", &out_dir, page, target);
@@ -475,5 +489,7 @@ mod test_create_pdf_thumbnail {
                 total_pages: 3
             })
         ));
+
+        Ok(())
     }
 }

src/main.rs (11697 → 11363)

diff --git a/src/main.rs b/src/main.rs
index 4ed99d2..4eba07c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -89,14 +89,13 @@ fn main() {
 
 #[cfg(test)]
 mod test_cli {
-    use std::fs;
     use std::path::PathBuf;
 
     use anyhow::Result;
     use assert_cmd::Command;
     use predicates::prelude::*;
 
-    use crate::test_utils::{get_dimensions, test_dir};
+    use crate::test_utils::get_dimensions;
 
     macro_rules! cli_tests {
         ($($name:ident: {
@@ -164,9 +163,8 @@ mod test_cli {
         let img_path = manifest_dir.join("src/tests/noise.jpg");
 
         // Create a temporary directory to be the CWD.
-        let tmp_dir = test_dir();
-        fs::create_dir_all(&tmp_dir)?;
-        let expected_thumbnail_path = tmp_dir.join("noise.jpg");
+        let tmp_dir = tempfile::tempdir()?;
+        let expected_thumbnail_path = tmp_dir.as_ref().join("noise.jpg");
 
         // Run the command, and check the image is created with the
         // correct dimensions in the temp dir.
@@ -291,20 +289,11 @@ mod test_cli {
 
 #[cfg(test)]
 pub mod test_utils {
-    use std::path::{Path, PathBuf};
+    use std::path::Path;
     use std::process::Command;
 
     use image::GenericImageView;
 
-    /// Return a path to a temporary directory to use for testing.
-    ///
-    /// This function does *not* create the directory, just the path.
-    pub fn test_dir() -> PathBuf {
-        let tmp_dir = tempfile::tempdir().unwrap();
-
-        tmp_dir.path().to_owned()
-    }
-
     /// Returns the dimensions `(width, height)` of an image or video file.
     pub fn get_dimensions(path: impl AsRef<Path>) -> (u32, u32) {
         let path = path.as_ref();