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
21KB

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