cobalt.rs/tests/mod.rs

204 lines
5 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate difference;
extern crate cobalt;
extern crate tempdir;
extern crate walkdir;
use std::path::Path;
use std::fs::{self, File};
use std::io::Read;
use tempdir::TempDir;
use walkdir::WalkDir;
use std::error::Error;
use cobalt::Config;
fn run_test(name: &str) -> Result<(), cobalt::Error> {
let target = format!("tests/target/{}/", name);
let mut config = Config::from_file(format!("tests/fixtures/{}/.cobalt.yml", name))
2017-01-03 08:44:53 +00:00
.unwrap_or(Default::default());
let destdir = TempDir::new(name).expect("Tempdir not created");
config.source = format!("tests/fixtures/{}/", name);
2017-01-03 08:44:53 +00:00
config.dest = destdir.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();
2016-02-18 07:13:59 +00:00
// try to create the target directory, ignore errors
fs::create_dir_all(&config.dest).is_ok();
let result = cobalt::build(&config);
if result.is_ok() {
Ignore underscored directories by default This is something I've been meaning to do for a long time now. I would like to add consistency in how we deal with files and folders that have a leading dot or underscore. This commit makes it so that both kinds are simply ignored by default when looking for documents to generate or assets to copy. I like to see those filenames as indication that the folders contain "meta" information. My idea of a good site generator is that your source file structure is not fundamentally different from the resulting file structure. You basically end up with the same structure but "enhanced" by the site generator (e.g. by turning .md and .liquid files into .html files). It is very useful to have an easy indicator like an underscore to show that this file will be used in the generation process but will not be there in the resulting site. You can now simply have a file called _data.json next to your template or blog file. The default post path has been changed to "posts". For compatibility it is still possible to set your default post path to "_posts", however, you now have to do it explicitly in your .cobalt.yml. This is good, as having _posts/blog-post.md generate to _posts/blog-post.html was just laughable. I will make PRs for that in all affected user pages. _layouts obviously stays the same. I changed the layout handling a bit, though. Layouts are now read from disk every time they are required. This needs a bit of optmization in the future but allows for better and more flexible layout specification.
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());
// walk through fixture and created tmp directory and compare files
Ignore underscored directories by default This is something I've been meaning to do for a long time now. I would like to add consistency in how we deal with files and folders that have a leading dot or underscore. This commit makes it so that both kinds are simply ignored by default when looking for documents to generate or assets to copy. I like to see those filenames as indication that the folders contain "meta" information. My idea of a good site generator is that your source file structure is not fundamentally different from the resulting file structure. You basically end up with the same structure but "enhanced" by the site generator (e.g. by turning .md and .liquid files into .html files). It is very useful to have an easy indicator like an underscore to show that this file will be used in the generation process but will not be there in the resulting site. You can now simply have a file called _data.json next to your template or blog file. The default post path has been changed to "posts". For compatibility it is still possible to set your default post path to "_posts", however, you now have to do it explicitly in your .cobalt.yml. This is good, as having _posts/blog-post.md generate to _posts/blog-post.html was just laughable. I will make PRs for that in all affected user pages. _layouts obviously stays the same. I changed the layout handling a bit, though. Layouts are now read from disk every time they are required. This needs a bit of optmization in the future but allows for better and more flexible layout specification.
2016-07-03 08:37:07 +00:00
for entry in walker {
let relative = entry.path()
2017-01-03 08:44:53 +00:00
.strip_prefix(&target)
.expect("Comparison error");
let mut original = String::new();
File::open(entry.path())
.expect("Comparison error")
.read_to_string(&mut original)
.expect("Could not read to string");
let mut created = String::new();
File::open(&Path::new(&config.dest).join(&relative))
.expect("Comparison error")
.read_to_string(&mut created)
.expect("Could not read to string");
assert_diff!(&original, &created, " ", 0);
}
Ignore underscored directories by default This is something I've been meaning to do for a long time now. I would like to add consistency in how we deal with files and folders that have a leading dot or underscore. This commit makes it so that both kinds are simply ignored by default when looking for documents to generate or assets to copy. I like to see those filenames as indication that the folders contain "meta" information. My idea of a good site generator is that your source file structure is not fundamentally different from the resulting file structure. You basically end up with the same structure but "enhanced" by the site generator (e.g. by turning .md and .liquid files into .html files). It is very useful to have an easy indicator like an underscore to show that this file will be used in the generation process but will not be there in the resulting site. You can now simply have a file called _data.json next to your template or blog file. The default post path has been changed to "posts". For compatibility it is still possible to set your default post path to "_posts", however, you now have to do it explicitly in your .cobalt.yml. This is good, as having _posts/blog-post.md generate to _posts/blog-post.html was just laughable. I will make PRs for that in all affected user pages. _layouts obviously stays the same. I changed the layout handling a bit, though. Layouts are now read from disk every time they are required. This needs a bit of optmization in the future but allows for better and more flexible layout specification.
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()
2017-01-03 08:44:53 +00:00
.strip_prefix(&config.dest)
.expect("Comparison error");
Ignore underscored directories by default This is something I've been meaning to do for a long time now. I would like to add consistency in how we deal with files and folders that have a leading dot or underscore. This commit makes it so that both kinds are simply ignored by default when looking for documents to generate or assets to copy. I like to see those filenames as indication that the folders contain "meta" information. My idea of a good site generator is that your source file structure is not fundamentally different from the resulting file structure. You basically end up with the same structure but "enhanced" by the site generator (e.g. by turning .md and .liquid files into .html files). It is very useful to have an easy indicator like an underscore to show that this file will be used in the generation process but will not be there in the resulting site. You can now simply have a file called _data.json next to your template or blog file. The default post path has been changed to "posts". For compatibility it is still possible to set your default post path to "_posts", however, you now have to do it explicitly in your .cobalt.yml. This is good, as having _posts/blog-post.md generate to _posts/blog-post.html was just laughable. I will make PRs for that in all affected user pages. _layouts obviously stays the same. I changed the layout handling a bit, though. Layouts are now read from disk every time they are required. This needs a bit of optmization in the future but allows for better and more flexible layout specification.
2016-07-03 08:37:07 +00:00
let relative = Path::new(&target).join(&relative);
2017-01-03 08:44:53 +00:00
File::open(&relative).expect(&format!("File {:?} does not exist in reference ({:?}).",
entry.path(),
relative));
Ignore underscored directories by default This is something I've been meaning to do for a long time now. I would like to add consistency in how we deal with files and folders that have a leading dot or underscore. This commit makes it so that both kinds are simply ignored by default when looking for documents to generate or assets to copy. I like to see those filenames as indication that the folders contain "meta" information. My idea of a good site generator is that your source file structure is not fundamentally different from the resulting file structure. You basically end up with the same structure but "enhanced" by the site generator (e.g. by turning .md and .liquid files into .html files). It is very useful to have an easy indicator like an underscore to show that this file will be used in the generation process but will not be there in the resulting site. You can now simply have a file called _data.json next to your template or blog file. The default post path has been changed to "posts". For compatibility it is still possible to set your default post path to "_posts", however, you now have to do it explicitly in your .cobalt.yml. This is good, as having _posts/blog-post.md generate to _posts/blog-post.html was just laughable. I will make PRs for that in all affected user pages. _layouts obviously stays the same. I changed the layout handling a bit, though. Layouts are now read from disk every time they are required. This needs a bit of optmization in the future but allows for better and more flexible layout specification.
2016-07-03 08:37:07 +00:00
}
}
// clean up
try!(destdir.close());
result
}
#[test]
pub fn copy_files() {
run_test("copy_files").expect("Build error");
}
#[test]
pub fn custom_paths() {
run_test("custom_paths").expect("Build error");
}
Ignore underscored directories by default This is something I've been meaning to do for a long time now. I would like to add consistency in how we deal with files and folders that have a leading dot or underscore. This commit makes it so that both kinds are simply ignored by default when looking for documents to generate or assets to copy. I like to see those filenames as indication that the folders contain "meta" information. My idea of a good site generator is that your source file structure is not fundamentally different from the resulting file structure. You basically end up with the same structure but "enhanced" by the site generator (e.g. by turning .md and .liquid files into .html files). It is very useful to have an easy indicator like an underscore to show that this file will be used in the generation process but will not be there in the resulting site. You can now simply have a file called _data.json next to your template or blog file. The default post path has been changed to "posts". For compatibility it is still possible to set your default post path to "_posts", however, you now have to do it explicitly in your .cobalt.yml. This is good, as having _posts/blog-post.md generate to _posts/blog-post.html was just laughable. I will make PRs for that in all affected user pages. _layouts obviously stays the same. I changed the layout handling a bit, though. Layouts are now read from disk every time they are required. This needs a bit of optmization in the future but allows for better and more flexible layout specification.
2016-07-03 08:37:07 +00:00
#[test]
pub fn custom_posts_folder() {
run_test("custom_posts_folder").expect("Build error");
}
2016-07-03 17:33:08 +00:00
#[test]
pub fn custom_post_path() {
run_test("custom_post_path").expect("Build error");
}
#[test]
pub fn dotfiles() {
run_test("dotfiles").expect("Build error");
}
#[test]
pub fn drafts() {
run_test("drafts").expect("Build error");
}
#[test]
pub fn drafts_not_shown_by_default() {
run_test("drafts_not_shown_by_default").expect("Build error");
}
2015-12-01 01:56:02 +00:00
#[test]
pub fn example() {
run_test("example").expect("Build error");
2015-12-01 01:56:02 +00:00
}
Ignore underscored directories by default This is something I've been meaning to do for a long time now. I would like to add consistency in how we deal with files and folders that have a leading dot or underscore. This commit makes it so that both kinds are simply ignored by default when looking for documents to generate or assets to copy. I like to see those filenames as indication that the folders contain "meta" information. My idea of a good site generator is that your source file structure is not fundamentally different from the resulting file structure. You basically end up with the same structure but "enhanced" by the site generator (e.g. by turning .md and .liquid files into .html files). It is very useful to have an easy indicator like an underscore to show that this file will be used in the generation process but will not be there in the resulting site. You can now simply have a file called _data.json next to your template or blog file. The default post path has been changed to "posts". For compatibility it is still possible to set your default post path to "_posts", however, you now have to do it explicitly in your .cobalt.yml. This is good, as having _posts/blog-post.md generate to _posts/blog-post.html was just laughable. I will make PRs for that in all affected user pages. _layouts obviously stays the same. I changed the layout handling a bit, though. Layouts are now read from disk every time they are required. This needs a bit of optmization in the future but allows for better and more flexible layout specification.
2016-07-03 08:37:07 +00:00
#[test]
pub fn hidden_posts_folder() {
run_test("hidden_posts_folder").expect("Build error");
}
#[test]
pub fn custom_template_extensions() {
run_test("custom_template_extensions").expect("Build error");
}
2017-01-03 08:44:53 +00:00
#[cfg(feature = "syntax-highlight")]
#[test]
pub fn syntax_highlight() {
run_test("syntax_highlight").expect("Build error");
}
#[test]
pub fn incomplete_rss() {
let err = run_test("incomplete_rss");
assert!(err.is_err());
let err = err.unwrap_err();
2017-01-03 08:44:53 +00:00
assert_eq!(format!("{}", err),
"name, description and link need to be defined in the config file to generate RSS");
2017-01-03 08:44:53 +00:00
assert_eq!(err.description(), "missing fields in config file");
}
#[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");
}
#[test]
pub fn liquid_raw() {
run_test("liquid_escaped").expect("Build error");
}
2016-01-16 22:10:12 +00:00
#[test]
pub fn no_extends_error() {
let err = run_test("no_extends_error");
2016-01-16 22:10:12 +00:00
assert!(err.is_err());
2017-01-03 08:44:53 +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
}
#[test]
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();
}
#[test]
pub fn yaml_error() {
let err = run_test("yaml_error");
assert!(err.is_err());
assert_eq!(err.unwrap_err().description(), "unexpected character: `@'");
}
#[test]
pub fn excerpts() {
run_test("excerpts").unwrap();
}
2017-02-09 22:50:09 +00:00
#[test]
pub fn posts_in_subfolder() {
run_test("posts_in_subfolder").unwrap();
}