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.

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