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.

428 lines
17KB

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