extern crate chrono; extern crate tempfile; extern crate uuid; use uuid::Uuid; use std::{env, fs}; #[test] fn test_build() { let uuid = Uuid::new_v4().to_string(); let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(&uuid); fs::create_dir(&project_dir).unwrap(); fs::create_dir(&project_dir.join("assets")).unwrap(); fs::create_dir(&project_dir.join("drafts")).unwrap(); fs::create_dir(&project_dir.join("pages")).unwrap(); fs::create_dir(&project_dir.join("posts")).unwrap(); fs::create_dir(&project_dir.join("public")).unwrap(); fs::create_dir(&project_dir.join("public").join("posts")).unwrap(); fs::create_dir(&project_dir.join("templates")).unwrap(); fs::write( project_dir.join("assets").join("style.css"), "body { background: blue; }", ) .unwrap(); fs::write( project_dir.join("templates").join("layout.html"), "{{ page_title }}{{ contents }}", ) .unwrap(); fs::write( project_dir.join("templates").join("post.html"), "

{{ title }}

{{ body }}
", ) .unwrap(); fs::write( project_dir.join("templates").join("page.html"), "

{{ title }}

{{ body }}
", ) .unwrap(); fs::write( project_dir.join("templates").join("post_listing.html"), "", ) .unwrap(); fs::write( project_dir.join("templates").join("post_listing_item.html"), "
  • {{ title }}
  • ", ) .unwrap(); fs::write( project_dir.join("posts").join("first-post.md"), "# First post | 2019-01-01\n\nThis is the first post\n\nIt has multiple paragraphs", ) .unwrap(); fs::write( project_dir.join("pages").join("first-page.md"), "# First page\n\nThis is the first page\n\nIt has multiple paragraphs", ) .unwrap(); fs::write( project_dir.join("casaubon.toml"), "site_name = \"Test Site\"\nurl =\"testsite.com\"\ndescription = \"Test Site\"", ) .unwrap(); casaubon::build(false, &project_dir); assert_eq!( "Test Site", fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(), ); assert_eq!( "First post | Test Site

    First pos\ t

    This is the first post

    It has multiple paragra\ phs

    ", fs::read_to_string( project_dir .join("public") .join("posts") .join("first-post") .join("index.html") ) .unwrap() .replace("\n", ""), ); assert_eq!( "First page | Test Site

    First pag\ e

    This is the first page

    It has multiple paragra\ phs

    ", fs::read_to_string( project_dir .join("public") .join("first-page") .join("index.html") ) .unwrap() .replace("\n", ""), ); assert_eq!( "body { background: blue; }", fs::read_to_string(project_dir.join("public").join("assets").join("style.css")).unwrap() ); } #[test] fn test_build_drafts() { let uuid = Uuid::new_v4().to_string(); let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(&uuid); fs::create_dir(&project_dir).unwrap(); env::set_current_dir(&project_dir).unwrap(); fs::create_dir(&project_dir.join("assets")).unwrap(); fs::create_dir(&project_dir.join("drafts")).unwrap(); fs::create_dir(&project_dir.join("pages")).unwrap(); fs::create_dir(&project_dir.join("posts")).unwrap(); fs::create_dir(&project_dir.join("public")).unwrap(); fs::create_dir(&project_dir.join("public").join("posts")).unwrap(); fs::create_dir(&project_dir.join("templates")).unwrap(); fs::write( project_dir.join("assets").join("style.css"), "body { background: blue; }", ) .unwrap(); fs::write( project_dir.join("templates").join("layout.html"), "{{ page_title }}{{ contents }}", ) .unwrap(); fs::write( project_dir.join("templates").join("post.html"), "

    {{ title }}

    {{ body }}
    ", ) .unwrap(); fs::write( project_dir.join("templates").join("page.html"), "

    {{ title }}

    {{ body }}
    ", ) .unwrap(); fs::write( project_dir.join("templates").join("post_listing.html"), "", ) .unwrap(); fs::write( project_dir.join("templates").join("post_listing_item.html"), "
  • {{ title }}
  • ", ) .unwrap(); fs::write( project_dir.join("posts").join("first-post.md"), "# First post | 2019-01-01\n\nThis is the first post", ) .unwrap(); fs::write( project_dir.join("pages").join("first-page.md"), "# First page\n\nThis is the first page", ) .unwrap(); fs::write( project_dir.join("drafts").join("first-draft.md"), "# First draft | 2019-01-01\n\nThis is the first draft", ) .unwrap(); fs::write( project_dir.join("casaubon.toml"), "site_name = \"Test Site\"\nurl =\"testsite.com\"\ndescription = \"Test Site\"", ) .unwrap(); casaubon::build(true, &project_dir); assert_eq!( "Test Site", fs::read_to_string(project_dir.join("public").join("index.html")).unwrap().replace("\n", ""), ); assert_eq!( "First post | Test Site

    First post

    This is the first post

    ", fs::read_to_string( project_dir .join("public") .join("posts") .join("first-post") .join("index.html") ).unwrap() .replace("\n", ""), ); assert_eq!( "First page | Test Site

    First page

    This is the first page

    ", fs::read_to_string( project_dir .join("public") .join("first-page") .join("index.html") ).unwrap() .replace("\n", ""), ); assert_eq!( "First draft | Test Site

    First draft

    This is the first draft

    ", fs::read_to_string( project_dir .join("public") .join("posts") .join("first-draft") .join("index.html") ).unwrap() .replace("\n", ""), ); } #[test] fn test_new() { let uuid = Uuid::new_v4().to_string(); let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(&uuid); casaubon::new(&uuid, &temp_dir.path()); for dir in &["public", "pages", "posts", "templates"] { fs::read_dir(&project_dir.join(dir)).unwrap(); } assert_eq!( format!( "{}

    {}

    {{{{ contents }}}}", uuid, uuid ), fs::read_to_string(&project_dir.join("templates").join("layout.html")) .unwrap() .replace("\n", "") .replace(" ", "") ); assert_eq!( format!("

    Posts

    "), fs::read_to_string(&project_dir.join("templates").join("post_listing.html")) .unwrap() .replace("\n", "") .replace(" ", "") ); assert_eq!( format!("
  • {{ date }} {{{{ title }}}}
  • "), fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html")) .unwrap() .replace("\n", "") .replace(" ", "") ); assert_eq!( format!("

    {{{{ title }}}}

    {{{{ body }}}}
    "), fs::read_to_string(&project_dir.join("templates").join("post.html")) .unwrap() .replace("\n", "") .replace(" ", "") ); assert_eq!( format!("

    {{{{ title }}}}

    {{{{ body }}}}
    "), fs::read_to_string(&project_dir.join("templates").join("page.html")) .unwrap() .replace("\n", "") .replace(" ", "") ); assert_eq!( format!( "site_name = \"{}\"\nurl = \"{}.com\"\ndescription = \"\"", &uuid, &uuid ), fs::read_to_string(&project_dir.join("casaubon.toml")).unwrap() ); }