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.

259 lines
9.7KB

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