From f6b4863795331870df08b45667c1ae9c922f7fc4 Mon Sep 17 00:00:00 2001 From: Jesus Pereira Date: Sun, 20 Jun 2021 02:28:41 -0400 Subject: [PATCH] Fix tinify not set key should panic --- src/tinify.rs | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/tinify.rs b/src/tinify.rs index bc1c62c..96d5b6f 100644 --- a/src/tinify.rs +++ b/src/tinify.rs @@ -3,7 +3,6 @@ use crate::source::Source; use lazy_static::lazy_static; use std::collections::HashMap; use std::sync::Mutex; -use std::process; lazy_static! { static ref OPTIONS: Mutex> = { @@ -28,11 +27,11 @@ pub fn set_key(new_key: &str) { } pub fn get_client() -> Client { - let key = OPTIONS.lock().unwrap().get("key").unwrap().clone(); - if key.len() == 0 { - eprintln!("Provide an API key with tinify::set_key(key)"); - process::exit(1); + let contains_key = OPTIONS.lock().unwrap().contains_key("key"); + if !contains_key { + panic!("Provide an API key with tinify::set_key(key)"); } + let key = OPTIONS.lock().unwrap().get("key").unwrap().clone(); let client = Client::new(key); client @@ -91,7 +90,7 @@ mod tests { lazy_static! { static ref INIT: Once = Once::new(); static ref PRIVATE_KEY: &'static str = "yjb7YwqyRZwzkGtCfDt6qmXs3QRQTJz3"; - static ref TMP_PATH: &'static str = "./tmp_test_image.png"; + static ref TMP_PATH: &'static Path = Path::new("./tmp_test_image.png"); static ref CLIENT: Client = Client { key: String::from(*PRIVATE_KEY), }; @@ -103,9 +102,24 @@ mod tests { }); } + #[test] + #[should_panic(expected="Provide an API key with tinify::set_key(key)")] + fn test_not_set_key() { + if !TMP_PATH.exists() { + create_file!(); + } + let contains_key = OPTIONS.lock().unwrap().contains_key("key"); + if contains_key { + OPTIONS.lock().unwrap().remove("key").unwrap(); + } + let source = from_file("./tmp_test_image.png"); + let _compress = source.to_file("./optimized.png"); + } + #[test] fn test_set_key_into_hash_map() { initialize(); + OPTIONS.lock().unwrap().insert("key", String::from(*PRIVATE_KEY)); let test_key = OPTIONS.lock().unwrap().get("key").unwrap().clone(); assert_eq!(test_key, String::from(*PRIVATE_KEY)); @@ -125,17 +139,16 @@ mod tests { #[test] fn test_from_file_get_source() { initialize(); - let path = Path::new(*TMP_PATH); - if !path.exists() { + if !TMP_PATH.exists() { create_file!(); } - let source = from_file(*TMP_PATH); + let source = from_file(TMP_PATH.to_str().unwrap()); let cloned_url = source.url.clone(); let expected = Source { url: cloned_url, }; - if path.exists() { - fs::remove_file(path).unwrap(); + if TMP_PATH.exists() { + fs::remove_file(*TMP_PATH).unwrap(); } assert_eq!(source, expected); @@ -144,18 +157,17 @@ mod tests { #[test] fn test_from_buffer_get_source() { initialize(); - let path = Path::new(*TMP_PATH); - if !path.exists() { + if !TMP_PATH.exists() { create_file!(); } - let buffer = fs::read(path).unwrap(); + let buffer = fs::read(*TMP_PATH).unwrap(); let source = from_buffer(&buffer); let cloned_url = source.url.clone(); let expected = Source { url: cloned_url, }; - if path.exists() { - fs::remove_file(path).unwrap(); + if TMP_PATH.exists() { + fs::remove_file(*TMP_PATH).unwrap(); } assert_eq!(source, expected);