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.

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