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.

177 lines
6.1KB

  1. extern crate gutenberg;
  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 glob::glob;
  9. use tempdir::TempDir;
  10. use gutenberg::{Site};
  11. #[test]
  12. fn test_can_parse_site() {
  13. let mut path = env::current_dir().unwrap().to_path_buf();
  14. path.push("test_site");
  15. let site = Site::new(&path).unwrap();
  16. // Correct number of pages (sections are pages too)
  17. assert_eq!(site.pages.len(), 10);
  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.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!(url_post.components.is_empty());
  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.components, vec!["posts".to_string()]);
  28. // That we have the right number of sections
  29. assert_eq!(site.sections.len(), 4);
  30. // And that the sections are correct
  31. let posts_section = &site.sections[&posts_path];
  32. assert_eq!(posts_section.subsections.len(), 1);
  33. assert_eq!(posts_section.pages.len(), 5);
  34. let tutorials_section = &site.sections[&posts_path.join("tutorials")];
  35. assert_eq!(tutorials_section.subsections.len(), 2);
  36. assert_eq!(tutorials_section.pages.len(), 0);
  37. let devops_section = &site.sections[&posts_path.join("tutorials").join("devops")];
  38. assert_eq!(devops_section.subsections.len(), 0);
  39. assert_eq!(devops_section.pages.len(), 2);
  40. let prog_section = &site.sections[&posts_path.join("tutorials").join("programming")];
  41. assert_eq!(prog_section.subsections.len(), 0);
  42. assert_eq!(prog_section.pages.len(), 2);
  43. }
  44. // 2 helper macros to make all the build testing more bearable
  45. macro_rules! file_exists {
  46. ($root: expr, $path: expr) => {
  47. {
  48. let mut path = $root.clone();
  49. for component in $path.split("/") {
  50. path = path.join(component);
  51. }
  52. Path::new(&path).exists()
  53. }
  54. }
  55. }
  56. macro_rules! file_contains {
  57. ($root: expr, $path: expr, $text: expr) => {
  58. {
  59. let mut path = $root.clone();
  60. for component in $path.split("/") {
  61. path = path.join(component);
  62. }
  63. let mut file = File::open(&path).unwrap();
  64. let mut s = String::new();
  65. file.read_to_string(&mut s).unwrap();
  66. s.contains($text)
  67. }
  68. }
  69. }
  70. #[test]
  71. fn test_can_build_site_without_live_reload() {
  72. let mut path = env::current_dir().unwrap().to_path_buf();
  73. path.push("test_site");
  74. let mut site = Site::new(&path).unwrap();
  75. let tmp_dir = TempDir::new("example").expect("create temp dir");
  76. let public = &tmp_dir.path().join("public");
  77. site.set_output_path(&public);
  78. site.build().unwrap();
  79. assert!(Path::new(&public).exists());
  80. assert!(file_exists!(public, "index.html"));
  81. assert!(file_exists!(public, "sitemap.xml"));
  82. assert!(file_exists!(public, "a-fixed-url/index.html"));
  83. assert!(file_exists!(public, "posts/python/index.html"));
  84. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  85. assert!(file_exists!(public, "posts/with-assets/index.html"));
  86. // Sections
  87. assert!(file_exists!(public, "posts/index.html"));
  88. assert!(file_exists!(public, "posts/tutorials/index.html"));
  89. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  90. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  91. // TODO: add assertion for syntax highlighting
  92. // No tags or categories
  93. assert_eq!(file_exists!(public, "categories/index.html"), false);
  94. assert_eq!(file_exists!(public, "tags/index.html"), false);
  95. // no live reload code
  96. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  97. }
  98. #[test]
  99. fn test_can_build_site_with_live_reload() {
  100. let mut path = env::current_dir().unwrap().to_path_buf();
  101. path.push("test_site");
  102. let mut site = Site::new(&path).unwrap();
  103. let tmp_dir = TempDir::new("example").expect("create temp dir");
  104. let public = &tmp_dir.path().join("public");
  105. site.set_output_path(&public);
  106. site.enable_live_reload();
  107. site.build().unwrap();
  108. assert!(Path::new(&public).exists());
  109. assert!(file_exists!(public, "index.html"));
  110. assert!(file_exists!(public, "sitemap.xml"));
  111. assert!(file_exists!(public, "a-fixed-url/index.html"));
  112. assert!(file_exists!(public, "posts/python/index.html"));
  113. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  114. assert!(file_exists!(public, "posts/with-assets/index.html"));
  115. // Sections
  116. assert!(file_exists!(public, "posts/index.html"));
  117. assert!(file_exists!(public, "posts/tutorials/index.html"));
  118. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  119. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  120. // TODO: add assertion for syntax highlighting
  121. // No tags or categories
  122. assert_eq!(file_exists!(public, "categories/index.html"), false);
  123. assert_eq!(file_exists!(public, "tags/index.html"), false);
  124. // no live reload code
  125. assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
  126. }
  127. #[test]
  128. fn test_can_build_site_with_categories() {
  129. let mut path = env::current_dir().unwrap().to_path_buf();
  130. path.push("test_site");
  131. let mut site = Site::new(&path).unwrap();
  132. let tmp_dir = TempDir::new("example").expect("create temp dir");
  133. site.set_output_path(&tmp_dir);
  134. site.build().unwrap();
  135. }
  136. #[test]
  137. fn test_can_build_site_with_tags() {
  138. let mut path = env::current_dir().unwrap().to_path_buf();
  139. path.push("test_site");
  140. let mut site = Site::new(&path).unwrap();
  141. let tmp_dir = TempDir::new("example").expect("create temp dir");
  142. site.set_output_path(&tmp_dir);
  143. site.build().unwrap();
  144. }