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.

397 lines
16KB

  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. // No tags or categories
  100. assert_eq!(file_exists!(public, "categories/index.html"), false);
  101. assert_eq!(file_exists!(public, "tags/index.html"), false);
  102. // no live reload code
  103. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  104. // Both pages and sections are in the sitemap
  105. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
  106. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
  107. // section is in the page context
  108. assert!(file_contains!(public, "posts/python/index.html", "Section:"));
  109. }
  110. #[test]
  111. fn can_build_site_with_live_reload() {
  112. let mut path = env::current_dir().unwrap().to_path_buf();
  113. path.push("test_site");
  114. let mut site = Site::new(&path, "config.toml").unwrap();
  115. site.load().unwrap();
  116. let tmp_dir = TempDir::new("example").expect("create temp dir");
  117. let public = &tmp_dir.path().join("public");
  118. site.set_output_path(&public);
  119. site.enable_live_reload();
  120. site.build().unwrap();
  121. assert!(Path::new(&public).exists());
  122. assert!(file_exists!(public, "index.html"));
  123. assert!(file_exists!(public, "sitemap.xml"));
  124. assert!(file_exists!(public, "robots.txt"));
  125. assert!(file_exists!(public, "a-fixed-url/index.html"));
  126. assert!(file_exists!(public, "posts/python/index.html"));
  127. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  128. assert!(file_exists!(public, "posts/with-assets/index.html"));
  129. // Sections
  130. assert!(file_exists!(public, "posts/index.html"));
  131. assert!(file_exists!(public, "posts/tutorials/index.html"));
  132. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  133. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  134. // TODO: add assertion for syntax highlighting
  135. // No tags or categories
  136. assert_eq!(file_exists!(public, "categories/index.html"), false);
  137. assert_eq!(file_exists!(public, "tags/index.html"), false);
  138. // no live reload code
  139. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  140. }
  141. #[test]
  142. fn can_build_site_with_categories() {
  143. let mut path = env::current_dir().unwrap().to_path_buf();
  144. path.push("test_site");
  145. let mut site = Site::new(&path, "config.toml").unwrap();
  146. site.config.generate_categories_pages = Some(true);
  147. site.load().unwrap();
  148. for (i, page) in site.pages.values_mut().enumerate() {
  149. page.meta.category = if i % 2 == 0 {
  150. Some("A".to_string())
  151. } else {
  152. Some("B".to_string())
  153. };
  154. }
  155. site.populate_tags_and_categories();
  156. let tmp_dir = TempDir::new("example").expect("create temp dir");
  157. let public = &tmp_dir.path().join("public");
  158. site.set_output_path(&public);
  159. site.build().unwrap();
  160. assert!(Path::new(&public).exists());
  161. assert_eq!(site.categories.unwrap().len(), 2);
  162. assert!(file_exists!(public, "index.html"));
  163. assert!(file_exists!(public, "sitemap.xml"));
  164. assert!(file_exists!(public, "robots.txt"));
  165. assert!(file_exists!(public, "a-fixed-url/index.html"));
  166. assert!(file_exists!(public, "posts/python/index.html"));
  167. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  168. assert!(file_exists!(public, "posts/with-assets/index.html"));
  169. // Sections
  170. assert!(file_exists!(public, "posts/index.html"));
  171. assert!(file_exists!(public, "posts/tutorials/index.html"));
  172. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  173. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  174. // TODO: add assertion for syntax highlighting
  175. // Categories are there
  176. assert!(file_exists!(public, "categories/index.html"));
  177. assert!(file_exists!(public, "categories/a/index.html"));
  178. assert!(file_exists!(public, "categories/b/index.html"));
  179. // Tags aren't
  180. assert_eq!(file_exists!(public, "tags/index.html"), false);
  181. // Categories are in the sitemap
  182. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/</loc>"));
  183. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"));
  184. }
  185. #[test]
  186. fn can_build_site_with_tags() {
  187. let mut path = env::current_dir().unwrap().to_path_buf();
  188. path.push("test_site");
  189. let mut site = Site::new(&path, "config.toml").unwrap();
  190. site.config.generate_tags_pages = Some(true);
  191. site.load().unwrap();
  192. for (i, page) in site.pages.values_mut().enumerate() {
  193. page.meta.tags = if i % 2 == 0 {
  194. Some(vec!["tag1".to_string(), "tag2".to_string()])
  195. } else {
  196. Some(vec!["tag with space".to_string()])
  197. };
  198. }
  199. site.populate_tags_and_categories();
  200. let tmp_dir = TempDir::new("example").expect("create temp dir");
  201. let public = &tmp_dir.path().join("public");
  202. site.set_output_path(&public);
  203. site.build().unwrap();
  204. assert!(Path::new(&public).exists());
  205. assert_eq!(site.tags.unwrap().len(), 3);
  206. assert!(file_exists!(public, "index.html"));
  207. assert!(file_exists!(public, "sitemap.xml"));
  208. assert!(file_exists!(public, "robots.txt"));
  209. assert!(file_exists!(public, "a-fixed-url/index.html"));
  210. assert!(file_exists!(public, "posts/python/index.html"));
  211. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  212. assert!(file_exists!(public, "posts/with-assets/index.html"));
  213. // Sections
  214. assert!(file_exists!(public, "posts/index.html"));
  215. assert!(file_exists!(public, "posts/tutorials/index.html"));
  216. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  217. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  218. // TODO: add assertion for syntax highlighting
  219. // Tags are there
  220. assert!(file_exists!(public, "tags/index.html"));
  221. assert!(file_exists!(public, "tags/tag1/index.html"));
  222. assert!(file_exists!(public, "tags/tag2/index.html"));
  223. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  224. // Categories aren't
  225. assert_eq!(file_exists!(public, "categories/index.html"), false);
  226. // Tags are in the sitemap
  227. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/</loc>"));
  228. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space/</loc>"));
  229. }
  230. #[test]
  231. fn can_build_site_and_insert_anchor_links() {
  232. let mut path = env::current_dir().unwrap().to_path_buf();
  233. path.push("test_site");
  234. let mut site = Site::new(&path, "config.toml").unwrap();
  235. site.load().unwrap();
  236. let tmp_dir = TempDir::new("example").expect("create temp dir");
  237. let public = &tmp_dir.path().join("public");
  238. site.set_output_path(&public);
  239. site.build().unwrap();
  240. assert!(Path::new(&public).exists());
  241. // anchor link inserted
  242. assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"gutenberg-anchor\" href=\"#title\""));
  243. }
  244. #[test]
  245. fn can_build_site_with_pagination_for_section() {
  246. let mut path = env::current_dir().unwrap().to_path_buf();
  247. path.push("test_site");
  248. let mut site = Site::new(&path, "config.toml").unwrap();
  249. site.load().unwrap();
  250. for section in site.sections.values_mut(){
  251. if section.is_index() {
  252. continue;
  253. }
  254. section.meta.paginate_by = Some(2);
  255. section.meta.template = Some("section_paginated.html".to_string());
  256. }
  257. let tmp_dir = TempDir::new("example").expect("create temp dir");
  258. let public = &tmp_dir.path().join("public");
  259. site.set_output_path(&public);
  260. site.build().unwrap();
  261. assert!(Path::new(&public).exists());
  262. assert!(file_exists!(public, "index.html"));
  263. assert!(file_exists!(public, "sitemap.xml"));
  264. assert!(file_exists!(public, "robots.txt"));
  265. assert!(file_exists!(public, "a-fixed-url/index.html"));
  266. assert!(file_exists!(public, "posts/python/index.html"));
  267. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  268. assert!(file_exists!(public, "posts/with-assets/index.html"));
  269. // Sections
  270. assert!(file_exists!(public, "posts/index.html"));
  271. // And pagination!
  272. assert!(file_exists!(public, "posts/page/1/index.html"));
  273. // even if there is no pages, only the section!
  274. assert!(file_exists!(public, "paginated/page/1/index.html"));
  275. assert!(file_exists!(public, "paginated/index.html"));
  276. // should redirect to posts/
  277. assert!(file_contains!(
  278. public,
  279. "posts/page/1/index.html",
  280. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  281. ));
  282. assert!(file_contains!(public, "posts/index.html", "Num pagers: 3"));
  283. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  284. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  285. assert!(file_contains!(public, "posts/index.html", "has_next"));
  286. assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  287. assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  288. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  289. assert!(file_exists!(public, "posts/page/2/index.html"));
  290. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 3"));
  291. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  292. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  293. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  294. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  295. assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  296. assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  297. }
  298. #[test]
  299. fn can_build_site_with_pagination_for_index() {
  300. let mut path = env::current_dir().unwrap().to_path_buf();
  301. path.push("test_site");
  302. let mut site = Site::new(&path, "config.toml").unwrap();
  303. site.load().unwrap();
  304. {
  305. let mut index = site.sections.get_mut(&path.join("content").join("_index.md")).unwrap();
  306. index.meta.paginate_by = Some(2);
  307. index.meta.template = Some("index_paginated.html".to_string());
  308. }
  309. let tmp_dir = TempDir::new("example").expect("create temp dir");
  310. let public = &tmp_dir.path().join("public");
  311. site.set_output_path(&public);
  312. site.build().unwrap();
  313. assert!(Path::new(&public).exists());
  314. assert!(file_exists!(public, "index.html"));
  315. assert!(file_exists!(public, "sitemap.xml"));
  316. assert!(file_exists!(public, "robots.txt"));
  317. assert!(file_exists!(public, "a-fixed-url/index.html"));
  318. assert!(file_exists!(public, "posts/python/index.html"));
  319. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  320. assert!(file_exists!(public, "posts/with-assets/index.html"));
  321. // And pagination!
  322. assert!(file_exists!(public, "page/1/index.html"));
  323. // even if there is no pages, only the section!
  324. assert!(file_exists!(public, "paginated/page/1/index.html"));
  325. assert!(file_exists!(public, "paginated/index.html"));
  326. // should redirect to index
  327. assert!(file_contains!(
  328. public,
  329. "page/1/index.html",
  330. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  331. ));
  332. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  333. assert!(file_contains!(public, "index.html", "Current index: 1"));
  334. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  335. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  336. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  337. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  338. }