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.

rebuild.rs 6.5KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. extern crate fs_extra;
  2. extern crate rebuild;
  3. extern crate site;
  4. extern crate tempfile;
  5. use std::env;
  6. use std::fs::{self, File};
  7. use std::io::prelude::*;
  8. use fs_extra::dir;
  9. use site::Site;
  10. use tempfile::tempdir;
  11. use rebuild::{after_content_change, after_content_rename};
  12. // Loads the test_site in a tempdir and build it there
  13. // Returns (site_path_in_tempdir, site)
  14. macro_rules! load_and_build_site {
  15. ($tmp_dir: expr) => {{
  16. let mut path =
  17. env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  18. path.push("test_site");
  19. let mut options = dir::CopyOptions::new();
  20. options.copy_inside = true;
  21. dir::copy(&path, &$tmp_dir, &options).unwrap();
  22. let site_path = $tmp_dir.path().join("test_site");
  23. let mut site = Site::new(&site_path, "config.toml").unwrap();
  24. site.load().unwrap();
  25. let public = &site_path.join("public");
  26. site.set_output_path(&public);
  27. site.build().unwrap();
  28. (site_path, site)
  29. }};
  30. }
  31. /// Replace the file at the path (starting from root) by the given content
  32. /// and return the file path that was modified
  33. macro_rules! edit_file {
  34. ($site_path: expr, $path: expr, $content: expr) => {{
  35. let mut t = $site_path.clone();
  36. for c in $path.split('/') {
  37. t.push(c);
  38. }
  39. let mut file = File::create(&t).expect("Could not open/create file");
  40. file.write_all($content).expect("Could not write to the file");
  41. t
  42. }};
  43. }
  44. macro_rules! file_contains {
  45. ($site_path: expr, $path: expr, $text: expr) => {{
  46. let mut path = $site_path.clone();
  47. for component in $path.split("/") {
  48. path.push(component);
  49. }
  50. let mut file = File::open(&path).unwrap();
  51. let mut s = String::new();
  52. file.read_to_string(&mut s).unwrap();
  53. println!("{:?} -> {}", path, s);
  54. s.contains($text)
  55. }};
  56. }
  57. /// Rename a file or a folder to the new given name
  58. macro_rules! rename {
  59. ($site_path: expr, $path: expr, $new_name: expr) => {{
  60. let mut t = $site_path.clone();
  61. for c in $path.split('/') {
  62. t.push(c);
  63. }
  64. let mut new_path = t.parent().unwrap().to_path_buf();
  65. new_path.push($new_name);
  66. fs::rename(&t, &new_path).unwrap();
  67. println!("Renamed {:?} to {:?}", t, new_path);
  68. (t, new_path)
  69. }};
  70. }
  71. #[test]
  72. fn can_rebuild_after_simple_change_to_page_content() {
  73. let tmp_dir = tempdir().expect("create temp dir");
  74. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  75. let file_path = edit_file!(
  76. site_path,
  77. "content/rebuild/first.md",
  78. br#"
  79. +++
  80. title = "first"
  81. weight = 1
  82. date = 2017-01-01
  83. +++
  84. Some content"#
  85. );
  86. let res = after_content_change(&mut site, &file_path);
  87. assert!(res.is_ok());
  88. assert!(file_contains!(site_path, "public/rebuild/first/index.html", "<p>Some content</p>"));
  89. }
  90. #[test]
  91. fn can_rebuild_after_title_change_page_global_func_usage() {
  92. let tmp_dir = tempdir().expect("create temp dir");
  93. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  94. let file_path = edit_file!(
  95. site_path,
  96. "content/rebuild/first.md",
  97. br#"
  98. +++
  99. title = "Premier"
  100. weight = 10
  101. date = 2017-01-01
  102. +++
  103. # A title"#
  104. );
  105. let res = after_content_change(&mut site, &file_path);
  106. assert!(res.is_ok());
  107. assert!(file_contains!(site_path, "public/rebuild/index.html", "<h1>Premier</h1>"));
  108. }
  109. #[test]
  110. fn can_rebuild_after_sort_change_in_section() {
  111. let tmp_dir = tempdir().expect("create temp dir");
  112. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  113. let file_path = edit_file!(
  114. site_path,
  115. "content/rebuild/_index.md",
  116. br#"
  117. +++
  118. paginate_by = 1
  119. sort_by = "weight"
  120. template = "rebuild.html"
  121. +++
  122. "#
  123. );
  124. let res = after_content_change(&mut site, &file_path);
  125. assert!(res.is_ok());
  126. assert!(file_contains!(
  127. site_path,
  128. "public/rebuild/index.html",
  129. "<h1>first</h1><h1>second</h1>"
  130. ));
  131. }
  132. #[test]
  133. fn can_rebuild_after_transparent_change() {
  134. let tmp_dir = tempdir().expect("create temp dir");
  135. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  136. let file_path = edit_file!(
  137. site_path,
  138. "content/posts/2018/_index.md",
  139. br#"
  140. +++
  141. transparent = false
  142. render = false
  143. +++
  144. "#
  145. );
  146. // Also remove pagination from posts section so we check whether the transparent page title
  147. // is there or not without dealing with pagination
  148. edit_file!(
  149. site_path,
  150. "content/posts/_index.md",
  151. br#"
  152. +++
  153. template = "section.html"
  154. insert_anchor_links = "left"
  155. +++
  156. "#
  157. );
  158. let res = after_content_change(&mut site, &file_path);
  159. assert!(res.is_ok());
  160. assert!(!file_contains!(
  161. site_path,
  162. "public/posts/index.html",
  163. "A transparent page"
  164. ));
  165. }
  166. #[test]
  167. fn can_rebuild_after_renaming_page() {
  168. let tmp_dir = tempdir().expect("create temp dir");
  169. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  170. let (old_path, new_path) = rename!(site_path, "content/posts/simple.md", "hard.md");
  171. let res = after_content_rename(&mut site, &old_path, &new_path);
  172. println!("{:?}", res);
  173. assert!(res.is_ok());
  174. assert!(file_contains!(
  175. site_path,
  176. "public/posts/hard/index.html",
  177. "A simple page"
  178. ));
  179. }
  180. // https://github.com/Keats/gutenberg/issues/385
  181. #[test]
  182. fn can_rebuild_after_renaming_colocated_asset_folder() {
  183. let tmp_dir = tempdir().expect("create temp dir");
  184. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  185. let (old_path, new_path) = rename!(site_path, "content/posts/with-assets", "with-assets-updated");
  186. assert!(file_contains!(site_path, "content/posts/with-assets-updated/index.md", "Hello"));
  187. let res = after_content_rename(&mut site, &old_path, &new_path);
  188. println!("{:?}", res);
  189. assert!(res.is_ok());
  190. assert!(file_contains!(
  191. site_path,
  192. "public/posts/with-assets-updated/index.html",
  193. "Hello world"
  194. ));
  195. }
  196. // https://github.com/Keats/gutenberg/issues/385
  197. #[test]
  198. fn can_rebuild_after_renaming_section_folder() {
  199. let tmp_dir = tempdir().expect("create temp dir");
  200. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  201. let (old_path, new_path) = rename!(site_path, "content/posts", "new-posts");
  202. assert!(file_contains!(site_path, "content/new-posts/simple.md", "simple"));
  203. let res = after_content_rename(&mut site, &old_path, &new_path);
  204. assert!(res.is_ok());
  205. assert!(file_contains!(site_path, "public/new-posts/simple/index.html", "simple"));
  206. }