Add tests for loading subscriptions file
This commit is contained in:
parent
65156db75e
commit
6d967f5eae
9 changed files with 104 additions and 6 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
target
|
target
|
||||||
*.mp3
|
*.mp3
|
||||||
*.webm
|
*.webm
|
||||||
*.txt
|
/subscriptions.txt
|
||||||
|
/downloaded.txt
|
||||||
|
|
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1017,6 +1017,7 @@ dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"scraper",
|
"scraper",
|
||||||
|
"tempfile",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -10,3 +10,6 @@ atom_syndication = "0.12.1"
|
||||||
reqwest = { version = "0.11.18", features = ["json", "blocking"] }
|
reqwest = { version = "0.11.18", features = ["json", "blocking"] }
|
||||||
scraper = "0.17.1"
|
scraper = "0.17.1"
|
||||||
clap = "4.3.19"
|
clap = "4.3.19"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempfile = "*"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{fmt::Display, string::FromUtf8Error};
|
use std::{fmt::Display, str::Utf8Error, string::FromUtf8Error};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
|
@ -9,6 +9,13 @@ impl Display for Error {
|
||||||
f.write_str(self.details.to_string().as_str())
|
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 {
|
impl From<FromUtf8Error> for Error {
|
||||||
fn from(value: FromUtf8Error) -> Self {
|
fn from(value: FromUtf8Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
|
@ -21,7 +21,6 @@ pub fn run(
|
||||||
fetch_download: FetchDownload,
|
fetch_download: FetchDownload,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for channel_name in subscriptions::lines_from(subscriptions)? {
|
for channel_name in subscriptions::lines_from(subscriptions)? {
|
||||||
let channel_name = channel_name?;
|
|
||||||
println!("Channel: {}", channel_name);
|
println!("Channel: {}", channel_name);
|
||||||
let feed_url = feed_find(site, &channel_name)?;
|
let feed_url = feed_find(site, &channel_name)?;
|
||||||
for entry in feed_get(&feed_url)?.entries() {
|
for entry in feed_get(&feed_url)?.entries() {
|
||||||
|
|
|
@ -1,10 +1,87 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use std::fs::File;
|
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 file = File::open(file_name)?;
|
||||||
let reader = BufReader::new(file);
|
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
4
test/data/subscriptions-blank-line.txt
Normal file
4
test/data/subscriptions-blank-line.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
@sub1
|
||||||
|
|
||||||
|
@sub2
|
||||||
|
@sub3
|
3
test/data/subscriptions-comment.txt
Normal file
3
test/data/subscriptions-comment.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
@sub1
|
||||||
|
#@sub2
|
||||||
|
@sub3
|
3
test/data/subscriptions.txt
Normal file
3
test/data/subscriptions.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
@sub1
|
||||||
|
@sub2
|
||||||
|
@sub3
|
Loading…
Add table
Reference in a new issue