added alias to run tests

This commit is contained in:
Alie 2024-01-26 18:41:44 +01:00
parent 0001567b55
commit cc3d55e5b9
2 changed files with 43 additions and 15 deletions

View File

@ -3,6 +3,9 @@ name = "mastodon-image-uploader-bot"
version = "0.2.2" version = "0.2.2"
edition = "2021" 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]

View File

@ -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 .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 write!(file, "{}\n", url)?; //.expect("Cannot write to posted file"); // maybe we should retry tbh
info!("Set url {} as posted", url); info!("Set url {} as posted", url);
Ok(()) Ok(())
} }
async fn post_image( async fn post_image(
@ -269,15 +269,13 @@ mod tests {
use super::*; use super::*;
const TMPTESTDIR: &str = "/tmp/botimage"; const TMPTESTDIR: &str = "/tmp/botimage";
const TEST_URL: &str = "https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b";
#[tokio::test] #[tokio::test]
async fn fetch_url_should_fetch() { async fn fetch_url_should_fetch() {
fetch_url( fetch_url(&TEST_URL.to_string(), &TMPTESTDIR.to_string())
&"https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b".to_string(), .await
&TMPTESTDIR.to_string(), .unwrap();
)
.await
.unwrap();
std::fs::read(TMPTESTDIR).unwrap(); std::fs::read(TMPTESTDIR).unwrap();
} }
@ -310,14 +308,9 @@ mod tests {
let config = get_config(); let config = get_config();
let account = get_account(&config).await; let account = get_account(&config).await;
let status = post_image( let status = post_image(&account, &TEST_URL.to_string(), &config, Visibility::Direct)
&account, .await
&"https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b".to_string(), .unwrap();
&config,
Visibility::Direct,
)
.await
.unwrap();
let response = account.get_status(&status.id).await.unwrap(); let response = account.get_status(&status.id).await.unwrap();
@ -333,4 +326,36 @@ mod tests {
assert_eq!(response.status(), StatusCode::OK) 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);
}
} }