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.

474 lines
20KB

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