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.

253 lines
9.0KB

  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. let mut i = 0;
  136. for (_, page) in &mut site.pages {
  137. page.meta.category = if i % 2 == 0 {
  138. Some("A".to_string())
  139. } else {
  140. Some("B".to_string())
  141. };
  142. i += 1;
  143. }
  144. let tmp_dir = TempDir::new("example").expect("create temp dir");
  145. let public = &tmp_dir.path().join("public");
  146. site.set_output_path(&public);
  147. site.build().unwrap();
  148. assert!(Path::new(&public).exists());
  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. }
  168. #[test]
  169. fn test_can_build_site_with_tags() {
  170. let mut path = env::current_dir().unwrap().to_path_buf();
  171. path.push("test_site");
  172. let mut site = Site::new(&path).unwrap();
  173. let mut i = 0;
  174. for (_, page) in &mut site.pages {
  175. page.meta.tags = if i % 2 == 0 {
  176. Some(vec!["tag1".to_string(), "tag2".to_string()])
  177. } else {
  178. Some(vec!["tag with space".to_string()])
  179. };
  180. i += 1;
  181. }
  182. let tmp_dir = TempDir::new("example").expect("create temp dir");
  183. let public = &tmp_dir.path().join("public");
  184. site.set_output_path(&public);
  185. site.build().unwrap();
  186. assert!(Path::new(&public).exists());
  187. assert!(file_exists!(public, "index.html"));
  188. assert!(file_exists!(public, "sitemap.xml"));
  189. assert!(file_exists!(public, "a-fixed-url/index.html"));
  190. assert!(file_exists!(public, "posts/python/index.html"));
  191. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  192. assert!(file_exists!(public, "posts/with-assets/index.html"));
  193. // Sections
  194. assert!(file_exists!(public, "posts/index.html"));
  195. assert!(file_exists!(public, "posts/tutorials/index.html"));
  196. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  197. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  198. // TODO: add assertion for syntax highlighting
  199. // Tags are there
  200. assert!(file_exists!(public, "tags/index.html"));
  201. assert!(file_exists!(public, "tags/tag1/index.html"));
  202. assert!(file_exists!(public, "tags/tag2/index.html"));
  203. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  204. // Categories aren't
  205. assert_eq!(file_exists!(public, "categories/index.html"), false);
  206. }