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