7mod create_parent_directory;
10mod get_thumbnail_dimensions;
13use crate::create_thumbnail::create_thumbnail;
14use crate::get_thumbnail_dimensions::TargetDimension;
16#[derive(Debug, Parser)]
17#[clap(version, about)]
19 /// Path to the image to be thumbnailed
22 /// Path to the directory to save the thumbnail in
26 /// Height of the thumbnail to create
30 /// Width of the thumbnail to create
36 let cli = Cli::parse();
38 let target = match (cli.width, cli.height) {
39 (Some(w), Some(h)) => TargetDimension::BoundingBox(w, h),
40 (Some(w), None) => TargetDimension::MaxWidth(w),
41 (None, Some(h)) => TargetDimension::MaxHeight(h),
44 "Failed to create thumbnail: you must pass at least one of --width or --height"
46 std::process::exit(1);
50 match create_thumbnail(&cli.path, &cli.out_dir, target) {
51 Ok(thumbnail_path) => print!("{}", thumbnail_path.display()),
54 std::process::exit(1);
61 reason = "cargo_bin is deprecated, cargo_bin! is not, `use` does not differentiate them. See https://github.com/assert-rs/assert_cmd/issues/258"
65 use std::path::PathBuf;
67 use assert_cmd::Command;
68 use predicates::prelude::*;
70 use crate::test_utils::get_dimensions;
73 fn it_creates_a_thumbnail_with_max_width() {
74 Command::cargo_bin("create_thumbnail")
76 .args(&["src/tests/red.png", "--width=50", "--out-dir=/tmp"])
79 .stdout("/tmp/red.png")
82 assert_eq!(get_dimensions(&PathBuf::from("/tmp/red.png")), (50, 100));
86 fn it_creates_a_thumbnail_with_max_height() {
87 Command::cargo_bin("create_thumbnail")
89 .args(&["src/tests/noise.jpg", "--height=128", "--out-dir=/tmp"])
92 .stdout("/tmp/noise.jpg")
95 assert_eq!(get_dimensions(&PathBuf::from("/tmp/noise.jpg")), (64, 128));
99 fn it_creates_a_thumbnail_with_a_bounding_box() {
100 Command::cargo_bin("create_thumbnail")
103 "src/tests/noise.jpg",
110 .stdout("/tmp/noise.jpg")
113 assert_eq!(get_dimensions(&PathBuf::from("/tmp/noise.jpg")), (32, 64));
117 fn it_fails_if_you_pass_neither_width_nor_height() {
118 Command::cargo_bin("create_thumbnail")
120 .args(&["src/tests/red.png", "--out-dir=/tmp"])
126 "Failed to create thumbnail: you must pass at least one of --width or --height\n",
131 fn it_fails_if_you_pass_a_non_existent_file() {
132 Command::cargo_bin("create_thumbnail")
134 .args(&["doesnotexist.txt", "--width=50", "--out-dir=/tmp"])
139 .stderr("Failed to open image: No such file or directory (os error 2)\n");
143 fn it_fails_if_you_pass_a_non_image() {
144 Command::cargo_bin("create_thumbnail")
146 .args(&["Cargo.toml", "--width=50", "--out-dir=/tmp"])
151 .stderr("Failed to open image: The file extension `.\"toml\"` was not recognized as an image format\n");
154 // TODO: Improve this error message.
156 // It's good to know the tool won't completely break when this happens, but ideally
157 // we'd return a more meaningful error message in this case.
159 fn it_fails_if_out_dir_is_a_file() {
160 Command::cargo_bin("create_thumbnail")
162 .args(&["src/images/noise.jpg", "--width=50", "--out-dir=README.md"])
167 .stderr("I/O error: File exists (os error 17)\n");
171 fn it_fails_if_you_try_to_overwrite_the_original_file() {
172 Command::cargo_bin("create_thumbnail")
174 .args(&["src/images/noise.jpg", "--width=50", "--out-dir=src/images"])
179 .stderr("Cannot write thumbnail to the same path as the original image\n");
183 fn it_prints_the_version() {
184 // Match strings like `create_thumbnail 1.2.3`
185 let is_version_string =
186 predicate::str::is_match(r"^create_thumbnail [0-9]+\.[0-9]+\.[0-9]+\n$").unwrap();
188 Command::cargo_bin("create_thumbnail")
193 .stdout(is_version_string)
198 fn it_prints_the_help() {
199 // Match strings like `create_thumbnail 1.2.3`
201 predicate::str::is_match(r"create_thumbnail \[OPTIONS\] --out-dir").unwrap();
203 Command::cargo_bin("create_thumbnail")
208 .stdout(is_help_text)
215 use std::path::PathBuf;
217 use image::GenericImageView;
219 /// Return a path to a temporary directory to use for testing.
221 /// This function does *not* create the directory, just the path.
222 pub fn test_dir() -> PathBuf {
223 let tmp_dir = tempfile::tempdir().unwrap();
225 tmp_dir.path().to_owned()
228 /// Return the dimensions for an image.
229 pub fn get_dimensions(path: &PathBuf) -> (u32, u32) {
230 let img = image::open(path).unwrap();