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 16KB

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