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

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