A static site generator written in Rust
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

integration_test.rs 9.9KB

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