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.

465 lines
20KB

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