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.

site.rs 20KB

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