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 19KB

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