A static site generator written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

integration_test.rs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. extern crate chrono;
  2. extern crate tempfile;
  3. extern crate uuid;
  4. use uuid::Uuid;
  5. use std::{env, fs};
  6. #[test]
  7. fn test_build() {
  8. let uuid = Uuid::new_v4().to_string();
  9. let temp_dir = tempfile::tempdir().unwrap();
  10. let project_dir = temp_dir.path().join(&uuid);
  11. fs::create_dir(&project_dir).unwrap();
  12. fs::create_dir(&project_dir.join("assets")).unwrap();
  13. fs::create_dir(&project_dir.join("drafts")).unwrap();
  14. fs::create_dir(&project_dir.join("pages")).unwrap();
  15. fs::create_dir(&project_dir.join("posts")).unwrap();
  16. fs::create_dir(&project_dir.join("public")).unwrap();
  17. fs::create_dir(&project_dir.join("public").join("posts")).unwrap();
  18. fs::create_dir(&project_dir.join("templates")).unwrap();
  19. fs::write(
  20. project_dir.join("assets").join("style.css"),
  21. "body { background: blue; }",
  22. )
  23. .unwrap();
  24. fs::write(
  25. project_dir.join("templates").join("layout.html"),
  26. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  27. )
  28. .unwrap();
  29. fs::write(
  30. project_dir.join("templates").join("post.html"),
  31. "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  32. )
  33. .unwrap();
  34. fs::write(
  35. project_dir.join("templates").join("page.html"),
  36. "<article class=\"page\"><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  37. )
  38. .unwrap();
  39. fs::write(
  40. project_dir.join("templates").join("post_listing.html"),
  41. "<ul>{{ post_listing }}</ul>",
  42. )
  43. .unwrap();
  44. fs::write(
  45. project_dir.join("templates").join("post_listing_item.html"),
  46. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  47. )
  48. .unwrap();
  49. fs::write(
  50. project_dir.join("posts").join("first-post.md"),
  51. "# First post | 2019-01-01\n\nThis is the first post\n\nIt has multiple paragraphs",
  52. )
  53. .unwrap();
  54. fs::write(
  55. project_dir.join("pages").join("first-page.md"),
  56. "# First page\n\nThis is the first page\n\nIt has multiple paragraphs",
  57. )
  58. .unwrap();
  59. fs::write(
  60. project_dir.join("casaubon.toml"),
  61. "site_name = \"Test Site\"\nurl =\"testsite.com\"\ndescription = \"Test Site\"",
  62. )
  63. .unwrap();
  64. casaubon::build(false, &project_dir);
  65. assert_eq!(
  66. "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/fir\
  67. st-post\">First post</a></li></ul></body></html>",
  68. fs::read_to_string(project_dir.join("public").join("index.html")).unwrap(),
  69. );
  70. assert_eq!(
  71. "<html><head><title>First post | Test Site</title></head><body><article><h1>First pos\
  72. t</h1><div><p>This is the first post</p><p>It has multiple paragra\
  73. phs</p></div></article></body></html>",
  74. fs::read_to_string(
  75. project_dir
  76. .join("public")
  77. .join("posts")
  78. .join("first-post")
  79. .join("index.html")
  80. )
  81. .unwrap()
  82. .replace('\n', ""),
  83. );
  84. assert_eq!(
  85. "<html><head><title>First page | Test Site</title></head><body><article class=\"page\"><h1>First pag\
  86. e</h1><div><p>This is the first page</p><p>It has multiple paragra\
  87. phs</p></div></article></body></html>",
  88. fs::read_to_string(
  89. project_dir
  90. .join("public")
  91. .join("first-page")
  92. .join("index.html")
  93. )
  94. .unwrap()
  95. .replace('\n', ""),
  96. );
  97. assert_eq!(
  98. "body { background: blue; }",
  99. fs::read_to_string(project_dir.join("public").join("assets").join("style.css")).unwrap()
  100. );
  101. }
  102. #[test]
  103. fn test_build_drafts() {
  104. let uuid = Uuid::new_v4().to_string();
  105. let temp_dir = tempfile::tempdir().unwrap();
  106. let project_dir = temp_dir.path().join(&uuid);
  107. fs::create_dir(&project_dir).unwrap();
  108. env::set_current_dir(&project_dir).unwrap();
  109. fs::create_dir(&project_dir.join("assets")).unwrap();
  110. fs::create_dir(&project_dir.join("drafts")).unwrap();
  111. fs::create_dir(&project_dir.join("pages")).unwrap();
  112. fs::create_dir(&project_dir.join("posts")).unwrap();
  113. fs::create_dir(&project_dir.join("public")).unwrap();
  114. fs::create_dir(&project_dir.join("public").join("posts")).unwrap();
  115. fs::create_dir(&project_dir.join("templates")).unwrap();
  116. fs::write(
  117. project_dir.join("assets").join("style.css"),
  118. "body { background: blue; }",
  119. )
  120. .unwrap();
  121. fs::write(
  122. project_dir.join("templates").join("layout.html"),
  123. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  124. )
  125. .unwrap();
  126. fs::write(
  127. project_dir.join("templates").join("post.html"),
  128. "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  129. )
  130. .unwrap();
  131. fs::write(
  132. project_dir.join("templates").join("page.html"),
  133. "<article class=\"page\"><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  134. )
  135. .unwrap();
  136. fs::write(
  137. project_dir.join("templates").join("post_listing.html"),
  138. "<ul>{{ post_listing }}</ul>",
  139. )
  140. .unwrap();
  141. fs::write(
  142. project_dir.join("templates").join("post_listing_item.html"),
  143. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  144. )
  145. .unwrap();
  146. fs::write(
  147. project_dir.join("posts").join("first-post.md"),
  148. "# First post | 2019-01-01\n\nThis is the first post",
  149. )
  150. .unwrap();
  151. fs::write(
  152. project_dir.join("pages").join("first-page.md"),
  153. "# First page\n\nThis is the first page",
  154. )
  155. .unwrap();
  156. fs::write(
  157. project_dir.join("drafts").join("first-draft.md"),
  158. "# First draft | 2019-01-01\n\nThis is the first draft",
  159. )
  160. .unwrap();
  161. fs::write(
  162. project_dir.join("casaubon.toml"),
  163. "site_name = \"Test Site\"\nurl =\"testsite.com\"\ndescription = \"Test Site\"",
  164. )
  165. .unwrap();
  166. casaubon::build(true, &project_dir);
  167. assert_eq!(
  168. "<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>",
  169. fs::read_to_string(project_dir.join("public").join("index.html")).unwrap().replace('\n', ""),
  170. );
  171. assert_eq!(
  172. "<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>",
  173. fs::read_to_string(
  174. project_dir
  175. .join("public")
  176. .join("posts")
  177. .join("first-post")
  178. .join("index.html")
  179. ).unwrap()
  180. .replace('\n', ""),
  181. );
  182. assert_eq!(
  183. "<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>",
  184. fs::read_to_string(
  185. project_dir
  186. .join("public")
  187. .join("first-page")
  188. .join("index.html")
  189. ).unwrap()
  190. .replace('\n', ""),
  191. );
  192. assert_eq!(
  193. "<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>",
  194. fs::read_to_string(
  195. project_dir
  196. .join("public")
  197. .join("posts")
  198. .join("first-draft")
  199. .join("index.html")
  200. ).unwrap()
  201. .replace('\n', ""),
  202. );
  203. }
  204. #[test]
  205. fn test_new() {
  206. let uuid = Uuid::new_v4().to_string();
  207. let temp_dir = tempfile::tempdir().unwrap();
  208. let project_dir = temp_dir.path().join(&uuid);
  209. casaubon::new(&uuid, temp_dir.path());
  210. for dir in &["public", "pages", "posts", "templates"] {
  211. fs::read_dir(&project_dir.join(dir)).unwrap();
  212. }
  213. assert_eq!(
  214. format!(
  215. "<html><head><title>{}</title></head><body><h1>{}</h1>{{{{ contents }}}}</body></html>",
  216. uuid, uuid
  217. ),
  218. fs::read_to_string(&project_dir.join("templates").join("layout.html"))
  219. .unwrap()
  220. .replace('\n', "")
  221. .replace(" ", "")
  222. );
  223. assert_eq!(
  224. format!("<div><h3>Posts</h3><ul>{{{{ post_listing }}}}</ul></div>"),
  225. fs::read_to_string(&project_dir.join("templates").join("post_listing.html"))
  226. .unwrap()
  227. .replace('\n', "")
  228. .replace(" ", "")
  229. );
  230. assert_eq!(
  231. format!("<li>{{ date }} <a href=\"/posts/{{{{ slug }}}}/\">{{{{ title }}}}</a></li>"),
  232. fs::read_to_string(&project_dir.join("templates").join("post_listing_item.html"))
  233. .unwrap()
  234. .replace('\n', "")
  235. .replace(" ", "")
  236. );
  237. assert_eq!(
  238. format!("<article><h1>{{{{ title }}}}</h1><div>{{{{ body }}}}</div></article>"),
  239. fs::read_to_string(&project_dir.join("templates").join("post.html"))
  240. .unwrap()
  241. .replace('\n', "")
  242. .replace(" ", "")
  243. );
  244. assert_eq!(
  245. format!("<article><h1>{{{{ title }}}}</h1><div>{{{{ body }}}}</div></article>"),
  246. fs::read_to_string(&project_dir.join("templates").join("page.html"))
  247. .unwrap()
  248. .replace('\n', "")
  249. .replace(" ", "")
  250. );
  251. assert_eq!(
  252. format!(
  253. "site_name = \"{}\"\nurl = \"{}.com\"\ndescription = \"\"",
  254. &uuid, &uuid
  255. ),
  256. fs::read_to_string(&project_dir.join("casaubon.toml")).unwrap()
  257. );
  258. }