]> git.r.bdr.sh - rbdr/blog/commitdiff
Add tests for generators
authorRuben Beltran del Rio <redacted>
Tue, 7 Jan 2025 19:19:02 +0000 (20:19 +0100)
committerRuben Beltran del Rio <redacted>
Tue, 7 Jan 2025 19:19:02 +0000 (20:19 +0100)
src/archiver/gemini.rs
src/archiver/raw.rs
src/generator/html.rs
src/generator/mod.rs
src/generator/rss.rs
src/generator/static_files.rs
src/generator/txt.rs
src/template.rs

index aa3ac29d934bb6ac7a9cd6a3e0c41add500b58d4..7839ff240f01b0c096669d7db5e08e425597e59c 100644 (file)
@@ -71,7 +71,7 @@ mod tests {
     fn test_fails_if_output_folder_is_not_writable() {
         let test_dir = setup_test_dir();
         let template_dir = test_dir.join("template");
-        let output_dir = test_dir.join("/unwritable/folder/please");
+        let output_dir = test_dir.join("unwritable/folder/please");
         create_dir_all(&template_dir).expect("Could not create template test directory");
 
         create_test_file(&template_dir.join("index.gmi"), "Simple {{= test }}");
index 8641177ef8eaef965a135a67f1c3d16ab01a265a..5cac709afa0d1ebc9fb2807dbda73e4bcb4de4be 100644 (file)
@@ -16,7 +16,6 @@ mod tests {
     use std::fs::create_dir_all;
 
     use super::*;
-    use crate::template::Value;
 
     use test_utilities::*;
 
index 70cc3206c0fac5f3a4534ffe2f2023a2d2102a11..7895a83ca71d35b47cf19f650ec56befb8ed1c20 100644 (file)
@@ -20,3 +20,159 @@ pub fn generate(
     }
     Ok(())
 }
+
+#[cfg(test)]
+mod tests {
+    use std::collections::HashMap;
+    use std::fs::create_dir_all;
+
+    use super::*;
+
+    use crate::template::Value;
+
+    use test_utilities::*;
+
+    #[test]
+    fn test_generates_html_with_default_layout() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+
+        let context = HashMap::from([
+            ("has_posts".to_string(), Value::Bool(true)),
+            (
+                "posts".to_string(),
+                Value::Collection(vec![
+                    HashMap::from([
+                        ("index".to_string(), Value::Unsigned(2828)),
+                        (
+                            "html".to_string(),
+                            Value::String("<p>Contextualization</p>".to_string()),
+                        ),
+                    ]),
+                    HashMap::from([
+                        ("index".to_string(), Value::Unsigned(2828)),
+                        (
+                            "html".to_string(),
+                            Value::String("<p>Contexternalization</p>".to_string()),
+                        ),
+                    ]),
+                ]),
+            ),
+        ]);
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contains(&output_dir.join("index.html"), "<p>Contextualization</p>");
+        assert_file_contains(&output_dir.join("index.html"), "<p>Contexternalization</p>");
+    }
+
+    #[test]
+    fn test_uses_custom_layout_if_available() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+        create_test_file(
+            &template_dir.join("index.html"),
+            "\
+{{~ posts:post }}
+    {{= post.html }}
+{{~}}
+",
+        );
+        let context = HashMap::from([(
+            "posts".to_string(),
+            Value::Collection(vec![
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontextualization</p>".to_string()),
+                )]),
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontexternalization</p>".to_string()),
+                )]),
+            ]),
+        )]);
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contents(
+            &output_dir.join("index.html"),
+            "\
+<p>Recontextualization</p>
+
+    <p>Recontexternalization</p>
+",
+        );
+    }
+
+    #[test]
+    fn test_fails_if_html_is_malformed() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+        create_test_file(
+            &template_dir.join("index.html"),
+            "\
+{{~ posts:post }}
+    {{ post.html }}
+{{~}}
+",
+        );
+        let context = HashMap::new();
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+
+    #[test]
+    fn test_fails_if_output_is_not_writable() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        create_dir_all(&template_dir).expect("Could not create template directory");
+        create_test_file(
+            &template_dir.join("index.html"),
+            "\
+{{~ posts:post }}
+    {{= post.html }}
+{{~}}
+",
+        );
+        let context = HashMap::from([(
+            "posts".to_string(),
+            Value::Collection(vec![
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontextualization</p>".to_string()),
+                )]),
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontexternalization</p>".to_string()),
+                )]),
+            ]),
+        )]);
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+}
index dbe1c2e3ef3162b871c63e72fa61fcb3ffaaeade..55946afb57f86f9e2f1e0a3c1fac7218583a121d 100644 (file)
@@ -38,3 +38,150 @@ fn available_generators() -> Vec<Generator> {
         txt::generate,
     ]
 }
+
+#[cfg(test)]
+mod tests {
+    use std::fs::create_dir_all;
+
+    use super::*;
+
+    use crate::metadata::Metadata;
+    use crate::post::Post;
+
+    use test_utilities::*;
+
+    #[test]
+    fn test_generates() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_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 }}
+{{~}}
+",
+        );
+        let posts = vec![
+            Post {
+                metadata: Metadata {
+                    id: "1736035200000".to_string(),
+                    created_on: 1_736_035_200_000,
+                },
+                index: 9,
+                html: "<p>Generatorial</p>".to_string(),
+                raw: "electricity generator".to_string(),
+            },
+            Post {
+                metadata: Metadata {
+                    id: "1736045200000".to_string(),
+                    created_on: 1_736_045_200_000,
+                },
+                index: 10,
+                html: "<p>Generation</p>".to_string(),
+                raw: "kero kero".to_string(),
+            },
+        ];
+
+        generate(&static_dir, &template_dir, &output_dir, &posts).expect("Generate failed");
+
+        assert_file_contents(&output_dir.join("file.txt"), "wow");
+        assert_file_contents(
+            &output_dir.join("index.html"),
+            "\
+<p>Generatorial</p>
+
+    <p>Generation</p>
+",
+        );
+        assert_file_contents(
+            &output_dir.join("feed.xml"),
+            "\
+1736035200000
+
+    1736045200000
+",
+        );
+    }
+
+    #[test]
+    fn test_fails_if_directories_do_not_exist() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+        let posts = vec![];
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &posts);
+
+        assert!(result.is_err());
+    }
+
+    #[test]
+    fn test_fails_if_a_generator_fails() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_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 }}
+{{~}}
+",
+        );
+        let posts = vec![
+            Post {
+                metadata: Metadata {
+                    id: "1736035200000".to_string(),
+                    created_on: 1_736_035_200_000,
+                },
+                index: 9,
+                html: "<p>Generatorial</p>".to_string(),
+                raw: "electricity generator".to_string(),
+            },
+            Post {
+                metadata: Metadata {
+                    id: "1736045200000".to_string(),
+                    created_on: 1_736_045_200_000,
+                },
+                index: 10,
+                html: "<p>Generation</p>".to_string(),
+                raw: "kero kero".to_string(),
+            },
+        ];
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &posts);
+
+        assert!(result.is_err());
+    }
+}
index e79694e96c15de7cd48c95f84324c074e7e6f46b..c4b8bdb01090fb65527f3a60ca0cd3544d179bdd 100644 (file)
@@ -20,3 +20,175 @@ pub fn generate(
     }
     Ok(())
 }
+
+#[cfg(test)]
+mod tests {
+    use std::collections::HashMap;
+    use std::fs::create_dir_all;
+
+    use super::*;
+
+    use crate::template::Value;
+
+    use test_utilities::*;
+
+    #[test]
+    fn test_generates_rss_with_default_layout() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+
+        let context = HashMap::from([
+            ("has_posts".to_string(), Value::Bool(true)),
+            (
+                "posts".to_string(),
+                Value::Collection(vec![
+                    HashMap::from([
+                        ("id".to_string(), Value::String("123".to_string())),
+                        (
+                            "created_on_utc".to_string(),
+                            Value::String("Last Saturday".to_string()),
+                        ),
+                        (
+                            "title".to_string(),
+                            Value::String("Big words I know.".to_string()),
+                        ),
+                        (
+                            "escaped_html".to_string(),
+                            Value::String("<p>Contextualization</p>".to_string()),
+                        ),
+                    ]),
+                    HashMap::from([
+                        ("id".to_string(), Value::String("456".to_string())),
+                        (
+                            "created_on_utc".to_string(),
+                            Value::String("This Saturday".to_string()),
+                        ),
+                        (
+                            "title".to_string(),
+                            Value::String("Big words I don't know.".to_string()),
+                        ),
+                        (
+                            "escaped_html".to_string(),
+                            Value::String("<p>Contexternalization</p>".to_string()),
+                        ),
+                    ]),
+                ]),
+            ),
+        ]);
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contains(&output_dir.join("feed.xml"), "<p>Contextualization</p>");
+        assert_file_contains(&output_dir.join("feed.xml"), "<p>Contexternalization</p>");
+    }
+
+    #[test]
+    fn test_uses_custom_layout_if_available() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+        create_test_file(
+            &template_dir.join("feed.xml"),
+            "\
+{{~ posts:post }}
+    {{= post.html }}
+{{~}}
+",
+        );
+        let context = HashMap::from([(
+            "posts".to_string(),
+            Value::Collection(vec![
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontextualization</p>".to_string()),
+                )]),
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontexternalization</p>".to_string()),
+                )]),
+            ]),
+        )]);
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contents(
+            &output_dir.join("feed.xml"),
+            "\
+<p>Recontextualization</p>
+
+    <p>Recontexternalization</p>
+",
+        );
+    }
+
+    #[test]
+    fn test_fails_if_rss_is_malformed() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+        create_test_file(
+            &template_dir.join("feed.xml"),
+            "\
+{{~ posts:post }}
+    {{ post.html }}
+{{~}}
+",
+        );
+        let context = HashMap::new();
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+
+    #[test]
+    fn test_fails_if_output_is_not_writable() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        create_dir_all(&template_dir).expect("Could not create template directory");
+        create_test_file(
+            &template_dir.join("feed.xml"),
+            "\
+{{~ posts:post }}
+    {{= post.html }}
+{{~}}
+",
+        );
+        let context = HashMap::from([(
+            "posts".to_string(),
+            Value::Collection(vec![
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontextualization</p>".to_string()),
+                )]),
+                HashMap::from([(
+                    "html".to_string(),
+                    Value::String("<p>Recontexternalization</p>".to_string()),
+                )]),
+            ]),
+        )]);
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+}
index b4ec9fdbae0dc2162d0267a509aa83a0cc514407..449dcc4493b62aa88b00539fdaddcec5d188e20f 100644 (file)
@@ -9,3 +9,63 @@ pub fn generate(source: &Path, _: &Path, target: &Path, _: &Context) -> Result<(
     }
     Ok(())
 }
+
+#[cfg(test)]
+mod tests {
+    use std::collections::HashMap;
+    use std::fs::create_dir_all;
+
+    use super::*;
+
+    use test_utilities::*;
+
+    #[test]
+    fn test_copies_files() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        create_dir_all(&static_dir).expect("Could not create static directory");
+        create_dir_all(&output_dir).expect("Could not create output directory");
+        create_test_file(&static_dir.join("test.jpg"), "Cool hat");
+
+        let context = HashMap::new();
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contents(&output_dir.join("test.jpg"), "Cool hat");
+    }
+
+    #[test]
+    fn test_creates_directories_if_they_do_not_exist() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        create_dir_all(&static_dir).expect("Could not create static directory");
+        create_dir_all(&static_dir.join("nested")).expect("Could not create output directory");
+        create_test_file(&static_dir.join("nested/test.mov"), "Cool hats, multiple.");
+
+        let context = HashMap::new();
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contents(&output_dir.join("nested/test.mov"), "Cool hats, multiple.");
+    }
+
+    #[test]
+    fn test_it_does_not_fail_if_source_directory_does_not_exist() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        let context = HashMap::new();
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_ok());
+    }
+}
index 0a53655c185913808684b01139676a3240016708..ef48c033d347b353c8e5b529868ef4850ac10267 100644 (file)
@@ -20,3 +20,153 @@ pub fn generate(
     }
     Ok(())
 }
+
+#[cfg(test)]
+mod tests {
+    use std::collections::HashMap;
+    use std::fs::create_dir_all;
+
+    use super::*;
+
+    use crate::template::Value;
+
+    use test_utilities::*;
+
+    #[test]
+    fn test_generates_txt_with_default_layout() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+
+        let context = HashMap::from([
+            ("has_posts".to_string(), Value::Bool(true)),
+            (
+                "posts".to_string(),
+                Value::Collection(vec![
+                    HashMap::from([(
+                        "raw".to_string(),
+                        Value::String("Contextualization".to_string()),
+                    )]),
+                    HashMap::from([(
+                        "raw".to_string(),
+                        Value::String("Contexternalization".to_string()),
+                    )]),
+                ]),
+            ),
+        ]);
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contains(&output_dir.join("index.txt"), "Contextualization");
+        assert_file_contains(&output_dir.join("index.txt"), "Contexternalization");
+    }
+
+    #[test]
+    fn test_uses_custom_layout_if_available() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+        create_test_file(
+            &template_dir.join("index.txt"),
+            "\
+{{~ posts:post }}
+    {{= post.raw }}
+{{~}}
+",
+        );
+        let context = HashMap::from([(
+            "posts".to_string(),
+            Value::Collection(vec![
+                HashMap::from([(
+                    "raw".to_string(),
+                    Value::String("Recontextualization".to_string()),
+                )]),
+                HashMap::from([(
+                    "raw".to_string(),
+                    Value::String("Recontexternalization".to_string()),
+                )]),
+            ]),
+        )]);
+
+        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
+
+        assert_file_contents(
+            &output_dir.join("index.txt"),
+            "\
+Recontextualization
+
+    Recontexternalization
+",
+        );
+    }
+
+    #[test]
+    fn test_fails_if_txt_is_malformed() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        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(&output_dir).expect("Could not create output directory");
+        create_test_file(
+            &template_dir.join("index.txt"),
+            "\
+{{~ posts:post }}
+    {{ post.raw }}
+{{~}}
+",
+        );
+        let context = HashMap::new();
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+
+    #[test]
+    fn test_fails_if_output_is_not_writable() {
+        let test_dir = setup_test_dir();
+        let static_dir = test_dir.join("static");
+        let template_dir = test_dir.join("templates");
+        let output_dir = test_dir.join("output");
+
+        create_dir_all(&template_dir).expect("Could not create template directory");
+        create_test_file(
+            &template_dir.join("index.txt"),
+            "\
+{{~ posts:post }}
+    {{= post.raw }}
+{{~}}
+",
+        );
+        let context = HashMap::from([(
+            "posts".to_string(),
+            Value::Collection(vec![
+                HashMap::from([(
+                    "raw".to_string(),
+                    Value::String("Recontextualization".to_string()),
+                )]),
+                HashMap::from([(
+                    "raw".to_string(),
+                    Value::String("Recontexternalization".to_string()),
+                )]),
+            ]),
+        )]);
+
+        let result = generate(&static_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+}
index 3708efb6a73dfc604e8510790da3c07cfe116721..aef3440c1706a40a6771afdd6237f054dc49efa7 100644 (file)
@@ -292,7 +292,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.rss" => Some(RSS_TEMPLATE.to_string()),
+        "feed.xml" => Some(RSS_TEMPLATE.to_string()),
         &_ => None,
     }
 }
@@ -707,7 +707,7 @@ One last OK
     fn test_finds_default_rss_templalte() {
         let template = find(
             &PathBuf::from("/norealpath/ifthisfails/then/wow"),
-            "index.rss",
+            "feed.xml",
         );
         assert!(template.is_some());
     }