add test for history::add
This commit is contained in:
parent
defa57de25
commit
20a4ef59d9
4 changed files with 86 additions and 1 deletions
|
@ -15,3 +15,75 @@ pub fn add(link: &Link, file_name: &str) -> Result<()> {
|
||||||
writeln!(file, "{}", link.href)?;
|
writeln!(file, "{}", link.href)?;
|
||||||
Ok(())
|
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<String> = 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<String> = 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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
use std::{fs::File, io::Write, str::from_utf8};
|
use std::{
|
||||||
|
fs::{read_to_string, File},
|
||||||
|
io::Write,
|
||||||
|
str::from_utf8,
|
||||||
|
};
|
||||||
|
|
||||||
use tempfile::{tempdir, TempDir};
|
use tempfile::{tempdir, TempDir};
|
||||||
|
|
||||||
|
@ -12,3 +16,10 @@ pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> {
|
||||||
write!(&file, "{data}")?;
|
write!(&file, "{data}")?;
|
||||||
Ok((dir, filename))
|
Ok((dir, filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
|
||||||
|
Ok(read_to_string(file_name)?
|
||||||
|
.lines()
|
||||||
|
.map(String::from)
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
2
test/data/downloads.txt
Normal file
2
test/data/downloads.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
line-1
|
||||||
|
line-2
|
0
test/data/empty.txt
Normal file
0
test/data/empty.txt
Normal file
Loading…
Add table
Reference in a new issue