2023-07-29 20:36:03 +01:00
|
|
|
use crate::file::FileEnv;
|
2023-07-25 10:55:02 +01:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2023-07-29 20:36:03 +01:00
|
|
|
use super::Link;
|
2023-07-25 10:55:02 +01:00
|
|
|
|
2023-07-29 20:36:03 +01:00
|
|
|
pub fn add(link: &Link, file_name: &str, e: &FileEnv) -> Result<()> {
|
|
|
|
(e.append_line)(file_name, &link.href)?;
|
2023-07-25 10:55:02 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-28 18:35:41 +01:00
|
|
|
|
|
|
|
#[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
|
2023-08-06 12:14:02 +01:00
|
|
|
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)?;
|
2023-07-28 18:35:41 +01:00
|
|
|
|
|
|
|
let link = Link {
|
|
|
|
href: "foo".to_string(),
|
|
|
|
rel: "bar".to_string(),
|
|
|
|
hreflang: None,
|
|
|
|
mime_type: None,
|
|
|
|
title: None,
|
|
|
|
length: None,
|
|
|
|
};
|
|
|
|
//when
|
2023-08-06 12:14:02 +01:00
|
|
|
add(
|
|
|
|
&link,
|
|
|
|
file_name,
|
|
|
|
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
|
|
|
)?;
|
2023-07-28 18:35:41 +01:00
|
|
|
|
|
|
|
//then
|
2023-08-06 12:14:02 +01:00
|
|
|
let content: Vec<String> = read_text_file(dir.path(), file_name)?;
|
2023-07-28 18:35:41 +01:00
|
|
|
drop(dir);
|
|
|
|
|
|
|
|
let expected = vec!["foo".to_string()];
|
|
|
|
assert_eq!(content, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn appends_to_exising_file() -> Result<()> {
|
|
|
|
// given
|
2023-08-06 12:14:02 +01:00
|
|
|
let file_name = "download.txt";
|
|
|
|
let dir = create_text_file(file_name, include_bytes!("../../test/data/downloads.txt"))?;
|
2023-07-28 18:35:41 +01:00
|
|
|
|
|
|
|
let link = Link {
|
|
|
|
href: "foo".to_string(),
|
|
|
|
rel: "bar".to_string(),
|
|
|
|
hreflang: None,
|
|
|
|
mime_type: None,
|
|
|
|
title: None,
|
|
|
|
length: None,
|
|
|
|
};
|
|
|
|
//when
|
2023-08-06 12:14:02 +01:00
|
|
|
add(
|
|
|
|
&link,
|
|
|
|
file_name,
|
|
|
|
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
|
|
|
)?;
|
2023-07-28 18:35:41 +01:00
|
|
|
|
|
|
|
//then
|
2023-08-06 12:14:02 +01:00
|
|
|
let content: Vec<String> = read_text_file(dir.path(), file_name)?;
|
2023-07-28 18:35:41 +01:00
|
|
|
drop(dir);
|
|
|
|
|
|
|
|
let expected = vec![
|
|
|
|
"line-1".to_string(),
|
|
|
|
"line-2".to_string(),
|
|
|
|
"foo".to_string(),
|
|
|
|
];
|
|
|
|
assert_eq!(content, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|