podal/src/history/add.rs
Paul Campbell 4eaf158483
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
i15-anyhow-context (#17)
Closes kemitix/podal#15

Reviewed-on: #17
Co-authored-by: Paul Campbell <pcampbell@kemitix.net>
Co-committed-by: Paul Campbell <pcampbell@kemitix.net>
2023-08-13 16:11:16 +01:00

97 lines
2.5 KiB
Rust

use crate::file::FileEnv;
use crate::prelude::*;
use super::Link;
pub fn add(link: &Link, file_name: &str, e: &FileEnv) -> Result<()> {
(e.append_line)(file_name, &link.href).context(format!("Appending to file: {}", file_name))?;
Ok(())
}
#[cfg(test)]
mod tests {
use crate::{
history::Link,
params::Args,
test_utils::{create_text_file, read_text_file},
};
use super::*;
#[test]
fn creates_file_if_missing() -> Result<()> {
//given
let file_name = "download.txt";
let dir = create_text_file(file_name, include_bytes!("../../test/data/empty.txt"))?;
let path = format!("{}/{}", dir.path().to_string_lossy(), file_name);
std::fs::remove_file(path)?;
let link = Link {
href: "foo".to_string(),
rel: "bar".to_string(),
hreflang: None,
mime_type: None,
title: None,
length: None,
};
//when
add(
&link,
file_name,
&FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
subscriptions: "subscriptions.txt".to_string(),
}),
)?;
//then
let content: Vec<String> = read_text_file(dir.path(), file_name)?;
drop(dir);
let expected = vec!["foo".to_string()];
assert_eq!(content, expected);
Ok(())
}
#[test]
fn appends_to_exising_file() -> Result<()> {
// given
let file_name = "download.txt";
let dir = create_text_file(file_name, include_bytes!("../../test/data/downloads.txt"))?;
let link = Link {
href: "foo".to_string(),
rel: "bar".to_string(),
hreflang: None,
mime_type: None,
title: None,
length: None,
};
//when
add(
&link,
file_name,
&FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
subscriptions: "subscriptions.txt".to_string(),
}),
)?;
//then
let content: Vec<String> = read_text_file(dir.path(), file_name)?;
drop(dir);
let expected = vec![
"line-1".to_string(),
"line-2".to_string(),
"foo".to_string(),
];
assert_eq!(content, expected);
Ok(())
}
}