A library for writing host-specific, single-binary configuration management and deployment tools
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.

104 lines
3.7 KiB

8 years ago
  1. extern crate regex;
  2. extern crate schematics;
  3. extern crate tempdir;
  4. use regex::Regex;
  5. use std::fs::{create_dir, File};
  6. use std::path::Path;
  7. use tempdir::TempDir;
  8. use schematics::storage::{SimpleStorage, Storage};
  9. fn get_dir<'a, I: IntoIterator<Item=&'a &'a str>>(content: I) -> TempDir {
  10. let tmp_dir = TempDir::new("unittest").expect("create temp dir");
  11. let storage_path = tmp_dir.path().join("_filename");
  12. create_dir(storage_path.clone()).unwrap();
  13. for path in content {
  14. let file_path = storage_path.join(path);
  15. File::create(file_path).expect("create temp file");
  16. }
  17. tmp_dir
  18. }
  19. fn get_storage(path: &Path) -> SimpleStorage {
  20. SimpleStorage::new(path.to_str().unwrap().into(), "filename".into())
  21. }
  22. // Normal cases
  23. #[test]
  24. fn single_file() {
  25. let dir = get_dir(&["12345"]);
  26. let storage = get_storage(dir.path());
  27. assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
  28. assert_eq!(dir.path().join("_filename").join("12345"), Path::new(&storage.read_filename().unwrap()));
  29. assert_eq!(storage.recent_date().unwrap(), 12345);
  30. }
  31. #[test]
  32. fn two_files() {
  33. let dir = get_dir(&["12345", "23456"]);
  34. let storage = get_storage(dir.path());
  35. assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
  36. assert_eq!(dir.path().join("_filename").join("23456"), Path::new(&storage.read_filename().unwrap()));
  37. assert_eq!(storage.recent_date().unwrap(), 23456);
  38. }
  39. #[test]
  40. fn another_two_files() {
  41. let dir = get_dir(&["23456", "12345"]);
  42. let storage = get_storage(dir.path());
  43. assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
  44. assert_eq!(dir.path().join("_filename").join("23456"), Path::new(&storage.read_filename().unwrap()));
  45. assert_eq!(storage.recent_date().unwrap(), 23456);
  46. }
  47. #[test]
  48. fn three_files() {
  49. let dir = get_dir(&["23456", "9", "12345"]);
  50. let storage = get_storage(dir.path());
  51. assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
  52. assert_eq!(dir.path().join("_filename").join("23456"), Path::new(&storage.read_filename().unwrap()));
  53. assert_eq!(storage.recent_date().unwrap(), 23456);
  54. }
  55. // Bad cases
  56. #[test]
  57. fn empty_storage() {
  58. let dir = TempDir::new("unittest").expect("create temp dir");
  59. let storage = get_storage(dir.path());
  60. assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
  61. assert!(storage.read_filename().is_err());
  62. assert_eq!(storage.read_filename().unwrap_err().description(), "entity not found");
  63. assert!(storage.recent_date().is_err());
  64. assert_eq!(storage.recent_date().unwrap_err().description(), "entity not found");
  65. }
  66. #[test]
  67. fn empty_storage_for_filename() {
  68. let dir = get_dir(&[]);
  69. let storage = get_storage(dir.path());
  70. assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
  71. assert!(storage.read_filename().is_err());
  72. assert_eq!(storage.read_filename().unwrap_err().description(), "Not found");
  73. assert!(storage.recent_date().is_err());
  74. assert_eq!(storage.recent_date().unwrap_err().description(), "Not found");
  75. }
  76. #[test]
  77. fn bad_file() {
  78. let dir = get_dir(&["abba"]);
  79. let storage = get_storage(dir.path());
  80. assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
  81. assert!(storage.read_filename().is_err());
  82. assert_eq!(storage.read_filename().unwrap_err().description(), "Not found");
  83. assert!(storage.recent_date().is_err());
  84. assert_eq!(storage.recent_date().unwrap_err().description(), "Not found");
  85. }