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.

417 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. // TODO: add assertion for syntax highlighting
  99. // aliases work
  100. assert!(file_exists!(public, "an-old-url/old-page/index.html"));
  101. assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
  102. // No tags or categories
  103. assert_eq!(file_exists!(public, "categories/index.html"), false);
  104. assert_eq!(file_exists!(public, "tags/index.html"), false);
  105. // no live reload code
  106. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  107. // Both pages and sections are in the sitemap
  108. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
  109. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
  110. }
  111. #[test]
  112. fn can_build_site_with_live_reload() {
  113. let mut path = env::current_dir().unwrap().to_path_buf();
  114. path.push("test_site");
  115. let mut site = Site::new(&path, "config.toml").unwrap();
  116. site.load().unwrap();
  117. let tmp_dir = TempDir::new("example").expect("create temp dir");
  118. let public = &tmp_dir.path().join("public");
  119. site.set_output_path(&public);
  120. site.enable_live_reload();
  121. site.build().unwrap();
  122. assert!(Path::new(&public).exists());
  123. assert!(file_exists!(public, "index.html"));
  124. assert!(file_exists!(public, "sitemap.xml"));
  125. assert!(file_exists!(public, "robots.txt"));
  126. assert!(file_exists!(public, "a-fixed-url/index.html"));
  127. assert!(file_exists!(public, "posts/python/index.html"));
  128. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  129. assert!(file_exists!(public, "posts/with-assets/index.html"));
  130. // Sections
  131. assert!(file_exists!(public, "posts/index.html"));
  132. assert!(file_exists!(public, "posts/tutorials/index.html"));
  133. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  134. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  135. // TODO: add assertion for syntax highlighting
  136. // No tags or categories
  137. assert_eq!(file_exists!(public, "categories/index.html"), false);
  138. assert_eq!(file_exists!(public, "tags/index.html"), false);
  139. // no live reload code
  140. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  141. }
  142. #[test]
  143. fn can_build_site_with_categories() {
  144. let mut path = env::current_dir().unwrap().to_path_buf();
  145. path.push("test_site");
  146. let mut site = Site::new(&path, "config.toml").unwrap();
  147. site.config.generate_categories_pages = Some(true);
  148. site.load().unwrap();
  149. for (i, page) in site.pages.values_mut().enumerate() {
  150. page.meta.category = if i % 2 == 0 {
  151. Some("A".to_string())
  152. } else {
  153. Some("B".to_string())
  154. };
  155. }
  156. site.populate_tags_and_categories();
  157. let tmp_dir = TempDir::new("example").expect("create temp dir");
  158. let public = &tmp_dir.path().join("public");
  159. site.set_output_path(&public);
  160. site.build().unwrap();
  161. assert!(Path::new(&public).exists());
  162. assert_eq!(site.categories.unwrap().len(), 2);
  163. assert!(file_exists!(public, "index.html"));
  164. assert!(file_exists!(public, "sitemap.xml"));
  165. assert!(file_exists!(public, "robots.txt"));
  166. assert!(file_exists!(public, "a-fixed-url/index.html"));
  167. assert!(file_exists!(public, "posts/python/index.html"));
  168. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  169. assert!(file_exists!(public, "posts/with-assets/index.html"));
  170. // Sections
  171. assert!(file_exists!(public, "posts/index.html"));
  172. assert!(file_exists!(public, "posts/tutorials/index.html"));
  173. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  174. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  175. // TODO: add assertion for syntax highlighting
  176. // Categories are there
  177. assert!(file_exists!(public, "categories/index.html"));
  178. assert!(file_exists!(public, "categories/a/index.html"));
  179. assert!(file_exists!(public, "categories/b/index.html"));
  180. // Tags aren't
  181. assert_eq!(file_exists!(public, "tags/index.html"), false);
  182. // Categories are in the sitemap
  183. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/</loc>"));
  184. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"));
  185. }
  186. #[test]
  187. fn can_build_site_with_tags() {
  188. let mut path = env::current_dir().unwrap().to_path_buf();
  189. path.push("test_site");
  190. let mut site = Site::new(&path, "config.toml").unwrap();
  191. site.config.generate_tags_pages = Some(true);
  192. site.load().unwrap();
  193. for (i, page) in site.pages.values_mut().enumerate() {
  194. page.meta.tags = if i % 2 == 0 {
  195. Some(vec!["tag1".to_string(), "tag2".to_string()])
  196. } else {
  197. Some(vec!["tag with space".to_string()])
  198. };
  199. }
  200. site.populate_tags_and_categories();
  201. let tmp_dir = TempDir::new("example").expect("create temp dir");
  202. let public = &tmp_dir.path().join("public");
  203. site.set_output_path(&public);
  204. site.build().unwrap();
  205. assert!(Path::new(&public).exists());
  206. assert_eq!(site.tags.unwrap().len(), 3);
  207. assert!(file_exists!(public, "index.html"));
  208. assert!(file_exists!(public, "sitemap.xml"));
  209. assert!(file_exists!(public, "robots.txt"));
  210. assert!(file_exists!(public, "a-fixed-url/index.html"));
  211. assert!(file_exists!(public, "posts/python/index.html"));
  212. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  213. assert!(file_exists!(public, "posts/with-assets/index.html"));
  214. // Sections
  215. assert!(file_exists!(public, "posts/index.html"));
  216. assert!(file_exists!(public, "posts/tutorials/index.html"));
  217. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  218. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  219. // TODO: add assertion for syntax highlighting
  220. // Tags are there
  221. assert!(file_exists!(public, "tags/index.html"));
  222. assert!(file_exists!(public, "tags/tag1/index.html"));
  223. assert!(file_exists!(public, "tags/tag2/index.html"));
  224. assert!(file_exists!(public, "tags/tag-with-space/index.html"));
  225. // Categories aren't
  226. assert_eq!(file_exists!(public, "categories/index.html"), false);
  227. // Tags are in the sitemap
  228. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/</loc>"));
  229. assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space/</loc>"));
  230. }
  231. #[test]
  232. fn can_build_site_and_insert_anchor_links() {
  233. let mut path = env::current_dir().unwrap().to_path_buf();
  234. path.push("test_site");
  235. let mut site = Site::new(&path, "config.toml").unwrap();
  236. site.load().unwrap();
  237. let tmp_dir = TempDir::new("example").expect("create temp dir");
  238. let public = &tmp_dir.path().join("public");
  239. site.set_output_path(&public);
  240. site.build().unwrap();
  241. assert!(Path::new(&public).exists());
  242. // anchor link inserted
  243. assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"gutenberg-anchor\" href=\"#title\""));
  244. }
  245. #[test]
  246. fn can_build_site_with_pagination_for_section() {
  247. let mut path = env::current_dir().unwrap().to_path_buf();
  248. path.push("test_site");
  249. let mut site = Site::new(&path, "config.toml").unwrap();
  250. site.load().unwrap();
  251. for section in site.sections.values_mut(){
  252. if section.is_index() {
  253. continue;
  254. }
  255. section.meta.paginate_by = Some(2);
  256. section.meta.template = Some("section_paginated.html".to_string());
  257. }
  258. let tmp_dir = TempDir::new("example").expect("create temp dir");
  259. let public = &tmp_dir.path().join("public");
  260. site.set_output_path(&public);
  261. site.build().unwrap();
  262. assert!(Path::new(&public).exists());
  263. assert!(file_exists!(public, "index.html"));
  264. assert!(file_exists!(public, "sitemap.xml"));
  265. assert!(file_exists!(public, "robots.txt"));
  266. assert!(file_exists!(public, "a-fixed-url/index.html"));
  267. assert!(file_exists!(public, "posts/python/index.html"));
  268. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  269. assert!(file_exists!(public, "posts/with-assets/index.html"));
  270. // Sections
  271. assert!(file_exists!(public, "posts/index.html"));
  272. // And pagination!
  273. assert!(file_exists!(public, "posts/page/1/index.html"));
  274. // even if there is no pages, only the section!
  275. assert!(file_exists!(public, "paginated/page/1/index.html"));
  276. assert!(file_exists!(public, "paginated/index.html"));
  277. // should redirect to posts/
  278. assert!(file_contains!(
  279. public,
  280. "posts/page/1/index.html",
  281. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  282. ));
  283. assert!(file_contains!(public, "posts/index.html", "Num pagers: 3"));
  284. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  285. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  286. assert!(file_contains!(public, "posts/index.html", "has_next"));
  287. assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  288. assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  289. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  290. assert!(file_exists!(public, "posts/page/2/index.html"));
  291. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 3"));
  292. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  293. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  294. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  295. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  296. assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
  297. assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
  298. }
  299. #[test]
  300. fn can_build_site_with_pagination_for_index() {
  301. let mut path = env::current_dir().unwrap().to_path_buf();
  302. path.push("test_site");
  303. let mut site = Site::new(&path, "config.toml").unwrap();
  304. site.load().unwrap();
  305. {
  306. let mut index = site.sections.get_mut(&path.join("content").join("_index.md")).unwrap();
  307. index.meta.paginate_by = Some(2);
  308. index.meta.template = Some("index_paginated.html".to_string());
  309. }
  310. let tmp_dir = TempDir::new("example").expect("create temp dir");
  311. let public = &tmp_dir.path().join("public");
  312. site.set_output_path(&public);
  313. site.build().unwrap();
  314. assert!(Path::new(&public).exists());
  315. assert!(file_exists!(public, "index.html"));
  316. assert!(file_exists!(public, "sitemap.xml"));
  317. assert!(file_exists!(public, "robots.txt"));
  318. assert!(file_exists!(public, "a-fixed-url/index.html"));
  319. assert!(file_exists!(public, "posts/python/index.html"));
  320. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  321. assert!(file_exists!(public, "posts/with-assets/index.html"));
  322. // And pagination!
  323. assert!(file_exists!(public, "page/1/index.html"));
  324. // even if there is no pages, only the section!
  325. assert!(file_exists!(public, "paginated/page/1/index.html"));
  326. assert!(file_exists!(public, "paginated/index.html"));
  327. // should redirect to index
  328. assert!(file_contains!(
  329. public,
  330. "page/1/index.html",
  331. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  332. ));
  333. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  334. assert!(file_contains!(public, "index.html", "Current index: 1"));
  335. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  336. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  337. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  338. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  339. }
  340. #[test]
  341. fn can_build_rss_feed() {
  342. let mut path = env::current_dir().unwrap().to_path_buf();
  343. path.push("test_site");
  344. let mut site = Site::new(&path, "config.toml").unwrap();
  345. site.load().unwrap();
  346. let tmp_dir = TempDir::new("example").expect("create temp dir");
  347. let public = &tmp_dir.path().join("public");
  348. site.set_output_path(&public);
  349. site.build().unwrap();
  350. assert!(Path::new(&public).exists());
  351. assert!(file_exists!(public, "rss.xml"));
  352. // latest article is posts/simple.md
  353. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  354. // Next is posts/python.md
  355. assert!(file_contains!(public, "rss.xml", "Python in posts"));
  356. }