From e54c136f5ad81f64512a96b90d0ca439c4bd271a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 30 Jul 2023 14:42:30 +0100 Subject: [PATCH] use a function generators for NetworkEnv --- src/feed/find.rs | 31 +++++++++---------------------- src/lib.rs | 12 ++++++++++++ src/network/env.rs | 20 +++++++++++++------- src/network/mod.rs | 3 +++ src/test_utils.rs | 24 +++++++++++++++++++++++- 5 files changed, 60 insertions(+), 30 deletions(-) diff --git a/src/feed/find.rs b/src/feed/find.rs index 27cef15..af6325a 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -23,16 +23,18 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result { #[cfg(test)] mod tests { - use crate::errors::Error; + use crate::test_utils::{ + mock_fetch_as_text_with_rss_url, stub_download_as_mp3, stub_fetch_as_bytes, + }; use super::*; #[test] fn finds_rss_url() -> Result<()> { //given let network_env = NetworkEnv { - fetch_as_text: dummy_fetch_as_text, - fetch_as_bytes: dummy_fetch_as_bytes, - download_as_mp3: dummy_download_as_mp3, + fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"), + fetch_as_bytes: stub_fetch_as_bytes(), + download_as_mp3: stub_download_as_mp3(), }; //when let result = find("site", "@channel", &network_env)?; @@ -45,9 +47,9 @@ mod tests { fn error_if_channel_name_is_invalid() -> Result<()> { //given let network_env = NetworkEnv { - fetch_as_text: dummy_fetch_as_text, - fetch_as_bytes: dummy_fetch_as_bytes, - download_as_mp3: dummy_download_as_mp3, + fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"), + fetch_as_bytes: stub_fetch_as_bytes(), + download_as_mp3: stub_download_as_mp3(), }; //when let result = find("site", "invalid-channel-name", &network_env); @@ -55,19 +57,4 @@ mod tests { assert!(result.is_err()); Ok(()) } - - fn dummy_fetch_as_text(_url: &str) -> Result { - Ok(r#" - - - - "# - .to_string()) - } - fn dummy_fetch_as_bytes(_url: &str) -> Result { - Err(Error::message("Not implemented")) - } - fn dummy_download_as_mp3(_url: &str) -> Result<()> { - Err(Error::message("Not implemented")) - } } diff --git a/src/lib.rs b/src/lib.rs index b7f1a9d..a890a5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,3 +34,15 @@ pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn downloads_two_items_from_two_feeds_ignoring_existing_items_in_history() -> Result<()> { + //given + //when + //then + Ok(()) + } +} diff --git a/src/network/env.rs b/src/network/env.rs index 7cbd626..24ec8cd 100644 --- a/src/network/env.rs +++ b/src/network/env.rs @@ -2,17 +2,23 @@ use std::process::Command; use crate::prelude::*; +pub type NetworkFetchAsTextFn = Box Result>; + +pub type NetworkFetchAsBytesFn = Box Result>; + +pub type NetworkDownloadAsMp3Fn = Box Result<()>>; + pub struct NetworkEnv { - pub fetch_as_text: fn(url: &str) -> Result, - pub fetch_as_bytes: fn(url: &str) -> Result, - pub download_as_mp3: fn(url: &str) -> Result<()>, + pub fetch_as_text: NetworkFetchAsTextFn, + pub fetch_as_bytes: NetworkFetchAsBytesFn, + pub download_as_mp3: NetworkDownloadAsMp3Fn, } impl Default for NetworkEnv { fn default() -> Self { Self { - fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?), - fetch_as_bytes: |url| Ok(reqwest::blocking::get(url)?.bytes()?), - download_as_mp3: |url| { + fetch_as_text: Box::new(|url| Ok(reqwest::blocking::get(url)?.text()?)), + fetch_as_bytes: Box::new(|url| Ok(reqwest::blocking::get(url)?.bytes()?)), + download_as_mp3: Box::new(|url| { let cmd = "yt-dlp"; // println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href); let output = Command::new(cmd) @@ -26,7 +32,7 @@ impl Default for NetworkEnv { println!("{}", String::from_utf8(output.stdout)?); } Ok(()) - }, + }), } } } diff --git a/src/network/mod.rs b/src/network/mod.rs index 3af1de6..2d626a3 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -1,3 +1,6 @@ mod env; +pub use env::NetworkDownloadAsMp3Fn; pub use env::NetworkEnv; +pub use env::NetworkFetchAsBytesFn; +pub use env::NetworkFetchAsTextFn; diff --git a/src/test_utils.rs b/src/test_utils.rs index 305ae44..f73b782 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -6,7 +6,11 @@ use std::{ use tempfile::{tempdir, TempDir}; -use crate::prelude::*; +use crate::{ + errors::Error, + network::{NetworkDownloadAsMp3Fn, NetworkFetchAsBytesFn, NetworkFetchAsTextFn}, + prelude::*, +}; pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> { let data = from_utf8(data)?; @@ -23,3 +27,21 @@ pub fn read_text_file(file_name: &str) -> Result> { .map(String::from) .collect()) } +pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn { + Box::new(move |_url: &str| { + Ok(format!( + r#" + + + + "#, + url + )) + }) +} +pub fn stub_fetch_as_bytes() -> NetworkFetchAsBytesFn { + Box::new(|_url: &str| Err(Error::message("Not implemented"))) +} +pub fn stub_download_as_mp3() -> NetworkDownloadAsMp3Fn { + Box::new(|_url: &str| Err(Error::message("Not implemented"))) +}