Skip to content

Commit

Permalink
Merge branch 'main' into api_versions_rewrite_without_test
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored Nov 25, 2024
2 parents ee94d1f + 7edc4f6 commit 2e7c8aa
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion shotover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ aws-sdk-kms = { version = "1.1.0", optional = true }
chacha20poly1305 = { version = "0.10.0", features = ["std"], optional = true }
generic-array = { version = "0.14", features = ["serde"], optional = true }
kafka-protocol = { version = "0.13.0", optional = true, default-features = false, features = ["messages_enums", "broker", "client"] }
rustls = { version = "0.23.0", default-features = false, features = ["tls12"] }
rustls = { version = "0.23.18", default-features = false, features = ["tls12"] }
tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] }
rustls-pemfile = "2.0.0"
rustls-pki-types = "1.0.1"
Expand Down
2 changes: 1 addition & 1 deletion test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ docker-compose-runner = "0.3.0"
j4rs = "0.21.0"
futures-util = "0.3.28"
http = "1.1.0"
rustls = { version = "0.23.2", default-features = false }
rustls = { version = "0.23.18", default-features = false }
rustls-pki-types = "1.0.1"
rustls-pemfile = "2.0.0"
tokio-tungstenite = { version = "0.24", features = ["rustls-tls-native-roots"] }
Expand Down
62 changes: 61 additions & 1 deletion test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod mock_cassandra;
pub mod shotover_process;
mod test_tracing;

use anyhow::{anyhow, Result};
use anyhow::{anyhow, Error, Result};
use std::path::Path;
use subprocess::{Exec, Redirection};

Expand Down Expand Up @@ -38,16 +38,76 @@ pub fn run_command(command: &str, args: &[&str]) -> Result<String> {
}
}

fn command_not_found_error(command: &str, args: &[&str]) -> Error {
// Maps a command to its associated dependency; however, if the name of the command and dependency is the same, we just use the command name.
// Currently, the only use case is mapping `npm` to its dependency `nodejs`.
let dependency = match command {
"npm" => "nodejs",
_ => command,
};

let args_part = if !args.is_empty() {
format!(" {}", args.join(" "))
} else {
String::new()
};

anyhow!(
"Attempted to run the command `{}{}` but {} does not exist. Have you installed {}?",
command,
args_part,
command,
dependency
)
}

pub async fn run_command_async(current_dir: &Path, command: &str, args: &[&str]) {
let output = tokio::process::Command::new(command)
.args(args)
.current_dir(current_dir)
.kill_on_drop(true)
.status()
.await
.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
return command_not_found_error(command, args);
}

anyhow!(e)
})
.unwrap();

if !output.success() {
panic!("command {command} {args:?} failed. See above output.")
}
}

#[cfg(test)]
mod tests {
use super::*;
use futures_util::FutureExt;

#[tokio::test]
async fn test_run_command_async_not_found_message() {
let dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let command = "shotover_non_existent_command";
let args = &["arg1", "arg2"];
let result = std::panic::AssertUnwindSafe(run_command_async(dir, command, args))
.catch_unwind()
.await;

assert!(result.is_err(), "Expected a panic but none occurred");

if let Err(panic_info) = result {
if let Some(error_message) = panic_info.downcast_ref::<String>() {
assert!(
error_message.contains("Attempted to run the command `shotover_non_existent_command arg1 arg2` but shotover_non_existent_command does not exist. Have you installed shotover_non_existent_command?"),
"Error message did not contain the expected NotFound error got: {}",
error_message
);
} else {
panic!("Panic payload was not a string: {:?}", panic_info);
}
}
}
}

0 comments on commit 2e7c8aa

Please sign in to comment.