Skip to main content

Create a new run_command() function/macro

ID
00b5557
date
2025-01-06 20:41:34+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
4e7970b
message
Create a new `run_command()` function/macro
changed files
3 files, 70 additions, 11 deletions

Changed files

Cargo.lock (17587) → Cargo.lock (18076)

diff --git a/Cargo.lock b/Cargo.lock
index 5eaf43e..cf41795 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -221,7 +221,7 @@ dependencies = [
  "image-webp",
  "kmeans_colors",
  "palette",
- "regex",
+ "predicates",
 ]
 
 [[package]]
@@ -241,6 +241,15 @@ dependencies = [
 ]
 
 [[package]]
+name = "float-cmp"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
 name = "getrandom"
 version = "0.2.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -341,6 +350,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "normalize-line-endings"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
+
+[[package]]
 name = "num-traits"
 version = "0.2.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -398,7 +413,10 @@ checksum = "68b87bfd4605926cdfefc1c3b5f8fe560e3feca9d5552cf68c466d3d8236c7e8"
 dependencies = [
  "anstyle",
  "difflib",
+ "float-cmp",
+ "normalize-line-endings",
  "predicates-core",
+ "regex",
 ]
 
 [[package]]

Cargo.toml (500) → Cargo.toml (500)

diff --git a/Cargo.toml b/Cargo.toml
index 0d25350..731ac2a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,7 +7,7 @@ edition = "2018"
 assert_cmd = "2.0.16"
 clap = { version = "4.5.21", features = ["derive"] }
 image-webp = "0.2.0"
-regex = "1.11.1"
+predicates = "3"
 
 [dependencies.kmeans_colors]
 version = "0.6.0"

src/main.rs (10964) → src/main.rs (12127)

diff --git a/src/main.rs b/src/main.rs
index 899d235..fb65a73 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -85,7 +85,9 @@ mod tests {
 
     use assert_cmd::assert::OutputAssertExt;
     use assert_cmd::Command;
-    use regex::Regex;
+    use predicates::prelude::*;
+
+    use crate::test_helpers::run_command;
 
     // Note: for the purposes of these tests, I mostly trust the k-means code
     // provided by the external library.
@@ -336,14 +338,19 @@ mod tests {
 
     #[test]
     fn it_prints_the_version() {
-        let output = get_success(&["--version"]);
-
-        let re = Regex::new(r"^dominant_colours [0-9]+\.[0-9]+\.[0-9]+\n$").unwrap();
-
-        assert!(re.is_match(&output.stdout));
-
-        assert_eq!(output.exit_code, 0);
-        assert_eq!(output.stderr, "");
+        let result = crate::run_command!("--version");
+
+        // This predicate checks that the output looks something
+        // like `dominant_colours 1.2.3`
+        let predicate_fn =
+            predicate::str::is_match(r"^dominant_colours [0-9]+\.[0-9]+\.[0-9]+\n$").unwrap();
+
+        // Check the command:
+        //  - succeeded
+        //  - with stdout that matched the given regex
+        //  - with empty stderr
+        //
+        result.success().stdout(predicate_fn).stderr("");
     }
 
     struct DcOutput {
@@ -380,3 +387,37 @@ mod tests {
         }
     }
 }
+
+#[cfg(test)]
+#[macro_use]
+mod test_helpers {
+    use assert_cmd::assert::Assert;
+    use assert_cmd::Command;
+
+    #[macro_export]
+    macro_rules! run_command {
+        // Match zero arguments
+        () => {
+            run_command(&[])
+        };
+
+        // Match one or more arguments
+        ($($arg:expr),+ $(,)?) => {{
+            let args = &[$($arg),*];
+            run_command(args)
+        }};
+    }
+
+    /// Run this command-line tool with the given arguments.
+    ///
+    /// This returns an `assert_cmd::assert::Assert` that will allow
+    /// you to make assertions about the output.
+    /// See https://docs.rs/assert_cmd/latest/assert_cmd/assert/struct.Assert.html
+    pub fn run_command(args: &[&str]) -> Assert {
+        let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
+
+        let assert = cmd.args(args).assert();
+
+        assert
+    }
+}