2020-01-26 00:18:35 +00:00
|
|
|
#![allow(dead_code)]
|
2018-12-28 16:30:47 +00:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-12-21 21:52:39 +00:00
|
|
|
use site::Site;
|
|
|
|
use tempfile::{tempdir, TempDir};
|
2018-12-28 16:30:47 +00:00
|
|
|
|
|
|
|
// 2 helper macros to make all the build testing more bearable
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! file_exists {
|
|
|
|
($root: expr, $path: expr) => {{
|
|
|
|
let mut path = $root.clone();
|
|
|
|
for component in $path.split("/") {
|
|
|
|
path = path.join(component);
|
|
|
|
}
|
|
|
|
std::path::Path::new(&path).exists()
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! file_contains {
|
|
|
|
($root: expr, $path: expr, $text: expr) => {{
|
|
|
|
use std::io::prelude::*;
|
|
|
|
let mut path = $root.clone();
|
|
|
|
for component in $path.split("/") {
|
|
|
|
path = path.join(component);
|
|
|
|
}
|
2019-01-04 19:31:31 +00:00
|
|
|
let mut file = std::fs::File::open(&path).expect(&format!("Failed to open {:?}", $path));
|
2018-12-28 16:30:47 +00:00
|
|
|
let mut s = String::new();
|
|
|
|
file.read_to_string(&mut s).unwrap();
|
2019-01-04 19:31:31 +00:00
|
|
|
println!("{}", s);
|
2018-12-28 16:30:47 +00:00
|
|
|
s.contains($text)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We return the tmpdir otherwise it would get out of scope and be deleted
|
|
|
|
/// The tests can ignore it if they dont need it by prefixing it with a `_`
|
|
|
|
pub fn build_site(name: &str) -> (Site, TempDir, PathBuf) {
|
|
|
|
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
|
|
|
|
path.push(name);
|
|
|
|
let mut site = Site::new(&path, "config.toml").unwrap();
|
|
|
|
site.load().unwrap();
|
|
|
|
let tmp_dir = tempdir().expect("create temp dir");
|
|
|
|
let public = &tmp_dir.path().join("public");
|
|
|
|
site.set_output_path(&public);
|
2019-01-04 19:31:31 +00:00
|
|
|
site.build().expect("Couldn't build the site");
|
2018-12-28 16:30:47 +00:00
|
|
|
(site, tmp_dir, public.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as `build_site` but has a hook to setup some config options
|
2018-12-29 10:17:43 +00:00
|
|
|
pub fn build_site_with_setup<F>(name: &str, mut setup_cb: F) -> (Site, TempDir, PathBuf)
|
|
|
|
where
|
|
|
|
F: FnMut(Site) -> (Site, bool),
|
|
|
|
{
|
2018-12-28 16:30:47 +00:00
|
|
|
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
|
|
|
|
path.push(name);
|
|
|
|
let site = Site::new(&path, "config.toml").unwrap();
|
|
|
|
let (mut site, needs_loading) = setup_cb(site);
|
|
|
|
if needs_loading {
|
|
|
|
site.load().unwrap();
|
|
|
|
}
|
|
|
|
let tmp_dir = tempdir().expect("create temp dir");
|
|
|
|
let public = &tmp_dir.path().join("public");
|
|
|
|
site.set_output_path(&public);
|
2019-01-04 19:31:31 +00:00
|
|
|
site.build().expect("Couldn't build the site");
|
2018-12-28 16:30:47 +00:00
|
|
|
(site, tmp_dir, public.clone())
|
|
|
|
}
|