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.

429 lines
18KB

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