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.

482 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(), 15);
  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(), 7);
  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. // TODO: add assertion for syntax highlighting
  126. // aliases work
  127. assert!(file_exists!(public, "an-old-url/old-page/index.html"));
  128. assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
  129. // html aliases work
  130. assert!(file_exists!(public, "an-old-url/an-old-alias.html"));
  131. assert!(file_contains!(public, "an-old-url/an-old-alias.html", "something-else"));
  132. // redirect_to works
  133. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  134. assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker"));
  135. // No tags or categories
  136. assert_eq!(file_exists!(public, "categories/index.html"), false);
  137. assert_eq!(file_exists!(public, "tags/index.html"), false);
  138. // Theme files are there
  139. assert!(file_exists!(public, "sample.css"));
  140. assert!(file_exists!(public, "some.js"));
  141. // SASS and SCSS files compile correctly
  142. assert!(file_exists!(public, "blog.css"));
  143. assert!(file_contains!(public, "blog.css", "red"));
  144. assert!(file_contains!(public, "blog.css", "blue"));
  145. assert!(!file_contains!(public, "blog.css", "@import \"included\""));
  146. assert!(file_contains!(public, "blog.css", "2rem")); // check include
  147. assert!(!file_exists!(public, "_included.css"));
  148. assert!(file_exists!(public, "scss.css"));
  149. assert!(file_exists!(public, "sass.css"));
  150. assert!(file_exists!(public, "nested_sass/sass.css"));
  151. assert!(file_exists!(public, "nested_sass/scss.css"));
  152. // no live reload code
  153. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  154. // Both pages and sections are in the sitemap
  155. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
  156. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
  157. // Drafts are not in the sitemap
  158. assert!(!file_contains!(public, "sitemap.xml", "draft"));
  159. // robots.txt has been rendered from the template
  160. assert!(file_contains!(public, "robots.txt", "User-agent: gutenberg"));
  161. assert!(file_contains!(public, "robots.txt", "Sitemap: https://replace-this-with-your-url.com/sitemap.xml"));
  162. }
  163. #[test]
  164. fn can_build_site_with_live_reload() {
  165. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  166. path.push("test_site");
  167. let mut site = Site::new(&path, "config.toml").unwrap();
  168. site.load().unwrap();
  169. let tmp_dir = tempdir().expect("create temp dir");
  170. let public = &tmp_dir.path().join("public");
  171. site.set_output_path(&public);
  172. site.enable_live_reload();
  173. site.build().unwrap();
  174. assert!(Path::new(&public).exists());
  175. assert!(file_exists!(public, "index.html"));
  176. assert!(file_exists!(public, "sitemap.xml"));
  177. assert!(file_exists!(public, "robots.txt"));
  178. assert!(file_exists!(public, "a-fixed-url/index.html"));
  179. assert!(file_exists!(public, "posts/python/index.html"));
  180. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  181. assert!(file_exists!(public, "posts/with-assets/index.html"));
  182. // Sections
  183. assert!(file_exists!(public, "posts/index.html"));
  184. assert!(file_exists!(public, "posts/tutorials/index.html"));
  185. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  186. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  187. // TODO: add assertion for syntax highlighting
  188. // No tags or categories
  189. assert_eq!(file_exists!(public, "categories/index.html"), false);
  190. assert_eq!(file_exists!(public, "tags/index.html"), false);
  191. // no live reload code
  192. assert!(file_contains!(public, "index.html", "/livereload.js"));
  193. // the summary anchor link has been created
  194. assert!(file_contains!(public, "posts/python/index.html", r#"<a name="continue-reading"></a>"#));
  195. assert!(file_contains!(public, "posts/draft/index.html", r#"THEME_SHORTCODE"#));
  196. }
  197. #[test]
  198. fn can_build_site_with_taxonomies() {
  199. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  200. path.push("test_site");
  201. let mut site = Site::new(&path, "config.toml").unwrap();
  202. site.load().unwrap();
  203. for (i, (_, page)) in site.library.pages_mut().iter_mut().enumerate() {
  204. page.meta.taxonomies = {
  205. let mut taxonomies = HashMap::new();
  206. taxonomies.insert("categories".to_string(), vec![if i % 2 == 0 { "A" } else { "B" }.to_string()]);
  207. taxonomies
  208. };
  209. }
  210. site.populate_taxonomies().unwrap();
  211. let tmp_dir = tempdir().expect("create temp dir");
  212. let public = &tmp_dir.path().join("public");
  213. site.set_output_path(&public);
  214. site.build().unwrap();
  215. assert!(Path::new(&public).exists());
  216. assert_eq!(site.taxonomies.len(), 1);
  217. assert!(file_exists!(public, "index.html"));
  218. assert!(file_exists!(public, "sitemap.xml"));
  219. assert!(file_exists!(public, "robots.txt"));
  220. assert!(file_exists!(public, "a-fixed-url/index.html"));
  221. assert!(file_exists!(public, "posts/python/index.html"));
  222. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  223. assert!(file_exists!(public, "posts/with-assets/index.html"));
  224. // Sections
  225. assert!(file_exists!(public, "posts/index.html"));
  226. assert!(file_exists!(public, "posts/tutorials/index.html"));
  227. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  228. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  229. // Categories are there
  230. assert!(file_exists!(public, "categories/index.html"));
  231. assert!(file_exists!(public, "categories/a/index.html"));
  232. assert!(file_exists!(public, "categories/b/index.html"));
  233. assert!(file_exists!(public, "categories/a/rss.xml"));
  234. assert!(file_contains!(public, "categories/a/rss.xml", "https://replace-this-with-your-url.com/categories/a/rss.xml"));
  235. // Extending from a theme works
  236. assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
  237. // Tags aren't
  238. assert_eq!(file_exists!(public, "tags/index.html"), false);
  239. // Categories are in the sitemap
  240. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/</loc>"));
  241. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"));
  242. }
  243. #[test]
  244. fn can_build_site_and_insert_anchor_links() {
  245. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  246. path.push("test_site");
  247. let mut site = Site::new(&path, "config.toml").unwrap();
  248. site.load().unwrap();
  249. let tmp_dir = tempdir().expect("create temp dir");
  250. let public = &tmp_dir.path().join("public");
  251. site.set_output_path(&public);
  252. site.build().unwrap();
  253. assert!(Path::new(&public).exists());
  254. // anchor link inserted
  255. assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"gutenberg-anchor\" href=\"#title\""));
  256. }
  257. #[test]
  258. fn can_build_site_with_pagination_for_section() {
  259. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  260. path.push("test_site");
  261. let mut site = Site::new(&path, "config.toml").unwrap();
  262. site.load().unwrap();
  263. for (_, section) in site.library.sections_mut() {
  264. if section.is_index() {
  265. continue;
  266. }
  267. section.meta.paginate_by = Some(2);
  268. section.meta.template = Some("section_paginated.html".to_string());
  269. }
  270. let tmp_dir = tempdir().expect("create temp dir");
  271. let public = &tmp_dir.path().join("public");
  272. site.set_output_path(&public);
  273. site.build().unwrap();
  274. assert!(Path::new(&public).exists());
  275. assert!(file_exists!(public, "index.html"));
  276. assert!(file_exists!(public, "sitemap.xml"));
  277. assert!(file_exists!(public, "robots.txt"));
  278. assert!(file_exists!(public, "a-fixed-url/index.html"));
  279. assert!(file_exists!(public, "posts/python/index.html"));
  280. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  281. assert!(file_exists!(public, "posts/with-assets/index.html"));
  282. // Sections
  283. assert!(file_exists!(public, "posts/index.html"));
  284. // And pagination!
  285. assert!(file_exists!(public, "posts/page/1/index.html"));
  286. // even if there is no pages, only the section!
  287. assert!(file_exists!(public, "paginated/page/1/index.html"));
  288. assert!(file_exists!(public, "paginated/index.html"));
  289. // should redirect to posts/
  290. assert!(file_contains!(
  291. public,
  292. "posts/page/1/index.html",
  293. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  294. ));
  295. assert!(file_contains!(public, "posts/index.html", "Num pagers: 4"));
  296. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  297. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  298. assert!(!file_contains!(public, "posts/index.html", "has_prev"));
  299. assert!(file_contains!(public, "posts/index.html", "has_next"));
  300. assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  301. assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  302. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  303. assert!(file_exists!(public, "posts/page/2/index.html"));
  304. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 4"));
  305. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  306. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  307. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  308. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  309. assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  310. assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  311. assert!(file_exists!(public, "posts/page/3/index.html"));
  312. assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 4"));
  313. assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
  314. assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
  315. assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
  316. assert!(file_contains!(public, "posts/page/3/index.html", "has_next"));
  317. assert!(file_contains!(public, "posts/page/3/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  318. assert!(file_contains!(public, "posts/page/3/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  319. assert!(file_exists!(public, "posts/page/4/index.html"));
  320. assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 4"));
  321. assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
  322. assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
  323. assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
  324. assert!(!file_contains!(public, "posts/page/4/index.html", "has_next"));
  325. assert!(file_contains!(public, "posts/page/4/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  326. assert!(file_contains!(public, "posts/page/4/index.html", "Last: https://replace-this-with-your-url.com/posts/page/4/"));
  327. }
  328. #[test]
  329. fn can_build_site_with_pagination_for_index() {
  330. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  331. path.push("test_site");
  332. let mut site = Site::new(&path, "config.toml").unwrap();
  333. site.load().unwrap();
  334. {
  335. let index = site.library.get_section_mut(&path.join("content").join("_index.md")).unwrap();
  336. index.meta.paginate_by = Some(2);
  337. index.meta.template = Some("index_paginated.html".to_string());
  338. }
  339. let tmp_dir = tempdir().expect("create temp dir");
  340. let public = &tmp_dir.path().join("public");
  341. site.set_output_path(&public);
  342. site.build().unwrap();
  343. assert!(Path::new(&public).exists());
  344. assert!(file_exists!(public, "index.html"));
  345. assert!(file_exists!(public, "sitemap.xml"));
  346. assert!(file_exists!(public, "robots.txt"));
  347. assert!(file_exists!(public, "a-fixed-url/index.html"));
  348. assert!(file_exists!(public, "posts/python/index.html"));
  349. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  350. assert!(file_exists!(public, "posts/with-assets/index.html"));
  351. // And pagination!
  352. assert!(file_exists!(public, "page/1/index.html"));
  353. // even if there is no pages, only the section!
  354. assert!(file_exists!(public, "paginated/page/1/index.html"));
  355. assert!(file_exists!(public, "paginated/index.html"));
  356. // should redirect to index
  357. assert!(file_contains!(
  358. public,
  359. "page/1/index.html",
  360. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  361. ));
  362. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  363. assert!(file_contains!(public, "index.html", "Current index: 1"));
  364. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  365. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  366. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  367. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  368. }
  369. #[test]
  370. fn can_build_rss_feed() {
  371. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  372. path.push("test_site");
  373. let mut site = Site::new(&path, "config.toml").unwrap();
  374. site.load().unwrap();
  375. let tmp_dir = tempdir().expect("create temp dir");
  376. let public = &tmp_dir.path().join("public");
  377. site.set_output_path(&public);
  378. site.build().unwrap();
  379. assert!(Path::new(&public).exists());
  380. assert!(file_exists!(public, "rss.xml"));
  381. // latest article is posts/extra-syntax.md
  382. assert!(file_contains!(public, "rss.xml", "Extra Syntax"));
  383. // Next is posts/simple.md
  384. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  385. }
  386. #[test]
  387. fn can_build_search_index() {
  388. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  389. path.push("test_site");
  390. let mut site = Site::new(&path, "config.toml").unwrap();
  391. site.load().unwrap();
  392. site.config.build_search_index = true;
  393. let tmp_dir = tempdir().expect("create temp dir");
  394. let public = &tmp_dir.path().join("public");
  395. site.set_output_path(&public);
  396. site.build().unwrap();
  397. assert!(Path::new(&public).exists());
  398. assert!(file_exists!(public, "elasticlunr.min.js"));
  399. assert!(file_exists!(public, "search_index.en.js"));
  400. }
  401. #[test]
  402. fn can_build_with_extra_syntaxes() {
  403. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  404. path.push("test_site");
  405. let mut site = Site::new(&path, "config.toml").unwrap();
  406. site.load().unwrap();
  407. let tmp_dir = tempdir().expect("create temp dir");
  408. let public = &tmp_dir.path().join("public");
  409. site.set_output_path(&public);
  410. site.build().unwrap();
  411. assert!(&public.exists());
  412. assert!(file_exists!(public, "posts/extra-syntax/index.html"));
  413. assert!(file_contains!(public, "posts/extra-syntax/index.html",
  414. r#"<span style="color:#d08770;">test</span>"#));
  415. }