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.

152 lines
4.0 KiB

8 years ago
5 years ago
8 years ago
8 years ago
5 years ago
8 years ago
8 years ago
5 years ago
5 years ago
5 years ago
8 years ago
5 years ago
5 years ago
5 years ago
8 years ago
5 years ago
5 years ago
5 years ago
8 years ago
5 years ago
5 years ago
5 years ago
8 years ago
8 years ago
5 years ago
5 years ago
8 years ago
5 years ago
4 years ago
5 years ago
8 years ago
5 years ago
4 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 years ago
8 years ago
5 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 years ago
8 years ago
  1. use regex::Regex;
  2. use schematics::storage::{SimpleStorage, Storage};
  3. use std::fs::{create_dir, File};
  4. use std::path::Path;
  5. use tempfile::{tempdir, TempDir};
  6. fn get_dir<'a, I: IntoIterator<Item = &'a &'a str>>(content: I) -> TempDir {
  7. let tmp_dir = tempdir().expect("create temp dir");
  8. let storage_path = tmp_dir.path().join("_filename");
  9. create_dir(storage_path.clone()).unwrap();
  10. for path in content {
  11. let file_path = storage_path.join(path);
  12. File::create(file_path).expect("create temp file");
  13. }
  14. tmp_dir
  15. }
  16. fn get_storage(path: &Path) -> SimpleStorage {
  17. SimpleStorage::new(path.join("_filename").into())
  18. }
  19. // Normal cases
  20. #[test]
  21. fn single_file() {
  22. let dir = get_dir(&["12345"]);
  23. let storage = get_storage(dir.path());
  24. assert!(
  25. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  26. .unwrap()
  27. .is_match(storage.write_filename().to_str().unwrap())
  28. );
  29. assert_eq!(
  30. dir.path().join("_filename").join("12345"),
  31. Path::new(&*storage.read_filename().unwrap())
  32. );
  33. assert_eq!(storage.recent_date().unwrap(), 12345);
  34. }
  35. #[test]
  36. fn two_files() {
  37. let dir = get_dir(&["12345", "23456"]);
  38. let storage = get_storage(dir.path());
  39. assert!(
  40. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  41. .unwrap()
  42. .is_match(storage.write_filename().to_str().unwrap())
  43. );
  44. assert_eq!(
  45. dir.path().join("_filename").join("23456"),
  46. Path::new(&*storage.read_filename().unwrap())
  47. );
  48. assert_eq!(storage.recent_date().unwrap(), 23456);
  49. }
  50. #[test]
  51. fn another_two_files() {
  52. let dir = get_dir(&["23456", "12345"]);
  53. let storage = get_storage(dir.path());
  54. assert!(
  55. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  56. .unwrap()
  57. .is_match(storage.write_filename().to_str().unwrap())
  58. );
  59. assert_eq!(
  60. dir.path().join("_filename").join("23456"),
  61. Path::new(&*storage.read_filename().unwrap())
  62. );
  63. assert_eq!(storage.recent_date().unwrap(), 23456);
  64. }
  65. #[test]
  66. fn three_files() {
  67. let dir = get_dir(&["23456", "9", "12345"]);
  68. let storage = get_storage(dir.path());
  69. assert!(
  70. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  71. .unwrap()
  72. .is_match(storage.write_filename().to_str().unwrap())
  73. );
  74. assert_eq!(
  75. dir.path().join("_filename").join("23456"),
  76. Path::new(&*storage.read_filename().unwrap())
  77. );
  78. assert_eq!(storage.recent_date().unwrap(), 23456);
  79. }
  80. // Bad cases
  81. #[test]
  82. fn empty_storage() {
  83. let dir = tempdir().expect("create temp dir");
  84. let storage = get_storage(dir.path());
  85. assert!(
  86. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  87. .unwrap()
  88. .is_match(storage.write_filename().to_str().unwrap())
  89. );
  90. assert!(storage.read_filename().is_err());
  91. assert_eq!(
  92. storage.read_filename().unwrap_err().to_string(),
  93. "No such file or directory (os error 2)"
  94. );
  95. assert!(storage.recent_date().is_err());
  96. assert_eq!(
  97. storage.recent_date().unwrap_err().to_string(),
  98. "No such file or directory (os error 2)"
  99. );
  100. }
  101. #[test]
  102. fn empty_storage_for_filename() {
  103. let dir = get_dir(&[]);
  104. let storage = get_storage(dir.path());
  105. assert!(
  106. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  107. .unwrap()
  108. .is_match(storage.write_filename().to_str().unwrap())
  109. );
  110. assert!(storage.read_filename().is_err());
  111. assert_eq!(
  112. storage.read_filename().unwrap_err().to_string(),
  113. "Not found"
  114. );
  115. assert!(storage.recent_date().is_err());
  116. assert_eq!(storage.recent_date().unwrap_err().to_string(), "Not found");
  117. }
  118. #[test]
  119. fn bad_file() {
  120. let dir = get_dir(&["abba"]);
  121. let storage = get_storage(dir.path());
  122. assert!(
  123. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  124. .unwrap()
  125. .is_match(storage.write_filename().to_str().unwrap())
  126. );
  127. assert!(storage.read_filename().is_err());
  128. assert_eq!(
  129. storage.read_filename().unwrap_err().to_string(),
  130. "Not found"
  131. );
  132. assert!(storage.recent_date().is_err());
  133. assert_eq!(storage.recent_date().unwrap_err().to_string(), "Not found");
  134. }