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.

463 lines
20KB

  1. extern crate site;
  2. extern crate tempfile;
  3. use std::collections::HashMap;
  4. use std::env;
  5. use std::path::Path;
  6. use std::fs::File;
  7. use std::io::prelude::*;
  8. use tempfile::tempdir;
  9. use site::Site;
  10. #[test]
  11. fn can_parse_site() {
  12. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  13. path.push("test_site");
  14. let mut site = Site::new(&path, "config.toml").unwrap();
  15. site.load().unwrap();
  16. // Correct number of pages (sections are pages too)
  17. assert_eq!(site.pages.len(), 15);
  18. let posts_path = path.join("content").join("posts");
  19. // Make sure we remove all the pwd + content from the sections
  20. let basic = &site.pages[&posts_path.join("simple.md")];
  21. assert_eq!(basic.file.components, vec!["posts".to_string()]);
  22. // Make sure the page with a url doesn't have any sections
  23. let url_post = &site.pages[&posts_path.join("fixed-url.md")];
  24. assert_eq!(url_post.path, "a-fixed-url/");
  25. // Make sure the article in a folder with only asset doesn't get counted as a section
  26. let asset_folder_post = &site.pages[&posts_path.join("with-assets").join("index.md")];
  27. assert_eq!(asset_folder_post.file.components, vec!["posts".to_string()]);
  28. // That we have the right number of sections
  29. assert_eq!(site.sections.len(), 7);
  30. // And that the sections are correct
  31. let index_section = &site.sections[&path.join("content").join("_index.md")];
  32. assert_eq!(index_section.subsections.len(), 3);
  33. assert_eq!(index_section.pages.len(), 1);
  34. let posts_section = &site.sections[&posts_path.join("_index.md")];
  35. assert_eq!(posts_section.subsections.len(), 1);
  36. assert_eq!(posts_section.pages.len(), 7);
  37. let tutorials_section = &site.sections[&posts_path.join("tutorials").join("_index.md")];
  38. assert_eq!(tutorials_section.subsections.len(), 2);
  39. assert_eq!(tutorials_section.subsections[0].clone().meta.title.unwrap(), "Programming");
  40. assert_eq!(tutorials_section.subsections[1].clone().meta.title.unwrap(), "DevOps");
  41. assert_eq!(tutorials_section.pages.len(), 0);
  42. let devops_section = &site.sections[&posts_path.join("tutorials").join("devops").join("_index.md")];
  43. assert_eq!(devops_section.subsections.len(), 0);
  44. assert_eq!(devops_section.pages.len(), 2);
  45. let prog_section = &site.sections[&posts_path.join("tutorials").join("programming").join("_index.md")];
  46. assert_eq!(prog_section.subsections.len(), 0);
  47. assert_eq!(prog_section.pages.len(), 2);
  48. }
  49. // 2 helper macros to make all the build testing more bearable
  50. macro_rules! file_exists {
  51. ($root: expr, $path: expr) => {
  52. {
  53. let mut path = $root.clone();
  54. for component in $path.split("/") {
  55. path = path.join(component);
  56. }
  57. Path::new(&path).exists()
  58. }
  59. }
  60. }
  61. macro_rules! file_contains {
  62. ($root: expr, $path: expr, $text: expr) => {
  63. {
  64. let mut path = $root.clone();
  65. for component in $path.split("/") {
  66. path = path.join(component);
  67. }
  68. let mut file = File::open(&path).unwrap();
  69. let mut s = String::new();
  70. file.read_to_string(&mut s).unwrap();
  71. println!("{}", s);
  72. s.contains($text)
  73. }
  74. }
  75. }
  76. #[test]
  77. fn can_build_site_without_live_reload() {
  78. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  79. path.push("test_site");
  80. let mut site = Site::new(&path, "config.toml").unwrap();
  81. site.load().unwrap();
  82. let tmp_dir = tempdir().expect("create temp dir");
  83. let public = &tmp_dir.path().join("public");
  84. site.set_output_path(&public);
  85. site.build().unwrap();
  86. assert!(&public.exists());
  87. assert!(file_exists!(public, "index.html"));
  88. assert!(file_exists!(public, "sitemap.xml"));
  89. assert!(file_exists!(public, "robots.txt"));
  90. assert!(file_exists!(public, "a-fixed-url/index.html"));
  91. assert!(file_exists!(public, "posts/python/index.html"));
  92. // Shortcodes work
  93. assert!(file_contains!(public, "posts/python/index.html", "Basic shortcode"));
  94. assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob"));
  95. assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob_Sponge"));
  96. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  97. assert!(file_exists!(public, "posts/with-assets/index.html"));
  98. assert!(file_exists!(public, "posts/no-section/simple/index.html"));
  99. // Sections
  100. assert!(file_exists!(public, "posts/index.html"));
  101. assert!(file_exists!(public, "posts/tutorials/index.html"));
  102. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  103. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  104. // Ensure subsection pages are correctly filled
  105. assert!(file_contains!(public, "posts/tutorials/index.html", "Sub-pages: 2"));
  106. // TODO: add assertion for syntax highlighting
  107. // aliases work
  108. assert!(file_exists!(public, "an-old-url/old-page/index.html"));
  109. assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
  110. // html aliases work
  111. assert!(file_exists!(public, "an-old-url/an-old-alias.html"));
  112. assert!(file_contains!(public, "an-old-url/an-old-alias.html", "something-else"));
  113. // redirect_to works
  114. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  115. assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker"));
  116. // No tags or categories
  117. assert_eq!(file_exists!(public, "categories/index.html"), false);
  118. assert_eq!(file_exists!(public, "tags/index.html"), false);
  119. // Theme files are there
  120. assert!(file_exists!(public, "sample.css"));
  121. assert!(file_exists!(public, "some.js"));
  122. // SASS and SCSS files compile correctly
  123. assert!(file_exists!(public, "blog.css"));
  124. assert!(file_contains!(public, "blog.css", "red"));
  125. assert!(file_contains!(public, "blog.css", "blue"));
  126. assert!(!file_contains!(public, "blog.css", "@import \"included\""));
  127. assert!(file_contains!(public, "blog.css", "2rem")); // check include
  128. assert!(!file_exists!(public, "_included.css"));
  129. assert!(file_exists!(public, "scss.css"));
  130. assert!(file_exists!(public, "sass.css"));
  131. assert!(file_exists!(public, "nested_sass/sass.css"));
  132. assert!(file_exists!(public, "nested_sass/scss.css"));
  133. // no live reload code
  134. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  135. // Both pages and sections are in the sitemap
  136. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
  137. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
  138. // Drafts are not in the sitemap
  139. assert!(!file_contains!(public, "sitemap.xml", "draft"));
  140. // robots.txt has been rendered from the template
  141. assert!(file_contains!(public, "robots.txt", "User-agent: gutenberg"));
  142. assert!(file_contains!(public, "robots.txt", "Sitemap: https://replace-this-with-your-url.com/sitemap.xml"));
  143. }
  144. #[test]
  145. fn can_build_site_with_live_reload() {
  146. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  147. path.push("test_site");
  148. let mut site = Site::new(&path, "config.toml").unwrap();
  149. site.load().unwrap();
  150. let tmp_dir = tempdir().expect("create temp dir");
  151. let public = &tmp_dir.path().join("public");
  152. site.set_output_path(&public);
  153. site.enable_live_reload();
  154. site.build().unwrap();
  155. assert!(Path::new(&public).exists());
  156. assert!(file_exists!(public, "index.html"));
  157. assert!(file_exists!(public, "sitemap.xml"));
  158. assert!(file_exists!(public, "robots.txt"));
  159. assert!(file_exists!(public, "a-fixed-url/index.html"));
  160. assert!(file_exists!(public, "posts/python/index.html"));
  161. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  162. assert!(file_exists!(public, "posts/with-assets/index.html"));
  163. // Sections
  164. assert!(file_exists!(public, "posts/index.html"));
  165. assert!(file_exists!(public, "posts/tutorials/index.html"));
  166. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  167. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  168. // TODO: add assertion for syntax highlighting
  169. // No tags or categories
  170. assert_eq!(file_exists!(public, "categories/index.html"), false);
  171. assert_eq!(file_exists!(public, "tags/index.html"), false);
  172. // no live reload code
  173. assert!(file_contains!(public, "index.html", "/livereload.js"));
  174. // the summary anchor link has been created
  175. assert!(file_contains!(public, "posts/python/index.html", r#"<a name="continue-reading"></a>"#));
  176. assert!(file_contains!(public, "posts/draft/index.html", r#"THEME_SHORTCODE"#));
  177. }
  178. #[test]
  179. fn can_build_site_with_taxonomies() {
  180. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  181. path.push("test_site");
  182. let mut site = Site::new(&path, "config.toml").unwrap();
  183. site.load().unwrap();
  184. for (i, page) in site.pages.values_mut().enumerate() {
  185. page.meta.taxonomies = {
  186. let mut taxonomies = HashMap::new();
  187. taxonomies.insert("categories".to_string(), vec![if i % 2 == 0 { "A" } else { "B" }.to_string()]);
  188. taxonomies
  189. };
  190. }
  191. site.populate_taxonomies().unwrap();
  192. let tmp_dir = tempdir().expect("create temp dir");
  193. let public = &tmp_dir.path().join("public");
  194. site.set_output_path(&public);
  195. site.build().unwrap();
  196. assert!(Path::new(&public).exists());
  197. assert_eq!(site.taxonomies.len(), 1);
  198. assert!(file_exists!(public, "index.html"));
  199. assert!(file_exists!(public, "sitemap.xml"));
  200. assert!(file_exists!(public, "robots.txt"));
  201. assert!(file_exists!(public, "a-fixed-url/index.html"));
  202. assert!(file_exists!(public, "posts/python/index.html"));
  203. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  204. assert!(file_exists!(public, "posts/with-assets/index.html"));
  205. // Sections
  206. assert!(file_exists!(public, "posts/index.html"));
  207. assert!(file_exists!(public, "posts/tutorials/index.html"));
  208. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  209. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  210. // Categories are there
  211. assert!(file_exists!(public, "categories/index.html"));
  212. assert!(file_exists!(public, "categories/a/index.html"));
  213. assert!(file_exists!(public, "categories/b/index.html"));
  214. assert!(file_exists!(public, "categories/a/rss.xml"));
  215. assert!(file_contains!(public, "categories/a/rss.xml", "https://replace-this-with-your-url.com/categories/a/rss.xml"));
  216. // Extending from a theme works
  217. assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
  218. // Tags aren't
  219. assert_eq!(file_exists!(public, "tags/index.html"), false);
  220. // Categories are in the sitemap
  221. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/</loc>"));
  222. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"));
  223. }
  224. #[test]
  225. fn can_build_site_and_insert_anchor_links() {
  226. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  227. path.push("test_site");
  228. let mut site = Site::new(&path, "config.toml").unwrap();
  229. site.load().unwrap();
  230. let tmp_dir = tempdir().expect("create temp dir");
  231. let public = &tmp_dir.path().join("public");
  232. site.set_output_path(&public);
  233. site.build().unwrap();
  234. assert!(Path::new(&public).exists());
  235. // anchor link inserted
  236. assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"gutenberg-anchor\" href=\"#title\""));
  237. }
  238. #[test]
  239. fn can_build_site_with_pagination_for_section() {
  240. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  241. path.push("test_site");
  242. let mut site = Site::new(&path, "config.toml").unwrap();
  243. site.load().unwrap();
  244. for section in site.sections.values_mut(){
  245. if section.is_index() {
  246. continue;
  247. }
  248. section.meta.paginate_by = Some(2);
  249. section.meta.template = Some("section_paginated.html".to_string());
  250. }
  251. let tmp_dir = tempdir().expect("create temp dir");
  252. let public = &tmp_dir.path().join("public");
  253. site.set_output_path(&public);
  254. site.build().unwrap();
  255. assert!(Path::new(&public).exists());
  256. assert!(file_exists!(public, "index.html"));
  257. assert!(file_exists!(public, "sitemap.xml"));
  258. assert!(file_exists!(public, "robots.txt"));
  259. assert!(file_exists!(public, "a-fixed-url/index.html"));
  260. assert!(file_exists!(public, "posts/python/index.html"));
  261. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  262. assert!(file_exists!(public, "posts/with-assets/index.html"));
  263. // Sections
  264. assert!(file_exists!(public, "posts/index.html"));
  265. // And pagination!
  266. assert!(file_exists!(public, "posts/page/1/index.html"));
  267. // even if there is no pages, only the section!
  268. assert!(file_exists!(public, "paginated/page/1/index.html"));
  269. assert!(file_exists!(public, "paginated/index.html"));
  270. // should redirect to posts/
  271. assert!(file_contains!(
  272. public,
  273. "posts/page/1/index.html",
  274. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  275. ));
  276. assert!(file_contains!(public, "posts/index.html", "Num pagers: 4"));
  277. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  278. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  279. assert!(!file_contains!(public, "posts/index.html", "has_prev"));
  280. assert!(file_contains!(public, "posts/index.html", "has_next"));
  281. assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  282. assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  283. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  284. assert!(file_exists!(public, "posts/page/2/index.html"));
  285. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 4"));
  286. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  287. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  288. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  289. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  290. assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  291. assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  292. assert!(file_exists!(public, "posts/page/3/index.html"));
  293. assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 4"));
  294. assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
  295. assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
  296. assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
  297. assert!(file_contains!(public, "posts/page/3/index.html", "has_next"));
  298. assert!(file_contains!(public, "posts/page/3/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  299. assert!(file_contains!(public, "posts/page/3/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  300. assert!(file_exists!(public, "posts/page/4/index.html"));
  301. assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 4"));
  302. assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
  303. assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
  304. assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
  305. assert!(!file_contains!(public, "posts/page/4/index.html", "has_next"));
  306. assert!(file_contains!(public, "posts/page/4/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  307. assert!(file_contains!(public, "posts/page/4/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  308. }
  309. #[test]
  310. fn can_build_site_with_pagination_for_index() {
  311. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  312. path.push("test_site");
  313. let mut site = Site::new(&path, "config.toml").unwrap();
  314. site.load().unwrap();
  315. {
  316. let index = site.sections.get_mut(&path.join("content").join("_index.md")).unwrap();
  317. index.meta.paginate_by = Some(2);
  318. index.meta.template = Some("index_paginated.html".to_string());
  319. }
  320. let tmp_dir = tempdir().expect("create temp dir");
  321. let public = &tmp_dir.path().join("public");
  322. site.set_output_path(&public);
  323. site.build().unwrap();
  324. assert!(Path::new(&public).exists());
  325. assert!(file_exists!(public, "index.html"));
  326. assert!(file_exists!(public, "sitemap.xml"));
  327. assert!(file_exists!(public, "robots.txt"));
  328. assert!(file_exists!(public, "a-fixed-url/index.html"));
  329. assert!(file_exists!(public, "posts/python/index.html"));
  330. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  331. assert!(file_exists!(public, "posts/with-assets/index.html"));
  332. // And pagination!
  333. assert!(file_exists!(public, "page/1/index.html"));
  334. // even if there is no pages, only the section!
  335. assert!(file_exists!(public, "paginated/page/1/index.html"));
  336. assert!(file_exists!(public, "paginated/index.html"));
  337. // should redirect to index
  338. assert!(file_contains!(
  339. public,
  340. "page/1/index.html",
  341. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  342. ));
  343. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  344. assert!(file_contains!(public, "index.html", "Current index: 1"));
  345. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  346. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  347. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  348. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  349. }
  350. #[test]
  351. fn can_build_rss_feed() {
  352. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  353. path.push("test_site");
  354. let mut site = Site::new(&path, "config.toml").unwrap();
  355. site.load().unwrap();
  356. let tmp_dir = tempdir().expect("create temp dir");
  357. let public = &tmp_dir.path().join("public");
  358. site.set_output_path(&public);
  359. site.build().unwrap();
  360. assert!(Path::new(&public).exists());
  361. assert!(file_exists!(public, "rss.xml"));
  362. // latest article is posts/extra-syntax.md
  363. assert!(file_contains!(public, "rss.xml", "Extra Syntax"));
  364. // Next is posts/simple.md
  365. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  366. }
  367. #[test]
  368. fn can_build_search_index() {
  369. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  370. path.push("test_site");
  371. let mut site = Site::new(&path, "config.toml").unwrap();
  372. site.load().unwrap();
  373. site.config.build_search_index = true;
  374. let tmp_dir = tempdir().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, "elasticlunr.min.js"));
  380. assert!(file_exists!(public, "search_index.en.js"));
  381. }
  382. #[test]
  383. fn can_build_with_extra_syntaxes() {
  384. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  385. path.push("test_site");
  386. let mut site = Site::new(&path, "config.toml").unwrap();
  387. site.load().unwrap();
  388. let tmp_dir = tempdir().expect("create temp dir");
  389. let public = &tmp_dir.path().join("public");
  390. site.set_output_path(&public);
  391. site.build().unwrap();
  392. assert!(&public.exists());
  393. assert!(file_exists!(public, "posts/extra-syntax/index.html"));
  394. assert!(file_contains!(public, "posts/extra-syntax/index.html",
  395. r#"<span style="color:#d08770;">test</span>"#));
  396. }