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.

689 lines
27KB

  1. extern crate config;
  2. extern crate site;
  3. extern crate tempfile;
  4. use std::collections::HashMap;
  5. use std::env;
  6. use std::fs::File;
  7. use std::io::prelude::*;
  8. use std::path::Path;
  9. use config::Taxonomy;
  10. use site::Site;
  11. use tempfile::tempdir;
  12. #[test]
  13. fn can_parse_site() {
  14. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  15. path.push("test_site");
  16. let mut site = Site::new(&path, "config.toml").unwrap();
  17. site.load().unwrap();
  18. // Correct number of pages (sections do not count as pages)
  19. assert_eq!(site.library.pages().len(), 22);
  20. let posts_path = path.join("content").join("posts");
  21. // Make sure the page with a url doesn't have any sections
  22. let url_post = site.library.get_page(&posts_path.join("fixed-url.md")).unwrap();
  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 =
  26. site.library.get_page(&posts_path.join("with-assets").join("index.md")).unwrap();
  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.library.sections().len(), 11);
  30. // And that the sections are correct
  31. let index_section = site.library.get_section(&path.join("content").join("_index.md")).unwrap();
  32. assert_eq!(index_section.subsections.len(), 4);
  33. assert_eq!(index_section.pages.len(), 1);
  34. assert!(index_section.ancestors.is_empty());
  35. let posts_section = site.library.get_section(&posts_path.join("_index.md")).unwrap();
  36. assert_eq!(posts_section.subsections.len(), 2);
  37. assert_eq!(posts_section.pages.len(), 10);
  38. assert_eq!(
  39. posts_section.ancestors,
  40. vec![*site.library.get_section_key(&index_section.file.path).unwrap()]
  41. );
  42. // Make sure we remove all the pwd + content from the sections
  43. let basic = site.library.get_page(&posts_path.join("simple.md")).unwrap();
  44. assert_eq!(basic.file.components, vec!["posts".to_string()]);
  45. assert_eq!(
  46. basic.ancestors,
  47. vec![
  48. *site.library.get_section_key(&index_section.file.path).unwrap(),
  49. *site.library.get_section_key(&posts_section.file.path).unwrap(),
  50. ]
  51. );
  52. let tutorials_section =
  53. site.library.get_section(&posts_path.join("tutorials").join("_index.md")).unwrap();
  54. assert_eq!(tutorials_section.subsections.len(), 2);
  55. let sub1 = site.library.get_section_by_key(tutorials_section.subsections[0]);
  56. let sub2 = site.library.get_section_by_key(tutorials_section.subsections[1]);
  57. assert_eq!(sub1.clone().meta.title.unwrap(), "Programming");
  58. assert_eq!(sub2.clone().meta.title.unwrap(), "DevOps");
  59. assert_eq!(tutorials_section.pages.len(), 0);
  60. let devops_section = site
  61. .library
  62. .get_section(&posts_path.join("tutorials").join("devops").join("_index.md"))
  63. .unwrap();
  64. assert_eq!(devops_section.subsections.len(), 0);
  65. assert_eq!(devops_section.pages.len(), 2);
  66. assert_eq!(
  67. devops_section.ancestors,
  68. vec![
  69. *site.library.get_section_key(&index_section.file.path).unwrap(),
  70. *site.library.get_section_key(&posts_section.file.path).unwrap(),
  71. *site.library.get_section_key(&tutorials_section.file.path).unwrap(),
  72. ]
  73. );
  74. let prog_section = site
  75. .library
  76. .get_section(&posts_path.join("tutorials").join("programming").join("_index.md"))
  77. .unwrap();
  78. assert_eq!(prog_section.subsections.len(), 0);
  79. assert_eq!(prog_section.pages.len(), 2);
  80. }
  81. // 2 helper macros to make all the build testing more bearable
  82. macro_rules! file_exists {
  83. ($root: expr, $path: expr) => {{
  84. let mut path = $root.clone();
  85. for component in $path.split("/") {
  86. path = path.join(component);
  87. }
  88. Path::new(&path).exists()
  89. }};
  90. }
  91. macro_rules! file_contains {
  92. ($root: expr, $path: expr, $text: expr) => {{
  93. let mut path = $root.clone();
  94. for component in $path.split("/") {
  95. path = path.join(component);
  96. }
  97. let mut file = File::open(&path).unwrap();
  98. let mut s = String::new();
  99. file.read_to_string(&mut s).unwrap();
  100. println!("{}", s);
  101. s.contains($text)
  102. }};
  103. }
  104. #[test]
  105. fn can_build_site_without_live_reload() {
  106. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  107. path.push("test_site");
  108. let mut site = Site::new(&path, "config.toml").unwrap();
  109. site.load().unwrap();
  110. let tmp_dir = tempdir().expect("create temp dir");
  111. let public = &tmp_dir.path().join("public");
  112. site.set_output_path(&public);
  113. site.build().unwrap();
  114. assert!(&public.exists());
  115. assert!(file_exists!(public, "index.html"));
  116. assert!(file_exists!(public, "sitemap.xml"));
  117. assert!(file_exists!(public, "robots.txt"));
  118. assert!(file_exists!(public, "a-fixed-url/index.html"));
  119. assert!(file_exists!(public, "posts/python/index.html"));
  120. // Shortcodes work
  121. assert!(file_contains!(public, "posts/python/index.html", "Basic shortcode"));
  122. assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob"));
  123. assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob_Sponge"));
  124. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  125. assert!(file_exists!(public, "posts/with-assets/index.html"));
  126. assert!(file_exists!(public, "posts/no-section/simple/index.html"));
  127. // Sections
  128. assert!(file_exists!(public, "posts/index.html"));
  129. assert!(file_exists!(public, "posts/tutorials/index.html"));
  130. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  131. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  132. // Ensure subsection pages are correctly filled
  133. assert!(file_contains!(public, "posts/tutorials/index.html", "Sub-pages: 2"));
  134. // Pages and section get their relative path
  135. assert!(file_contains!(public, "posts/tutorials/index.html", "posts/tutorials/_index.md"));
  136. assert!(file_contains!(
  137. public,
  138. "posts/tutorials/devops/nix/index.html",
  139. "posts/tutorials/devops/nix.md"
  140. ));
  141. // aliases work
  142. assert!(file_exists!(public, "an-old-url/old-page/index.html"));
  143. assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
  144. // html aliases work
  145. assert!(file_exists!(public, "an-old-url/an-old-alias.html"));
  146. assert!(file_contains!(public, "an-old-url/an-old-alias.html", "something-else"));
  147. // redirect_to works
  148. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  149. assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker"));
  150. // We do have categories
  151. assert_eq!(file_exists!(public, "categories/index.html"), true);
  152. assert_eq!(file_exists!(public, "categories/a-category/index.html"), true);
  153. assert_eq!(file_exists!(public, "categories/a-category/rss.xml"), true);
  154. // But no tags
  155. assert_eq!(file_exists!(public, "tags/index.html"), false);
  156. // Theme files are there
  157. assert!(file_exists!(public, "sample.css"));
  158. assert!(file_exists!(public, "some.js"));
  159. // SASS and SCSS files compile correctly
  160. assert!(file_exists!(public, "blog.css"));
  161. assert!(file_contains!(public, "blog.css", "red"));
  162. assert!(file_contains!(public, "blog.css", "blue"));
  163. assert!(!file_contains!(public, "blog.css", "@import \"included\""));
  164. assert!(file_contains!(public, "blog.css", "2rem")); // check include
  165. assert!(!file_exists!(public, "_included.css"));
  166. assert!(file_exists!(public, "scss.css"));
  167. assert!(file_exists!(public, "sass.css"));
  168. assert!(file_exists!(public, "nested_sass/sass.css"));
  169. assert!(file_exists!(public, "nested_sass/scss.css"));
  170. // no live reload code
  171. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  172. // Both pages and sections are in the sitemap
  173. assert!(file_contains!(
  174. public,
  175. "sitemap.xml",
  176. "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"
  177. ));
  178. assert!(file_contains!(
  179. public,
  180. "sitemap.xml",
  181. "<loc>https://replace-this-with-your-url.com/posts/</loc>"
  182. ));
  183. // Drafts are not in the sitemap
  184. assert!(!file_contains!(public, "sitemap.xml", "draft"));
  185. // robots.txt has been rendered from the template
  186. assert!(file_contains!(public, "robots.txt", "User-agent: zola"));
  187. assert!(file_contains!(
  188. public,
  189. "robots.txt",
  190. "Sitemap: https://replace-this-with-your-url.com/sitemap.xml"
  191. ));
  192. }
  193. #[test]
  194. fn can_build_site_with_live_reload() {
  195. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  196. path.push("test_site");
  197. let mut site = Site::new(&path, "config.toml").unwrap();
  198. site.load().unwrap();
  199. let tmp_dir = tempdir().expect("create temp dir");
  200. let public = &tmp_dir.path().join("public");
  201. site.set_output_path(&public);
  202. site.enable_live_reload(1000);
  203. site.build().unwrap();
  204. assert!(Path::new(&public).exists());
  205. assert!(file_exists!(public, "index.html"));
  206. assert!(file_exists!(public, "sitemap.xml"));
  207. assert!(file_exists!(public, "robots.txt"));
  208. assert!(file_exists!(public, "a-fixed-url/index.html"));
  209. assert!(file_exists!(public, "posts/python/index.html"));
  210. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  211. assert!(file_exists!(public, "posts/with-assets/index.html"));
  212. // Sections
  213. assert!(file_exists!(public, "posts/index.html"));
  214. assert!(file_exists!(public, "posts/tutorials/index.html"));
  215. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  216. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  217. // TODO: add assertion for syntax highlighting
  218. // We do have categories
  219. assert_eq!(file_exists!(public, "categories/index.html"), true);
  220. assert_eq!(file_exists!(public, "categories/a-category/index.html"), true);
  221. assert_eq!(file_exists!(public, "categories/a-category/rss.xml"), true);
  222. // But no tags
  223. assert_eq!(file_exists!(public, "tags/index.html"), false);
  224. // no live reload code
  225. assert!(file_contains!(public, "index.html", "/livereload.js"));
  226. // the summary anchor link has been created
  227. assert!(file_contains!(
  228. public,
  229. "posts/python/index.html",
  230. r#"<a name="continue-reading"></a>"#
  231. ));
  232. assert!(file_contains!(public, "posts/draft/index.html", r#"THEME_SHORTCODE"#));
  233. }
  234. #[test]
  235. fn can_build_site_with_taxonomies() {
  236. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  237. path.push("test_site");
  238. let mut site = Site::new(&path, "config.toml").unwrap();
  239. site.load().unwrap();
  240. for (i, (_, page)) in site.library.pages_mut().iter_mut().enumerate() {
  241. page.meta.taxonomies = {
  242. let mut taxonomies = HashMap::new();
  243. taxonomies.insert(
  244. "categories".to_string(),
  245. vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
  246. );
  247. taxonomies
  248. };
  249. }
  250. site.populate_taxonomies().unwrap();
  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_eq!(site.taxonomies.len(), 1);
  257. assert!(file_exists!(public, "index.html"));
  258. assert!(file_exists!(public, "sitemap.xml"));
  259. assert!(file_exists!(public, "robots.txt"));
  260. assert!(file_exists!(public, "a-fixed-url/index.html"));
  261. assert!(file_exists!(public, "posts/python/index.html"));
  262. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  263. assert!(file_exists!(public, "posts/with-assets/index.html"));
  264. // Sections
  265. assert!(file_exists!(public, "posts/index.html"));
  266. assert!(file_exists!(public, "posts/tutorials/index.html"));
  267. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  268. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  269. // Categories are there
  270. assert!(file_exists!(public, "categories/index.html"));
  271. assert!(file_exists!(public, "categories/a/index.html"));
  272. assert!(file_exists!(public, "categories/b/index.html"));
  273. assert!(file_exists!(public, "categories/a/rss.xml"));
  274. assert!(file_contains!(
  275. public,
  276. "categories/a/rss.xml",
  277. "https://replace-this-with-your-url.com/categories/a/rss.xml"
  278. ));
  279. // Extending from a theme works
  280. assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
  281. // Tags aren't
  282. assert_eq!(file_exists!(public, "tags/index.html"), false);
  283. // Categories are in the sitemap
  284. assert!(file_contains!(
  285. public,
  286. "sitemap.xml",
  287. "<loc>https://replace-this-with-your-url.com/categories/</loc>"
  288. ));
  289. assert!(file_contains!(
  290. public,
  291. "sitemap.xml",
  292. "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"
  293. ));
  294. }
  295. #[test]
  296. fn can_build_site_and_insert_anchor_links() {
  297. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  298. path.push("test_site");
  299. let mut site = Site::new(&path, "config.toml").unwrap();
  300. site.load().unwrap();
  301. let tmp_dir = tempdir().expect("create temp dir");
  302. let public = &tmp_dir.path().join("public");
  303. site.set_output_path(&public);
  304. site.build().unwrap();
  305. assert!(Path::new(&public).exists());
  306. // anchor link inserted
  307. assert!(file_contains!(
  308. public,
  309. "posts/something-else/index.html",
  310. "<h1 id=\"title\"><a class=\"zola-anchor\" href=\"#title\""
  311. ));
  312. }
  313. #[test]
  314. fn can_build_site_with_pagination_for_section() {
  315. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  316. path.push("test_site");
  317. let mut site = Site::new(&path, "config.toml").unwrap();
  318. site.load().unwrap();
  319. for (_, section) in site.library.sections_mut() {
  320. if section.is_index() {
  321. continue;
  322. }
  323. section.meta.paginate_by = Some(2);
  324. section.meta.template = Some("section_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. // Sections
  339. assert!(file_exists!(public, "posts/index.html"));
  340. // And pagination!
  341. assert!(file_exists!(public, "posts/page/1/index.html"));
  342. // even if there is no pages, only the section!
  343. assert!(file_exists!(public, "paginated/page/1/index.html"));
  344. assert!(file_exists!(public, "paginated/index.html"));
  345. // should redirect to posts/
  346. assert!(file_contains!(
  347. public,
  348. "posts/page/1/index.html",
  349. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  350. ));
  351. assert!(file_contains!(public, "posts/index.html", "Num pagers: 5"));
  352. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  353. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  354. assert!(!file_contains!(public, "posts/index.html", "has_prev"));
  355. assert!(file_contains!(public, "posts/index.html", "has_next"));
  356. assert!(file_contains!(
  357. public,
  358. "posts/index.html",
  359. "First: https://replace-this-with-your-url.com/posts/"
  360. ));
  361. assert!(file_contains!(
  362. public,
  363. "posts/index.html",
  364. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  365. ));
  366. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  367. assert!(file_exists!(public, "posts/page/2/index.html"));
  368. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 5"));
  369. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  370. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  371. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  372. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  373. assert!(file_contains!(
  374. public,
  375. "posts/page/2/index.html",
  376. "First: https://replace-this-with-your-url.com/posts/"
  377. ));
  378. assert!(file_contains!(
  379. public,
  380. "posts/page/2/index.html",
  381. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  382. ));
  383. assert!(file_exists!(public, "posts/page/3/index.html"));
  384. assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 5"));
  385. assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
  386. assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
  387. assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
  388. assert!(file_contains!(public, "posts/page/3/index.html", "has_next"));
  389. assert!(file_contains!(
  390. public,
  391. "posts/page/3/index.html",
  392. "First: https://replace-this-with-your-url.com/posts/"
  393. ));
  394. assert!(file_contains!(
  395. public,
  396. "posts/page/3/index.html",
  397. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  398. ));
  399. assert!(file_exists!(public, "posts/page/4/index.html"));
  400. assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 5"));
  401. assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
  402. assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
  403. assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
  404. assert!(file_contains!(public, "posts/page/4/index.html", "has_next"));
  405. assert!(file_contains!(
  406. public,
  407. "posts/page/4/index.html",
  408. "First: https://replace-this-with-your-url.com/posts/"
  409. ));
  410. assert!(file_contains!(
  411. public,
  412. "posts/page/4/index.html",
  413. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  414. ));
  415. }
  416. #[test]
  417. fn can_build_site_with_pagination_for_index() {
  418. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  419. path.push("test_site");
  420. let mut site = Site::new(&path, "config.toml").unwrap();
  421. site.load().unwrap();
  422. {
  423. let index = site.library.get_section_mut(&path.join("content").join("_index.md")).unwrap();
  424. index.meta.paginate_by = Some(2);
  425. index.meta.template = Some("index_paginated.html".to_string());
  426. }
  427. let tmp_dir = tempdir().expect("create temp dir");
  428. let public = &tmp_dir.path().join("public");
  429. site.set_output_path(&public);
  430. site.build().unwrap();
  431. assert!(Path::new(&public).exists());
  432. assert!(file_exists!(public, "index.html"));
  433. assert!(file_exists!(public, "sitemap.xml"));
  434. assert!(file_exists!(public, "robots.txt"));
  435. assert!(file_exists!(public, "a-fixed-url/index.html"));
  436. assert!(file_exists!(public, "posts/python/index.html"));
  437. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  438. assert!(file_exists!(public, "posts/with-assets/index.html"));
  439. // And pagination!
  440. assert!(file_exists!(public, "page/1/index.html"));
  441. // even if there is no pages, only the section!
  442. assert!(file_exists!(public, "paginated/page/1/index.html"));
  443. assert!(file_exists!(public, "paginated/index.html"));
  444. // should redirect to index
  445. assert!(file_contains!(
  446. public,
  447. "page/1/index.html",
  448. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  449. ));
  450. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  451. assert!(file_contains!(public, "index.html", "Current index: 1"));
  452. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  453. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  454. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  455. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  456. }
  457. #[test]
  458. fn can_build_site_with_pagination_for_taxonomy() {
  459. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  460. path.push("test_site");
  461. let mut site = Site::new(&path, "config.toml").unwrap();
  462. site.config.taxonomies.push(Taxonomy {
  463. name: "tags".to_string(),
  464. paginate_by: Some(2),
  465. paginate_path: None,
  466. rss: true,
  467. });
  468. site.load().unwrap();
  469. for (i, (_, page)) in site.library.pages_mut().iter_mut().enumerate() {
  470. page.meta.taxonomies = {
  471. let mut taxonomies = HashMap::new();
  472. taxonomies
  473. .insert("tags".to_string(), vec![if i % 2 == 0 { "A" } else { "B" }.to_string()]);
  474. taxonomies
  475. };
  476. }
  477. site.populate_taxonomies().unwrap();
  478. let tmp_dir = tempdir().expect("create temp dir");
  479. let public = &tmp_dir.path().join("public");
  480. site.set_output_path(&public);
  481. site.build().unwrap();
  482. assert!(Path::new(&public).exists());
  483. assert!(file_exists!(public, "index.html"));
  484. assert!(file_exists!(public, "sitemap.xml"));
  485. assert!(file_exists!(public, "robots.txt"));
  486. assert!(file_exists!(public, "a-fixed-url/index.html"));
  487. assert!(file_exists!(public, "posts/python/index.html"));
  488. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  489. assert!(file_exists!(public, "posts/with-assets/index.html"));
  490. // Tags
  491. assert!(file_exists!(public, "tags/index.html"));
  492. // With RSS
  493. assert!(file_exists!(public, "tags/a/rss.xml"));
  494. assert!(file_exists!(public, "tags/b/rss.xml"));
  495. // And pagination!
  496. assert!(file_exists!(public, "tags/a/pages/1/index.html"));
  497. assert!(file_exists!(public, "tags/b/pages/1/index.html"));
  498. assert!(file_exists!(public, "tags/a/pages/2/index.html"));
  499. assert!(file_exists!(public, "tags/b/pages/2/index.html"));
  500. // should redirect to posts/
  501. assert!(file_contains!(
  502. public,
  503. "tags/a/pages/1/index.html",
  504. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/tags/a/\""
  505. ));
  506. assert!(file_contains!(public, "tags/a/index.html", "Num pagers: 6"));
  507. assert!(file_contains!(public, "tags/a/index.html", "Page size: 2"));
  508. assert!(file_contains!(public, "tags/a/index.html", "Current index: 1"));
  509. assert!(!file_contains!(public, "tags/a/index.html", "has_prev"));
  510. assert!(file_contains!(public, "tags/a/index.html", "has_next"));
  511. assert!(file_contains!(
  512. public,
  513. "tags/a/index.html",
  514. "First: https://replace-this-with-your-url.com/tags/a/"
  515. ));
  516. assert!(file_contains!(
  517. public,
  518. "tags/a/index.html",
  519. "Last: https://replace-this-with-your-url.com/tags/a/pages/6/"
  520. ));
  521. assert_eq!(file_contains!(public, "tags/a/index.html", "has_prev"), false);
  522. }
  523. #[test]
  524. fn can_build_rss_feed() {
  525. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  526. path.push("test_site");
  527. let mut site = Site::new(&path, "config.toml").unwrap();
  528. site.load().unwrap();
  529. let tmp_dir = tempdir().expect("create temp dir");
  530. let public = &tmp_dir.path().join("public");
  531. site.set_output_path(&public);
  532. site.build().unwrap();
  533. assert!(Path::new(&public).exists());
  534. assert!(file_exists!(public, "rss.xml"));
  535. // latest article is posts/extra-syntax.md
  536. assert!(file_contains!(public, "rss.xml", "Extra Syntax"));
  537. // Next is posts/simple.md
  538. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  539. }
  540. #[test]
  541. fn can_build_search_index() {
  542. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  543. path.push("test_site");
  544. let mut site = Site::new(&path, "config.toml").unwrap();
  545. site.load().unwrap();
  546. site.config.build_search_index = true;
  547. let tmp_dir = tempdir().expect("create temp dir");
  548. let public = &tmp_dir.path().join("public");
  549. site.set_output_path(&public);
  550. site.build().unwrap();
  551. assert!(Path::new(&public).exists());
  552. assert!(file_exists!(public, "elasticlunr.min.js"));
  553. assert!(file_exists!(public, "search_index.en.js"));
  554. }
  555. #[test]
  556. fn can_build_with_extra_syntaxes() {
  557. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  558. path.push("test_site");
  559. let mut site = Site::new(&path, "config.toml").unwrap();
  560. site.load().unwrap();
  561. let tmp_dir = tempdir().expect("create temp dir");
  562. let public = &tmp_dir.path().join("public");
  563. site.set_output_path(&public);
  564. site.build().unwrap();
  565. assert!(&public.exists());
  566. assert!(file_exists!(public, "posts/extra-syntax/index.html"));
  567. assert!(file_contains!(
  568. public,
  569. "posts/extra-syntax/index.html",
  570. r#"<span style="color:#d08770;">test</span>"#
  571. ));
  572. }
  573. #[test]
  574. fn can_apply_page_templates() {
  575. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  576. path.push("test_site");
  577. let mut site = Site::new(&path, "config.toml").unwrap();
  578. site.load().unwrap();
  579. let template_path = path.join("content").join("applying_page_template");
  580. let template_section = site.library.get_section(&template_path.join("_index.md")).unwrap();
  581. assert_eq!(template_section.subsections.len(), 2);
  582. assert_eq!(template_section.pages.len(), 2);
  583. let from_section_config = site.library.get_page_by_key(template_section.pages[0]);
  584. assert_eq!(from_section_config.meta.template, Some("page_template.html".into()));
  585. assert_eq!(from_section_config.meta.title, Some("From section config".into()));
  586. let override_page_template = site.library.get_page_by_key(template_section.pages[1]);
  587. assert_eq!(override_page_template.meta.template, Some("page_template_override.html".into()));
  588. assert_eq!(override_page_template.meta.title, Some("Override".into()));
  589. // It should have applied recursively as well
  590. let another_section =
  591. site.library.get_section(&template_path.join("another_section").join("_index.md")).unwrap();
  592. assert_eq!(another_section.subsections.len(), 0);
  593. assert_eq!(another_section.pages.len(), 1);
  594. let changed_recursively = site.library.get_page_by_key(another_section.pages[0]);
  595. assert_eq!(changed_recursively.meta.template, Some("page_template.html".into()));
  596. assert_eq!(changed_recursively.meta.title, Some("Changed recursively".into()));
  597. // But it should not have override a children page_template
  598. let yet_another_section = site
  599. .library
  600. .get_section(&template_path.join("yet_another_section").join("_index.md"))
  601. .unwrap();
  602. assert_eq!(yet_another_section.subsections.len(), 0);
  603. assert_eq!(yet_another_section.pages.len(), 1);
  604. let child = site.library.get_page_by_key(yet_another_section.pages[0]);
  605. assert_eq!(child.meta.template, Some("page_template_child.html".into()));
  606. assert_eq!(child.meta.title, Some("Local section override".into()));
  607. }