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.

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