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.

158 lines
3.9 KiB

8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 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 tempdir::TempDir;
  6. fn get_dir<'a, I: IntoIterator<Item = &'a &'a str>>(content: I) -> TempDir {
  7. let tmp_dir = TempDir::new("unittest").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.to_str().unwrap().into(), "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())
  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())
  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())
  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())
  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::new("unittest").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())
  89. );
  90. assert!(storage.read_filename().is_err());
  91. assert_eq!(
  92. storage.read_filename().unwrap_err().description(),
  93. "entity not found"
  94. );
  95. assert!(storage.recent_date().is_err());
  96. assert_eq!(
  97. storage.recent_date().unwrap_err().description(),
  98. "entity not found"
  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())
  109. );
  110. assert!(storage.read_filename().is_err());
  111. assert_eq!(
  112. storage.read_filename().unwrap_err().description(),
  113. "Not found"
  114. );
  115. assert!(storage.recent_date().is_err());
  116. assert_eq!(
  117. storage.recent_date().unwrap_err().description(),
  118. "Not found"
  119. );
  120. }
  121. #[test]
  122. fn bad_file() {
  123. let dir = get_dir(&["abba"]);
  124. let storage = get_storage(dir.path());
  125. assert!(
  126. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  127. .unwrap()
  128. .is_match(&storage.write_filename())
  129. );
  130. assert!(storage.read_filename().is_err());
  131. assert_eq!(
  132. storage.read_filename().unwrap_err().description(),
  133. "Not found"
  134. );
  135. assert!(storage.recent_date().is_err());
  136. assert_eq!(
  137. storage.recent_date().unwrap_err().description(),
  138. "Not found"
  139. );
  140. }