Repository: MarcoIeni/release-plz (rust)

Issue:
Test code completions
## Motivations

When we updated clap to version 4, the `generate-completions` subcommand broke. See https://github.com/MarcoIeni/release-plz/pull/328. We should add a test.

## Solution

Use [assert_cmd](https://crates.io/crates/assert_cmd) to add a test where we run this subcommand and we check that the outcome is successful.

## Alternatives

If you want to use an alternative to assert_cmd, leave a comment.


Pull request description:
Add tests for shell completions
An attempt to fix #330


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index fe9f3c9f..8216a094 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -63,6 +63,20 @@ dependencies = [
  "serde_json",
 ]
 
+[[package]]
+name = "assert_cmd"
+version = "2.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba45b8163c49ab5f972e59a8a5a03b6d2972619d486e19ec9fe744f7c2753d3c"
+dependencies = [
+ "bstr 1.0.1",
+ "doc-comment",
+ "predicates",
+ "predicates-core",
+ "predicates-tree",
+ "wait-timeout",
+]
+
 [[package]]
 name = "async-channel"
 version = "1.7.1"
@@ -158,6 +172,18 @@ dependencies = [
  "regex-automata",
 ]
 
+[[package]]
+name = "bstr"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fca0852af221f458706eb0725c03e4ed6c46af9ac98e6a689d5e634215d594dd"
+dependencies = [
+ "memchr",
+ "once_cell",
+ "regex-automata",
+ "serde",
+]
+
 [[package]]
 name = "bumpalo"
 version = "3.11.1"
@@ -877,6 +903,12 @@ version = "0.4.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
 
+[[package]]
+name = "difflib"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
+
 [[package]]
 name = "digest"
 version = "0.10.5"
@@ -1394,7 +1426,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "0a1e17342619edbc21a964c2afbeb6c820c6a2560032872f397bb97ea127bd0a"
 dependencies = [
  "aho-corasick",
- "bstr",
+ "bstr 0.2.17",
  "fnv",
  "log",
  "regex",
@@ -2086,7 +2118,7 @@ version = "0.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4ea3ebcd72a54701f56345f16785a6d3ac2df7e986d273eb4395c0b01db17952"
 dependencies = [
- "bstr",
+ "bstr 0.2.17",
  "winapi",
 ]
 
@@ -2307,6 +2339,33 @@ version = "0.2.17"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
 
+[[package]]
+name = "predicates"
+version = "2.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed6bd09a7f7e68f3f0bf710fb7ab9c4615a488b58b5f653382a687701e458c92"
+dependencies = [
+ "difflib",
+ "itertools",
+ "predicates-core",
+]
+
+[[package]]
+name = "predicates-core"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72f883590242d3c6fc5bf50299011695fa6590c2c70eac95ee1bdb9a733ad1a2"
+
+[[package]]
+name = "predicates-tree"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "54ff541861505aabf6ea722d2131ee980b8276e10a1297b94e896dd8b621850d"
+dependencies = [
+ "predicates-core",
+ "termtree",
+]
+
 [[package]]
 name = "pretty_env_logger"
 version = "0.4.0"
@@ -2520,6 +2579,7 @@ name = "release-plz"
 version = "0.2.25"
 dependencies = [
  "anyhow",
+ "assert_cmd",
  "chrono",
  "clap 4.0.23",
  "clap_complete 4.0.5",
@@ -3184,6 +3244,12 @@ dependencies = [
  "windows-sys 0.42.0",
 ]
 
+[[package]]
+name = "termtree"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95059e91184749cb66be6dc994f67f182b6d897cb3df74a5bf66b5e709295fd8"
+
 [[package]]
 name = "test_logs"
 version = "0.1.10"
@@ -3653,6 +3719,15 @@ dependencies = [
  "quote",
 ]
 
+[[package]]
+name = "wait-timeout"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
+dependencies = [
+ "libc",
+]
+
 [[package]]
 name = "waker-fn"
 version = "1.1.0"
diff --git a/crates/release_plz/Cargo.toml b/crates/release_plz/Cargo.toml
index 7e8b15df..640f7fcf 100644
--- a/crates/release_plz/Cargo.toml
+++ b/crates/release_plz/Cargo.toml
@@ -28,3 +28,6 @@ tracing-log.workspace = true
 tracing-subscriber = { workspace = true, features = ["env-filter"] }
 tracing.workspace = true
 url.workspace = true
+
+[dev-dependencies]
+assert_cmd = "2.0.6"


Test patch:
diff --git a/crates/release_plz/tests/completion_test.rs b/crates/release_plz/tests/completion_test.rs
new file mode 100644
index 00000000..d66e7421
--- /dev/null
+++ b/crates/release_plz/tests/completion_test.rs
@@ -0,0 +1,15 @@
+use assert_cmd::Command;
+use clap::ValueEnum;
+use clap_complete::Shell;
+
+#[test]
+fn test_generate_completions() -> anyhow::Result<()> {
+    for &shell in Shell::value_variants() {
+        Command::cargo_bin(env!("CARGO_PKG_NAME"))?
+            .arg("generate-completions")
+            .arg(shell.to_string())
+            .assert()
+            .success();
+    }
+    Ok(())
+}