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;
|
2016-02-17 20:39:44 +00:00
|
|
|
use cobalt::Config;
|
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 target = format!("tests/target/{}/", name);
|
2016-05-21 17:34:55 +00:00
|
|
|
let mut config = Config::from_file(format!("tests/fixtures/{}/.cobalt.yml", name))
|
|
|
|
.unwrap_or(Default::default());
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2016-02-17 20:39:44 +00:00
|
|
|
config.source = format!("tests/fixtures/{}/", name);
|
|
|
|
config.dest = format!("tests/tmp/{}/", name);
|
|
|
|
|
2016-02-18 07:13:59 +00:00
|
|
|
// try to create the target directory, ignore errors
|
2016-05-21 17:34:55 +00:00
|
|
|
fs::create_dir_all(&config.dest).is_ok();
|
2016-02-17 20:39:44 +00:00
|
|
|
|
|
|
|
let result = cobalt::build(&config);
|
2015-11-24 18:00:06 +00:00
|
|
|
|
|
|
|
if result.is_ok() {
|
2016-07-03 08:37:07 +00:00
|
|
|
let walker = WalkDir::new(&target)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
.filter(|e| e.file_type().is_file());
|
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
|
2016-07-03 08:37:07 +00:00
|
|
|
for entry in walker {
|
2016-05-21 17:34:55 +00:00
|
|
|
let relative = entry.path()
|
2016-07-03 08:37:07 +00:00
|
|
|
.strip_prefix(&target)
|
2016-05-21 17:34:55 +00:00
|
|
|
.expect("Comparison error");
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
let mut original = String::new();
|
2016-05-21 17:34:55 +00:00
|
|
|
File::open(entry.path())
|
|
|
|
.expect("Comparison error")
|
|
|
|
.read_to_string(&mut original)
|
|
|
|
.expect("Could not read to string");
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
let mut created = String::new();
|
2016-02-17 20:39:44 +00:00
|
|
|
File::open(&Path::new(&config.dest).join(&relative))
|
|
|
|
.expect("Comparison error")
|
2015-11-24 18:00:06 +00:00
|
|
|
.read_to_string(&mut created)
|
2016-05-21 17:34:55 +00:00
|
|
|
.expect("Could not read to string");
|
2015-11-06 20:22:45 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
difference::assert_diff(&original, &created, " ", 0);
|
|
|
|
}
|
2016-07-03 08:37:07 +00:00
|
|
|
|
|
|
|
// ensure no unnecessary files were created
|
|
|
|
let walker = WalkDir::new(&config.dest)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
.filter(|e| e.file_type().is_file());
|
|
|
|
|
|
|
|
for entry in walker {
|
|
|
|
let relative = entry.path()
|
|
|
|
.strip_prefix(&config.dest)
|
|
|
|
.expect("Comparison error");
|
|
|
|
let relative = Path::new(&target).join(&relative);
|
|
|
|
|
|
|
|
File::open(&relative)
|
|
|
|
.expect(&format!("File {:?} does not exist in reference ({:?}).", entry.path(), relative));
|
|
|
|
}
|
2015-11-06 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 20:39:44 +00:00
|
|
|
// clean up
|
2016-05-21 17:34:55 +00:00
|
|
|
fs::remove_dir_all(&config.dest).is_ok();
|
2016-02-17 20:39:44 +00:00
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
result
|
2015-11-06 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 17:34:55 +00:00
|
|
|
#[test]
|
|
|
|
pub fn copy_files() {
|
|
|
|
run_test("copy_files").expect("Build error");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn custom_paths() {
|
|
|
|
run_test("custom_paths").expect("Build error");
|
|
|
|
}
|
|
|
|
|
2016-07-03 08:37:07 +00:00
|
|
|
#[test]
|
|
|
|
pub fn custom_posts_folder() {
|
|
|
|
run_test("custom_posts_folder").expect("Build error");
|
|
|
|
}
|
|
|
|
|
2015-11-06 20:22:45 +00:00
|
|
|
#[test]
|
2016-02-17 20:39:44 +00:00
|
|
|
pub fn dotfiles() {
|
|
|
|
run_test("dotfiles").expect("Build error");
|
2015-11-06 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 01:56:02 +00:00
|
|
|
#[test]
|
2016-02-17 20:39:44 +00:00
|
|
|
pub fn example() {
|
|
|
|
run_test("example").expect("Build error");
|
2015-12-01 01:56:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 08:37:07 +00:00
|
|
|
#[test]
|
|
|
|
pub fn hidden_posts_folder() {
|
|
|
|
run_test("hidden_posts_folder").expect("Build error");
|
|
|
|
}
|
|
|
|
|
2016-03-03 07:45:04 +00:00
|
|
|
#[test]
|
|
|
|
pub fn custom_template_extensions() {
|
|
|
|
run_test("custom_template_extensions").expect("Build error");
|
|
|
|
}
|
|
|
|
|
2016-01-15 23:41:23 +00:00
|
|
|
#[test]
|
2016-02-17 20:39:44 +00:00
|
|
|
pub fn incomplete_rss() {
|
|
|
|
let err = run_test("incomplete_rss");
|
|
|
|
assert!(err.is_err());
|
2016-05-21 17:34:55 +00:00
|
|
|
assert_eq!(err.unwrap_err().description(),
|
|
|
|
"name, description and link need to be defined in the config file to generate RSS");
|
2016-01-15 23:41:23 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
#[test]
|
|
|
|
pub fn liquid_error() {
|
|
|
|
let err = run_test("liquid_error");
|
|
|
|
assert!(err.is_err());
|
2016-05-21 17:34:55 +00:00
|
|
|
assert_eq!(err.unwrap_err().description(),
|
|
|
|
"{{{ is not a valid identifier");
|
2015-11-24 18:00:06 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:10:12 +00:00
|
|
|
#[test]
|
2016-02-17 20:39:44 +00:00
|
|
|
pub fn no_extends_error() {
|
|
|
|
let err = run_test("no_extends_error");
|
2016-01-16 22:10:12 +00:00
|
|
|
assert!(err.is_err());
|
2016-07-03 08:37:07 +00:00
|
|
|
assert!(err
|
|
|
|
.unwrap_err()
|
|
|
|
.description()
|
|
|
|
.contains("Layout default_nonexistent.liquid can not be read (defined in tests/fixtures/no_extends_error/index.liquid)")
|
|
|
|
);
|
2016-01-16 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 18:00:06 +00:00
|
|
|
#[test]
|
2016-02-17 20:39:44 +00:00
|
|
|
pub fn sort_posts() {
|
|
|
|
run_test("sort_posts").expect("Build error");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn rss() {
|
|
|
|
run_test("rss").expect("Build error");
|
|
|
|
}
|
|
|
|
|
2016-05-07 15:09:16 +00:00
|
|
|
#[test]
|
|
|
|
pub fn ignore_files() {
|
|
|
|
run_test("ignore_files").unwrap();
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:39:44 +00:00
|
|
|
#[test]
|
|
|
|
pub fn yaml_error() {
|
|
|
|
let err = run_test("yaml_error");
|
2015-11-24 18:00:06 +00:00
|
|
|
assert!(err.is_err());
|
2016-02-17 20:39:44 +00:00
|
|
|
assert_eq!(err.unwrap_err().description(), "unexpected character: `@'");
|
2015-11-24 18:00:06 +00:00
|
|
|
}
|