Skip to content

Commit aaa7598

Browse files
add test to load handlebars templates
1 parent 791dc04 commit aaa7598

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/web/mod.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use iron::{self, Handler, status};
5454
use iron::headers::{CacheControl, CacheDirective, ContentType};
5555
use router::{Router, NoRoute};
5656
use staticfile::Static;
57-
use handlebars_iron::{HandlebarsEngine, DirectorySource};
57+
use handlebars_iron::{HandlebarsEngine, DirectorySource, SourceError};
5858
use time;
5959
use postgres::Connection;
6060
use semver::{Version, VersionReq};
@@ -66,6 +66,16 @@ const STATIC_FILE_CACHE_DURATION: u64 = 60 * 60 * 24 * 30 * 12; // 12 months
6666
const STYLE_CSS: &'static str = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
6767
const OPENSEARCH_XML: &'static [u8] = include_bytes!("opensearch.xml");
6868

69+
fn handlebars_engine() -> Result<HandlebarsEngine, SourceError> {
70+
// TODO: Use DocBuilderOptions for paths
71+
let mut hbse = HandlebarsEngine::new();
72+
hbse.add(Box::new(DirectorySource::new("./templates", ".hbs")));
73+
74+
// load templates
75+
try!(hbse.reload());
76+
77+
Ok(hbse)
78+
}
6979

7080
struct CratesfyiHandler {
7181
shared_resource_handler: Box<Handler>,
@@ -77,14 +87,10 @@ struct CratesfyiHandler {
7787

7888
impl CratesfyiHandler {
7989
fn chain<H: Handler>(base: H) -> Chain {
80-
// TODO: Use DocBuilderOptions for paths
81-
let mut hbse = HandlebarsEngine::new();
82-
hbse.add(Box::new(DirectorySource::new("./templates", ".hbs")));
83-
84-
// load templates
85-
if let Err(e) = hbse.reload() {
86-
panic!("Failed to load handlebar templates: {}", e.description());
87-
}
90+
let hbse = match handlebars_engine() {
91+
Ok(hbse) => hbse,
92+
Err(e) => panic!("Failed to load handlebar templates: {}", e.description()),
93+
};
8894

8995
let mut chain = Chain::new(base);
9096
chain.link_before(pool::Pool::new());
@@ -560,4 +566,11 @@ mod test {
560566
assert_eq!(latest_version(&versions, "0.9.0"), Some("1.1.0".to_owned()));
561567
assert_eq!(latest_version(&versions, "invalidversion"), None);
562568
}
569+
570+
#[test]
571+
fn test_templates_are_valid() {
572+
if let Err(e) = handlebars_engine() {
573+
panic!("Failed to load handlebar templates: {}", e.description());
574+
}
575+
}
563576
}

0 commit comments

Comments
 (0)