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.

site.rs 18KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. extern crate site;
  2. extern crate front_matter;
  3. extern crate tempdir;
  4. use std::env;
  5. use std::path::Path;
  6. use std::fs::File;
  7. use std::io::prelude::*;
  8. use tempdir::TempDir;
  9. use site::Site;
  10. use front_matter::InsertAnchor;
  11. #[test]
  12. fn can_parse_site() {
  13. let mut path = env::current_dir().unwrap().to_path_buf();
  14. path.push("test_site");
  15. let mut site = Site::new(&path, "config.toml").unwrap();
  16. site.load().unwrap();
  17. // Correct number of pages (sections are pages too)
  18. assert_eq!(site.pages.len(), 12);
  19. let posts_path = path.join("content").join("posts");
  20. // Make sure we remove all the pwd + content from the sections
  21. let basic = &site.pages[&posts_path.join("simple.md")];
  22. assert_eq!(basic.file.components, vec!["posts".to_string()]);
  23. // Make sure the page with a url doesn't have any sections
  24. let url_post = &site.pages[&posts_path.join("fixed-url.md")];
  25. assert_eq!(url_post.path, "a-fixed-url/");
  26. // Make sure the article in a folder with only asset doesn't get counted as a section
  27. let asset_folder_post = &site.pages[&posts_path.join("with-assets").join("index.md")];
  28. assert_eq!(asset_folder_post.file.components, vec!["posts".to_string()]);
  29. // That we have the right number of sections
  30. assert_eq!(site.sections.len(), 6);
  31. // And that the sections are correct
  32. let index_section = &site.sections[&path.join("content").join("_index.md")];
  33. assert_eq!(index_section.subsections.len(), 2);
  34. assert_eq!(index_section.pages.len(), 1);
  35. let posts_section = &site.sections[&posts_path.join("_index.md")];
  36. assert_eq!(posts_section.subsections.len(), 1);
  37. assert_eq!(posts_section.pages.len(), 6);
  38. let tutorials_section = &site.sections[&posts_path.join("tutorials").join("_index.md")];
  39. assert_eq!(tutorials_section.subsections.len(), 2);
  40. assert_eq!(tutorials_section.subsections[0].clone().meta.title.unwrap(), "Programming");
  41. assert_eq!(tutorials_section.subsections[1].clone().meta.title.unwrap(), "DevOps");
  42. assert_eq!(tutorials_section.pages.len(), 0);
  43. let devops_section = &site.sections[&posts_path.join("tutorials").join("devops").join("_index.md")];
  44. assert_eq!(devops_section.subsections.len(), 0);
  45. assert_eq!(devops_section.pages.len(), 2);
  46. let prog_section = &site.sections[&posts_path.join("tutorials").join("programming").join("_index.md")];
  47. assert_eq!(prog_section.subsections.len(), 0);
  48. assert_eq!(prog_section.pages.len(), 2);
  49. }
  50. // 2 helper macros to make all the build testing more bearable
  51. macro_rules! file_exists {
  52. ($root: expr, $path: expr) => {
  53. {
  54. let mut path = $root.clone();
  55. for component in $path.split("/") {
  56. path = path.join(component);
  57. }
  58. Path::new(&path).exists()
  59. }
  60. }
  61. }
  62. macro_rules! file_contains {
  63. ($root: expr, $path: expr, $text: expr) => {
  64. {
  65. let mut path = $root.clone();
  66. for component in $path.split("/") {
  67. path = path.join(component);
  68. }
  69. let mut file = File::open(&path).unwrap();
  70. let mut s = String::new();
  71. file.read_to_string(&mut s).unwrap();
  72. println!("{}", s);
  73. s.contains($text)
  74. }
  75. }
  76. }
  77. #[test]
  78. fn can_build_site_without_live_reload() {
  79. let mut path = env::current_dir().unwrap().to_path_buf();
  80. path.push("test_site");
  81. let mut site = Site::new(&path, "config.toml").unwrap();
  82. site.load().unwrap();
  83. let tmp_dir = TempDir::new("example").expect("create temp dir");
  84. let public = &tmp_dir.path().join("public");
  85. site.set_output_path(&public);
  86. site.build().unwrap();
  87. assert!(Path::new(&public).exists());
  88. assert!(file_exists!(public, "index.html"));
  89. assert!(file_exists!(public, "sitemap.xml"));
  90. assert!(file_exists!(public, "robots.txt"));
  91. assert!(file_exists!(public, "a-fixed-url/index.html"));
  92. assert!(file_exists!(public, "posts/python/index.html"));
  93. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  94. assert!(file_exists!(public, "posts/with-assets/index.html"));
  95. assert!(file_exists!(public, "posts/no-section/simple/index.html"));
  96. // Sections
  97. assert!(file_exists!(public, "posts/index.html"));
  98. assert!(file_exists!(public, "posts/tutorials/index.html"));
  99. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  100. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  101. // Ensure subsection pages are correctly filled
  102. assert!(file_contains!(public, "posts/tutorials/index.html", "Sub-pages: 2"));
  103. // TODO: add assertion for syntax highlighting
  104. // aliases work
  105. assert!(file_exists!(public, "an-old-url/old-page/index.html"));
  106. assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
  107. // redirect_to works
  108. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  109. assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker"));
  110. // No tags or categories
  111. assert_eq!(file_exists!(public, "categories/index.html"), false);
  112. assert_eq!(file_exists!(public, "tags/index.html"), false);
  113. // Theme files are there
  114. assert!(file_exists!(public, "sample.css"));
  115. assert!(file_exists!(public, "some.js"));
  116. // no live reload code
  117. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  118. // Both pages and sections are in the sitemap
  119. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
  120. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
  121. // Drafts are not in the sitemap
  122. assert!(!file_contains!(public, "sitemap.xml", "draft"));
  123. }
  124. #[test]
  125. fn can_build_site_with_live_reload() {
  126. let mut path = env::current_dir().unwrap().to_path_buf();
  127. path.push("test_site");
  128. let mut site = Site::new(&path, "config.toml").unwrap();
  129. site.load().unwrap();
  130. let tmp_dir = TempDir::new("example").expect("create temp dir");
  131. let public = &tmp_dir.path().join("public");
  132. site.set_output_path(&public);
  133. site.enable_live_reload();
  134. site.build().unwrap();
  135. assert!(Path::new(&public).exists());
  136. assert!(file_exists!(public, "index.html"));
  137. assert!(file_exists!(public, "sitemap.xml"));
  138. assert!(file_exists!(public, "robots.txt"));
  139. assert!(file_exists!(public, "a-fixed-url/index.html"));
  140. assert!(file_exists!(public, "posts/python/index.html"));
  141. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  142. assert!(file_exists!(public, "posts/with-assets/index.html"));
  143. // Sections
  144. assert!(file_exists!(public, "posts/index.html"));
  145. assert!(file_exists!(public, "posts/tutorials/index.html"));
  146. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  147. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  148. // TODO: add assertion for syntax highlighting
  149. // No tags or categories
  150. assert_eq!(file_exists!(public, "categories/index.html"), false);
  151. assert_eq!(file_exists!(public, "tags/index.html"), false);
  152. // no live reload code
  153. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  154. }
  155. #[test]
  156. fn can_build_site_with_categories() {
  157. let mut path = env::current_dir().unwrap().to_path_buf();
  158. path.push("test_site");
  159. let mut site = Site::new(&path, "config.toml").unwrap();
  160. site.config.generate_categories_pages = Some(true);
  161. site.load().unwrap();
  162. for (i, page) in site.pages.values_mut().enumerate() {
  163. page.meta.category = if i % 2 == 0 {
  164. Some("A".to_string())
  165. } else {
  166. Some("B".to_string())
  167. };
  168. }
  169. site.populate_tags_and_categories();
  170. let tmp_dir = TempDir::new("example").expect("create temp dir");
  171. let public = &tmp_dir.path().join("public");
  172. site.set_output_path(&public);
  173. site.build().unwrap();
  174. assert!(Path::new(&public).exists());
  175. assert_eq!(site.categories.unwrap().len(), 2);
  176. assert!(file_exists!(public, "index.html"));
  177. assert!(file_exists!(public, "sitemap.xml"));
  178. assert!(file_exists!(public, "robots.txt"));
  179. assert!(file_exists!(public, "a-fixed-url/index.html"));
  180. assert!(file_exists!(public, "posts/python/index.html"));
  181. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  182. assert!(file_exists!(public, "posts/with-assets/index.html"));
  183. // Sections
  184. assert!(file_exists!(public, "posts/index.html"));
  185. assert!(file_exists!(public, "posts/tutorials/index.html"));
  186. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  187. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  188. // TODO: add assertion for syntax highlighting
  189. // Categories are there
  190. assert!(file_exists!(public, "categories/index.html"));
  191. assert!(file_exists!(public, "categories/a/index.html"));
  192. assert!(file_exists!(public, "categories/b/index.html"));
  193. // Extending from a theme works
  194. assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
  195. // Tags aren't
  196. assert_eq!(file_exists!(public, "tags/index.html"), false);
  197. // Categories are in the sitemap
  198. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/</loc>"));
  199. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"));
  200. }
  201. #[test]
  202. fn can_build_site_with_tags() {
  203. let mut path = env::current_dir().unwrap().to_path_buf();
  204. path.push("test_site");
  205. let mut site = Site::new(&path, "config.toml").unwrap();
  206. site.config.generate_tags_pages = Some(true);
  207. site.load().unwrap();
  208. for (i, page) in site.pages.values_mut().enumerate() {
  209. page.meta.tags = if i % 2 == 0 {
  210. Some(vec!["tag1".to_string(), "tag2".to_string()])
  211. } else {
  212. Some(vec!["tag with space".to_string()])
  213. };
  214. }
  215. site.populate_tags_and_categories();
  216. let tmp_dir = TempDir::new("example").expect("create temp dir");
  217. let public = &tmp_dir.path().join("public");
  218. site.set_output_path(&public);
  219. site.build().unwrap();
  220. assert!(Path::new(&public).exists());
  221. assert_eq!(site.tags.unwrap().len(), 3);
  222. assert!(file_exists!(public, "index.html"));
  223. assert!(file_exists!(public, "sitemap.xml"));
  224. assert!(file_exists!(public, "robots.txt"));
  225. assert!(file_exists!(public, "a-fixed-url/index.html"));
  226. assert!(file_exists!(public, "posts/python/index.html"));
  227. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  228. assert!(file_exists!(public, "posts/with-assets/index.html"));
  229. // Sections
  230. assert!(file_exists!(public, "posts/index.html"));
  231. assert!(file_exists!(public, "posts/tutorials/index.html"));
  232. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  233. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  234. // TODO: add assertion for syntax highlighting
  235. // Tags are there
  236. assert!(file_exists!(public, "tags/index.html"));
  237. assert!(file_exists!(public, "tags/tag1/index.html"));
  238. assert!(file_exists!(public, "tags/tag2/index.html"));
  239. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  240. // Categories aren't
  241. assert_eq!(file_exists!(public, "categories/index.html"), false);
  242. // Tags are in the sitemap
  243. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/</loc>"));
  244. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space/</loc>"));
  245. }
  246. #[test]
  247. fn can_build_site_and_insert_anchor_links() {
  248. let mut path = env::current_dir().unwrap().to_path_buf();
  249. path.push("test_site");
  250. let mut site = Site::new(&path, "config.toml").unwrap();
  251. site.load().unwrap();
  252. let tmp_dir = TempDir::new("example").expect("create temp dir");
  253. let public = &tmp_dir.path().join("public");
  254. site.set_output_path(&public);
  255. site.build().unwrap();
  256. assert!(Path::new(&public).exists());
  257. // anchor link inserted
  258. assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"gutenberg-anchor\" href=\"#title\""));
  259. }
  260. #[test]
  261. fn can_build_site_with_pagination_for_section() {
  262. let mut path = env::current_dir().unwrap().to_path_buf();
  263. path.push("test_site");
  264. let mut site = Site::new(&path, "config.toml").unwrap();
  265. site.load().unwrap();
  266. for section in site.sections.values_mut(){
  267. if section.is_index() {
  268. continue;
  269. }
  270. section.meta.paginate_by = Some(2);
  271. section.meta.template = Some("section_paginated.html".to_string());
  272. }
  273. let tmp_dir = TempDir::new("example").expect("create temp dir");
  274. let public = &tmp_dir.path().join("public");
  275. site.set_output_path(&public);
  276. site.build().unwrap();
  277. assert!(Path::new(&public).exists());
  278. assert!(file_exists!(public, "index.html"));
  279. assert!(file_exists!(public, "sitemap.xml"));
  280. assert!(file_exists!(public, "robots.txt"));
  281. assert!(file_exists!(public, "a-fixed-url/index.html"));
  282. assert!(file_exists!(public, "posts/python/index.html"));
  283. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  284. assert!(file_exists!(public, "posts/with-assets/index.html"));
  285. // Sections
  286. assert!(file_exists!(public, "posts/index.html"));
  287. // And pagination!
  288. assert!(file_exists!(public, "posts/page/1/index.html"));
  289. // even if there is no pages, only the section!
  290. assert!(file_exists!(public, "paginated/page/1/index.html"));
  291. assert!(file_exists!(public, "paginated/index.html"));
  292. // should redirect to posts/
  293. assert!(file_contains!(
  294. public,
  295. "posts/page/1/index.html",
  296. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  297. ));
  298. assert!(file_contains!(public, "posts/index.html", "Num pagers: 3"));
  299. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  300. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  301. assert!(file_contains!(public, "posts/index.html", "has_next"));
  302. assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  303. assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  304. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  305. assert!(file_exists!(public, "posts/page/2/index.html"));
  306. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 3"));
  307. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  308. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  309. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  310. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  311. assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  312. assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  313. }
  314. #[test]
  315. fn can_build_site_with_pagination_for_index() {
  316. let mut path = env::current_dir().unwrap().to_path_buf();
  317. path.push("test_site");
  318. let mut site = Site::new(&path, "config.toml").unwrap();
  319. site.load().unwrap();
  320. {
  321. let index = site.sections.get_mut(&path.join("content").join("_index.md")).unwrap();
  322. index.meta.paginate_by = Some(2);
  323. index.meta.template = Some("index_paginated.html".to_string());
  324. }
  325. let tmp_dir = TempDir::new("example").expect("create temp dir");
  326. let public = &tmp_dir.path().join("public");
  327. site.set_output_path(&public);
  328. site.build().unwrap();
  329. assert!(Path::new(&public).exists());
  330. assert!(file_exists!(public, "index.html"));
  331. assert!(file_exists!(public, "sitemap.xml"));
  332. assert!(file_exists!(public, "robots.txt"));
  333. assert!(file_exists!(public, "a-fixed-url/index.html"));
  334. assert!(file_exists!(public, "posts/python/index.html"));
  335. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  336. assert!(file_exists!(public, "posts/with-assets/index.html"));
  337. // And pagination!
  338. assert!(file_exists!(public, "page/1/index.html"));
  339. // even if there is no pages, only the section!
  340. assert!(file_exists!(public, "paginated/page/1/index.html"));
  341. assert!(file_exists!(public, "paginated/index.html"));
  342. // should redirect to index
  343. assert!(file_contains!(
  344. public,
  345. "page/1/index.html",
  346. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  347. ));
  348. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  349. assert!(file_contains!(public, "index.html", "Current index: 1"));
  350. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  351. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  352. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  353. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  354. }
  355. #[test]
  356. fn can_build_rss_feed() {
  357. let mut path = env::current_dir().unwrap().to_path_buf();
  358. path.push("test_site");
  359. let mut site = Site::new(&path, "config.toml").unwrap();
  360. site.load().unwrap();
  361. let tmp_dir = TempDir::new("example").expect("create temp dir");
  362. let public = &tmp_dir.path().join("public");
  363. site.set_output_path(&public);
  364. site.build().unwrap();
  365. assert!(Path::new(&public).exists());
  366. assert!(file_exists!(public, "rss.xml"));
  367. // latest article is posts/simple.md
  368. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  369. // Next is posts/python.md
  370. assert!(file_contains!(public, "rss.xml", "Python in posts"));
  371. }