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.

710 lines
28KB

  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. // sitemap contains the pager pages
  416. assert!(file_contains!(
  417. public,
  418. "sitemap.xml",
  419. "<loc>https://replace-this-with-your-url.com/posts/page/4/</loc>"
  420. ));
  421. }
  422. #[test]
  423. fn can_build_site_with_pagination_for_index() {
  424. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  425. path.push("test_site");
  426. let mut site = Site::new(&path, "config.toml").unwrap();
  427. site.load().unwrap();
  428. {
  429. let index = site.library.get_section_mut(&path.join("content").join("_index.md")).unwrap();
  430. index.meta.paginate_by = Some(2);
  431. index.meta.template = Some("index_paginated.html".to_string());
  432. }
  433. let tmp_dir = tempdir().expect("create temp dir");
  434. let public = &tmp_dir.path().join("public");
  435. site.set_output_path(&public);
  436. site.build().unwrap();
  437. assert!(Path::new(&public).exists());
  438. assert!(file_exists!(public, "index.html"));
  439. assert!(file_exists!(public, "sitemap.xml"));
  440. assert!(file_exists!(public, "robots.txt"));
  441. assert!(file_exists!(public, "a-fixed-url/index.html"));
  442. assert!(file_exists!(public, "posts/python/index.html"));
  443. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  444. assert!(file_exists!(public, "posts/with-assets/index.html"));
  445. // And pagination!
  446. assert!(file_exists!(public, "page/1/index.html"));
  447. // even if there is no pages, only the section!
  448. assert!(file_exists!(public, "paginated/page/1/index.html"));
  449. assert!(file_exists!(public, "paginated/index.html"));
  450. // should redirect to index
  451. assert!(file_contains!(
  452. public,
  453. "page/1/index.html",
  454. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  455. ));
  456. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  457. assert!(file_contains!(public, "index.html", "Current index: 1"));
  458. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  459. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  460. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  461. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  462. // sitemap contains the pager pages
  463. assert!(file_contains!(
  464. public,
  465. "sitemap.xml",
  466. "<loc>https://replace-this-with-your-url.com/page/1/</loc>"
  467. ))
  468. }
  469. #[test]
  470. fn can_build_site_with_pagination_for_taxonomy() {
  471. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  472. path.push("test_site");
  473. let mut site = Site::new(&path, "config.toml").unwrap();
  474. site.config.taxonomies.push(Taxonomy {
  475. name: "tags".to_string(),
  476. paginate_by: Some(2),
  477. paginate_path: None,
  478. rss: true,
  479. });
  480. site.load().unwrap();
  481. for (i, (_, page)) in site.library.pages_mut().iter_mut().enumerate() {
  482. page.meta.taxonomies = {
  483. let mut taxonomies = HashMap::new();
  484. taxonomies
  485. .insert("tags".to_string(), vec![if i % 2 == 0 { "A" } else { "B" }.to_string()]);
  486. taxonomies
  487. };
  488. }
  489. site.populate_taxonomies().unwrap();
  490. let tmp_dir = tempdir().expect("create temp dir");
  491. let public = &tmp_dir.path().join("public");
  492. site.set_output_path(&public);
  493. site.build().unwrap();
  494. assert!(Path::new(&public).exists());
  495. assert!(file_exists!(public, "index.html"));
  496. assert!(file_exists!(public, "sitemap.xml"));
  497. assert!(file_exists!(public, "robots.txt"));
  498. assert!(file_exists!(public, "a-fixed-url/index.html"));
  499. assert!(file_exists!(public, "posts/python/index.html"));
  500. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  501. assert!(file_exists!(public, "posts/with-assets/index.html"));
  502. // Tags
  503. assert!(file_exists!(public, "tags/index.html"));
  504. // With RSS
  505. assert!(file_exists!(public, "tags/a/rss.xml"));
  506. assert!(file_exists!(public, "tags/b/rss.xml"));
  507. // And pagination!
  508. assert!(file_exists!(public, "tags/a/page/1/index.html"));
  509. assert!(file_exists!(public, "tags/b/page/1/index.html"));
  510. assert!(file_exists!(public, "tags/a/page/2/index.html"));
  511. assert!(file_exists!(public, "tags/b/page/2/index.html"));
  512. // should redirect to posts/
  513. assert!(file_contains!(
  514. public,
  515. "tags/a/page/1/index.html",
  516. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/tags/a/\""
  517. ));
  518. assert!(file_contains!(public, "tags/a/index.html", "Num pagers: 6"));
  519. assert!(file_contains!(public, "tags/a/index.html", "Page size: 2"));
  520. assert!(file_contains!(public, "tags/a/index.html", "Current index: 1"));
  521. assert!(!file_contains!(public, "tags/a/index.html", "has_prev"));
  522. assert!(file_contains!(public, "tags/a/index.html", "has_next"));
  523. assert!(file_contains!(
  524. public,
  525. "tags/a/index.html",
  526. "First: https://replace-this-with-your-url.com/tags/a/"
  527. ));
  528. assert!(file_contains!(
  529. public,
  530. "tags/a/index.html",
  531. "Last: https://replace-this-with-your-url.com/tags/a/page/6/"
  532. ));
  533. assert_eq!(file_contains!(public, "tags/a/index.html", "has_prev"), false);
  534. // sitemap contains the pager pages
  535. assert!(file_contains!(
  536. public,
  537. "sitemap.xml",
  538. "<loc>https://replace-this-with-your-url.com/tags/a/page/6/</loc>"
  539. ))
  540. }
  541. #[test]
  542. fn can_build_rss_feed() {
  543. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  544. path.push("test_site");
  545. let mut site = Site::new(&path, "config.toml").unwrap();
  546. site.load().unwrap();
  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, "rss.xml"));
  553. // latest article is posts/extra-syntax.md
  554. assert!(file_contains!(public, "rss.xml", "Extra Syntax"));
  555. // Next is posts/simple.md
  556. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  557. }
  558. #[test]
  559. fn can_build_search_index() {
  560. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  561. path.push("test_site");
  562. let mut site = Site::new(&path, "config.toml").unwrap();
  563. site.load().unwrap();
  564. site.config.build_search_index = true;
  565. let tmp_dir = tempdir().expect("create temp dir");
  566. let public = &tmp_dir.path().join("public");
  567. site.set_output_path(&public);
  568. site.build().unwrap();
  569. assert!(Path::new(&public).exists());
  570. assert!(file_exists!(public, "elasticlunr.min.js"));
  571. assert!(file_exists!(public, "search_index.en.js"));
  572. }
  573. #[test]
  574. fn can_build_with_extra_syntaxes() {
  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 tmp_dir = tempdir().expect("create temp dir");
  580. let public = &tmp_dir.path().join("public");
  581. site.set_output_path(&public);
  582. site.build().unwrap();
  583. assert!(&public.exists());
  584. assert!(file_exists!(public, "posts/extra-syntax/index.html"));
  585. assert!(file_contains!(
  586. public,
  587. "posts/extra-syntax/index.html",
  588. r#"<span style="color:#d08770;">test</span>"#
  589. ));
  590. }
  591. #[test]
  592. fn can_apply_page_templates() {
  593. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  594. path.push("test_site");
  595. let mut site = Site::new(&path, "config.toml").unwrap();
  596. site.load().unwrap();
  597. let template_path = path.join("content").join("applying_page_template");
  598. let template_section = site.library.get_section(&template_path.join("_index.md")).unwrap();
  599. assert_eq!(template_section.subsections.len(), 2);
  600. assert_eq!(template_section.pages.len(), 2);
  601. let from_section_config = site.library.get_page_by_key(template_section.pages[0]);
  602. assert_eq!(from_section_config.meta.template, Some("page_template.html".into()));
  603. assert_eq!(from_section_config.meta.title, Some("From section config".into()));
  604. let override_page_template = site.library.get_page_by_key(template_section.pages[1]);
  605. assert_eq!(override_page_template.meta.template, Some("page_template_override.html".into()));
  606. assert_eq!(override_page_template.meta.title, Some("Override".into()));
  607. // It should have applied recursively as well
  608. let another_section =
  609. site.library.get_section(&template_path.join("another_section").join("_index.md")).unwrap();
  610. assert_eq!(another_section.subsections.len(), 0);
  611. assert_eq!(another_section.pages.len(), 1);
  612. let changed_recursively = site.library.get_page_by_key(another_section.pages[0]);
  613. assert_eq!(changed_recursively.meta.template, Some("page_template.html".into()));
  614. assert_eq!(changed_recursively.meta.title, Some("Changed recursively".into()));
  615. // But it should not have override a children page_template
  616. let yet_another_section = site
  617. .library
  618. .get_section(&template_path.join("yet_another_section").join("_index.md"))
  619. .unwrap();
  620. assert_eq!(yet_another_section.subsections.len(), 0);
  621. assert_eq!(yet_another_section.pages.len(), 1);
  622. let child = site.library.get_page_by_key(yet_another_section.pages[0]);
  623. assert_eq!(child.meta.template, Some("page_template_child.html".into()));
  624. assert_eq!(child.meta.title, Some("Local section override".into()));
  625. }