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.

432 lines
18KB

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