Add tests for loading subscriptions file

This commit is contained in:
Paul Campbell 2023-07-25 16:57:08 +01:00
parent 65156db75e
commit 6d967f5eae
9 changed files with 104 additions and 6 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
target
*.mp3
*.webm
*.txt
/subscriptions.txt
/downloaded.txt

1
Cargo.lock generated
View file

@ -1017,6 +1017,7 @@ dependencies = [
"clap",
"reqwest",
"scraper",
"tempfile",
]
[[package]]

View file

@ -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 = "*"

View file

@ -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<Utf8Error> for Error {
fn from(value: Utf8Error) -> Self {
Self {
details: value.to_string(),
}
}
}
impl From<FromUtf8Error> for Error {
fn from(value: FromUtf8Error) -> Self {
Self {

View file

@ -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() {

View file

@ -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<Lines<BufReader<File>>> {
pub fn lines_from(file_name: &str) -> Result<Vec<String>> {
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))
}
}

View file

@ -0,0 +1,4 @@
@sub1
@sub2
@sub3

View file

@ -0,0 +1,3 @@
@sub1
#@sub2
@sub3

View file

@ -0,0 +1,3 @@
@sub1
@sub2
@sub3