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.

common.rs 2.2KB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. extern crate site;
  2. extern crate tempfile;
  3. use std::env;
  4. use std::path::PathBuf;
  5. use self::site::Site;
  6. use self::tempfile::{tempdir, TempDir};
  7. // 2 helper macros to make all the build testing more bearable
  8. #[macro_export]
  9. macro_rules! file_exists {
  10. ($root: expr, $path: expr) => {{
  11. let mut path = $root.clone();
  12. for component in $path.split("/") {
  13. path = path.join(component);
  14. }
  15. std::path::Path::new(&path).exists()
  16. }};
  17. }
  18. #[macro_export]
  19. macro_rules! file_contains {
  20. ($root: expr, $path: expr, $text: expr) => {{
  21. use std::io::prelude::*;
  22. let mut path = $root.clone();
  23. for component in $path.split("/") {
  24. path = path.join(component);
  25. }
  26. let mut file = std::fs::File::open(&path).unwrap();
  27. let mut s = String::new();
  28. file.read_to_string(&mut s).unwrap();
  29. // println!("{}", s);
  30. s.contains($text)
  31. }};
  32. }
  33. /// We return the tmpdir otherwise it would get out of scope and be deleted
  34. /// The tests can ignore it if they dont need it by prefixing it with a `_`
  35. pub fn build_site(name: &str) -> (Site, TempDir, PathBuf) {
  36. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  37. path.push(name);
  38. let mut site = Site::new(&path, "config.toml").unwrap();
  39. site.load().unwrap();
  40. let tmp_dir = tempdir().expect("create temp dir");
  41. let public = &tmp_dir.path().join("public");
  42. site.set_output_path(&public);
  43. site.build().unwrap();
  44. (site, tmp_dir, public.clone())
  45. }
  46. /// Same as `build_site` but has a hook to setup some config options
  47. pub fn build_site_with_setup<F>(name: &str, mut setup_cb: F) -> (Site, TempDir, PathBuf)
  48. where
  49. F: FnMut(Site) -> (Site, bool),
  50. {
  51. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  52. path.push(name);
  53. let site = Site::new(&path, "config.toml").unwrap();
  54. let (mut site, needs_loading) = setup_cb(site);
  55. if needs_loading {
  56. site.load().unwrap();
  57. }
  58. let tmp_dir = tempdir().expect("create temp dir");
  59. let public = &tmp_dir.path().join("public");
  60. site.set_output_path(&public);
  61. site.build().unwrap();
  62. (site, tmp_dir, public.clone())
  63. }