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.

127 lines
3.7KB

  1. extern crate rebuild;
  2. extern crate site;
  3. extern crate tempfile;
  4. extern crate fs_extra;
  5. use std::env;
  6. use std::fs::{remove_dir_all, File};
  7. use std::io::prelude::*;
  8. use fs_extra::dir;
  9. use tempfile::tempdir;
  10. use site::Site;
  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. {
  17. let mut path = 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. }
  35. /// Replace the file at the path (starting from root) by the given content
  36. /// and return the file path that was modified
  37. macro_rules! edit_file {
  38. ($site_path: expr, $path: expr, $content: expr) => {
  39. {
  40. let mut t = $site_path.clone();
  41. for c in $path.split('/') {
  42. t.push(c);
  43. }
  44. let mut file = File::create(&t).expect("Could not open/create file");
  45. file.write_all($content).expect("Could not write to the file");
  46. t
  47. }
  48. }
  49. }
  50. macro_rules! file_contains {
  51. ($site_path: expr, $path: expr, $text: expr) => {
  52. {
  53. let mut path = $site_path.clone();
  54. for component in $path.split("/") {
  55. path.push(component);
  56. }
  57. let mut file = File::open(&path).unwrap();
  58. let mut s = String::new();
  59. file.read_to_string(&mut s).unwrap();
  60. println!("{:?} -> {}", path, s);
  61. s.contains($text)
  62. }
  63. }
  64. }
  65. #[test]
  66. fn can_rebuild_after_simple_change_to_page_content() {
  67. let tmp_dir = tempdir().expect("create temp dir");
  68. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  69. let file_path = edit_file!(site_path, "content/rebuild/first.md", br#"
  70. +++
  71. title = "first"
  72. weight = 1
  73. date = 2017-01-01
  74. +++
  75. Some content"#);
  76. let res = after_content_change(&mut site, &file_path);
  77. assert!(res.is_ok());
  78. assert!(file_contains!(site_path, "public/rebuild/first/index.html", "<p>Some content</p>"));
  79. }
  80. #[test]
  81. fn can_rebuild_after_title_change_page_global_func_usage() {
  82. let tmp_dir = tempdir().expect("create temp dir");
  83. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  84. let file_path = edit_file!(site_path, "content/rebuild/first.md", br#"
  85. +++
  86. title = "Premier"
  87. weight = 10
  88. date = 2017-01-01
  89. +++
  90. # A title"#);
  91. let res = after_content_change(&mut site, &file_path);
  92. assert!(res.is_ok());
  93. assert!(file_contains!(site_path, "public/rebuild/index.html", "<h1>Premier</h1>"));
  94. }
  95. #[test]
  96. fn can_rebuild_after_sort_change_in_section() {
  97. let tmp_dir = tempdir().expect("create temp dir");
  98. let (site_path, mut site) = load_and_build_site!(tmp_dir);
  99. let file_path = edit_file!(site_path, "content/rebuild/_index.md", br#"
  100. +++
  101. paginate_by = 1
  102. sort_by = "weight"
  103. template = "rebuild.html"
  104. +++
  105. "#);
  106. let res = after_content_change(&mut site, &file_path);
  107. assert!(res.is_ok());
  108. assert!(file_contains!(site_path, "public/rebuild/index.html", "<h1>first</h1><h1>second</h1>"));
  109. }