use crate::prelude::*; use atom_syndication::Link; use std::fs::OpenOptions; use std::io::prelude::*; pub fn add(link: &Link, file_name: &str) -> Result<()> { let mut file = OpenOptions::new() .write(true) .append(true) .create(true) .open(file_name) .unwrap(); writeln!(file, "{}", link.href)?; Ok(()) } #[cfg(test)] mod tests { use crate::{ history::Link, test_utils::{create_text_file, read_text_file}, }; use super::*; #[test] fn creates_file_if_missing() -> Result<()> { //given let (dir, file_name) = create_text_file("download.txt", include_bytes!("../../test/data/empty.txt"))?; std::fs::remove_file(&file_name)?; 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)?; //then let content: Vec = read_text_file(&file_name)?; drop(dir); let expected = vec!["foo".to_string()]; assert_eq!(content, expected); Ok(()) } #[test] fn appends_to_exising_file() -> Result<()> { // given let (dir, file_name) = create_text_file( "download.txt", 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)?; //then let content: Vec = read_text_file(&file_name)?; drop(dir); let expected = vec![ "line-1".to_string(), "line-2".to_string(), "foo".to_string(), ]; assert_eq!(content, expected); Ok(()) } }