]> git.r.bdr.sh - rbdr/blog/blame - src/generator/mod.rs
Add tests for generators
[rbdr/blog] / src / generator / mod.rs
CommitLineData
29982470
RBR
1mod html;
2mod rss;
d7fef30a 3mod static_files;
29982470
RBR
4mod txt;
5
29982470 6use crate::post::Post;
b17907fa 7use crate::template::Context;
d7fef30a 8use std::io::Result;
b17907fa
RBR
9use std::path::Path;
10
11type Generator = fn(&Path, &Path, &Path, &Context) -> Result<()>;
29982470 12
d7fef30a 13pub fn generate(
b17907fa
RBR
14 static_directory: &Path,
15 template_directory: &Path,
16 output_directory: &Path,
17 posts: &[Post],
d7fef30a 18) -> Result<()> {
29982470 19 let generators = available_generators();
b17907fa 20 let context = Post::to_template_context(posts);
29982470 21 for generator in generators {
d7fef30a
RBR
22 generator(
23 static_directory,
24 template_directory,
25 output_directory,
26 &context,
27 )?;
29982470
RBR
28 }
29 Ok(())
30}
31
b17907fa 32fn available_generators() -> Vec<Generator> {
29982470
RBR
33 vec![
34 static_files::generate,
60307a9a 35 // These three are actually the same. Can generalize, don't know how in rust yet.
29982470
RBR
36 html::generate,
37 rss::generate,
d7fef30a 38 txt::generate,
29982470
RBR
39 ]
40}
795d79af
RBR
41
42#[cfg(test)]
43mod tests {
44 use std::fs::create_dir_all;
45
46 use super::*;
47
48 use crate::metadata::Metadata;
49 use crate::post::Post;
50
51 use test_utilities::*;
52
53 #[test]
54 fn test_generates() {
55 let test_dir = setup_test_dir();
56 let static_dir = test_dir.join("static");
57 let template_dir = test_dir.join("templates");
58 let output_dir = test_dir.join("output");
59
60 create_dir_all(&static_dir).expect("Could not create static directory");
61 create_dir_all(&template_dir).expect("Could not create template directory");
62 create_dir_all(&output_dir).expect("Could not create output directory");
63 create_test_file(&static_dir.join("file.txt"), "wow");
64 create_test_file(
65 &template_dir.join("index.html"),
66 "\
67{{~ posts:post }}
68 {{= post.html }}
69{{~}}
70",
71 );
72 create_test_file(
73 &template_dir.join("feed.xml"),
74 "\
75{{~ posts:post }}
76 {{= post.id }}
77{{~}}
78",
79 );
80 create_test_file(
81 &template_dir.join("index.txt"),
82 "\
83{{~ posts:post }}
84 {{= post.raw }}
85{{~}}
86",
87 );
88 let posts = vec![
89 Post {
90 metadata: Metadata {
91 id: "1736035200000".to_string(),
92 created_on: 1_736_035_200_000,
93 },
94 index: 9,
95 html: "<p>Generatorial</p>".to_string(),
96 raw: "electricity generator".to_string(),
97 },
98 Post {
99 metadata: Metadata {
100 id: "1736045200000".to_string(),
101 created_on: 1_736_045_200_000,
102 },
103 index: 10,
104 html: "<p>Generation</p>".to_string(),
105 raw: "kero kero".to_string(),
106 },
107 ];
108
109 generate(&static_dir, &template_dir, &output_dir, &posts).expect("Generate failed");
110
111 assert_file_contents(&output_dir.join("file.txt"), "wow");
112 assert_file_contents(
113 &output_dir.join("index.html"),
114 "\
115<p>Generatorial</p>
116
117 <p>Generation</p>
118",
119 );
120 assert_file_contents(
121 &output_dir.join("feed.xml"),
122 "\
1231736035200000
124
125 1736045200000
126",
127 );
128 }
129
130 #[test]
131 fn test_fails_if_directories_do_not_exist() {
132 let test_dir = setup_test_dir();
133 let static_dir = test_dir.join("static");
134 let template_dir = test_dir.join("templates");
135 let output_dir = test_dir.join("output");
136 let posts = vec![];
137
138 let result = generate(&static_dir, &template_dir, &output_dir, &posts);
139
140 assert!(result.is_err());
141 }
142
143 #[test]
144 fn test_fails_if_a_generator_fails() {
145 let test_dir = setup_test_dir();
146 let static_dir = test_dir.join("static");
147 let template_dir = test_dir.join("templates");
148 let output_dir = test_dir.join("output");
149
150 create_dir_all(&static_dir).expect("Could not create static directory");
151 create_dir_all(&template_dir).expect("Could not create template directory");
152 create_dir_all(&output_dir).expect("Could not create output directory");
153 create_test_file(&static_dir.join("file.txt"), "wow");
154 create_test_file(
155 &template_dir.join("index.html"),
156 "\
157{{~ posts:post }}
158 {{ post.html }}
159{{~}}
160",
161 );
162 let posts = vec![
163 Post {
164 metadata: Metadata {
165 id: "1736035200000".to_string(),
166 created_on: 1_736_035_200_000,
167 },
168 index: 9,
169 html: "<p>Generatorial</p>".to_string(),
170 raw: "electricity generator".to_string(),
171 },
172 Post {
173 metadata: Metadata {
174 id: "1736045200000".to_string(),
175 created_on: 1_736_045_200_000,
176 },
177 index: 10,
178 html: "<p>Generation</p>".to_string(),
179 raw: "kero kero".to_string(),
180 },
181 ];
182
183 let result = generate(&static_dir, &template_dir, &output_dir, &posts);
184
185 assert!(result.is_err());
186 }
187}