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.

289 lines
8.6KB

  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, $site: expr) => {{
  16. let mut path =
  17. env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  18. path.push($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($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, "test_site");
  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, "test_site");
  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, "test_site");
  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, "test_site");
  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!(site_path, "public/posts/index.html", "A transparent page"));
  161. }
  162. #[test]
  163. fn can_rebuild_after_renaming_page() {
  164. let tmp_dir = tempdir().expect("create temp dir");
  165. let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
  166. let (old_path, new_path) = rename!(site_path, "content/posts/simple.md", "hard.md");
  167. let res = after_content_rename(&mut site, &old_path, &new_path);
  168. println!("{:?}", res);
  169. assert!(res.is_ok());
  170. assert!(file_contains!(site_path, "public/posts/hard/index.html", "A simple page"));
  171. }
  172. // https://github.com/Keats/gutenberg/issues/385
  173. #[test]
  174. fn can_rebuild_after_renaming_colocated_asset_folder() {
  175. let tmp_dir = tempdir().expect("create temp dir");
  176. let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
  177. let (old_path, new_path) =
  178. rename!(site_path, "content/posts/with-assets", "with-assets-updated");
  179. assert!(file_contains!(site_path, "content/posts/with-assets-updated/index.md", "Hello"));
  180. let res = after_content_rename(&mut site, &old_path, &new_path);
  181. println!("{:?}", res);
  182. assert!(res.is_ok());
  183. assert!(file_contains!(
  184. site_path,
  185. "public/posts/with-assets-updated/index.html",
  186. "Hello world"
  187. ));
  188. }
  189. // https://github.com/Keats/gutenberg/issues/385
  190. #[test]
  191. fn can_rebuild_after_renaming_section_folder() {
  192. let tmp_dir = tempdir().expect("create temp dir");
  193. let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
  194. let (old_path, new_path) = rename!(site_path, "content/posts", "new-posts");
  195. assert!(file_contains!(site_path, "content/new-posts/simple.md", "simple"));
  196. let res = after_content_rename(&mut site, &old_path, &new_path);
  197. assert!(res.is_ok());
  198. assert!(file_contains!(site_path, "public/new-posts/simple/index.html", "simple"));
  199. }
  200. #[test]
  201. fn can_rebuild_after_renaming_non_md_asset_in_colocated_folder() {
  202. let tmp_dir = tempdir().expect("create temp dir");
  203. let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
  204. let (old_path, new_path) =
  205. rename!(site_path, "content/posts/with-assets/zola.png", "gutenberg.png");
  206. // Testing that we don't try to load some images as markdown or something
  207. let res = after_content_rename(&mut site, &old_path, &new_path);
  208. assert!(res.is_ok());
  209. }
  210. #[test]
  211. fn can_rebuild_after_deleting_file() {
  212. let tmp_dir = tempdir().expect("create temp dir");
  213. let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
  214. let path = site_path.join("content").join("posts").join("fixed-slug.md");
  215. fs::remove_file(&path).unwrap();
  216. let res = after_content_change(&mut site, &path);
  217. println!("{:?}", res);
  218. assert!(res.is_ok());
  219. }
  220. #[test]
  221. fn can_rebuild_after_editing_in_colocated_asset_folder_with_language() {
  222. let tmp_dir = tempdir().expect("create temp dir");
  223. let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site_i18n");
  224. let file_path = edit_file!(
  225. site_path,
  226. "content/blog/with-assets/index.fr.md",
  227. br#"
  228. +++
  229. date = 2018-11-11
  230. +++
  231. Edite
  232. "#
  233. );
  234. let res = after_content_change(&mut site, &file_path);
  235. println!("{:?}", res);
  236. assert!(res.is_ok());
  237. assert!(file_contains!(site_path, "public/fr/blog/with-assets/index.html", "Edite"));
  238. }
  239. // https://github.com/getzola/zola/issues/620
  240. #[test]
  241. fn can_rebuild_after_renaming_section_and_deleting_file() {
  242. let tmp_dir = tempdir().expect("create temp dir");
  243. let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site");
  244. let (old_path, new_path) = rename!(site_path, "content/posts/", "post/");
  245. let res = after_content_rename(&mut site, &old_path, &new_path);
  246. assert!(res.is_ok());
  247. let path = site_path.join("content").join("_index.md");
  248. fs::remove_file(&path).unwrap();
  249. let res = after_content_change(&mut site, &path);
  250. println!("{:?}", res);
  251. assert!(res.is_ok());
  252. }