From defa57de258e75d50f47b83774e8482c7b7bde8d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 27 Jul 2023 21:20:32 +0100 Subject: [PATCH] extract test_utils module --- src/lib.rs | 3 +++ src/subscriptions.rs | 12 +----------- src/test_utils.rs | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 src/test_utils.rs diff --git a/src/lib.rs b/src/lib.rs index 243cbd3..ce77f4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,9 @@ pub mod history; pub mod prelude; mod subscriptions; +#[cfg(test)] +mod test_utils; + use feed::FeedEnv; use fetch::FetchEnv; use history::HistoryEnv; diff --git a/src/subscriptions.rs b/src/subscriptions.rs index 17ebde7..5ecc379 100644 --- a/src/subscriptions.rs +++ b/src/subscriptions.rs @@ -17,9 +17,8 @@ pub fn lines_from(file_name: &str) -> Result> { #[cfg(test)] mod tests { - use std::{fs::File, io::Write, str::from_utf8}; - use tempfile::{tempdir, TempDir}; + use crate::test_utils::create_text_file; use super::*; @@ -73,13 +72,4 @@ mod tests { assert_eq!(result, ["@sub1", "@sub3"]); 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)) - } } diff --git a/src/test_utils.rs b/src/test_utils.rs new file mode 100644 index 0000000..36e67ff --- /dev/null +++ b/src/test_utils.rs @@ -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)) +}