diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-03-06 23:45:38 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-03-06 23:45:38 +0100 |
| commit | cd63ee5ae6542f0603ab34ac9affc33f7c2e37eb (patch) | |
| tree | 08750426cb3ba66df0c7c113c81790e8ba6adb06 /src | |
| parent | 7761f8b4814b86bb23fb3d1b8498b147bd1313e9 (diff) | |
Add tests to generate
Diffstat (limited to 'src')
| -rw-r--r-- | src/command/generate.rs | 203 | ||||
| -rw-r--r-- | src/template.rs | 2 |
2 files changed, 202 insertions, 3 deletions
diff --git a/src/command/generate.rs b/src/command/generate.rs index 45d5850..5485b85 100644 --- a/src/command/generate.rs +++ b/src/command/generate.rs @@ -31,18 +31,15 @@ impl Generate { } fn find_blog_content(post_path: &Path) -> Option<String> { - println!("{}", post_path.display()); let entries = read_dir(post_path).ok()?; for entry in entries.filter_map(Result::ok) { let entry_path = entry.path(); - println!("{}", entry_path.display()); match entry_path.extension() { Some(extension) => { if extension == "gmi" { let mut file = File::open(entry_path).ok()?; let mut contents = String::new(); file.read_to_string(&mut contents).ok()?; - println!("{contents}"); return Some(contents); } } @@ -106,3 +103,203 @@ impl super::Command for Generate { "\t\t\t\tGenerates the blog assets" } } + +#[cfg(test)] +mod tests { + use std::fs::{File, create_dir_all}; + use std::io::Write; + + use super::*; + use crate::command::Command; + use crate::configuration::Configuration; + + use test_utilities::*; + + #[test] + fn test_generate_command() { + let generate = Generate::new(); + + let test_dir = setup_test_dir(); + let static_dir = test_dir.join("static"); + let template_dir = test_dir.join("templates"); + let blog_output_dir = test_dir.join("blog_output"); + let archive_output_dir = test_dir.join("archive_output"); + let posts_dir = test_dir.join("posts"); + let archive_dir = test_dir.join("archive"); + + create_dir_all(&static_dir).expect("Could not create static directory"); + create_dir_all(&template_dir).expect("Could not create template directory"); + create_dir_all(&blog_output_dir).expect("Could not create blog output directory"); + create_dir_all(&archive_output_dir).expect("Could not create archive output directory"); + create_dir_all(&posts_dir).expect("Could not create output directory"); + create_test_file(&static_dir.join("file.txt"), "wow"); + create_test_file( + &template_dir.join("index.html"), + "\ +{{~ posts:post }} + {{= post.html }} +{{~}} +", + ); + create_test_file( + &template_dir.join("feed.xml"), + "\ +{{~ posts:post }} + {{= post.id }} +{{~}} +", + ); + create_test_file( + &template_dir.join("index.txt"), + "\ +{{~ posts:post }} + {{= post.raw }} +{{~}} +", + ); + + // Create the posts + create_dir_all(&posts_dir.join("0")) + .expect("Could not create post 0 directory"); + create_test_file( + &posts_dir.join("0/metadata.json"), + "{ \"id\": \"1736035200000\", \"created_on\": 1736035200000 }" + ); + create_test_file( + &posts_dir.join("0/ignored"), + "IGNORED" + ); + create_test_file( + &posts_dir.join("0/the-latest.gmi"), + "The latest is here." + ); + create_dir_all(&posts_dir.join("1")) + .expect("Could not create post 1 directory"); + create_test_file( + &posts_dir.join("1/metadata.json"), + "{ \"id\": \"1736045200000\", \"created_on\": 1736045200000 }" + ); + create_test_file( + &posts_dir.join("1/the-latest.gmi"), + "Often forgotten." + ); + create_dir_all(&posts_dir.join("2")) + .expect("Could not create post 2 directory"); + create_test_file( + &posts_dir.join("2/metadata.json"), + "{ \"id\": \"1736045200009\", \"created_on\": 1736045200009 }" + ); + + create_dir_all(&posts_dir.join("3")) + .expect("Could not create post 3 directory"); + let invalid_bytes = vec![0xFF, 0xFF, 0xFF, 0xFF]; + create_test_file( + &posts_dir.join("3/metadata.json"), + "{ \"id\": \"1736045200099\", \"created_on\": 1736045200099 }" + ); + let mut bad_file = File::create(&posts_dir.join("3/bad-file.gmi")) + .expect("Could not create bad file."); + bad_file + .write_all(&invalid_bytes) + .expect("Could not write bad bytes to bad file."); + + // Create the archive posts + create_dir_all(&archive_dir.join("1736035200000")) + .expect("Could not create post 0 directory"); + create_test_file( + &archive_dir.join("1736035200000/metadata.json"), + "{ \"id\": \"1736035200000\", \"created_on\": 1736035200000 }" + ); + create_test_file( + &archive_dir.join("1736035200000/the-latest.gmi"), + "The latest is here." + ); + create_dir_all(&archive_dir.join("1736045200000")) + .expect("Could not create post 1 archive directory"); + create_test_file( + &archive_dir.join("1736045200000/metadata.json"), + "{ \"id\": \"1736045200000\", \"created_on\": 1736045200000 }" + ); + create_test_file( + &posts_dir.join("1/the-latest.gmi"), + "Often forgotten." + ); + + let mut configuration = Configuration::new(); + configuration.archive_directory = archive_dir.clone(); + configuration.static_directory = static_dir.clone(); + configuration.templates_directory = template_dir.clone(); + configuration.blog_output_directory = blog_output_dir.clone(); + configuration.archive_output_directory = archive_output_dir.clone(); + configuration.posts_directory = posts_dir.clone(); + configuration.max_posts = 4; + + generate + .execute(None, &configuration, "generate") + .expect("Could not generate"); + + assert_file_contents(&blog_output_dir.join("file.txt"), "wow"); + + assert_file_contents( + &blog_output_dir.join("index.html"), + "\ +<p>The latest is here.</p> + + + <p>Often forgotten.</p> +", + ); + assert_file_contents( + &blog_output_dir.join("feed.xml"), + "\ +1736035200000 + + 1736045200000 +", + ); + assert_file_contains( + &archive_output_dir.join("index.gmi"), + "\n=> ./1736035200000/the-latest.gmi 2025-01-05 the latest", + ); + assert_file_contains( + &archive_output_dir.join("index.gph"), + "\n[0|2025-01-05 the latest|./1736035200000/the-latest.gmi|server|port]", + ); + assert_file_contents( + &archive_output_dir.join("1736035200000/the-latest.gmi"), + "The latest is here." + ); + + cleanup_test_dir(&test_dir); + } + + #[test] + fn generate_before_dependencies() { + let generate = Generate::new(); + let dependencies = generate.before_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + #[test] + fn generate_after_dependencies() { + let generate = Generate::new(); + let dependencies = generate.after_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + // These two tests feel pointless but I'm doing it for the coverage :p + + #[test] + fn generate_command_output() { + let generate = Generate::new(); + generate.command(); + } + + #[test] + fn generate_help_output() { + let generate = Generate::new(); + generate.help(); + } +} diff --git a/src/template.rs b/src/template.rs index 7e47e65..2e4c8a6 100644 --- a/src/template.rs +++ b/src/template.rs @@ -7,6 +7,7 @@ use std::path::Path; const TXT_TEMPLATE: &str = include_str!("../templates/index.txt"); const HTML_TEMPLATE: &str = include_str!("../templates/index.html"); const GMI_TEMPLATE: &str = include_str!("../templates/index.gmi"); +const GPH_TEMPLATE: &str = include_str!("../templates/index.gph"); const RSS_TEMPLATE: &str = include_str!("../templates/feed.xml"); // Parse and Render @@ -292,6 +293,7 @@ fn find_default(filename: &str) -> Option<String> { "index.txt" => Some(TXT_TEMPLATE.to_string()), "index.html" => Some(HTML_TEMPLATE.to_string()), "index.gmi" => Some(GMI_TEMPLATE.to_string()), + "index.gph" => Some(GPH_TEMPLATE.to_string()), "feed.xml" => Some(RSS_TEMPLATE.to_string()), &_ => None, } |