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.

287 lines
11KB

  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. //println!("{:#?}", posts_section.pages);
  36. assert_eq!(posts_section.pages.len(), 4);
  37. let tutorials_section = &site.sections[&posts_path.join("tutorials")];
  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")];
  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")];
  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. 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. }
  106. #[test]
  107. fn test_can_build_site_with_live_reload() {
  108. let mut path = env::current_dir().unwrap().to_path_buf();
  109. path.push("test_site");
  110. let mut site = Site::new(&path, "config.toml").unwrap();
  111. site.load().unwrap();
  112. let tmp_dir = TempDir::new("example").expect("create temp dir");
  113. let public = &tmp_dir.path().join("public");
  114. site.set_output_path(&public);
  115. site.enable_live_reload();
  116. site.build().unwrap();
  117. assert!(Path::new(&public).exists());
  118. assert!(file_exists!(public, "index.html"));
  119. assert!(file_exists!(public, "sitemap.xml"));
  120. assert!(file_exists!(public, "robots.txt"));
  121. assert!(file_exists!(public, "a-fixed-url/index.html"));
  122. assert!(file_exists!(public, "posts/python/index.html"));
  123. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  124. assert!(file_exists!(public, "posts/with-assets/index.html"));
  125. // Sections
  126. assert!(file_exists!(public, "posts/index.html"));
  127. assert!(file_exists!(public, "posts/tutorials/index.html"));
  128. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  129. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  130. // TODO: add assertion for syntax highlighting
  131. // No tags or categories
  132. assert_eq!(file_exists!(public, "categories/index.html"), false);
  133. assert_eq!(file_exists!(public, "tags/index.html"), false);
  134. // no live reload code
  135. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  136. }
  137. #[test]
  138. fn test_can_build_site_with_categories() {
  139. let mut path = env::current_dir().unwrap().to_path_buf();
  140. path.push("test_site");
  141. let mut site = Site::new(&path, "config.toml").unwrap();
  142. site.load().unwrap();
  143. for (i, page) in site.pages.values_mut().enumerate() {
  144. page.meta.category = if i % 2 == 0 {
  145. Some("A".to_string())
  146. } else {
  147. Some("B".to_string())
  148. };
  149. }
  150. site.populate_tags_and_categories();
  151. let tmp_dir = TempDir::new("example").expect("create temp dir");
  152. let public = &tmp_dir.path().join("public");
  153. site.set_output_path(&public);
  154. site.build().unwrap();
  155. assert!(Path::new(&public).exists());
  156. assert_eq!(site.categories.len(), 2);
  157. assert!(file_exists!(public, "index.html"));
  158. assert!(file_exists!(public, "sitemap.xml"));
  159. assert!(file_exists!(public, "robots.txt"));
  160. assert!(file_exists!(public, "a-fixed-url/index.html"));
  161. assert!(file_exists!(public, "posts/python/index.html"));
  162. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  163. assert!(file_exists!(public, "posts/with-assets/index.html"));
  164. // Sections
  165. assert!(file_exists!(public, "posts/index.html"));
  166. assert!(file_exists!(public, "posts/tutorials/index.html"));
  167. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  168. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  169. // TODO: add assertion for syntax highlighting
  170. // Categories are there
  171. assert!(file_exists!(public, "categories/index.html"));
  172. assert!(file_exists!(public, "categories/a/index.html"));
  173. assert!(file_exists!(public, "categories/b/index.html"));
  174. // Tags aren't
  175. assert_eq!(file_exists!(public, "tags/index.html"), false);
  176. // Categories are in the sitemap
  177. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories</loc>"));
  178. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a</loc>"));
  179. }
  180. #[test]
  181. fn test_can_build_site_with_tags() {
  182. let mut path = env::current_dir().unwrap().to_path_buf();
  183. path.push("test_site");
  184. let mut site = Site::new(&path, "config.toml").unwrap();
  185. site.load().unwrap();
  186. for (i, page) in site.pages.values_mut().enumerate() {
  187. page.meta.tags = if i % 2 == 0 {
  188. Some(vec!["tag1".to_string(), "tag2".to_string()])
  189. } else {
  190. Some(vec!["tag with space".to_string()])
  191. };
  192. }
  193. site.populate_tags_and_categories();
  194. let tmp_dir = TempDir::new("example").expect("create temp dir");
  195. let public = &tmp_dir.path().join("public");
  196. site.set_output_path(&public);
  197. site.build().unwrap();
  198. assert!(Path::new(&public).exists());
  199. assert_eq!(site.tags.len(), 3);
  200. assert!(file_exists!(public, "index.html"));
  201. assert!(file_exists!(public, "sitemap.xml"));
  202. assert!(file_exists!(public, "robots.txt"));
  203. assert!(file_exists!(public, "a-fixed-url/index.html"));
  204. assert!(file_exists!(public, "posts/python/index.html"));
  205. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  206. assert!(file_exists!(public, "posts/with-assets/index.html"));
  207. // Sections
  208. assert!(file_exists!(public, "posts/index.html"));
  209. assert!(file_exists!(public, "posts/tutorials/index.html"));
  210. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  211. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  212. // TODO: add assertion for syntax highlighting
  213. // Tags are there
  214. assert!(file_exists!(public, "tags/index.html"));
  215. assert!(file_exists!(public, "tags/tag1/index.html"));
  216. assert!(file_exists!(public, "tags/tag2/index.html"));
  217. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  218. // Categories aren't
  219. assert_eq!(file_exists!(public, "categories/index.html"), false);
  220. // Tags are in the sitemap
  221. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags</loc>"));
  222. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space</loc>"));
  223. }
  224. #[test]
  225. fn test_can_build_site_and_insert_anchor_links() {
  226. let mut path = env::current_dir().unwrap().to_path_buf();
  227. path.push("test_site");
  228. let mut site = Site::new(&path, "config.toml").unwrap();
  229. site.config.insert_anchor_links = Some(true);
  230. site.load().unwrap();
  231. let tmp_dir = TempDir::new("example").expect("create temp dir");
  232. let public = &tmp_dir.path().join("public");
  233. site.set_output_path(&public);
  234. site.build().unwrap();
  235. assert!(Path::new(&public).exists());
  236. // anchor link inserted
  237. assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"anchor\" href=\"#title\""));
  238. }