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.

421 lines
17KB

  1. extern crate site;
  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 site::Site;
  10. #[test]
  11. fn 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(), 11);
  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.file.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_eq!(url_post.path, "a-fixed-url/");
  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.file.components, vec!["posts".to_string()]);
  28. // That we have the right number of sections
  29. assert_eq!(site.sections.len(), 6);
  30. // And that the sections are correct
  31. let index_section = &site.sections[&path.join("content").join("_index.md")];
  32. assert_eq!(index_section.subsections.len(), 2);
  33. assert_eq!(index_section.pages.len(), 1);
  34. let posts_section = &site.sections[&posts_path.join("_index.md")];
  35. assert_eq!(posts_section.subsections.len(), 1);
  36. assert_eq!(posts_section.pages.len(), 5);
  37. let tutorials_section = &site.sections[&posts_path.join("tutorials").join("_index.md")];
  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").join("_index.md")];
  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").join("_index.md")];
  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. println!("{}", s);
  70. s.contains($text)
  71. }
  72. }
  73. }
  74. #[test]
  75. fn can_build_site_without_live_reload() {
  76. let mut path = env::current_dir().unwrap().to_path_buf();
  77. path.push("test_site");
  78. let mut site = Site::new(&path, "config.toml").unwrap();
  79. site.load().unwrap();
  80. let tmp_dir = TempDir::new("example").expect("create temp dir");
  81. let public = &tmp_dir.path().join("public");
  82. site.set_output_path(&public);
  83. site.build().unwrap();
  84. assert!(Path::new(&public).exists());
  85. assert!(file_exists!(public, "index.html"));
  86. assert!(file_exists!(public, "sitemap.xml"));
  87. assert!(file_exists!(public, "robots.txt"));
  88. assert!(file_exists!(public, "a-fixed-url/index.html"));
  89. assert!(file_exists!(public, "posts/python/index.html"));
  90. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  91. assert!(file_exists!(public, "posts/with-assets/index.html"));
  92. assert!(file_exists!(public, "posts/no-section/simple/index.html"));
  93. // Sections
  94. assert!(file_exists!(public, "posts/index.html"));
  95. assert!(file_exists!(public, "posts/tutorials/index.html"));
  96. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  97. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  98. // TODO: add assertion for syntax highlighting
  99. // aliases work
  100. assert!(file_exists!(public, "an-old-url/old-page/index.html"));
  101. assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
  102. // redirect_to works
  103. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  104. assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker"));
  105. // No tags or categories
  106. assert_eq!(file_exists!(public, "categories/index.html"), false);
  107. assert_eq!(file_exists!(public, "tags/index.html"), false);
  108. // no live reload code
  109. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  110. // Both pages and sections are in the sitemap
  111. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
  112. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
  113. }
  114. #[test]
  115. fn can_build_site_with_live_reload() {
  116. let mut path = env::current_dir().unwrap().to_path_buf();
  117. path.push("test_site");
  118. let mut site = Site::new(&path, "config.toml").unwrap();
  119. site.load().unwrap();
  120. let tmp_dir = TempDir::new("example").expect("create temp dir");
  121. let public = &tmp_dir.path().join("public");
  122. site.set_output_path(&public);
  123. site.enable_live_reload();
  124. site.build().unwrap();
  125. assert!(Path::new(&public).exists());
  126. assert!(file_exists!(public, "index.html"));
  127. assert!(file_exists!(public, "sitemap.xml"));
  128. assert!(file_exists!(public, "robots.txt"));
  129. assert!(file_exists!(public, "a-fixed-url/index.html"));
  130. assert!(file_exists!(public, "posts/python/index.html"));
  131. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  132. assert!(file_exists!(public, "posts/with-assets/index.html"));
  133. // Sections
  134. assert!(file_exists!(public, "posts/index.html"));
  135. assert!(file_exists!(public, "posts/tutorials/index.html"));
  136. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  137. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  138. // TODO: add assertion for syntax highlighting
  139. // No tags or categories
  140. assert_eq!(file_exists!(public, "categories/index.html"), false);
  141. assert_eq!(file_exists!(public, "tags/index.html"), false);
  142. // no live reload code
  143. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  144. }
  145. #[test]
  146. fn can_build_site_with_categories() {
  147. let mut path = env::current_dir().unwrap().to_path_buf();
  148. path.push("test_site");
  149. let mut site = Site::new(&path, "config.toml").unwrap();
  150. site.config.generate_categories_pages = Some(true);
  151. site.load().unwrap();
  152. for (i, page) in site.pages.values_mut().enumerate() {
  153. page.meta.category = if i % 2 == 0 {
  154. Some("A".to_string())
  155. } else {
  156. Some("B".to_string())
  157. };
  158. }
  159. site.populate_tags_and_categories();
  160. let tmp_dir = TempDir::new("example").expect("create temp dir");
  161. let public = &tmp_dir.path().join("public");
  162. site.set_output_path(&public);
  163. site.build().unwrap();
  164. assert!(Path::new(&public).exists());
  165. assert_eq!(site.categories.unwrap().len(), 2);
  166. assert!(file_exists!(public, "index.html"));
  167. assert!(file_exists!(public, "sitemap.xml"));
  168. assert!(file_exists!(public, "robots.txt"));
  169. assert!(file_exists!(public, "a-fixed-url/index.html"));
  170. assert!(file_exists!(public, "posts/python/index.html"));
  171. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  172. assert!(file_exists!(public, "posts/with-assets/index.html"));
  173. // Sections
  174. assert!(file_exists!(public, "posts/index.html"));
  175. assert!(file_exists!(public, "posts/tutorials/index.html"));
  176. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  177. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  178. // TODO: add assertion for syntax highlighting
  179. // Categories are there
  180. assert!(file_exists!(public, "categories/index.html"));
  181. assert!(file_exists!(public, "categories/a/index.html"));
  182. assert!(file_exists!(public, "categories/b/index.html"));
  183. // Tags aren't
  184. assert_eq!(file_exists!(public, "tags/index.html"), false);
  185. // Categories are in the sitemap
  186. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/</loc>"));
  187. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"));
  188. }
  189. #[test]
  190. fn can_build_site_with_tags() {
  191. let mut path = env::current_dir().unwrap().to_path_buf();
  192. path.push("test_site");
  193. let mut site = Site::new(&path, "config.toml").unwrap();
  194. site.config.generate_tags_pages = Some(true);
  195. site.load().unwrap();
  196. for (i, page) in site.pages.values_mut().enumerate() {
  197. page.meta.tags = if i % 2 == 0 {
  198. Some(vec!["tag1".to_string(), "tag2".to_string()])
  199. } else {
  200. Some(vec!["tag with space".to_string()])
  201. };
  202. }
  203. site.populate_tags_and_categories();
  204. let tmp_dir = TempDir::new("example").expect("create temp dir");
  205. let public = &tmp_dir.path().join("public");
  206. site.set_output_path(&public);
  207. site.build().unwrap();
  208. assert!(Path::new(&public).exists());
  209. assert_eq!(site.tags.unwrap().len(), 3);
  210. assert!(file_exists!(public, "index.html"));
  211. assert!(file_exists!(public, "sitemap.xml"));
  212. assert!(file_exists!(public, "robots.txt"));
  213. assert!(file_exists!(public, "a-fixed-url/index.html"));
  214. assert!(file_exists!(public, "posts/python/index.html"));
  215. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  216. assert!(file_exists!(public, "posts/with-assets/index.html"));
  217. // Sections
  218. assert!(file_exists!(public, "posts/index.html"));
  219. assert!(file_exists!(public, "posts/tutorials/index.html"));
  220. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  221. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  222. // TODO: add assertion for syntax highlighting
  223. // Tags are there
  224. assert!(file_exists!(public, "tags/index.html"));
  225. assert!(file_exists!(public, "tags/tag1/index.html"));
  226. assert!(file_exists!(public, "tags/tag2/index.html"));
  227. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  228. // Categories aren't
  229. assert_eq!(file_exists!(public, "categories/index.html"), false);
  230. // Tags are in the sitemap
  231. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/</loc>"));
  232. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space/</loc>"));
  233. }
  234. #[test]
  235. fn can_build_site_and_insert_anchor_links() {
  236. let mut path = env::current_dir().unwrap().to_path_buf();
  237. path.push("test_site");
  238. let mut site = Site::new(&path, "config.toml").unwrap();
  239. site.load().unwrap();
  240. let tmp_dir = TempDir::new("example").expect("create temp dir");
  241. let public = &tmp_dir.path().join("public");
  242. site.set_output_path(&public);
  243. site.build().unwrap();
  244. assert!(Path::new(&public).exists());
  245. // anchor link inserted
  246. assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"gutenberg-anchor\" href=\"#title\""));
  247. }
  248. #[test]
  249. fn can_build_site_with_pagination_for_section() {
  250. let mut path = env::current_dir().unwrap().to_path_buf();
  251. path.push("test_site");
  252. let mut site = Site::new(&path, "config.toml").unwrap();
  253. site.load().unwrap();
  254. for section in site.sections.values_mut(){
  255. if section.is_index() {
  256. continue;
  257. }
  258. section.meta.paginate_by = Some(2);
  259. section.meta.template = Some("section_paginated.html".to_string());
  260. }
  261. let tmp_dir = TempDir::new("example").expect("create temp dir");
  262. let public = &tmp_dir.path().join("public");
  263. site.set_output_path(&public);
  264. site.build().unwrap();
  265. assert!(Path::new(&public).exists());
  266. assert!(file_exists!(public, "index.html"));
  267. assert!(file_exists!(public, "sitemap.xml"));
  268. assert!(file_exists!(public, "robots.txt"));
  269. assert!(file_exists!(public, "a-fixed-url/index.html"));
  270. assert!(file_exists!(public, "posts/python/index.html"));
  271. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  272. assert!(file_exists!(public, "posts/with-assets/index.html"));
  273. // Sections
  274. assert!(file_exists!(public, "posts/index.html"));
  275. // And pagination!
  276. assert!(file_exists!(public, "posts/page/1/index.html"));
  277. // even if there is no pages, only the section!
  278. assert!(file_exists!(public, "paginated/page/1/index.html"));
  279. assert!(file_exists!(public, "paginated/index.html"));
  280. // should redirect to posts/
  281. assert!(file_contains!(
  282. public,
  283. "posts/page/1/index.html",
  284. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  285. ));
  286. assert!(file_contains!(public, "posts/index.html", "Num pagers: 3"));
  287. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  288. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  289. assert!(file_contains!(public, "posts/index.html", "has_next"));
  290. assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  291. assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  292. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  293. assert!(file_exists!(public, "posts/page/2/index.html"));
  294. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 3"));
  295. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  296. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  297. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  298. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  299. assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  300. assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  301. }
  302. #[test]
  303. fn can_build_site_with_pagination_for_index() {
  304. let mut path = env::current_dir().unwrap().to_path_buf();
  305. path.push("test_site");
  306. let mut site = Site::new(&path, "config.toml").unwrap();
  307. site.load().unwrap();
  308. {
  309. let mut index = site.sections.get_mut(&path.join("content").join("_index.md")).unwrap();
  310. index.meta.paginate_by = Some(2);
  311. index.meta.template = Some("index_paginated.html".to_string());
  312. }
  313. let tmp_dir = TempDir::new("example").expect("create temp dir");
  314. let public = &tmp_dir.path().join("public");
  315. site.set_output_path(&public);
  316. site.build().unwrap();
  317. assert!(Path::new(&public).exists());
  318. assert!(file_exists!(public, "index.html"));
  319. assert!(file_exists!(public, "sitemap.xml"));
  320. assert!(file_exists!(public, "robots.txt"));
  321. assert!(file_exists!(public, "a-fixed-url/index.html"));
  322. assert!(file_exists!(public, "posts/python/index.html"));
  323. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  324. assert!(file_exists!(public, "posts/with-assets/index.html"));
  325. // And pagination!
  326. assert!(file_exists!(public, "page/1/index.html"));
  327. // even if there is no pages, only the section!
  328. assert!(file_exists!(public, "paginated/page/1/index.html"));
  329. assert!(file_exists!(public, "paginated/index.html"));
  330. // should redirect to index
  331. assert!(file_contains!(
  332. public,
  333. "page/1/index.html",
  334. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  335. ));
  336. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  337. assert!(file_contains!(public, "index.html", "Current index: 1"));
  338. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  339. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  340. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  341. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  342. }
  343. #[test]
  344. fn can_build_rss_feed() {
  345. let mut path = env::current_dir().unwrap().to_path_buf();
  346. path.push("test_site");
  347. let mut site = Site::new(&path, "config.toml").unwrap();
  348. site.load().unwrap();
  349. let tmp_dir = TempDir::new("example").expect("create temp dir");
  350. let public = &tmp_dir.path().join("public");
  351. site.set_output_path(&public);
  352. site.build().unwrap();
  353. assert!(Path::new(&public).exists());
  354. assert!(file_exists!(public, "rss.xml"));
  355. // latest article is posts/simple.md
  356. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  357. // Next is posts/python.md
  358. assert!(file_contains!(public, "rss.xml", "Python in posts"));
  359. }