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