Skip to content

Commit

Permalink
Fix tinify not set key should panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieroner committed Jun 20, 2021
1 parent 20e01a0 commit f6b4863
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/tinify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<&'static str, String>> = {
Expand All @@ -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
Expand Down Expand Up @@ -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),
};
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit f6b4863

Please sign in to comment.