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.

614 lines
24KB

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