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.

466 lines
19KB

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