Skip to content

Commit

Permalink
chore: Add tests for errors
Browse files Browse the repository at this point in the history
In order to better cover the expected bevahior of errors, this commit
adds a few tests to exercise the crate errors we use.
  • Loading branch information
endoze committed Nov 12, 2023
1 parent b551675 commit 49f7d22
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl<'a> Iterator for GetSecretsResultIter<'a> {
/// It provides methods to retrieve a list of stored secrets, retrieve a
/// specific secret's value, and to update a specific secret's value.
#[async_trait]
#[cfg(not(tarpaulin_include))]
pub trait QuerySecrets {
/// Requests a list of secrets from the secret provider.
///
Expand Down
72 changes: 72 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(missing_docs)]
#[derive(thiserror::Error, Debug)]
#[repr(u8)]
/// Enum to define all of the possible errors that can occur during normal
/// use of Nysm.
pub enum NysmError {
Expand Down Expand Up @@ -38,3 +39,74 @@ impl PartialEq for NysmError {
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}

#[cfg(not(tarpaulin_include))]
#[cfg(test)]
mod tests {
use super::*;
use serde::de::Error;

#[test]
fn test_serde_json_error() {
let error = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
assert_eq!(error.to_string(), "Unable to parse data as json");
}

#[test]
fn test_serde_yaml_error() {
let error = NysmError::SerdeYaml(serde_yaml::Error::custom("custom error"));
assert_eq!(error.to_string(), "Unable to parse data as yaml");
}

#[test]
fn test_bat_print_error() {
let error = NysmError::BatPrint(Into::<bat::error::Error>::into("custom error"));
assert_eq!(error.to_string(), "Unable to pretty print data");
}

#[test]
fn test_io_error() {
let error = NysmError::IO(std::io::Error::new(
std::io::ErrorKind::Other,
"custom error",
));
assert_eq!(
error.to_string(),
"Unable to read/write file caused by: custom error"
);
}

#[test]
fn test_aws_list_secrets_no_list_error() {
let error = NysmError::AwsListSecretsNoList;
assert_eq!(
error.to_string(),
"Unable to retrieve list of secrets from aws response"
);
}

#[test]
fn test_aws_secret_value_no_value_string_error() {
let error = NysmError::AwsSecretValueNoValueString;
assert_eq!(
error.to_string(),
"Unable to retrieve string value from aws response"
);
}

#[test]
fn test_aws_secret_value_update_error() {
let error = NysmError::AwsSecretValueUpdate;
assert_eq!(error.to_string(), "Unable to update secret value");
}

#[test]
fn test_partial_eq() {
let error1 = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
let error2 = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
let error3 = NysmError::SerdeYaml(serde_yaml::Error::custom("different error"));

assert_eq!(error1, error2);
assert_ne!(error1, error3);
}
}

0 comments on commit 49f7d22

Please sign in to comment.