]> git.r.bdr.sh - rbdr/blog/blobdiff - src/archiver/gopher.rs
Add tests for generators
[rbdr/blog] / src / archiver / gopher.rs
index fc0c04f221a6516a58397ee652de76e5a252a368..fe37616e3e0323eb1cabe49880fa88a3335da2fb 100644 (file)
@@ -1,4 +1,4 @@
-use crate::template::{find, parse, TemplateContext};
+use crate::template::{find, parse, Context};
 use std::fs::write;
 use std::io::{Error, ErrorKind::Other, Result};
 use std::path::Path;
@@ -9,7 +9,7 @@ pub fn archive(
     _: &Path,
     template_directory: &Path,
     target: &Path,
-    context: &TemplateContext,
+    context: &Context,
 ) -> Result<()> {
     if let Some(template) = find(template_directory, FILENAME) {
         let parsed_template = parse(&template)
@@ -20,3 +20,67 @@ pub fn archive(
     }
     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_renders_the_context_into_the_template() {
+        let test_dir = setup_test_dir();
+        let template_dir = test_dir.join("template");
+        let output_dir = test_dir.join("output");
+        create_dir_all(&output_dir).expect("Could not create output test directory");
+        create_dir_all(&template_dir).expect("Could not create template test directory");
+
+        create_test_file(&template_dir.join("index.gph"), "Simple {{= test }}");
+
+        let context: Context =
+            HashMap::from([("test".to_string(), Value::String("things".to_string()))]);
+
+        archive(&test_dir, &template_dir, &output_dir, &context).expect("Archive failed");
+
+        assert_file_contents(&output_dir.join("index.gph"), "Simple things");
+    }
+
+    #[test]
+    fn test_fails_if_template_is_malformed() {
+        let test_dir = setup_test_dir();
+        let template_dir = test_dir.join("template");
+        let output_dir = test_dir.join("output");
+        create_dir_all(&output_dir).expect("Could not create output test directory");
+        create_dir_all(&template_dir).expect("Could not create template test directory");
+
+        create_test_file(&template_dir.join("index.gph"), "Simple {{ test }}");
+
+        let context: Context =
+            HashMap::from([("test".to_string(), Value::String("things".to_string()))]);
+
+        let result = archive(&test_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+
+    #[test]
+    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("/gopherunwritable/folder/please");
+        create_dir_all(&template_dir).expect("Could not create template test directory");
+
+        create_test_file(&template_dir.join("index.gph"), "Simple {{= test }}");
+
+        let context: Context =
+            HashMap::from([("test".to_string(), Value::String("things".to_string()))]);
+
+        let result = archive(&test_dir, &template_dir, &output_dir, &context);
+
+        assert!(result.is_err());
+    }
+}