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.3KB

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