From 6d967f5eae821ba2bc7c923671db0500cc1784c2 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 25 Jul 2023 16:57:08 +0100 Subject: [PATCH] Add tests for loading subscriptions file --- .gitignore | 3 +- Cargo.lock | 1 + Cargo.toml | 3 + src/errors.rs | 9 ++- src/lib.rs | 1 - src/subscriptions.rs | 83 +++++++++++++++++++++++++- test/data/subscriptions-blank-line.txt | 4 ++ test/data/subscriptions-comment.txt | 3 + test/data/subscriptions.txt | 3 + 9 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 test/data/subscriptions-blank-line.txt create mode 100644 test/data/subscriptions-comment.txt create mode 100644 test/data/subscriptions.txt diff --git a/.gitignore b/.gitignore index 2143bde..c233fd4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target *.mp3 *.webm -*.txt +/subscriptions.txt +/downloaded.txt diff --git a/Cargo.lock b/Cargo.lock index 1fe5cc9..f20c98c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1017,6 +1017,7 @@ dependencies = [ "clap", "reqwest", "scraper", + "tempfile", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 1705009..0c54643 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,6 @@ atom_syndication = "0.12.1" reqwest = { version = "0.11.18", features = ["json", "blocking"] } scraper = "0.17.1" clap = "4.3.19" + +[dev-dependencies] +tempfile = "*" diff --git a/src/errors.rs b/src/errors.rs index 1e2baff..6769040 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, string::FromUtf8Error}; +use std::{fmt::Display, str::Utf8Error, string::FromUtf8Error}; #[derive(Debug)] pub struct Error { @@ -9,6 +9,13 @@ impl Display for Error { f.write_str(self.details.to_string().as_str()) } } +impl From for Error { + fn from(value: Utf8Error) -> Self { + Self { + details: value.to_string(), + } + } +} impl From for Error { fn from(value: FromUtf8Error) -> Self { Self { diff --git a/src/lib.rs b/src/lib.rs index b7bee6f..ee239ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,6 @@ pub fn run( fetch_download: FetchDownload, ) -> Result<()> { for channel_name in subscriptions::lines_from(subscriptions)? { - let channel_name = channel_name?; println!("Channel: {}", channel_name); let feed_url = feed_find(site, &channel_name)?; for entry in feed_get(&feed_url)?.entries() { diff --git a/src/subscriptions.rs b/src/subscriptions.rs index a9f7cde..5e5a955 100644 --- a/src/subscriptions.rs +++ b/src/subscriptions.rs @@ -1,10 +1,87 @@ use crate::prelude::*; use std::fs::File; -use std::io::{BufRead, BufReader, Lines}; +use std::io::{BufRead, BufReader}; -pub fn lines_from(file_name: &str) -> Result>> { +pub fn lines_from(file_name: &str) -> Result> { let file = File::open(file_name)?; let reader = BufReader::new(file); - Ok(reader.lines()) + let mut lines = vec![]; + for line in reader.lines() { + if let Ok(line) = line { + if line.starts_with('@') { + lines.push(line); + } + } + } + Ok(lines) +} + +#[cfg(test)] +mod tests { + use std::{fs::File, io::Write, str::from_utf8}; + + use tempfile::{tempdir, TempDir}; + + use super::*; + + #[test] + fn can_load_file() -> Result<()> { + //given + let (dir, file_name) = create_text_file( + "subscriptions.txt", + include_bytes!("../test/data/subscriptions.txt"), + )?; + + //when + let result = lines_from(&file_name)?; + + //then + drop(dir); + assert_eq!(result, ["@sub1", "@sub2", "@sub3"]); + Ok(()) + } + + #[test] + fn ignores_blank_lines() -> Result<()> { + //given + let (dir, file_name) = create_text_file( + "subscriptions.txt", + include_bytes!("../test/data/subscriptions-blank-line.txt"), + )?; + + //when + let result = lines_from(&file_name)?; + + //then + drop(dir); + assert_eq!(result, ["@sub1", "@sub2", "@sub3"]); + Ok(()) + } + + #[test] + fn ignores_comments() -> Result<()> { + //given + let (dir, file_name) = create_text_file( + "subscriptions.txt", + include_bytes!("../test/data/subscriptions-comment.txt"), + )?; + + //when + let result = lines_from(&file_name)?; + + //then + drop(dir); + 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/test/data/subscriptions-blank-line.txt b/test/data/subscriptions-blank-line.txt new file mode 100644 index 0000000..ef718e3 --- /dev/null +++ b/test/data/subscriptions-blank-line.txt @@ -0,0 +1,4 @@ +@sub1 + +@sub2 +@sub3 diff --git a/test/data/subscriptions-comment.txt b/test/data/subscriptions-comment.txt new file mode 100644 index 0000000..851ac8a --- /dev/null +++ b/test/data/subscriptions-comment.txt @@ -0,0 +1,3 @@ +@sub1 +#@sub2 +@sub3 diff --git a/test/data/subscriptions.txt b/test/data/subscriptions.txt new file mode 100644 index 0000000..a1714b6 --- /dev/null +++ b/test/data/subscriptions.txt @@ -0,0 +1,3 @@ +@sub1 +@sub2 +@sub3