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.

433 lines
18KB

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