diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-03 20:59:02 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-03 20:59:02 +0100 |
| commit | 260e8ec69b8e08b9fd105bf688e7a3a9fafecd61 (patch) | |
| tree | 3cbbccc74357dc8ddc37fb507005f4866df3b3fb /src/file_handler | |
| parent | 45fbf824248215a737d5d0b52920b43b68941ad3 (diff) | |
Add first tests
Diffstat (limited to 'src/file_handler')
| -rw-r--r-- | src/file_handler/file_strategies/file.rs | 142 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/gemini.rs | 220 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/layout.rs | 117 | ||||
| -rw-r--r-- | src/file_handler/mod.rs | 191 |
4 files changed, 639 insertions, 31 deletions
diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index 2346128..5faa689 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -31,7 +31,7 @@ impl FileHandlerStrategy for Strategy { } } - fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &String) { + fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &str) { return self.handle(source, destination, file); } @@ -39,3 +39,143 @@ impl FileHandlerStrategy for Strategy { return self.handle(source, destination, file); } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + + fn setup() -> Strategy { + Strategy {} + } + + fn fixtures_dir() -> PathBuf { + PathBuf::from("tests/fixtures") + } + + fn fixture_path(filename: &str) -> PathBuf { + fixtures_dir().join(filename) + } + + mod file_type_tests { + use super::*; + + #[test] + fn identifies_regular_files() { + let strategy = setup(); + assert!(strategy.is(&fixture_path("image.png"))); + assert!(strategy.is(&fixture_path("style.css"))); + assert!(!strategy.is(&fixtures_dir())); + } + + #[test] + fn identifies_file_type() { + let strategy = setup(); + assert!(matches!(strategy.identify(), FileType::File)); + } + + #[test] + fn handles_correct_file_type() { + let strategy = setup(); + assert!(strategy.can_handle(&FileType::File)); + assert!(!strategy.can_handle(&FileType::Layout)); + assert!(!strategy.can_handle(&FileType::Gemini)); + assert!(!strategy.can_handle(&FileType::Unknown)); + } + } + + mod file_handling_tests { + use super::*; + + #[test] + fn copies_single_file() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + + let file = File { + path: fixture_path("image.png"), + file_type: FileType::File, + }; + + strategy.handle(&source, &output, &file); + + let copied_path = output.join("image.png"); + assert!(copied_path.exists()); + + // Verify file contents are identical + let original = fs::read(&file.path).unwrap(); + let copied = fs::read(&copied_path).unwrap(); + assert_eq!(original, copied); + + // Cleanup + let _ = fs::remove_file(copied_path); + } + + #[test] + fn copies_nested_file() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + + let file = File { + path: fixture_path("assets/style.css"), + file_type: FileType::File, + }; + + strategy.handle(&source, &output, &file); + + let copied_path = output.join("assets").join("style.css"); + assert!(copied_path.exists()); + + // Verify file contents are identical + let original = fs::read(&file.path).unwrap(); + let copied = fs::read(&copied_path).unwrap(); + assert_eq!(original, copied); + + // Cleanup + let _ = fs::remove_file(copied_path); + let _ = fs::remove_dir(output.join("assets")); + } + + #[test] + fn handle_html_copies_file() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output_html"); + + let file = File { + path: fixture_path("image.png"), + file_type: FileType::File, + }; + + strategy.handle_html(&source, &output, &file, "unused layout"); + + let copied_path = output.join("image.png"); + assert!(copied_path.exists()); + + // Cleanup + let _ = fs::remove_file(copied_path); + } + + #[test] + fn handle_gemini_copies_file() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output_gemini"); + + let file = File { + path: fixture_path("image.png"), + file_type: FileType::File, + }; + + strategy.handle_gemini(&source, &output, &file); + + let copied_path = output.join("image.png"); + assert!(copied_path.exists()); + + // Cleanup + let _ = fs::remove_file(copied_path); + } + } +} diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index 977464c..d8bde14 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -6,6 +6,7 @@ use std::fs::{create_dir_all, read_to_string, File as IOFile}; use crate::file_handler::{File, FileType, FileHandlerStrategy}; use crate::gemini_parser::parse; +use crate::html_renderer::render_html; impl Strategy { fn is_title(&self, line: &str) -> bool { @@ -44,7 +45,7 @@ impl FileHandlerStrategy for Strategy { } } - fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String) { + fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &str) { let gemini_contents = read_to_string(&file.path).unwrap(); // Front matter extraction @@ -52,21 +53,23 @@ impl FileHandlerStrategy for Strategy { let mut lines_found = 0; let mut title = ""; let mut description = ""; - for line in lines[..2].iter() { - if self.is_title(&line) { - title = self.get_title(&line).trim(); - lines_found = lines_found + 1; - continue; - } - if self.is_description(&line) { - description = self.get_description(&line).trim(); - lines_found = lines_found + 1; - continue; + if let Some(slice) = lines.get(..2) { + for line in slice.iter() { + if self.is_title(&line) { + title = self.get_title(&line).trim(); + lines_found = lines_found + 1; + continue; + } + if self.is_description(&line) { + description = self.get_description(&line).trim(); + lines_found = lines_found + 1; + continue; + } } } let gemini_source = lines[lines_found..].join("\n"); - let content_html = parse(&gemini_source[..]); + let content_html = render_html(parse(&gemini_source[..])); let generated_html = layout .replace("{{ title }}", title) @@ -90,14 +93,16 @@ impl FileHandlerStrategy for Strategy { // Front matter extraction let lines: Vec<&str> = gemini_contents.split("\n").collect(); let mut lines_found = 0; - for line in lines[..2].iter() { - if self.is_title(&line) { - lines_found = lines_found + 1; - continue; - } - if self.is_description(&line) { - lines_found = lines_found + 1; - continue; + if let Some(slice) = lines.get(..2) { + for line in slice.iter() { + if self.is_title(&line) { + lines_found = lines_found + 1; + continue; + } + if self.is_description(&line) { + lines_found = lines_found + 1; + continue; + } } } @@ -112,3 +117,178 @@ impl FileHandlerStrategy for Strategy { destination_file.write_all(gemini_source.as_bytes()).unwrap(); } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + + fn setup() -> Strategy { + Strategy {} + } + + fn fixtures_dir() -> PathBuf { + PathBuf::from("tests/fixtures") + } + + fn fixture_path(filename: &str) -> PathBuf { + fixtures_dir().join(filename) + } + + fn read_fixture(filename: &str) -> String { + fs::read_to_string(fixture_path(filename)) + .unwrap_or_else(|_| panic!("Failed to read fixture file: {}", filename)) + } + + mod front_matter_tests { + use super::*; + + #[test] + fn detects_title() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let first_line = content.lines().next().unwrap(); + assert!(strategy.is_title(first_line)); + } + + #[test] + fn detects_description() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let second_line = content.lines().nth(1).unwrap(); + assert!(strategy.is_description(second_line)); + } + + #[test] + fn extracts_title() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let first_line = content.lines().next().unwrap(); + assert_eq!(strategy.get_title(first_line).trim(), "Test Title"); + } + + #[test] + fn extracts_description() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let second_line = content.lines().nth(1).unwrap(); + assert_eq!(strategy.get_description(second_line).trim(), "Test Description"); + } + } + + mod file_type_tests { + use super::*; + + #[test] + fn identifies_gemini_files() { + let strategy = setup(); + assert!(strategy.is(&fixture_path("test1.gmi"))); + assert!(!strategy.is(&fixture_path("_layout.html"))); + assert!(!strategy.is(&fixtures_dir())); + } + + #[test] + fn identifies_file_type() { + let strategy = setup(); + assert!(matches!(strategy.identify(), FileType::Gemini)); + } + + #[test] + fn handles_correct_file_type() { + let strategy = setup(); + assert!(strategy.can_handle(&FileType::Gemini)); + assert!(!strategy.can_handle(&FileType::Layout)); + assert!(!strategy.can_handle(&FileType::File)); + assert!(!strategy.can_handle(&FileType::Unknown)); + } + } + + mod file_handling_tests { + use super::*; + + #[test] + fn handles_html_generation() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + let layout = read_fixture("_layout.html"); + + let file = File { + path: fixture_path("test1.gmi"), + file_type: FileType::Gemini, + }; + + strategy.handle_html( + &source, + &output, + &file, + &layout, + ); + + let generated_path = output.join("test1.html"); + assert!(generated_path.exists()); + + let content = fs::read_to_string(generated_path.clone()).unwrap(); + assert!(content.contains("Test Title")); + assert!(content.contains("<h1>")); + + // Cleanup + let _ = fs::remove_file(generated_path); + } + + #[test] + fn handles_gemini_generation() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + + let file = File { + path: fixture_path("test1.gmi"), + file_type: FileType::Gemini, + }; + + strategy.handle_gemini( + &source, + &output, + &file, + ); + + let generated_path = output.join("test1.gmi"); + assert!(generated_path.exists()); + + let content = fs::read_to_string(&generated_path).unwrap(); + assert!(content.contains("# Heading")); + assert!(!content.contains("Test Title")); // Front matter should be removed + + // Cleanup + let _ = fs::remove_file(generated_path); + } + + #[test] + fn handles_nested_structure() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + let layout = read_fixture("_layout.html"); + + let file = File { + path: fixture_path("nested/nested.gmi"), + file_type: FileType::Gemini, + }; + + strategy.handle_html( + &source, + &output, + &file, + &layout, + ); + + let generated_path = output.join("nested").join("nested.html"); + assert!(generated_path.exists()); + + // Cleanup + let _ = fs::remove_file(generated_path); + let _ = fs::remove_dir(output.join("nested")); + } + } +} diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index f51bf7a..44b5000 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -22,6 +22,121 @@ impl FileHandlerStrategy for Strategy { // We don't implement handling for layout, as we assume there's only one // and it got handled before. - fn handle_html(&self, _s: &PathBuf, _d: &PathBuf, _f: &File, _l: &String) {} + fn handle_html(&self, _s: &PathBuf, _d: &PathBuf, _f: &File, _l: &str) {} fn handle_gemini(&self, _s: &PathBuf, _d: &PathBuf, _f: &File) {} } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + fn setup() -> Strategy { + Strategy {} + } + + mod is_tests { + use super::*; + + #[test] + fn identifies_layout_file() { + let strategy = setup(); + let path = PathBuf::from("_layout.html"); + assert!(strategy.is(&path)); + } + + #[test] + fn rejects_non_layout_html() { + let strategy = setup(); + let path = PathBuf::from("regular.html"); + assert!(!strategy.is(&path)); + } + + #[test] + fn rejects_layout_with_different_extension() { + let strategy = setup(); + let path = PathBuf::from("_layout.txt"); + assert!(!strategy.is(&path)); + } + + #[test] + fn rejects_layout_with_prefix() { + let strategy = setup(); + let path = PathBuf::from("prefix_layout.html"); + assert!(!strategy.is(&path)); + } + + #[test] + fn rejects_directory_named_layout() { + let strategy = setup(); + let path = PathBuf::from("tests/fixtures"); + + assert!(!strategy.is(&path)); + } + } + + mod identify_tests { + use super::*; + + #[test] + fn returns_layout_type() { + let strategy = setup(); + assert!(matches!(strategy.identify(), FileType::Layout)); + } + } + + mod can_handle_tests { + use super::*; + + #[test] + fn handles_layout_type() { + let strategy = setup(); + assert!(strategy.can_handle(&FileType::Layout)); + } + + #[test] + fn rejects_non_layout_types() { + let strategy = setup(); + assert!(!strategy.can_handle(&FileType::File)); + assert!(!strategy.can_handle(&FileType::Gemini)); + assert!(!strategy.can_handle(&FileType::Unknown)); + } + } + + mod handle_methods { + use super::*; + + #[test] + fn handle_html_does_nothing() { + let strategy = setup(); + let file = File { + path: PathBuf::from("test.html"), + file_type: FileType::Layout, + }; + + // Should not panic + strategy.handle_html( + &PathBuf::from("source"), + &PathBuf::from("dest"), + &file, + "layout content" + ); + } + + #[test] + fn handle_gemini_does_nothing() { + let strategy = setup(); + let file = File { + path: PathBuf::from("test.gmi"), + file_type: FileType::Layout, + }; + + // Should not panic + strategy.handle_gemini( + &PathBuf::from("source"), + &PathBuf::from("dest"), + &file + ); + } + } +} diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index 9a0133a..8ba50d4 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -35,7 +35,7 @@ impl FileHandler { FileType::Unknown } - pub fn get_layout_or_panic(&mut self, files: &Vec<File>) -> Result<(), &str> { + pub fn get_layout_or_panic(&mut self, files: &[File]) -> Result<(), &str> { for file in files { match file.file_type { FileType::Layout => { @@ -49,20 +49,22 @@ impl FileHandler { Err("No layout found. Please ensure there's a _layout.html file at the root") } - pub fn handle_all(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, files: &Vec<File>) { - for file in files { + pub fn handle_all(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, files: &[File]) { + files.iter().for_each(|file| { self.handle(source, html_destination, gemini_destination, file); - } + }); } pub fn handle(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, file: &File) { - for strategy in self.strategies.iter() { - if strategy.can_handle(&file.file_type) { - let layout = self.layout.as_ref().unwrap(); + if let Some(strategy) = self.strategies + .iter() + .find(|s| s.can_handle(&file.file_type)) + { + let layout = self.layout.as_ref() + .expect("Layout should be initialized before handling files"); strategy.handle_html(source, html_destination, file, layout); strategy.handle_gemini(source, gemini_destination, file); return; - } } } } @@ -71,10 +73,11 @@ pub trait FileHandlerStrategy { fn is(&self, path: &PathBuf) -> bool; fn identify(&self) -> FileType; fn can_handle(&self, file_type: &FileType) -> bool; - fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String); + fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &str); fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File); } +#[derive(Debug, Clone, PartialEq)] pub enum FileType { Gemini, File, @@ -82,7 +85,177 @@ pub enum FileType { Unknown, } +#[derive(PartialEq, Debug)] pub struct File { pub path: PathBuf, pub file_type: FileType, } + + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + fn create_test_file(path: &str, file_type: FileType) -> File { + File { + path: PathBuf::from(path), + file_type, + } + } + + #[test] + fn test_identify_gemini_file() { + let handler = FileHandler::default(); + let path = PathBuf::from("test.gmi"); + assert!(matches!(handler.identify(&path), FileType::Gemini)); + } + + #[test] + fn test_identify_layout_file() { + let handler = FileHandler::default(); + let path = PathBuf::from("_layout.html"); + assert!(matches!(handler.identify(&path), FileType::Layout)); + } + + #[test] + fn test_identify_regular_file() { + let handler = FileHandler::default(); + let path = PathBuf::from("regular.html"); + assert!(matches!(handler.identify(&path), FileType::File)); + } + + #[test] + fn test_identify_unknown_file() { + let handler = FileHandler::default(); + let path = PathBuf::from("tests/fixtures"); + assert!(matches!(handler.identify(&path), FileType::Unknown)); + } + + #[test] + fn test_get_layout_success() { + let mut handler = FileHandler::default(); + let files = vec![ + create_test_file("test.gmi", FileType::Gemini), + create_test_file("tests/fixtures/_layout.html", FileType::Layout), + create_test_file("regular.html", FileType::File), + ]; + + assert!(handler.get_layout_or_panic(&files).is_ok()); + } + + #[test] + fn test_get_layout_failure() { + let mut handler = FileHandler::default(); + let files = vec![ + create_test_file("test.gmi", FileType::Gemini), + create_test_file("regular.html", FileType::File), + ]; + + assert!(handler.get_layout_or_panic(&files).is_err()); + } + + // Mock strategy for testing + struct MockStrategy { + is_match: bool, + file_type: FileType, + } + + impl FileHandlerStrategy for MockStrategy { + fn is(&self, _path: &PathBuf) -> bool { + self.is_match + } + + fn identify(&self) -> FileType { + self.file_type.clone() + } + + fn can_handle(&self, file_type: &FileType) -> bool { + &self.file_type == file_type + } + + fn handle_html(&self, _source: &PathBuf, _destination: &PathBuf, _file: &File, _layout: &str) {} + fn handle_gemini(&self, _source: &PathBuf, _destination: &PathBuf, _file: &File) {} + } + + #[test] + fn test_custom_strategy() { + let mock_strategy = MockStrategy { + is_match: true, + file_type: FileType::Gemini, + }; + + let handler = FileHandler { + strategies: vec![Box::new(mock_strategy)], + layout: None, + }; + + let path = PathBuf::from("test.whatever"); + assert!(matches!(handler.identify(&path), FileType::Gemini)); + } + + #[test] + fn test_handle_all_empty_files() { + let handler = FileHandler::default(); + let files: Vec<File> = vec![]; + + // Should not panic with empty vector + handler.handle_all( + &PathBuf::from("source"), + &PathBuf::from("tests/fixtures/output"), + &PathBuf::from("tests/fixtures/output"), + &files + ); + } + + #[test] + fn test_handle_with_layout() { + let mut handler = FileHandler::default(); + handler.layout = Some("test layout".to_string()); + + let file = create_test_file("tests/fixtures/test1.gmi", FileType::Gemini); + + // Should not panic with valid layout + handler.handle( + &PathBuf::from(""), + &PathBuf::from("tests/fixtures/output"), + &PathBuf::from("tests/fixtures/output"), + &file + ); + } + + #[test] + #[should_panic(expected = "Layout should be initialized before handling files")] + fn test_handle_without_layout() { + let handler = FileHandler::default(); + let file = create_test_file("test.gmi", FileType::Gemini); + + handler.handle( + &PathBuf::from("source"), + &PathBuf::from("tests/fixtures/output"), + &PathBuf::from("tests/fixtures/output"), + &file + ); + } + + #[test] + fn test_slice_handling() { + let mut handler = FileHandler::default(); + let files = vec![ + create_test_file("tests/fixtures/test1.gmi", FileType::Gemini), + create_test_file("tests/fixtures/_layout.html", FileType::Layout), + create_test_file("tests/fixtures/test2.gmi", FileType::Gemini), + create_test_file("tests/fixtures/test3.gmi", FileType::Gemini), + ]; + + let _ = handler.get_layout_or_panic(&files[1..]); + + // Test with slice + handler.handle_all( + &PathBuf::from(""), + &PathBuf::from("tests/fixtures/output"), + &PathBuf::from("tests/fixtures/output"), + &files[1..] // Test with slice of last three elements + ); + } +} |