123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- 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()
- );
- }
|