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.

469 lines
20KB

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