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.

385 lines
15KB

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