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.

485 lines
21KB

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