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.

264 lines
9.8KB

  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 glob::glob;
  9. use tempdir::TempDir;
  10. use gutenberg::{Site};
  11. #[test]
  12. fn test_can_parse_site() {
  13. let mut path = env::current_dir().unwrap().to_path_buf();
  14. path.push("test_site");
  15. let mut site = Site::new(&path).unwrap();
  16. site.parse().unwrap();
  17. // Correct number of pages (sections are pages too)
  18. assert_eq!(site.pages.len(), 10);
  19. let posts_path = path.join("content").join("posts");
  20. // Make sure we remove all the pwd + content from the sections
  21. let basic = &site.pages[&posts_path.join("simple.md")];
  22. assert_eq!(basic.components, vec!["posts".to_string()]);
  23. // Make sure the page with a url doesn't have any sections
  24. let url_post = &site.pages[&posts_path.join("fixed-url.md")];
  25. assert!(url_post.components.is_empty());
  26. // Make sure the article in a folder with only asset doesn't get counted as a section
  27. let asset_folder_post = &site.pages[&posts_path.join("with-assets").join("index.md")];
  28. assert_eq!(asset_folder_post.components, vec!["posts".to_string()]);
  29. // That we have the right number of sections
  30. assert_eq!(site.sections.len(), 4);
  31. // And that the sections are correct
  32. let posts_section = &site.sections[&posts_path];
  33. assert_eq!(posts_section.subsections.len(), 1);
  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).unwrap();
  76. site.parse().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, "a-fixed-url/index.html"));
  85. assert!(file_exists!(public, "posts/python/index.html"));
  86. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  87. assert!(file_exists!(public, "posts/with-assets/index.html"));
  88. // Sections
  89. assert!(file_exists!(public, "posts/index.html"));
  90. assert!(file_exists!(public, "posts/tutorials/index.html"));
  91. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  92. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  93. // TODO: add assertion for syntax highlighting
  94. // No tags or categories
  95. assert_eq!(file_exists!(public, "categories/index.html"), false);
  96. assert_eq!(file_exists!(public, "tags/index.html"), false);
  97. // no live reload code
  98. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  99. // Both pages and sections are in the sitemap
  100. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple</loc>"));
  101. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts</loc>"));
  102. }
  103. #[test]
  104. fn test_can_build_site_with_live_reload() {
  105. let mut path = env::current_dir().unwrap().to_path_buf();
  106. path.push("test_site");
  107. let mut site = Site::new(&path).unwrap();
  108. site.parse().unwrap();
  109. let tmp_dir = TempDir::new("example").expect("create temp dir");
  110. let public = &tmp_dir.path().join("public");
  111. site.set_output_path(&public);
  112. site.enable_live_reload();
  113. site.build().unwrap();
  114. assert!(Path::new(&public).exists());
  115. assert!(file_exists!(public, "index.html"));
  116. assert!(file_exists!(public, "sitemap.xml"));
  117. assert!(file_exists!(public, "a-fixed-url/index.html"));
  118. assert!(file_exists!(public, "posts/python/index.html"));
  119. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  120. assert!(file_exists!(public, "posts/with-assets/index.html"));
  121. // Sections
  122. assert!(file_exists!(public, "posts/index.html"));
  123. assert!(file_exists!(public, "posts/tutorials/index.html"));
  124. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  125. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  126. // TODO: add assertion for syntax highlighting
  127. // No tags or categories
  128. assert_eq!(file_exists!(public, "categories/index.html"), false);
  129. assert_eq!(file_exists!(public, "tags/index.html"), false);
  130. // no live reload code
  131. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  132. }
  133. #[test]
  134. fn test_can_build_site_with_categories() {
  135. let mut path = env::current_dir().unwrap().to_path_buf();
  136. path.push("test_site");
  137. let mut site = Site::new(&path).unwrap();
  138. site.parse().unwrap();
  139. for (i, page) in site.pages.values_mut().enumerate() {
  140. page.meta.category = if i % 2 == 0 {
  141. Some("A".to_string())
  142. } else {
  143. Some("B".to_string())
  144. };
  145. }
  146. site.parse_tags_and_categories();
  147. let tmp_dir = TempDir::new("example").expect("create temp dir");
  148. let public = &tmp_dir.path().join("public");
  149. site.set_output_path(&public);
  150. site.build().unwrap();
  151. assert!(Path::new(&public).exists());
  152. assert_eq!(site.categories.len(), 2);
  153. assert!(file_exists!(public, "index.html"));
  154. assert!(file_exists!(public, "sitemap.xml"));
  155. assert!(file_exists!(public, "a-fixed-url/index.html"));
  156. assert!(file_exists!(public, "posts/python/index.html"));
  157. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  158. assert!(file_exists!(public, "posts/with-assets/index.html"));
  159. // Sections
  160. assert!(file_exists!(public, "posts/index.html"));
  161. assert!(file_exists!(public, "posts/tutorials/index.html"));
  162. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  163. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  164. // TODO: add assertion for syntax highlighting
  165. // Categories are there
  166. assert!(file_exists!(public, "categories/index.html"));
  167. assert!(file_exists!(public, "categories/a/index.html"));
  168. assert!(file_exists!(public, "categories/b/index.html"));
  169. // Tags aren't
  170. assert_eq!(file_exists!(public, "tags/index.html"), false);
  171. // Categories are in the sitemap
  172. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories</loc>"));
  173. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a</loc>"));
  174. }
  175. #[test]
  176. fn test_can_build_site_with_tags() {
  177. let mut path = env::current_dir().unwrap().to_path_buf();
  178. path.push("test_site");
  179. let mut site = Site::new(&path).unwrap();
  180. site.parse().unwrap();
  181. for (i, page) in site.pages.values_mut().enumerate() {
  182. page.meta.tags = if i % 2 == 0 {
  183. Some(vec!["tag1".to_string(), "tag2".to_string()])
  184. } else {
  185. Some(vec!["tag with space".to_string()])
  186. };
  187. }
  188. site.parse_tags_and_categories();
  189. let tmp_dir = TempDir::new("example").expect("create temp dir");
  190. let public = &tmp_dir.path().join("public");
  191. site.set_output_path(&public);
  192. site.build().unwrap();
  193. assert!(Path::new(&public).exists());
  194. assert_eq!(site.tags.len(), 3);
  195. assert!(file_exists!(public, "index.html"));
  196. assert!(file_exists!(public, "sitemap.xml"));
  197. assert!(file_exists!(public, "a-fixed-url/index.html"));
  198. assert!(file_exists!(public, "posts/python/index.html"));
  199. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  200. assert!(file_exists!(public, "posts/with-assets/index.html"));
  201. // Sections
  202. assert!(file_exists!(public, "posts/index.html"));
  203. assert!(file_exists!(public, "posts/tutorials/index.html"));
  204. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  205. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  206. // TODO: add assertion for syntax highlighting
  207. // Tags are there
  208. assert!(file_exists!(public, "tags/index.html"));
  209. assert!(file_exists!(public, "tags/tag1/index.html"));
  210. assert!(file_exists!(public, "tags/tag2/index.html"));
  211. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  212. // Categories aren't
  213. assert_eq!(file_exists!(public, "categories/index.html"), false);
  214. // Tags are in the sitemap
  215. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags</loc>"));
  216. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space</loc>"));
  217. }