From 5e0536d756b1d7e809da06213ccfaf75e64cfbdd Mon Sep 17 00:00:00 2001 From: Fries Date: Thu, 13 Jul 2023 09:29:28 -0700 Subject: [PATCH] add some tests for the shared crate this should test some stuff out in the test crate --- crates/cli/src/commands/add.rs | 2 +- crates/cli/src/commands/print.rs | 2 +- crates/cli/src/commands/remove.rs | 2 +- crates/shared/src/errors.rs | 22 +++++++++++++---- crates/shared/src/lib.rs | 2 ++ crates/shared/src/names.rs | 2 +- crates/shared/src/tests.rs | 39 +++++++++++++++++++++++++++++++ src/sites.rs | 2 +- 8 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 crates/shared/src/tests.rs diff --git a/crates/cli/src/commands/add.rs b/crates/cli/src/commands/add.rs index 13be1e4..72292e7 100644 --- a/crates/cli/src/commands/add.rs +++ b/crates/cli/src/commands/add.rs @@ -13,7 +13,7 @@ pub(crate) fn add( separator: &String, ) -> Result<(), Error> { let names_file = names::read_names_file(path)?; - let mut names = names::load_names(names_file)?; + let mut names = names::load_names(&names_file)?; if names.iter().any(|site| site.url.contains(url)) { return Err(Error { diff --git a/crates/cli/src/commands/print.rs b/crates/cli/src/commands/print.rs index bb67e65..bd89f13 100644 --- a/crates/cli/src/commands/print.rs +++ b/crates/cli/src/commands/print.rs @@ -15,7 +15,7 @@ pub(crate) fn print( json: bool, ) -> Result<(), Error> { let names_file = names::read_names_file(path)?; - let mut names = names::load_names(names_file)?; + let mut names = names::load_names(&names_file)?; if let Some(filter) = filter { names.retain(|f| &f.url == filter); diff --git a/crates/cli/src/commands/remove.rs b/crates/cli/src/commands/remove.rs index ebf68b7..5d37370 100644 --- a/crates/cli/src/commands/remove.rs +++ b/crates/cli/src/commands/remove.rs @@ -5,7 +5,7 @@ use crate::commands::utils::{site_string, PrintOptions}; pub(crate) fn remove(path: &Path, url: &String, separator: &String) -> Result<(), Error> { let names_file = names::read_names_file(path)?; - let mut names = names::load_names(names_file)?; + let mut names = names::load_names(&names_file)?; names.retain(|site| { if &site.url == url { diff --git a/crates/shared/src/errors.rs b/crates/shared/src/errors.rs index adfaacd..3017cdf 100644 --- a/crates/shared/src/errors.rs +++ b/crates/shared/src/errors.rs @@ -14,10 +14,8 @@ pub struct Error { pub data: String, } -pub(crate) static DIRECTORIES_ERROR_MESSAGE: &str = "could not retreive a valid home path from the operating system. maybe try to define the HOME enviroment variable if you\'re on a unix or unix like operating system."; - -impl core::fmt::Debug for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl Error { + fn error_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "A {:?} error has occured.\nDetails: {}", @@ -25,3 +23,19 @@ impl core::fmt::Debug for Error { ) } } + +impl std::error::Error for Error {} + +pub(crate) static DIRECTORIES_ERROR_MESSAGE: &str = "could not retreive a valid home path from the operating system. maybe try to define the HOME enviroment variable if you\'re on a unix or unix like operating system."; + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.error_fmt(f) + } +} + +impl std::fmt::Debug for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.error_fmt(f) + } +} diff --git a/crates/shared/src/lib.rs b/crates/shared/src/lib.rs index feb60a9..c03acc9 100644 --- a/crates/shared/src/lib.rs +++ b/crates/shared/src/lib.rs @@ -1,3 +1,5 @@ pub mod directories; pub mod errors; pub mod names; +#[cfg(test)] +mod tests; diff --git a/crates/shared/src/names.rs b/crates/shared/src/names.rs index 734587e..15dda61 100644 --- a/crates/shared/src/names.rs +++ b/crates/shared/src/names.rs @@ -8,7 +8,7 @@ pub struct Site { pub name: Option, } -pub fn load_names(names: String) -> Result, Error> { +pub fn load_names(names: &str) -> Result, Error> { match serde_json::from_str::>(&names) { Ok(content) => { log::debug!("successfully parsed names.json."); diff --git a/crates/shared/src/tests.rs b/crates/shared/src/tests.rs new file mode 100644 index 0000000..0b0f4be --- /dev/null +++ b/crates/shared/src/tests.rs @@ -0,0 +1,39 @@ +use crate::{ + errors::Error, + names::{load_names, read_names_file}, +}; +use std::{env, error}; + +#[test] +fn test_name_parsing() -> Result<(), Error> { + let names = + load_names(r#"[{"url": "sus.com", "name": "sussy"}, {"url": "sussy.com", "name": null}]"#)?; + + assert_eq!(names.len(), 2); + assert_eq!(names[0].url, "sus.com"); + assert_eq!(names[0].name.as_ref().unwrap(), "sussy"); + assert_eq!(names[1].url, "sussy.com"); + assert!(names[1].name.is_none()); + + Ok(()) +} + +#[test] +fn test_invalid_parsing() { + let no_url_field = load_names(r#"[{"name":""}]"#); + let no_fields = load_names(r#"[{}]"#); + let trailing_array = load_names("["); + + assert!(no_url_field.is_err()); + assert!(no_fields.is_err()); + assert!(trailing_array.is_err()); +} + +#[test] +fn reading_a_non_existent_names_file() -> Result<(), Box> { + let temp_file = env::temp_dir().join("meowy-test-names.json"); + let contents = read_names_file(&temp_file)?; + assert_eq!(contents, "[]"); + std::fs::remove_file(temp_file)?; + Ok(()) +} diff --git a/src/sites.rs b/src/sites.rs index ead801c..3094ee9 100644 --- a/src/sites.rs +++ b/src/sites.rs @@ -36,5 +36,5 @@ pub fn init_names() -> Result<(), Error> { fn get_names() -> Result, Error> { let names_path = directories::get_names_path()?; let names_file = names::read_names_file(&names_path)?; - names::load_names(names_file) + names::load_names(&names_file) }