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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. // 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.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!(url_post.components.is_empty());
  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.components, vec!["posts".to_string()]);
  28. // That we have the right number of sections
  29. assert_eq!(site.sections.len(), 4);
  30. // And that the sections are correct
  31. let posts_section = &site.sections[&posts_path];
  32. assert_eq!(posts_section.subsections.len(), 1);
  33. //println!("{:#?}", posts_section.pages);
  34. assert_eq!(posts_section.pages.len(), 5);
  35. let tutorials_section = &site.sections[&posts_path.join("tutorials")];
  36. assert_eq!(tutorials_section.subsections.len(), 2);
  37. assert_eq!(tutorials_section.pages.len(), 0);
  38. let devops_section = &site.sections[&posts_path.join("tutorials").join("devops")];
  39. assert_eq!(devops_section.subsections.len(), 0);
  40. assert_eq!(devops_section.pages.len(), 2);
  41. let prog_section = &site.sections[&posts_path.join("tutorials").join("programming")];
  42. assert_eq!(prog_section.subsections.len(), 0);
  43. assert_eq!(prog_section.pages.len(), 2);
  44. }
  45. // 2 helper macros to make all the build testing more bearable
  46. macro_rules! file_exists {
  47. ($root: expr, $path: expr) => {
  48. {
  49. let mut path = $root.clone();
  50. for component in $path.split("/") {
  51. path = path.join(component);
  52. }
  53. Path::new(&path).exists()
  54. }
  55. }
  56. }
  57. macro_rules! file_contains {
  58. ($root: expr, $path: expr, $text: expr) => {
  59. {
  60. let mut path = $root.clone();
  61. for component in $path.split("/") {
  62. path = path.join(component);
  63. }
  64. let mut file = File::open(&path).unwrap();
  65. let mut s = String::new();
  66. file.read_to_string(&mut s).unwrap();
  67. s.contains($text)
  68. }
  69. }
  70. }
  71. #[test]
  72. fn test_can_build_site_without_live_reload() {
  73. let mut path = env::current_dir().unwrap().to_path_buf();
  74. path.push("test_site");
  75. let mut site = Site::new(&path, "config.toml").unwrap();
  76. site.load().unwrap();
  77. let tmp_dir = TempDir::new("example").expect("create temp dir");
  78. let public = &tmp_dir.path().join("public");
  79. site.set_output_path(&public);
  80. site.build().unwrap();
  81. assert!(Path::new(&public).exists());
  82. assert!(file_exists!(public, "index.html"));
  83. assert!(file_exists!(public, "sitemap.xml"));
  84. assert!(file_exists!(public, "robots.txt"));
  85. assert!(file_exists!(public, "a-fixed-url/index.html"));
  86. assert!(file_exists!(public, "posts/python/index.html"));
  87. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  88. assert!(file_exists!(public, "posts/with-assets/index.html"));
  89. // Sections
  90. assert!(file_exists!(public, "posts/index.html"));
  91. assert!(file_exists!(public, "posts/tutorials/index.html"));
  92. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  93. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  94. // TODO: add assertion for syntax highlighting
  95. // No tags or categories
  96. assert_eq!(file_exists!(public, "categories/index.html"), false);
  97. assert_eq!(file_exists!(public, "tags/index.html"), false);
  98. // no live reload code
  99. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  100. // Both pages and sections are in the sitemap
  101. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple</loc>"));
  102. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts</loc>"));
  103. }
  104. #[test]
  105. fn test_can_build_site_with_live_reload() {
  106. let mut path = env::current_dir().unwrap().to_path_buf();
  107. path.push("test_site");
  108. let mut site = Site::new(&path, "config.toml").unwrap();
  109. site.load().unwrap();
  110. let tmp_dir = TempDir::new("example").expect("create temp dir");
  111. let public = &tmp_dir.path().join("public");
  112. site.set_output_path(&public);
  113. site.enable_live_reload();
  114. site.build().unwrap();
  115. assert!(Path::new(&public).exists());
  116. assert!(file_exists!(public, "index.html"));
  117. assert!(file_exists!(public, "sitemap.xml"));
  118. assert!(file_exists!(public, "robots.txt"));
  119. assert!(file_exists!(public, "a-fixed-url/index.html"));
  120. assert!(file_exists!(public, "posts/python/index.html"));
  121. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  122. assert!(file_exists!(public, "posts/with-assets/index.html"));
  123. // Sections
  124. assert!(file_exists!(public, "posts/index.html"));
  125. assert!(file_exists!(public, "posts/tutorials/index.html"));
  126. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  127. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  128. // TODO: add assertion for syntax highlighting
  129. // No tags or categories
  130. assert_eq!(file_exists!(public, "categories/index.html"), false);
  131. assert_eq!(file_exists!(public, "tags/index.html"), false);
  132. // no live reload code
  133. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  134. }
  135. #[test]
  136. fn test_can_build_site_with_categories() {
  137. let mut path = env::current_dir().unwrap().to_path_buf();
  138. path.push("test_site");
  139. let mut site = Site::new(&path, "config.toml").unwrap();
  140. site.load().unwrap();
  141. for (i, page) in site.pages.values_mut().enumerate() {
  142. page.meta.category = if i % 2 == 0 {
  143. Some("A".to_string())
  144. } else {
  145. Some("B".to_string())
  146. };
  147. }
  148. site.populate_tags_and_categories();
  149. let tmp_dir = TempDir::new("example").expect("create temp dir");
  150. let public = &tmp_dir.path().join("public");
  151. site.set_output_path(&public);
  152. site.build().unwrap();
  153. assert!(Path::new(&public).exists());
  154. assert_eq!(site.categories.len(), 2);
  155. assert!(file_exists!(public, "index.html"));
  156. assert!(file_exists!(public, "sitemap.xml"));
  157. assert!(file_exists!(public, "robots.txt"));
  158. assert!(file_exists!(public, "a-fixed-url/index.html"));
  159. assert!(file_exists!(public, "posts/python/index.html"));
  160. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  161. assert!(file_exists!(public, "posts/with-assets/index.html"));
  162. // Sections
  163. assert!(file_exists!(public, "posts/index.html"));
  164. assert!(file_exists!(public, "posts/tutorials/index.html"));
  165. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  166. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  167. // TODO: add assertion for syntax highlighting
  168. // Categories are there
  169. assert!(file_exists!(public, "categories/index.html"));
  170. assert!(file_exists!(public, "categories/a/index.html"));
  171. assert!(file_exists!(public, "categories/b/index.html"));
  172. // Tags aren't
  173. assert_eq!(file_exists!(public, "tags/index.html"), false);
  174. // Categories are in the sitemap
  175. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories</loc>"));
  176. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a</loc>"));
  177. }
  178. #[test]
  179. fn test_can_build_site_with_tags() {
  180. let mut path = env::current_dir().unwrap().to_path_buf();
  181. path.push("test_site");
  182. let mut site = Site::new(&path, "config.toml").unwrap();
  183. site.load().unwrap();
  184. for (i, page) in site.pages.values_mut().enumerate() {
  185. page.meta.tags = if i % 2 == 0 {
  186. Some(vec!["tag1".to_string(), "tag2".to_string()])
  187. } else {
  188. Some(vec!["tag with space".to_string()])
  189. };
  190. }
  191. site.populate_tags_and_categories();
  192. let tmp_dir = TempDir::new("example").expect("create temp dir");
  193. let public = &tmp_dir.path().join("public");
  194. site.set_output_path(&public);
  195. site.build().unwrap();
  196. assert!(Path::new(&public).exists());
  197. assert_eq!(site.tags.len(), 3);
  198. assert!(file_exists!(public, "index.html"));
  199. assert!(file_exists!(public, "sitemap.xml"));
  200. assert!(file_exists!(public, "robots.txt"));
  201. assert!(file_exists!(public, "a-fixed-url/index.html"));
  202. assert!(file_exists!(public, "posts/python/index.html"));
  203. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  204. assert!(file_exists!(public, "posts/with-assets/index.html"));
  205. // Sections
  206. assert!(file_exists!(public, "posts/index.html"));
  207. assert!(file_exists!(public, "posts/tutorials/index.html"));
  208. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  209. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  210. // TODO: add assertion for syntax highlighting
  211. // Tags are there
  212. assert!(file_exists!(public, "tags/index.html"));
  213. assert!(file_exists!(public, "tags/tag1/index.html"));
  214. assert!(file_exists!(public, "tags/tag2/index.html"));
  215. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  216. // Categories aren't
  217. assert_eq!(file_exists!(public, "categories/index.html"), false);
  218. // Tags are in the sitemap
  219. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags</loc>"));
  220. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space</loc>"));
  221. }