Skip to main content

cli: switch to table-driven tests

ID
46d8c54
date
2026-09-26 14:48:11+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
92d1a04
message
cli: switch to table-driven tests

As well as making the tests cleaner, this fixes a flaky test where two
tests could write to `/tmp/noise.jpg`, and a thumbnail created by one
test could affect the result of another test.
changed files
1 file, 41 additions, 37 deletions

Changed files

src/main.rs (8978 → 9277)

diff --git a/src/main.rs b/src/main.rs
index b32fa59..c10765c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -72,48 +72,52 @@ mod test_cli {
 
     use crate::test_utils::{get_dimensions, test_dir};
 
-    #[test]
-    fn it_creates_a_thumbnail_with_max_width() {
-        Command::cargo_bin("create_thumbnail")
-            .unwrap()
-            .args(&["src/tests/red.png", "--width=50", "--out-dir=/tmp"])
-            .assert()
-            .success()
-            .stdout("/tmp/red.png")
-            .stderr("");
+    macro_rules! cli_tests {
+        ($($name:ident: {
+            args:              $args:expr,
+            expected_filename: $expected_filename:expr,
+            expected_dims:     $expected_dims:expr,
+        },)*) => {
+        $(
+            #[test]
+            fn $name() -> Result<()> {
+                let out_dir = tempfile::tempdir()?;
+                let thumbnail_path = out_dir.path().join($expected_filename);
 
-        assert_eq!(get_dimensions("/tmp/red.png"), (50, 100));
-    }
+                Command::cargo_bin("create_thumbnail")
+                    .unwrap()
+                    .args($args)
+                    .arg("--out-dir")
+                    .arg(out_dir.path().display().to_string())
+                    .assert()
+                    .success()
+                    .stdout(thumbnail_path.display().to_string())
+                    .stderr("");
 
-    #[test]
-    fn it_creates_a_thumbnail_with_max_height() {
-        Command::cargo_bin("create_thumbnail")
-            .unwrap()
-            .args(&["src/tests/noise.jpg", "--height=128", "--out-dir=/tmp"])
-            .assert()
-            .success()
-            .stdout("/tmp/noise.jpg")
-            .stderr("");
+                assert_eq!(get_dimensions(thumbnail_path), $expected_dims);
 
-        assert_eq!(get_dimensions("/tmp/noise.jpg"), (64, 128));
+                Ok(())
+            }
+        )*
+        }
     }
 
-    #[test]
-    fn it_creates_a_thumbnail_with_a_bounding_box() {
-        Command::cargo_bin("create_thumbnail")
-            .unwrap()
-            .args(&[
-                "src/tests/noise.jpg",
-                "--width=64",
-                "--height=64",
-                "--out-dir=/tmp",
-            ])
-            .assert()
-            .success()
-            .stdout("/tmp/noise.jpg")
-            .stderr("");
-
-        assert_eq!(get_dimensions("/tmp/noise.jpg"), (32, 64));
+    cli_tests! {
+        max_width: {
+            args:              &["src/tests/red.png", "--width=50"],
+            expected_filename: "red.png",
+            expected_dims:     (50, 100),
+        },
+        max_height: {
+            args:              &["src/tests/noise.jpg", "--height=128"],
+            expected_filename: "noise.jpg",
+            expected_dims:     (64, 128),
+        },
+        bounding_box: {
+            args:              &["src/tests/noise.jpg", "--width=64", "--height=64"],
+            expected_filename: "noise.jpg",
+            expected_dims:     (32, 64),
+        },
     }
 
     // Omitting `--out-dir` creates the thumbnail in CWD.