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.

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