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.

138 lines
3.7KB

  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::{remove_dir_all, 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;
  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. // delete useless sections for those tests
  24. remove_dir_all(site_path.join("content").join("paginated")).unwrap();
  25. remove_dir_all(site_path.join("content").join("posts")).unwrap();
  26. let mut site = Site::new(&site_path, "config.toml").unwrap();
  27. site.load().unwrap();
  28. let public = &site_path.join("public");
  29. site.set_output_path(&public);
  30. site.build().unwrap();
  31. (site_path, site)
  32. }};
  33. }
  34. /// Replace the file at the path (starting from root) by the given content
  35. /// and return the file path that was modified
  36. macro_rules! edit_file {
  37. ($site_path: expr, $path: expr, $content: expr) => {{
  38. let mut t = $site_path.clone();
  39. for c in $path.split('/') {
  40. t.push(c);
  41. }
  42. let mut file = File::create(&t).expect("Could not open/create file");
  43. file.write_all($content).expect("Could not write to the file");
  44. t
  45. }};
  46. }
  47. macro_rules! file_contains {
  48. ($site_path: expr, $path: expr, $text: expr) => {{
  49. let mut path = $site_path.clone();
  50. for component in $path.split("/") {
  51. path.push(component);
  52. }
  53. let mut file = File::open(&path).unwrap();
  54. let mut s = String::new();
  55. file.read_to_string(&mut s).unwrap();
  56. println!("{:?} -> {}", path, s);
  57. s.contains($text)
  58. }};
  59. }
  60. #[test]
  61. fn can_rebuild_after_simple_change_to_page_content() {
  62. let tmp_dir = tempdir().expect("create temp dir");
  63. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  64. let file_path = edit_file!(
  65. site_path,
  66. "content/rebuild/first.md",
  67. br#"
  68. +++
  69. title = "first"
  70. weight = 1
  71. date = 2017-01-01
  72. +++
  73. Some content"#
  74. );
  75. let res = after_content_change(&mut site, &file_path);
  76. assert!(res.is_ok());
  77. assert!(file_contains!(site_path, "public/rebuild/first/index.html", "<p>Some content</p>"));
  78. }
  79. #[test]
  80. fn can_rebuild_after_title_change_page_global_func_usage() {
  81. let tmp_dir = tempdir().expect("create temp dir");
  82. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  83. let file_path = edit_file!(
  84. site_path,
  85. "content/rebuild/first.md",
  86. br#"
  87. +++
  88. title = "Premier"
  89. weight = 10
  90. date = 2017-01-01
  91. +++
  92. # A title"#
  93. );
  94. let res = after_content_change(&mut site, &file_path);
  95. assert!(res.is_ok());
  96. assert!(file_contains!(site_path, "public/rebuild/index.html", "<h1>Premier</h1>"));
  97. }
  98. #[test]
  99. fn can_rebuild_after_sort_change_in_section() {
  100. let tmp_dir = tempdir().expect("create temp dir");
  101. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  102. let file_path = edit_file!(
  103. site_path,
  104. "content/rebuild/_index.md",
  105. br#"
  106. +++
  107. paginate_by = 1
  108. sort_by = "weight"
  109. template = "rebuild.html"
  110. +++
  111. "#
  112. );
  113. let res = after_content_change(&mut site, &file_path);
  114. assert!(res.is_ok());
  115. assert!(file_contains!(
  116. site_path,
  117. "public/rebuild/index.html",
  118. "<h1>first</h1><h1>second</h1>"
  119. ));
  120. }