casaubon 0.3.2

A feature-poor static site generator
Documentation
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"),
        "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("post.html"),
        "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("page.html"),
        "<article class=\"page\"><h1>{{ title }}</h1><div>{{ body }}</div></article>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("post_listing.html"),
        "<ul>{{ post_listing }}</ul>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("post_listing_item.html"),
        "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
    )
    .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!(
        "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
         st-post\">First post</a></li></ul></body></html>",
        fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
    );

    assert_eq!(
        "<html><head><title>First post | Test Site</title></head><body><article><h1>First pos\
         t</h1><div><p>This is the first post</p><p>It has multiple paragra\
         phs</p></div></article></body></html>",
        fs::read_to_string(
            project_dir
                .join("public")
                .join("posts")
                .join("first-post")
                .join("index.html")
        )
        .unwrap()
        .replace("\n", ""),
    );

    assert_eq!(
        "<html><head><title>First page | Test Site</title></head><body><article class=\"page\"><h1>First pag\
         e</h1><div><p>This is the first page</p><p>It has multiple paragra\
         phs</p></div></article></body></html>",
        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"),
        "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("post.html"),
        "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("page.html"),
        "<article class=\"page\"><h1>{{ title }}</h1><div>{{ body }}</div></article>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("post_listing.html"),
        "<ul>{{ post_listing }}</ul>",
    )
    .unwrap();
    fs::write(
        project_dir.join("templates").join("post_listing_item.html"),
        "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
    )
    .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!(
        "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/first-draft\">First draft</a></li></ul></body></html>",
        fs::read_to_string(project_dir.join("public").join("index.html")).unwrap().replace("\n", ""),
    );

    assert_eq!(
        "<html><head><title>First post | Test Site</title></head><body><article><h1>First post</h1><div><p>This is the first post</p></div></article></body></html>",
        fs::read_to_string(
            project_dir
                .join("public")
                .join("posts")
                .join("first-post")
                .join("index.html")
        ).unwrap()
        .replace("\n", ""),
    );

    assert_eq!(
        "<html><head><title>First page | Test Site</title></head><body><article class=\"page\"><h1>First page</h1><div><p>This is the first page</p></div></article></body></html>",
        fs::read_to_string(
            project_dir
                .join("public")
                .join("first-page")
                .join("index.html")
        ).unwrap()
        .replace("\n", ""),
    );

    assert_eq!(
        "<html><head><title>First draft | Test Site</title></head><body><article><h1>First draft</h1><div><p>This is the first draft</p></div></article></body></html>",
        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!(
            "<html><head><title>{}</title></head><body><h1>{}</h1>{{{{ contents }}}}</body></html>",
            uuid, uuid
        ),
        fs::read_to_string(&project_dir.join("templates").join("layout.html"))
            .unwrap()
            .replace("\n", "")
            .replace("  ", "")
    );
    assert_eq!(
        format!("<div><h3>Posts</h3><ul>{{{{ post_listing }}}}</ul></div>"),
        fs::read_to_string(&project_dir.join("templates").join("post_listing.html"))
            .unwrap()
            .replace("\n", "")
            .replace("  ", "")
    );
    assert_eq!(
        format!("<li>{{ date }} <a href=\"/posts/{{{{ slug }}}}/\">{{{{ title }}}}</a></li>"),
        fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
            .unwrap()
            .replace("\n", "")
            .replace("  ", "")
    );
    assert_eq!(
        format!("<article><h1>{{{{ title }}}}</h1><div>{{{{ body }}}}</div></article>"),
        fs::read_to_string(&project_dir.join("templates").join("post.html"))
            .unwrap()
            .replace("\n", "")
            .replace("  ", "")
    );
    assert_eq!(
        format!("<article><h1>{{{{ title }}}}</h1><div>{{{{ body }}}}</div></article>"),
        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()
    );
}