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

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