From 7fe1c1366cbe77afcc6e5f11e4f9e895283e3487 Mon Sep 17 00:00:00 2001 From: James Tease Date: Mon, 3 Jul 2017 11:56:48 +0100 Subject: [PATCH 1/2] Add timestamp to layouts for cachebusting --- Cargo.lock | 2 +- src/cobalt.rs | 2 ++ .../timestamp/_layouts/default.liquid | 13 ++++++++ .../fixtures/timestamp/_layouts/posts.liquid | 10 ++++++ tests/fixtures/timestamp/index.liquid | 7 ++++ .../posts/2014-08-24-my-first-blogpost.md | 10 ++++++ tests/mod.rs | 32 +++++++++++++++++++ .../posts/2014-08-24-my-first-blogpost.html | 13 ++++++++ 8 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/timestamp/_layouts/default.liquid create mode 100644 tests/fixtures/timestamp/_layouts/posts.liquid create mode 100644 tests/fixtures/timestamp/index.liquid create mode 100644 tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md create mode 100644 tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html diff --git a/Cargo.lock b/Cargo.lock index e859d3d9..bf9d8601 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "cobalt-bin" -version = "0.6.1" +version = "0.7.1" dependencies = [ "assert_cli 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/cobalt.rs b/src/cobalt.rs index f9b7a352..a3f16d90 100644 --- a/src/cobalt.rs +++ b/src/cobalt.rs @@ -176,12 +176,14 @@ pub fn build(config: &Config) -> Result<()> { .collect(); trace!("Generating other documents"); + let timestamp = Value::Str(UTC::now().timestamp().to_string()); for mut doc in documents { trace!("Generating {}", doc.path); if config.dump.contains(&Dump::Liquid) { create_liquid_dump(dest, &doc.path, &doc.content, &doc.attributes)?; } + doc.attributes.insert("timestamp".to_owned(), timestamp.clone()); let mut context = doc.get_render_context(&posts_data); let doc_html = try!(doc.render(&mut context, source, &layouts, &mut layouts_cache)); diff --git a/tests/fixtures/timestamp/_layouts/default.liquid b/tests/fixtures/timestamp/_layouts/default.liquid new file mode 100644 index 00000000..b522876e --- /dev/null +++ b/tests/fixtures/timestamp/_layouts/default.liquid @@ -0,0 +1,13 @@ + + + + test + + + +

{{ path }}

+ + {{ content }} + + + diff --git a/tests/fixtures/timestamp/_layouts/posts.liquid b/tests/fixtures/timestamp/_layouts/posts.liquid new file mode 100644 index 00000000..46d21c6d --- /dev/null +++ b/tests/fixtures/timestamp/_layouts/posts.liquid @@ -0,0 +1,10 @@ + + + + My blog - {{ title }} + + + {{ content }} + + + diff --git a/tests/fixtures/timestamp/index.liquid b/tests/fixtures/timestamp/index.liquid new file mode 100644 index 00000000..93de6018 --- /dev/null +++ b/tests/fixtures/timestamp/index.liquid @@ -0,0 +1,7 @@ +extends: default.liquid +--- +This is my Index page! + +{% for post in posts %} + {{ post.title }} +{% endfor %} diff --git a/tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md b/tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md new file mode 100644 index 00000000..982e38c4 --- /dev/null +++ b/tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md @@ -0,0 +1,10 @@ +extends: posts.liquid + +title: My first Blogpost +date: 24/08/2014 at 15:36 +--- +# {{ title }} + +Hey there this is my first blogpost and this is super awesome. + +My Blog is lorem ipsum like, yes it is.. diff --git a/tests/mod.rs b/tests/mod.rs index da93efed..af65d810 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -3,6 +3,8 @@ extern crate difference; extern crate cobalt; extern crate tempdir; extern crate walkdir; +extern crate chrono; +extern crate regex; use std::path::Path; use std::fs::{self, File}; @@ -11,6 +13,8 @@ use tempdir::TempDir; use walkdir::WalkDir; use std::error::Error; use cobalt::Config; +use chrono::{UTC}; +use regex::{Regex}; fn run_test(name: &str) -> Result<(), cobalt::Error> { let target = format!("tests/target/{}/", name); @@ -226,3 +230,31 @@ pub fn empty_frontmatter() { pub fn querystrings() { run_test("querystrings").expect("Build error"); } + +#[test] +pub fn timestamp() { + // timestamp adds current ms time so need to build the target folder at run time + let mut config = Config::from_file("tests/fixtures/timestamp/.cobalt.yml") + .unwrap_or_default(); + config.source = "tests/fixtures/timestamp/".to_string(); + config.dest = "tests/target/timestamp/".to_string(); + + fs::create_dir_all(&config.dest).is_ok(); + + let timestamp = UTC::now().timestamp().to_string(); + let result = cobalt::build(&config); + + if result.is_ok() { + // check timestamp is in the target layout + let mut content = String::new(); + let _file = File::open("tests/target/timestamp/index.html").unwrap().read_to_string(&mut content); + let re = Regex::new(×tamp.as_str()).unwrap(); + assert!(re.is_match(&content.as_str())); + + // run standard test to make sure it doesn't fall over + run_test("timestamp").expect("Build error"); + + // delete file with timestamp otherwise next test build will fail + let _deleted = fs::remove_file("tests/target/timestamp/index.html"); + } +} \ No newline at end of file diff --git a/tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html b/tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html new file mode 100644 index 00000000..163d0665 --- /dev/null +++ b/tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html @@ -0,0 +1,13 @@ + + + + My blog - My first Blogpost + + +

My first Blogpost

+

Hey there this is my first blogpost and this is super awesome.

+

My Blog is lorem ipsum like, yes it is..

+ + + + From cc562c921dc15b21f2fc11305c2c6170c3e35b87 Mon Sep 17 00:00:00 2001 From: James Tease Date: Mon, 3 Jul 2017 15:02:39 +0100 Subject: [PATCH 2/2] Fix lint --- src/cobalt.rs | 3 ++- tests/mod.rs | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cobalt.rs b/src/cobalt.rs index a3f16d90..8e45841e 100644 --- a/src/cobalt.rs +++ b/src/cobalt.rs @@ -183,7 +183,8 @@ pub fn build(config: &Config) -> Result<()> { if config.dump.contains(&Dump::Liquid) { create_liquid_dump(dest, &doc.path, &doc.content, &doc.attributes)?; } - doc.attributes.insert("timestamp".to_owned(), timestamp.clone()); + doc.attributes + .insert("timestamp".to_owned(), timestamp.clone()); let mut context = doc.get_render_context(&posts_data); let doc_html = try!(doc.render(&mut context, source, &layouts, &mut layouts_cache)); diff --git a/tests/mod.rs b/tests/mod.rs index af65d810..ea95a771 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -13,8 +13,8 @@ use tempdir::TempDir; use walkdir::WalkDir; use std::error::Error; use cobalt::Config; -use chrono::{UTC}; -use regex::{Regex}; +use chrono::UTC; +use regex::Regex; fn run_test(name: &str) -> Result<(), cobalt::Error> { let target = format!("tests/target/{}/", name); @@ -234,8 +234,7 @@ pub fn querystrings() { #[test] pub fn timestamp() { // timestamp adds current ms time so need to build the target folder at run time - let mut config = Config::from_file("tests/fixtures/timestamp/.cobalt.yml") - .unwrap_or_default(); + let mut config = Config::from_file("tests/fixtures/timestamp/.cobalt.yml").unwrap_or_default(); config.source = "tests/fixtures/timestamp/".to_string(); config.dest = "tests/target/timestamp/".to_string(); @@ -247,7 +246,9 @@ pub fn timestamp() { if result.is_ok() { // check timestamp is in the target layout let mut content = String::new(); - let _file = File::open("tests/target/timestamp/index.html").unwrap().read_to_string(&mut content); + let _file = File::open("tests/target/timestamp/index.html") + .unwrap() + .read_to_string(&mut content); let re = Regex::new(×tamp.as_str()).unwrap(); assert!(re.is_match(&content.as_str()));