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.

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