cobalt.rs/tests/mod.rs

158 lines
4.1 KiB
Rust
Raw Normal View History

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;
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))
.unwrap_or(Default::default());
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
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()
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
.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");
difference::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()
.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));
}
}
// clean up
fs::remove_dir_all(&config.dest).is_ok();
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");
}
#[test]
pub fn dotfiles() {
run_test("dotfiles").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");
}
#[test]
pub fn incomplete_rss() {
let err = run_test("incomplete_rss");
assert!(err.is_err());
assert_eq!(err.unwrap_err().description(),
"name, description and link need to be defined in the config file to generate RSS");
}
#[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 no_extends_error() {
let err = run_test("no_extends_error");
2016-01-16 22:10:12 +00:00
assert!(err.is_err());
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
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: `@'");
}