Skip to main content

Check the dimensions of the thumbnail created in the tests

ID
542c772
date
2024-08-20 10:58:27+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
1af5bac
message
Check the dimensions of the thumbnail created in the tests
changed files
3 files, 22 additions, 3 deletions

Changed files

src/main.rs (7928) → src/main.rs (8684)

diff --git a/src/main.rs b/src/main.rs
index 392c8a1..6d0f2d1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,11 +16,14 @@ use crate::is_animated_gif::is_animated_gif;
 
 /// Create a thumbnail for the image, and return the relative path of
 /// the thumbnail within the collection folder.
+///
+/// TODO: Having two Option<u32> arguments is confusing because they could
+/// easily be swapped.  Replace this with some sort of struct!
 pub fn create_thumbnail(
     path: &PathBuf,
     out_dir: &PathBuf,
-    height: Option<u32>,
     width: Option<u32>,
+    height: Option<u32>,
 ) -> io::Result<PathBuf> {
     let thumbnail_path = out_dir.join(path.file_name().unwrap());
     create_parent_directory(&thumbnail_path)?;
@@ -28,6 +31,9 @@ pub fn create_thumbnail(
     // TODO: Does this check do what I think?
     assert!(*path != thumbnail_path);
 
+    println!("w = {:?}", width);
+    println!("h = {:?}", height);
+
     let (new_width, new_height) = get_thumbnail_dimensions(&path, width, height)
         .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()))?;
 
@@ -46,7 +52,7 @@ mod test_create_thumbnail {
     use std::path::PathBuf;
 
     use super::create_thumbnail;
-    use crate::test_utils::test_dir;
+    use crate::test_utils::{get_dimensions, test_dir};
 
     #[test]
     fn creates_an_animated_gif_thumbnail() {
@@ -74,6 +80,7 @@ mod test_create_thumbnail {
 
         assert_eq!(thumbnail_path, out_dir.join("yellow.gif"));
         assert!(thumbnail_path.exists());
+        assert_eq!(get_dimensions(&thumbnail_path), (16, 8));
     }
 
     #[test]
@@ -88,6 +95,7 @@ mod test_create_thumbnail {
 
         assert_eq!(thumbnail_path, out_dir.join("red.png"));
         assert!(thumbnail_path.exists());
+        assert_eq!(get_dimensions(&thumbnail_path), (16, 32));
     }
 
     #[test]
@@ -102,6 +110,7 @@ mod test_create_thumbnail {
 
         assert_eq!(thumbnail_path, out_dir.join("noise.jpg"));
         assert!(thumbnail_path.exists());
+        assert_eq!(get_dimensions(&thumbnail_path), (16, 32));
     }
 
     #[test]
@@ -116,6 +125,7 @@ mod test_create_thumbnail {
 
         assert_eq!(thumbnail_path, out_dir.join("green.tiff"));
         assert!(thumbnail_path.exists());
+        assert_eq!(get_dimensions(&thumbnail_path), (16, 16));
     }
 
     #[test]
@@ -130,6 +140,7 @@ mod test_create_thumbnail {
 
         assert_eq!(thumbnail_path, out_dir.join("purple.webp"));
         assert!(thumbnail_path.exists());
+        assert_eq!(get_dimensions(&thumbnail_path), (16, 16));
     }
 }
 
@@ -162,7 +173,7 @@ fn main() {
 
     println!("args = {:?}", cli);
 
-    create_thumbnail(&cli.path, &cli.out_dir, cli.height, cli.width).unwrap();
+    create_thumbnail(&cli.path, &cli.out_dir, cli.width, cli.height).unwrap();
 }
 
 #[cfg(test)]
@@ -232,6 +243,7 @@ pub mod test_utils {
 
     use assert_cmd::assert::OutputAssertExt;
     use assert_cmd::Command;
+    use image::GenericImageView;
 
     /// Return a path to a temporary directory to use for testing.
     ///
@@ -242,6 +254,13 @@ pub mod test_utils {
         tmp_dir.path().to_owned()
     }
 
+    /// Return the dimensions for an image.
+    pub fn get_dimensions(path: &PathBuf) -> (u32, u32) {
+        let img = image::open(path).unwrap();
+
+        img.dimensions()
+    }
+
     pub struct DcOutput {
         pub exit_code: i32,
         pub stdout: String,

src/tests/noise.jpg (25175) → src/tests/noise.jpg (19075)

diff --git a/src/tests/noise.jpg b/src/tests/noise.jpg
index 0998e82..a1a0aa4 100644
Binary files a/src/tests/noise.jpg and b/src/tests/noise.jpg differ

src/tests/yellow.gif (500) → src/tests/yellow.gif (78)

diff --git a/src/tests/yellow.gif b/src/tests/yellow.gif
index 4f72bf4..500222b 100644
Binary files a/src/tests/yellow.gif and b/src/tests/yellow.gif differ