i5-add-tests (part 2) #7

Merged
kemitix merged 10 commits from i5-add-tests into main 2023-07-28 18:35:41 +01:00
3 changed files with 18 additions and 11 deletions
Showing only changes of commit defa57de25 - Show all commits

View file

@ -5,6 +5,9 @@ pub mod history;
pub mod prelude; pub mod prelude;
mod subscriptions; mod subscriptions;
#[cfg(test)]
mod test_utils;
use feed::FeedEnv; use feed::FeedEnv;
use fetch::FetchEnv; use fetch::FetchEnv;
use history::HistoryEnv; use history::HistoryEnv;

View file

@ -17,9 +17,8 @@ pub fn lines_from(file_name: &str) -> Result<Vec<String>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs::File, io::Write, str::from_utf8};
use tempfile::{tempdir, TempDir}; use crate::test_utils::create_text_file;
use super::*; use super::*;
@ -73,13 +72,4 @@ mod tests {
assert_eq!(result, ["@sub1", "@sub3"]); assert_eq!(result, ["@sub1", "@sub3"]);
Ok(()) Ok(())
} }
fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> {
let data = from_utf8(data)?;
let dir = tempdir()?;
let filename = format!("{}", &dir.path().join(name).display());
let file = File::create(&filename)?;
write!(&file, "{data}")?;
Ok((dir, filename))
}
} }

14
src/test_utils.rs Normal file
View file

@ -0,0 +1,14 @@
use std::{fs::File, io::Write, str::from_utf8};
use tempfile::{tempdir, TempDir};
use crate::prelude::*;
pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> {
let data = from_utf8(data)?;
let dir = tempdir()?;
let filename = format!("{}", &dir.path().join(name).display());
let file = File::create(&filename)?;
write!(&file, "{data}")?;
Ok((dir, filename))
}