Skip to main content

create_thumbnail: use CWD as output directory if no `--out-dir` flag

ID
72d9323
date
2026-09-22 06:57:21+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
41912f7
message
create_thumbnail: use CWD as output directory if no `--out-dir` flag

I forget to add this flag all the time, and the current working
directory is a safe default.
changed files
4 files, 54 additions, 8 deletions

Changed files

CHANGELOG.md (720 → 862)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7922899..e2f4d54 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Changelog
 
+## v1.2.0 - 2026-09-22
+
+Omitting the `--out-dir` parameter now creates the thumbnail in your current working directory, rather than failing.
+
 ## v1.1.1 - 2025-12-09
 
 Pin the version of the image crate to 0.25.8; there's an issue with [artefacts in JPEG images](https://github.com/image-rs/image/issues/2688) in 0.25.9.

Cargo.lock (36730 → 36741)

diff --git a/Cargo.lock b/Cargo.lock
index 0c094ce..b68cb00 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -324,8 +324,9 @@ dependencies = [
 
 [[package]]
 name = "create_thumbnail"
-version = "1.1.1"
+version = "1.2.0"
 dependencies = [
+ "anyhow",
  "assert_cmd",
  "clap",
  "image",

Cargo.toml (201 → 238)

diff --git a/Cargo.toml b/Cargo.toml
index 36738d2..afab24e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,11 +1,14 @@
 [package]
 name = "create_thumbnail"
-version = "1.1.1"
+version = "1.2.0"
 edition = "2021"
 
 [dependencies]
-assert_cmd = "2"
 clap = { version = "4", features = ["derive"] }
-image = "0.25.9"
+image = "0.25"
+
+[dev-dependencies]
+anyhow = "1.0.104"
+assert_cmd = "2"
 predicates = "3"
 tempfile = "3"

src/main.rs (6473 → 7867)

diff --git a/src/main.rs b/src/main.rs
index 6d76e43..82d26df 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,6 @@
 #![deny(warnings)]
 
+use std::env;
 use std::path::PathBuf;
 
 use clap::Parser;
@@ -19,8 +20,12 @@ struct Cli {
     /// Path to the image to be thumbnailed
     path: PathBuf,
 
-    /// Path to the directory to save the thumbnail in
-    #[arg(long)]
+    /// Path to the directory to save the thumbnail in. Defaults to
+    /// the current working directory.
+    #[arg(
+        long,
+        default_value_os_t = env::current_dir().unwrap_or_default()
+    )]
     out_dir: PathBuf,
 
     /// Height of the thumbnail to create
@@ -58,12 +63,14 @@ 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;
+    use crate::test_utils::{get_dimensions, test_dir};
 
     #[test]
     fn it_creates_a_thumbnail_with_max_width() {
@@ -109,6 +116,37 @@ mod test_cli {
         assert_eq!(get_dimensions(&PathBuf::from("/tmp/noise.jpg")), (32, 64));
     }
 
+    // Omitting `--out-dir` creates the thumbnail in CWD.
+    #[test]
+    fn it_defaults_to_working_directory() -> Result<()> {
+        // Get an absolute path to the source image
+        let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?);
+        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");
+
+        // Run the command, and check the image is created with the
+        // correct dimensions in the temp dir.
+        //
+        // Don't check the exact output, because the exact output may not
+        // match exactly; e.g. on macOS tmp_dir will be in /var, but the
+        // CLI output starts /private/var (the former symlinks the latter).
+        Command::cargo_bin("create_thumbnail")?
+            .current_dir(&tmp_dir)
+            .arg(&img_path)
+            .args(["--width=64", "--height=64"])
+            .assert()
+            .success()
+            .stderr("");
+
+        assert_eq!(get_dimensions(&expected_thumbnail_path), (32, 64));
+
+        Ok(())
+    }
+
     #[test]
     fn it_fails_if_you_pass_neither_width_nor_height() {
         Command::cargo_bin("create_thumbnail")
@@ -194,7 +232,7 @@ mod test_cli {
     fn it_prints_the_help() {
         // Match strings like `create_thumbnail 1.2.3`
         let is_help_text =
-            predicate::str::is_match(r"create_thumbnail \[OPTIONS\] --out-dir").unwrap();
+            predicate::str::is_match(r"create_thumbnail \[OPTIONS\] <PATH>").unwrap();
 
         Command::cargo_bin("create_thumbnail")
             .unwrap()