From cc3d55e5b91006ee3520e4ce5b8ed2cbc2ef2f8a Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 26 Jan 2024 18:41:44 +0100 Subject: [PATCH] added alias to run tests --- Cargo.toml | 3 +++ src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3c94170..dc378b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,9 @@ name = "mastodon-image-uploader-bot" version = "0.2.2" edition = "2021" +[alias] +test = "cargo test -- --test-threads=1" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/src/main.rs b/src/main.rs index 35015c9..2c5c042 100644 --- a/src/main.rs +++ b/src/main.rs @@ -178,7 +178,7 @@ fn set_url_as_posted(config: &Config, url: &String) -> DynResult<()> { .open(&config.files.posted)?; //.expect("Cannot open posted file"); // Maybe we should retry just in case write!(file, "{}\n", url)?; //.expect("Cannot write to posted file"); // maybe we should retry tbh info!("Set url {} as posted", url); - Ok(()) + Ok(()) } async fn post_image( @@ -269,15 +269,13 @@ mod tests { use super::*; const TMPTESTDIR: &str = "/tmp/botimage"; + const TEST_URL: &str = "https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b"; #[tokio::test] async fn fetch_url_should_fetch() { - fetch_url( - &"https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b".to_string(), - &TMPTESTDIR.to_string(), - ) - .await - .unwrap(); + fetch_url(&TEST_URL.to_string(), &TMPTESTDIR.to_string()) + .await + .unwrap(); std::fs::read(TMPTESTDIR).unwrap(); } @@ -310,14 +308,9 @@ mod tests { let config = get_config(); let account = get_account(&config).await; - let status = post_image( - &account, - &"https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b".to_string(), - &config, - Visibility::Direct, - ) - .await - .unwrap(); + let status = post_image(&account, &TEST_URL.to_string(), &config, Visibility::Direct) + .await + .unwrap(); let response = account.get_status(&status.id).await.unwrap(); @@ -333,4 +326,36 @@ mod tests { assert_eq!(response.status(), StatusCode::OK) } + + #[test] + fn set_as_posted_works() { + let config = get_config(); + + std::fs::write(&config.files.posted, "").unwrap(); + + set_url_as_posted(&config, &TEST_URL.to_string()).unwrap(); + let file = std::fs::read_to_string(&config.files.posted).unwrap(); + let url = file.lines().next_back().unwrap(); + assert_eq!(url, TEST_URL) + } + + #[test] + fn get_next_url_works() { + let config = get_config(); + // Reset file + std::fs::write(&config.files.posted, "").unwrap(); + + // Get test url + let url = get_next_url(&config).unwrap().unwrap(); + assert_eq!(url, TEST_URL); + + set_url_as_posted(&config, &TEST_URL.to_string()).unwrap(); + let file = std::fs::read_to_string(&config.files.posted).unwrap(); + let url = file.lines().next_back().unwrap(); + assert_eq!(url, TEST_URL); + + // Test that now it does not get it + let url = get_next_url(&config).unwrap(); + assert_eq!(url, None); + } }