2015-11-06 20:22:45 +00:00
|
|
|
extern crate difference;
|
|
|
|
extern crate cobalt;
|
|
|
|
extern crate walkdir;
|
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::Read;
|
|
|
|
use walkdir::WalkDir;
|
2015-11-24 18:00:06 +00:00
|
|
|
use std::error::Error;
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
fn run_test(name: &str) -> Result<(), cobalt::Error> {
|
2015-11-06 20:22:45 +00:00
|
|
|
let source = format!("tests/fixtures/{}/", name);
|
|
|
|
let target = format!("tests/target/{}/", name);
|
|
|
|
let dest = format!("tests/tmp/{}/", name);
|
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
let result = cobalt::build(&Path::new(&source), &Path::new(&dest), "_layouts", "_posts");
|
|
|
|
|
|
|
|
if result.is_ok() {
|
|
|
|
let walker = WalkDir::new(&target).into_iter();
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
// walk through fixture and created tmp directory and compare files
|
|
|
|
for entry in walker.filter_map(|e| e.ok()).filter(|e| e.file_type().is_file()) {
|
|
|
|
let relative = entry.path().to_str().unwrap().split(&target).last().unwrap();
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
let mut original = String::new();
|
|
|
|
File::open(entry.path()).unwrap().read_to_string(&mut original).unwrap();
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
let mut created = String::new();
|
|
|
|
File::open(&Path::new(&dest).join(&relative))
|
|
|
|
.unwrap()
|
|
|
|
.read_to_string(&mut created)
|
|
|
|
.unwrap();
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
difference::assert_diff(&original, &created, " ", 0);
|
|
|
|
}
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
// clean up
|
|
|
|
fs::remove_dir_all(dest).expect("Cleanup failed");
|
2015-11-06 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
result
|
2015-11-06 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn example() {
|
2015-11-24 18:00:06 +00:00
|
|
|
assert!(run_test("example").is_ok());
|
2015-11-06 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 01:56:02 +00:00
|
|
|
#[test]
|
|
|
|
pub fn dotfiles() {
|
|
|
|
assert!(run_test("dotfiles").is_ok());
|
|
|
|
}
|
|
|
|
|
2016-01-15 23:41:23 +00:00
|
|
|
#[test]
|
|
|
|
pub fn sort_posts() {
|
|
|
|
assert!(run_test("sort_posts").is_ok());
|
|
|
|
}
|
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
#[test]
|
|
|
|
pub fn liquid_error() {
|
|
|
|
let err = run_test("liquid_error");
|
|
|
|
assert!(err.is_err());
|
|
|
|
assert_eq!(err.unwrap_err().description(), "{{{ is not a valid identifier");
|
|
|
|
}
|
|
|
|
|
2016-01-16 22:10:12 +00:00
|
|
|
#[test]
|
|
|
|
pub fn yaml_error() {
|
|
|
|
let err = run_test("yaml_error");
|
|
|
|
assert!(err.is_err());
|
|
|
|
assert_eq!(err.unwrap_err().description(), "unexpected character: `@'");
|
|
|
|
}
|
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
#[test]
|
|
|
|
pub fn no_extends_error() {
|
|
|
|
let err = run_test("no_extends_error");
|
|
|
|
assert!(err.is_err());
|
2016-01-15 21:22:12 +00:00
|
|
|
assert_eq!(err.unwrap_err().description(), "No extends property creating _posts/2014-08-24-my-first-blogpost.md");
|
2015-11-24 18:00:06 +00:00
|
|
|
}
|