]> git.r.bdr.sh - rbdr/blog/blame - src/generator/rss.rs
Add tests for generators
[rbdr/blog] / src / generator / rss.rs
CommitLineData
b17907fa 1use crate::template::{find, parse, Context};
60307a9a 2use std::fs::write;
50f53dc4 3use std::io::{Error, ErrorKind::Other, Result};
b17907fa 4use std::path::Path;
29982470 5
60307a9a
RBR
6const FILENAME: &str = "feed.xml";
7
d7fef30a 8pub fn generate(
b17907fa
RBR
9 _: &Path,
10 template_directory: &Path,
11 target: &Path,
12 context: &Context,
d7fef30a 13) -> Result<()> {
b17907fa
RBR
14 if let Some(template) = find(template_directory, FILENAME) {
15 let parsed_template =
16 parse(&template).ok_or_else(|| Error::new(Other, "Unable to parse RSS template"))?;
17 let rendered_template = parsed_template.render(context)?;
18 let location = target.join(FILENAME);
19 write(location, rendered_template)?;
f6a545b0 20 }
29982470
RBR
21 Ok(())
22}
795d79af
RBR
23
24#[cfg(test)]
25mod tests {
26 use std::collections::HashMap;
27 use std::fs::create_dir_all;
28
29 use super::*;
30
31 use crate::template::Value;
32
33 use test_utilities::*;
34
35 #[test]
36 fn test_generates_rss_with_default_layout() {
37 let test_dir = setup_test_dir();
38 let static_dir = test_dir.join("static");
39 let template_dir = test_dir.join("templates");
40 let output_dir = test_dir.join("output");
41
42 create_dir_all(&static_dir).expect("Could not create static directory");
43 create_dir_all(&template_dir).expect("Could not create template directory");
44 create_dir_all(&output_dir).expect("Could not create output directory");
45
46 let context = HashMap::from([
47 ("has_posts".to_string(), Value::Bool(true)),
48 (
49 "posts".to_string(),
50 Value::Collection(vec![
51 HashMap::from([
52 ("id".to_string(), Value::String("123".to_string())),
53 (
54 "created_on_utc".to_string(),
55 Value::String("Last Saturday".to_string()),
56 ),
57 (
58 "title".to_string(),
59 Value::String("Big words I know.".to_string()),
60 ),
61 (
62 "escaped_html".to_string(),
63 Value::String("<p>Contextualization</p>".to_string()),
64 ),
65 ]),
66 HashMap::from([
67 ("id".to_string(), Value::String("456".to_string())),
68 (
69 "created_on_utc".to_string(),
70 Value::String("This Saturday".to_string()),
71 ),
72 (
73 "title".to_string(),
74 Value::String("Big words I don't know.".to_string()),
75 ),
76 (
77 "escaped_html".to_string(),
78 Value::String("<p>Contexternalization</p>".to_string()),
79 ),
80 ]),
81 ]),
82 ),
83 ]);
84
85 generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
86
87 assert_file_contains(&output_dir.join("feed.xml"), "<p>Contextualization</p>");
88 assert_file_contains(&output_dir.join("feed.xml"), "<p>Contexternalization</p>");
89 }
90
91 #[test]
92 fn test_uses_custom_layout_if_available() {
93 let test_dir = setup_test_dir();
94 let static_dir = test_dir.join("static");
95 let template_dir = test_dir.join("templates");
96 let output_dir = test_dir.join("output");
97
98 create_dir_all(&static_dir).expect("Could not create static directory");
99 create_dir_all(&template_dir).expect("Could not create template directory");
100 create_dir_all(&output_dir).expect("Could not create output directory");
101 create_test_file(
102 &template_dir.join("feed.xml"),
103 "\
104{{~ posts:post }}
105 {{= post.html }}
106{{~}}
107",
108 );
109 let context = HashMap::from([(
110 "posts".to_string(),
111 Value::Collection(vec![
112 HashMap::from([(
113 "html".to_string(),
114 Value::String("<p>Recontextualization</p>".to_string()),
115 )]),
116 HashMap::from([(
117 "html".to_string(),
118 Value::String("<p>Recontexternalization</p>".to_string()),
119 )]),
120 ]),
121 )]);
122
123 generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");
124
125 assert_file_contents(
126 &output_dir.join("feed.xml"),
127 "\
128<p>Recontextualization</p>
129
130 <p>Recontexternalization</p>
131",
132 );
133 }
134
135 #[test]
136 fn test_fails_if_rss_is_malformed() {
137 let test_dir = setup_test_dir();
138 let static_dir = test_dir.join("static");
139 let template_dir = test_dir.join("templates");
140 let output_dir = test_dir.join("output");
141
142 create_dir_all(&static_dir).expect("Could not create static directory");
143 create_dir_all(&template_dir).expect("Could not create template directory");
144 create_dir_all(&output_dir).expect("Could not create output directory");
145 create_test_file(
146 &template_dir.join("feed.xml"),
147 "\
148{{~ posts:post }}
149 {{ post.html }}
150{{~}}
151",
152 );
153 let context = HashMap::new();
154
155 let result = generate(&static_dir, &template_dir, &output_dir, &context);
156
157 assert!(result.is_err());
158 }
159
160 #[test]
161 fn test_fails_if_output_is_not_writable() {
162 let test_dir = setup_test_dir();
163 let static_dir = test_dir.join("static");
164 let template_dir = test_dir.join("templates");
165 let output_dir = test_dir.join("output");
166
167 create_dir_all(&template_dir).expect("Could not create template directory");
168 create_test_file(
169 &template_dir.join("feed.xml"),
170 "\
171{{~ posts:post }}
172 {{= post.html }}
173{{~}}
174",
175 );
176 let context = HashMap::from([(
177 "posts".to_string(),
178 Value::Collection(vec![
179 HashMap::from([(
180 "html".to_string(),
181 Value::String("<p>Recontextualization</p>".to_string()),
182 )]),
183 HashMap::from([(
184 "html".to_string(),
185 Value::String("<p>Recontexternalization</p>".to_string()),
186 )]),
187 ]),
188 )]);
189
190 let result = generate(&static_dir, &template_dir, &output_dir, &context);
191
192 assert!(result.is_err());
193 }
194}