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.

site.rs 23KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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(), 20);
  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(), 10);
  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(), 9);
  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. // No tags or categories
  149. assert_eq!(file_exists!(public, "categories/index.html"), false);
  150. assert_eq!(file_exists!(public, "tags/index.html"), false);
  151. // Theme files are there
  152. assert!(file_exists!(public, "sample.css"));
  153. assert!(file_exists!(public, "some.js"));
  154. // SASS and SCSS files compile correctly
  155. assert!(file_exists!(public, "blog.css"));
  156. assert!(file_contains!(public, "blog.css", "red"));
  157. assert!(file_contains!(public, "blog.css", "blue"));
  158. assert!(!file_contains!(public, "blog.css", "@import \"included\""));
  159. assert!(file_contains!(public, "blog.css", "2rem")); // check include
  160. assert!(!file_exists!(public, "_included.css"));
  161. assert!(file_exists!(public, "scss.css"));
  162. assert!(file_exists!(public, "sass.css"));
  163. assert!(file_exists!(public, "nested_sass/sass.css"));
  164. assert!(file_exists!(public, "nested_sass/scss.css"));
  165. // no live reload code
  166. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  167. // Both pages and sections are in the sitemap
  168. assert!(file_contains!(
  169. public,
  170. "sitemap.xml",
  171. "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"
  172. ));
  173. assert!(file_contains!(
  174. public,
  175. "sitemap.xml",
  176. "<loc>https://replace-this-with-your-url.com/posts/</loc>"
  177. ));
  178. // Drafts are not in the sitemap
  179. assert!(!file_contains!(public, "sitemap.xml", "draft"));
  180. // robots.txt has been rendered from the template
  181. assert!(file_contains!(public, "robots.txt", "User-agent: zola"));
  182. assert!(file_contains!(
  183. public,
  184. "robots.txt",
  185. "Sitemap: https://replace-this-with-your-url.com/sitemap.xml"
  186. ));
  187. }
  188. #[test]
  189. fn can_build_site_with_live_reload() {
  190. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  191. path.push("test_site");
  192. let mut site = Site::new(&path, "config.toml").unwrap();
  193. site.load().unwrap();
  194. let tmp_dir = tempdir().expect("create temp dir");
  195. let public = &tmp_dir.path().join("public");
  196. site.set_output_path(&public);
  197. site.enable_live_reload(1000);
  198. site.build().unwrap();
  199. assert!(Path::new(&public).exists());
  200. assert!(file_exists!(public, "index.html"));
  201. assert!(file_exists!(public, "sitemap.xml"));
  202. assert!(file_exists!(public, "robots.txt"));
  203. assert!(file_exists!(public, "a-fixed-url/index.html"));
  204. assert!(file_exists!(public, "posts/python/index.html"));
  205. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  206. assert!(file_exists!(public, "posts/with-assets/index.html"));
  207. // Sections
  208. assert!(file_exists!(public, "posts/index.html"));
  209. assert!(file_exists!(public, "posts/tutorials/index.html"));
  210. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  211. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  212. // TODO: add assertion for syntax highlighting
  213. // No tags or categories
  214. assert_eq!(file_exists!(public, "categories/index.html"), false);
  215. assert_eq!(file_exists!(public, "tags/index.html"), false);
  216. // no live reload code
  217. assert!(file_contains!(public, "index.html", "/livereload.js"));
  218. // the summary anchor link has been created
  219. assert!(file_contains!(
  220. public,
  221. "posts/python/index.html",
  222. r#"<a name="continue-reading"></a>"#
  223. ));
  224. assert!(file_contains!(public, "posts/draft/index.html", r#"THEME_SHORTCODE"#));
  225. }
  226. #[test]
  227. fn can_build_site_with_taxonomies() {
  228. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  229. path.push("test_site");
  230. let mut site = Site::new(&path, "config.toml").unwrap();
  231. site.load().unwrap();
  232. for (i, (_, page)) in site.library.pages_mut().iter_mut().enumerate() {
  233. page.meta.taxonomies = {
  234. let mut taxonomies = HashMap::new();
  235. taxonomies.insert(
  236. "categories".to_string(),
  237. vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
  238. );
  239. taxonomies
  240. };
  241. }
  242. site.populate_taxonomies().unwrap();
  243. let tmp_dir = tempdir().expect("create temp dir");
  244. let public = &tmp_dir.path().join("public");
  245. site.set_output_path(&public);
  246. site.build().unwrap();
  247. assert!(Path::new(&public).exists());
  248. assert_eq!(site.taxonomies.len(), 1);
  249. assert!(file_exists!(public, "index.html"));
  250. assert!(file_exists!(public, "sitemap.xml"));
  251. assert!(file_exists!(public, "robots.txt"));
  252. assert!(file_exists!(public, "a-fixed-url/index.html"));
  253. assert!(file_exists!(public, "posts/python/index.html"));
  254. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  255. assert!(file_exists!(public, "posts/with-assets/index.html"));
  256. // Sections
  257. assert!(file_exists!(public, "posts/index.html"));
  258. assert!(file_exists!(public, "posts/tutorials/index.html"));
  259. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  260. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  261. // Categories are there
  262. assert!(file_exists!(public, "categories/index.html"));
  263. assert!(file_exists!(public, "categories/a/index.html"));
  264. assert!(file_exists!(public, "categories/b/index.html"));
  265. assert!(file_exists!(public, "categories/a/rss.xml"));
  266. assert!(file_contains!(
  267. public,
  268. "categories/a/rss.xml",
  269. "https://replace-this-with-your-url.com/categories/a/rss.xml"
  270. ));
  271. // Extending from a theme works
  272. assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
  273. // Tags aren't
  274. assert_eq!(file_exists!(public, "tags/index.html"), false);
  275. // Categories are in the sitemap
  276. assert!(file_contains!(
  277. public,
  278. "sitemap.xml",
  279. "<loc>https://replace-this-with-your-url.com/categories/</loc>"
  280. ));
  281. assert!(file_contains!(
  282. public,
  283. "sitemap.xml",
  284. "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"
  285. ));
  286. }
  287. #[test]
  288. fn can_build_site_and_insert_anchor_links() {
  289. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  290. path.push("test_site");
  291. let mut site = Site::new(&path, "config.toml").unwrap();
  292. site.load().unwrap();
  293. let tmp_dir = tempdir().expect("create temp dir");
  294. let public = &tmp_dir.path().join("public");
  295. site.set_output_path(&public);
  296. site.build().unwrap();
  297. assert!(Path::new(&public).exists());
  298. // anchor link inserted
  299. assert!(file_contains!(
  300. public,
  301. "posts/something-else/index.html",
  302. "<h1 id=\"title\"><a class=\"zola-anchor\" href=\"#title\""
  303. ));
  304. }
  305. #[test]
  306. fn can_build_site_with_pagination_for_section() {
  307. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  308. path.push("test_site");
  309. let mut site = Site::new(&path, "config.toml").unwrap();
  310. site.load().unwrap();
  311. for (_, section) in site.library.sections_mut() {
  312. if section.is_index() {
  313. continue;
  314. }
  315. section.meta.paginate_by = Some(2);
  316. section.meta.template = Some("section_paginated.html".to_string());
  317. }
  318. let tmp_dir = tempdir().expect("create temp dir");
  319. let public = &tmp_dir.path().join("public");
  320. site.set_output_path(&public);
  321. site.build().unwrap();
  322. assert!(Path::new(&public).exists());
  323. assert!(file_exists!(public, "index.html"));
  324. assert!(file_exists!(public, "sitemap.xml"));
  325. assert!(file_exists!(public, "robots.txt"));
  326. assert!(file_exists!(public, "a-fixed-url/index.html"));
  327. assert!(file_exists!(public, "posts/python/index.html"));
  328. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  329. assert!(file_exists!(public, "posts/with-assets/index.html"));
  330. // Sections
  331. assert!(file_exists!(public, "posts/index.html"));
  332. // And pagination!
  333. assert!(file_exists!(public, "posts/page/1/index.html"));
  334. // even if there is no pages, only the section!
  335. assert!(file_exists!(public, "paginated/page/1/index.html"));
  336. assert!(file_exists!(public, "paginated/index.html"));
  337. // should redirect to posts/
  338. assert!(file_contains!(
  339. public,
  340. "posts/page/1/index.html",
  341. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  342. ));
  343. assert!(file_contains!(public, "posts/index.html", "Num pagers: 4"));
  344. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  345. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  346. assert!(!file_contains!(public, "posts/index.html", "has_prev"));
  347. assert!(file_contains!(public, "posts/index.html", "has_next"));
  348. assert!(file_contains!(
  349. public,
  350. "posts/index.html",
  351. "First: https://replace-this-with-your-url.com/posts/"
  352. ));
  353. assert!(file_contains!(
  354. public,
  355. "posts/index.html",
  356. "Last: https://replace-this-with-your-url.com/posts/page/4/"
  357. ));
  358. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  359. assert!(file_exists!(public, "posts/page/2/index.html"));
  360. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 4"));
  361. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  362. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  363. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  364. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  365. assert!(file_contains!(
  366. public,
  367. "posts/page/2/index.html",
  368. "First: https://replace-this-with-your-url.com/posts/"
  369. ));
  370. assert!(file_contains!(
  371. public,
  372. "posts/page/2/index.html",
  373. "Last: https://replace-this-with-your-url.com/posts/page/4/"
  374. ));
  375. assert!(file_exists!(public, "posts/page/3/index.html"));
  376. assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 4"));
  377. assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
  378. assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
  379. assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
  380. assert!(file_contains!(public, "posts/page/3/index.html", "has_next"));
  381. assert!(file_contains!(
  382. public,
  383. "posts/page/3/index.html",
  384. "First: https://replace-this-with-your-url.com/posts/"
  385. ));
  386. assert!(file_contains!(
  387. public,
  388. "posts/page/3/index.html",
  389. "Last: https://replace-this-with-your-url.com/posts/page/4/"
  390. ));
  391. assert!(file_exists!(public, "posts/page/4/index.html"));
  392. assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 4"));
  393. assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
  394. assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
  395. assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
  396. assert!(!file_contains!(public, "posts/page/4/index.html", "has_next"));
  397. assert!(file_contains!(
  398. public,
  399. "posts/page/4/index.html",
  400. "First: https://replace-this-with-your-url.com/posts/"
  401. ));
  402. assert!(file_contains!(
  403. public,
  404. "posts/page/4/index.html",
  405. "Last: https://replace-this-with-your-url.com/posts/page/4/"
  406. ));
  407. }
  408. #[test]
  409. fn can_build_site_with_pagination_for_index() {
  410. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  411. path.push("test_site");
  412. let mut site = Site::new(&path, "config.toml").unwrap();
  413. site.load().unwrap();
  414. {
  415. let index = site.library.get_section_mut(&path.join("content").join("_index.md")).unwrap();
  416. index.meta.paginate_by = Some(2);
  417. index.meta.template = Some("index_paginated.html".to_string());
  418. }
  419. let tmp_dir = tempdir().expect("create temp dir");
  420. let public = &tmp_dir.path().join("public");
  421. site.set_output_path(&public);
  422. site.build().unwrap();
  423. assert!(Path::new(&public).exists());
  424. assert!(file_exists!(public, "index.html"));
  425. assert!(file_exists!(public, "sitemap.xml"));
  426. assert!(file_exists!(public, "robots.txt"));
  427. assert!(file_exists!(public, "a-fixed-url/index.html"));
  428. assert!(file_exists!(public, "posts/python/index.html"));
  429. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  430. assert!(file_exists!(public, "posts/with-assets/index.html"));
  431. // And pagination!
  432. assert!(file_exists!(public, "page/1/index.html"));
  433. // even if there is no pages, only the section!
  434. assert!(file_exists!(public, "paginated/page/1/index.html"));
  435. assert!(file_exists!(public, "paginated/index.html"));
  436. // should redirect to index
  437. assert!(file_contains!(
  438. public,
  439. "page/1/index.html",
  440. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  441. ));
  442. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  443. assert!(file_contains!(public, "index.html", "Current index: 1"));
  444. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  445. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  446. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  447. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  448. }
  449. #[test]
  450. fn can_build_rss_feed() {
  451. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  452. path.push("test_site");
  453. let mut site = Site::new(&path, "config.toml").unwrap();
  454. site.load().unwrap();
  455. let tmp_dir = tempdir().expect("create temp dir");
  456. let public = &tmp_dir.path().join("public");
  457. site.set_output_path(&public);
  458. site.build().unwrap();
  459. assert!(Path::new(&public).exists());
  460. assert!(file_exists!(public, "rss.xml"));
  461. // latest article is posts/extra-syntax.md
  462. assert!(file_contains!(public, "rss.xml", "Extra Syntax"));
  463. // Next is posts/simple.md
  464. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  465. }
  466. #[test]
  467. fn can_build_search_index() {
  468. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  469. path.push("test_site");
  470. let mut site = Site::new(&path, "config.toml").unwrap();
  471. site.load().unwrap();
  472. site.config.build_search_index = true;
  473. let tmp_dir = tempdir().expect("create temp dir");
  474. let public = &tmp_dir.path().join("public");
  475. site.set_output_path(&public);
  476. site.build().unwrap();
  477. assert!(Path::new(&public).exists());
  478. assert!(file_exists!(public, "elasticlunr.min.js"));
  479. assert!(file_exists!(public, "search_index.en.js"));
  480. }
  481. #[test]
  482. fn can_build_with_extra_syntaxes() {
  483. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  484. path.push("test_site");
  485. let mut site = Site::new(&path, "config.toml").unwrap();
  486. site.load().unwrap();
  487. let tmp_dir = tempdir().expect("create temp dir");
  488. let public = &tmp_dir.path().join("public");
  489. site.set_output_path(&public);
  490. site.build().unwrap();
  491. assert!(&public.exists());
  492. assert!(file_exists!(public, "posts/extra-syntax/index.html"));
  493. assert!(file_contains!(
  494. public,
  495. "posts/extra-syntax/index.html",
  496. r#"<span style="color:#d08770;">test</span>"#
  497. ));
  498. }
  499. #[test]
  500. fn can_apply_page_templates() {
  501. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  502. path.push("test_site");
  503. let mut site = Site::new(&path, "config.toml").unwrap();
  504. site.load().unwrap();
  505. let template_path = path.join("content").join("applying_page_template");
  506. let template_section = site.library.get_section(&template_path.join("_index.md")).unwrap();
  507. assert_eq!(template_section.subsections.len(), 1);
  508. assert_eq!(template_section.pages.len(), 2);
  509. let from_section_config = site.library.get_page_by_key(template_section.pages[0]);
  510. assert_eq!(from_section_config.meta.template, Some("page_template.html".into()));
  511. assert_eq!(from_section_config.meta.title, Some("From section config".into()));
  512. let override_page_template = site.library.get_page_by_key(template_section.pages[1]);
  513. assert_eq!(override_page_template.meta.template, Some("page_template_override.html".into()));
  514. assert_eq!(override_page_template.meta.title, Some("Override".into()));
  515. // It should have applied recursively as well
  516. let another_section = site.library.get_section(&template_path.join("another_section").join("_index.md")).unwrap();
  517. assert_eq!(another_section.subsections.len(), 0);
  518. assert_eq!(another_section.pages.len(), 1);
  519. let changed_recursively = site.library.get_page_by_key(another_section.pages[0]);
  520. assert_eq!(changed_recursively.meta.template, Some("page_template.html".into()));
  521. assert_eq!(changed_recursively.meta.title, Some("Changed recursively".into()));
  522. }