use directory from CLI Args
This commit is contained in:
parent
fbcbb661ea
commit
8785aa076a
7 changed files with 114 additions and 49 deletions
|
@ -11,18 +11,29 @@ pub struct FileEnv {
|
|||
pub open: FileOpenFn,
|
||||
pub append_line: FileAppendLineFn,
|
||||
}
|
||||
impl Default for FileEnv {
|
||||
fn default() -> Self {
|
||||
impl FileEnv {
|
||||
pub fn create(directory: String) -> Self {
|
||||
let open_dir = directory.clone();
|
||||
let append_dir = directory.clone();
|
||||
Self {
|
||||
open: Box::new(|path| Ok(File::open(path)?)),
|
||||
append_line: Box::new(|file_name, line| {
|
||||
open: Box::new(move |file_name| {
|
||||
let path = format!("{}/{}", &open_dir, file_name);
|
||||
println!("opening {}", path);
|
||||
let file = File::open(&path)?;
|
||||
println!("opened {}", path);
|
||||
Ok(file)
|
||||
}),
|
||||
append_line: Box::new(move |file_name, line| {
|
||||
let path = format!("{}/{}", &append_dir, file_name);
|
||||
println!("appending to {}", path);
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(file_name)
|
||||
.open(&path)
|
||||
.unwrap();
|
||||
writeln!(file, "{}", line)?;
|
||||
println!("appended to {}", path);
|
||||
Ok(())
|
||||
}),
|
||||
}
|
||||
|
|
|
@ -25,14 +25,15 @@ mod tests {
|
|||
#[test]
|
||||
fn can_load_file() -> Result<()> {
|
||||
//given
|
||||
let (dir, file_name) = create_text_file(
|
||||
"subscriptions.txt",
|
||||
let file_name = "subscriptions.txt";
|
||||
let dir = create_text_file(
|
||||
file_name,
|
||||
include_bytes!("../../test/data/subscriptions.txt"),
|
||||
)?;
|
||||
let file_env = FileEnv::default();
|
||||
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
|
||||
|
||||
//when
|
||||
let result = lines_from(&file_name, &file_env)?;
|
||||
let result = lines_from(file_name, &file_env)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -43,14 +44,15 @@ mod tests {
|
|||
#[test]
|
||||
fn ignores_blank_lines() -> Result<()> {
|
||||
//given
|
||||
let (dir, file_name) = create_text_file(
|
||||
"subscriptions.txt",
|
||||
let file_name = "subscriptions.txt";
|
||||
let dir = create_text_file(
|
||||
file_name,
|
||||
include_bytes!("../../test/data/subscriptions-blank-line.txt"),
|
||||
)?;
|
||||
let file_env = FileEnv::default();
|
||||
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
|
||||
|
||||
//when
|
||||
let result = lines_from(&file_name, &file_env)?;
|
||||
let result = lines_from(file_name, &file_env)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -61,14 +63,15 @@ mod tests {
|
|||
#[test]
|
||||
fn ignores_comments() -> Result<()> {
|
||||
//given
|
||||
let (dir, file_name) = create_text_file(
|
||||
"subscriptions.txt",
|
||||
let file_name = "subscriptions.txt";
|
||||
let dir = create_text_file(
|
||||
file_name,
|
||||
include_bytes!("../../test/data/subscriptions-comment.txt"),
|
||||
)?;
|
||||
let file_env = FileEnv::default();
|
||||
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
|
||||
|
||||
//when
|
||||
let result = lines_from(&file_name, &file_env)?;
|
||||
let result = lines_from(file_name, &file_env)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::prelude::*;
|
|||
use super::Link;
|
||||
|
||||
pub fn add(link: &Link, file_name: &str, e: &FileEnv) -> Result<()> {
|
||||
println!("history::append::to {}", file_name);
|
||||
(e.append_line)(file_name, &link.href)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -21,9 +22,11 @@ mod tests {
|
|||
#[test]
|
||||
fn creates_file_if_missing() -> Result<()> {
|
||||
//given
|
||||
let (dir, file_name) =
|
||||
create_text_file("download.txt", include_bytes!("../../test/data/empty.txt"))?;
|
||||
std::fs::remove_file(&file_name)?;
|
||||
let file_name = "download.txt";
|
||||
let dir = create_text_file(file_name, include_bytes!("../../test/data/empty.txt"))?;
|
||||
let path = format!("{}/{}", dir.path().to_string_lossy(), file_name);
|
||||
println!("removing file {}", path);
|
||||
std::fs::remove_file(path)?;
|
||||
|
||||
let link = Link {
|
||||
href: "foo".to_string(),
|
||||
|
@ -34,10 +37,18 @@ mod tests {
|
|||
length: None,
|
||||
};
|
||||
//when
|
||||
add(&link, &file_name, &FileEnv::default())?;
|
||||
println!("about to add");
|
||||
add(
|
||||
&link,
|
||||
&file_name,
|
||||
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||
)?;
|
||||
println!("called add");
|
||||
|
||||
//then
|
||||
let content: Vec<String> = read_text_file(&file_name)?;
|
||||
println!("about to read file contents");
|
||||
let content: Vec<String> = read_text_file(&dir.path(), &file_name)?;
|
||||
println!("read file contents");
|
||||
drop(dir);
|
||||
|
||||
let expected = vec!["foo".to_string()];
|
||||
|
@ -49,10 +60,8 @@ mod tests {
|
|||
#[test]
|
||||
fn appends_to_exising_file() -> Result<()> {
|
||||
// given
|
||||
let (dir, file_name) = create_text_file(
|
||||
"download.txt",
|
||||
include_bytes!("../../test/data/downloads.txt"),
|
||||
)?;
|
||||
let file_name = "download.txt";
|
||||
let dir = create_text_file(file_name, include_bytes!("../../test/data/downloads.txt"))?;
|
||||
|
||||
let link = Link {
|
||||
href: "foo".to_string(),
|
||||
|
@ -63,10 +72,14 @@ mod tests {
|
|||
length: None,
|
||||
};
|
||||
//when
|
||||
add(&link, &file_name, &FileEnv::default())?;
|
||||
add(
|
||||
&link,
|
||||
file_name,
|
||||
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||
)?;
|
||||
|
||||
//then
|
||||
let content: Vec<String> = read_text_file(&file_name)?;
|
||||
let content: Vec<String> = read_text_file(&dir.path(), &file_name)?;
|
||||
drop(dir);
|
||||
|
||||
let expected = vec![
|
||||
|
|
|
@ -24,8 +24,8 @@ mod test {
|
|||
#[test]
|
||||
fn true_if_line_exists() -> Result<()> {
|
||||
//given
|
||||
let (dir, file_name) =
|
||||
create_text_file("file", include_bytes!("../../test/data/with-llamma.txt"))?;
|
||||
let file_name = "file";
|
||||
let dir = create_text_file(file_name, include_bytes!("../../test/data/with-llamma.txt"))?;
|
||||
let link = Link {
|
||||
href: "llamma".to_string(),
|
||||
rel: "".to_string(),
|
||||
|
@ -35,7 +35,11 @@ mod test {
|
|||
length: None,
|
||||
};
|
||||
//when
|
||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||
let result = find(
|
||||
&link,
|
||||
file_name,
|
||||
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||
)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -48,8 +52,11 @@ mod test {
|
|||
#[test]
|
||||
fn false_if_line_absent() -> Result<()> {
|
||||
//given
|
||||
let (dir, file_name) =
|
||||
create_text_file("file", include_bytes!("../../test/data/without-llamma.txt"))?;
|
||||
let file_name = "file";
|
||||
let dir = create_text_file(
|
||||
file_name,
|
||||
include_bytes!("../../test/data/without-llamma.txt"),
|
||||
)?;
|
||||
let link = Link {
|
||||
href: "llamma".to_string(),
|
||||
rel: "".to_string(),
|
||||
|
@ -60,7 +67,11 @@ mod test {
|
|||
};
|
||||
|
||||
//when
|
||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||
let result = find(
|
||||
&link,
|
||||
file_name,
|
||||
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||
)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -73,8 +84,9 @@ mod test {
|
|||
#[test]
|
||||
fn false_if_embedded_within_line() -> Result<()> {
|
||||
//given
|
||||
let (dir, file_name) = create_text_file(
|
||||
"file",
|
||||
let file_name = "file";
|
||||
let dir = create_text_file(
|
||||
file_name,
|
||||
include_bytes!("../../test/data/with-embedded-llamma.txt"),
|
||||
)?;
|
||||
let link = Link {
|
||||
|
@ -87,7 +99,11 @@ mod test {
|
|||
};
|
||||
|
||||
//when
|
||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||
let result = find(
|
||||
&link,
|
||||
file_name,
|
||||
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||
)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
|
28
src/lib.rs
28
src/lib.rs
|
@ -58,11 +58,12 @@ mod tests {
|
|||
let (tx, rx) = mpsc::channel::<String>(); // channel to recieve notice of downloaded urls
|
||||
|
||||
// two channels in subscriptions.txt
|
||||
let (subs_dir, subs_file_name) =
|
||||
create_text_file("subs", "@channel1\nignore me\n@channel2".as_bytes())?;
|
||||
let subs_file_name = "subs";
|
||||
let subs_dir =
|
||||
create_text_file(subs_file_name, "@channel1\nignore me\n@channel2".as_bytes())?;
|
||||
// one item from each channel is already listed in the downloads.txt file
|
||||
let (history_dir, history_file_name) =
|
||||
create_text_file("history", "c1-f2\nc2-f3".as_bytes())?;
|
||||
let history_file_name = "history";
|
||||
let history_dir = create_text_file(history_file_name, "c1-f2\nc2-f3".as_bytes())?;
|
||||
|
||||
let env = Env {
|
||||
network: NetworkEnv {
|
||||
|
@ -83,7 +84,24 @@ mod tests {
|
|||
download_as_mp3: mock_network_download_as_mp3(tx),
|
||||
},
|
||||
file: FileEnv {
|
||||
open: mock_file_open(vec![subs_file_name.clone(), history_file_name.clone()]),
|
||||
open: mock_file_open(HashMap::from([
|
||||
(
|
||||
subs_file_name.to_string(),
|
||||
format!(
|
||||
"{}/{}",
|
||||
subs_dir.path().to_string_lossy(),
|
||||
subs_file_name.to_string()
|
||||
),
|
||||
),
|
||||
(
|
||||
history_file_name.to_string(),
|
||||
format!(
|
||||
"{}/{}",
|
||||
history_dir.path().to_string_lossy(),
|
||||
history_file_name.to_string()
|
||||
),
|
||||
),
|
||||
])),
|
||||
append_line: mock_file_append_line(),
|
||||
},
|
||||
};
|
||||
|
|
|
@ -18,7 +18,7 @@ fn main() -> Result<()> {
|
|||
site,
|
||||
podal::Env {
|
||||
network: NetworkEnv::default(),
|
||||
file: FileEnv::default(),
|
||||
file: FileEnv::create(args.directory),
|
||||
},
|
||||
)?;
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ use std::{
|
|||
collections::HashMap,
|
||||
fs::{read_to_string, File},
|
||||
io::Write,
|
||||
path::Path,
|
||||
str::from_utf8,
|
||||
sync::mpsc::Sender,
|
||||
};
|
||||
|
@ -15,16 +16,18 @@ use crate::{
|
|||
prelude::*,
|
||||
};
|
||||
|
||||
pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> {
|
||||
pub fn create_text_file(name: &str, data: &[u8]) -> Result<TempDir> {
|
||||
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))
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
|
||||
pub fn read_text_file(path: &Path, file_name: &str) -> Result<Vec<String>> {
|
||||
let file_name = format!("{}/{}", path.to_str().unwrap(), file_name);
|
||||
println!("test_utils::read_text_file: {}", file_name);
|
||||
Ok(read_to_string(file_name)?
|
||||
.lines()
|
||||
.map(String::from)
|
||||
|
@ -58,10 +61,11 @@ pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
|||
}
|
||||
})
|
||||
}
|
||||
pub fn mock_file_open(real_paths: Vec<String>) -> FileOpenFn {
|
||||
pub fn mock_file_open(real_paths: HashMap<String, String>) -> FileOpenFn {
|
||||
Box::new(move |path: &str| {
|
||||
if real_paths.contains(&path.to_string()) {
|
||||
Ok(File::open(path)?)
|
||||
if let Some(real_path) = real_paths.get(&path.to_string()) {
|
||||
println!("opening {}", real_path);
|
||||
Ok(File::open(real_path)?)
|
||||
} else {
|
||||
Err(Error::message(
|
||||
format!("Not implemented: file_open: {}", path).as_str(),
|
||||
|
|
Loading…
Add table
Reference in a new issue