i5-add-tests (part 3) #9

Merged
kemitix merged 28 commits from i5-add-tests into main 2023-07-29 20:36:04 +01:00
7 changed files with 36 additions and 22 deletions
Showing only changes of commit 73b9ad960b - Show all commits

View file

@ -1,15 +1,14 @@
use crate::network::NetworkEnv;
use crate::prelude::*;
use crate::fetch::FetchGet;
pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> Result<String> {
pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
if let Some(channel_prefix) = channel_name.chars().next() {
if channel_prefix != '@' {
return Err(format!("Channel Name must begin with an '@': {}", channel_name).into());
}
}
let channel_url = format!("{}{}", site, channel_name);
let response = (e)(&channel_url)?;
let response = (e.fetch_as_text)(&channel_url)?;
let rss_url = scraper::Html::parse_document(&response)
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
.next()
@ -23,15 +22,16 @@ pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> Result<String> {
#[cfg(test)]
mod tests {
use crate::fetch::Response;
use super::*;
#[test]
fn finds_rss_url() -> Result<()> {
//given
let fetch_get = &(get as FetchGet);
let network_env = NetworkEnv {
fetch_as_text: dummy_fetch_as_text,
};
//when
let result = find("site", "@channel", fetch_get)?;
let result = find("site", "@channel", &network_env)?;
//then
assert_eq!(result, "the-rss-url");
Ok(())
@ -40,15 +40,17 @@ mod tests {
#[test]
fn error_if_channel_name_is_invalid() -> Result<()> {
//given
let fetch_get = &(get as FetchGet);
let network_env = NetworkEnv {
fetch_as_text: dummy_fetch_as_text,
};
//when
let result = find("site", "invalid-channel-name", fetch_get);
let result = find("site", "invalid-channel-name", &network_env);
//then
assert!(result.is_err());
Ok(())
}
fn get(_url: &str) -> Result<Response> {
fn dummy_fetch_as_text(_url: &str) -> Result<String> {
Ok(r#"
<html>
<link title="RSS" href="the-rss-url">

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
use crate::fetch::FetchGet;
use crate::network::NetworkEnv;
mod find;
mod get;
@ -14,5 +14,5 @@ pub struct FeedEnv {
pub get: FeedGet,
}
pub type FeedFind = fn(&str, &str, &FetchGet) -> Result<String>;
pub type FeedFind = fn(&str, &str, &NetworkEnv) -> Result<String>;
pub type FeedGet = fn(&str) -> Result<Feed>;

View file

@ -5,11 +5,10 @@ use std::process::Command;
pub struct FetchEnv {
pub download: FetchDownload,
pub get: FetchGet,
}
pub type FetchDownload = fn(&Link) -> Result<()>;
pub type FetchGet = fn(&str) -> Result<Response>;
pub type FetchGet = fn(&str) -> Result<String>;
pub fn download(link: &Link) -> Result<()> {
let cmd = "yt-dlp";
@ -26,9 +25,3 @@ pub fn download(link: &Link) -> Result<()> {
}
Ok(())
}
pub type Response = String;
pub fn get(url: &str) -> Result<Response> {
Ok(reqwest::blocking::get(url)?.text()?)
}

View file

@ -3,6 +3,7 @@ pub mod feed;
pub mod fetch;
pub mod file;
pub mod history;
pub mod network;
pub mod prelude;
#[cfg(test)]
@ -12,10 +13,12 @@ use feed::FeedEnv;
use fetch::FetchEnv;
use file::FileEnv;
use history::HistoryEnv;
use network::NetworkEnv;
use prelude::*;
pub struct Env {
pub feed: FeedEnv,
pub network: NetworkEnv,
pub history: HistoryEnv,
pub fetch: FetchEnv,
pub file: FileEnv,
@ -24,7 +27,7 @@ pub struct Env {
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
for channel_name in file::read::lines_from(subscriptions, &e.file)? {
println!("Channel: {}", channel_name);
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?;
let feed_url = (e.feed.find)(site, &channel_name, &e.network)?;
for entry in (e.feed.get)(&feed_url)?.entries() {
if let Some(link) = entry.links().get(0).cloned() {
if !(e.history.find)(&link, history, &e.file)? {

View file

@ -1,4 +1,5 @@
use podal::file::FileEnv;
use podal::network::NetworkEnv;
use podal::prelude::*;
use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv};
@ -25,8 +26,8 @@ fn main() -> Result<()> {
},
fetch: FetchEnv {
download: podal::fetch::download,
get: podal::fetch::get,
},
network: NetworkEnv::default(),
file: FileEnv::default(),
},
)?;

12
src/network/env.rs Normal file
View file

@ -0,0 +1,12 @@
use crate::prelude::*;
pub struct NetworkEnv {
pub fetch_as_text: fn(url: &str) -> Result<String>,
}
impl Default for NetworkEnv {
fn default() -> Self {
Self {
fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?),
}
}
}

3
src/network/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod env;
pub use env::NetworkEnv;