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.

162 lines
4.0 KiB

7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
  1. extern crate regex;
  2. extern crate schematics;
  3. extern crate tempdir;
  4. use regex::Regex;
  5. use schematics::storage::{SimpleStorage, Storage};
  6. use std::fs::{create_dir, File};
  7. use std::path::Path;
  8. use tempdir::TempDir;
  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!(
  28. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  29. .unwrap()
  30. .is_match(&storage.write_filename())
  31. );
  32. assert_eq!(
  33. dir.path().join("_filename").join("12345"),
  34. Path::new(&storage.read_filename().unwrap())
  35. );
  36. assert_eq!(storage.recent_date().unwrap(), 12345);
  37. }
  38. #[test]
  39. fn two_files() {
  40. let dir = get_dir(&["12345", "23456"]);
  41. let storage = get_storage(dir.path());
  42. assert!(
  43. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  44. .unwrap()
  45. .is_match(&storage.write_filename())
  46. );
  47. assert_eq!(
  48. dir.path().join("_filename").join("23456"),
  49. Path::new(&storage.read_filename().unwrap())
  50. );
  51. assert_eq!(storage.recent_date().unwrap(), 23456);
  52. }
  53. #[test]
  54. fn another_two_files() {
  55. let dir = get_dir(&["23456", "12345"]);
  56. let storage = get_storage(dir.path());
  57. assert!(
  58. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  59. .unwrap()
  60. .is_match(&storage.write_filename())
  61. );
  62. assert_eq!(
  63. dir.path().join("_filename").join("23456"),
  64. Path::new(&storage.read_filename().unwrap())
  65. );
  66. assert_eq!(storage.recent_date().unwrap(), 23456);
  67. }
  68. #[test]
  69. fn three_files() {
  70. let dir = get_dir(&["23456", "9", "12345"]);
  71. let storage = get_storage(dir.path());
  72. assert!(
  73. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  74. .unwrap()
  75. .is_match(&storage.write_filename())
  76. );
  77. assert_eq!(
  78. dir.path().join("_filename").join("23456"),
  79. Path::new(&storage.read_filename().unwrap())
  80. );
  81. assert_eq!(storage.recent_date().unwrap(), 23456);
  82. }
  83. // Bad cases
  84. #[test]
  85. fn empty_storage() {
  86. let dir = TempDir::new("unittest").expect("create temp dir");
  87. let storage = get_storage(dir.path());
  88. assert!(
  89. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  90. .unwrap()
  91. .is_match(&storage.write_filename())
  92. );
  93. assert!(storage.read_filename().is_err());
  94. assert_eq!(
  95. storage.read_filename().unwrap_err().description(),
  96. "entity not found"
  97. );
  98. assert!(storage.recent_date().is_err());
  99. assert_eq!(
  100. storage.recent_date().unwrap_err().description(),
  101. "entity not found"
  102. );
  103. }
  104. #[test]
  105. fn empty_storage_for_filename() {
  106. let dir = get_dir(&[]);
  107. let storage = get_storage(dir.path());
  108. assert!(
  109. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  110. .unwrap()
  111. .is_match(&storage.write_filename())
  112. );
  113. assert!(storage.read_filename().is_err());
  114. assert_eq!(
  115. storage.read_filename().unwrap_err().description(),
  116. "Not found"
  117. );
  118. assert!(storage.recent_date().is_err());
  119. assert_eq!(
  120. storage.recent_date().unwrap_err().description(),
  121. "Not found"
  122. );
  123. }
  124. #[test]
  125. fn bad_file() {
  126. let dir = get_dir(&["abba"]);
  127. let storage = get_storage(dir.path());
  128. assert!(
  129. Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
  130. .unwrap()
  131. .is_match(&storage.write_filename())
  132. );
  133. assert!(storage.read_filename().is_err());
  134. assert_eq!(
  135. storage.read_filename().unwrap_err().description(),
  136. "Not found"
  137. );
  138. assert!(storage.recent_date().is_err());
  139. assert_eq!(
  140. storage.recent_date().unwrap_err().description(),
  141. "Not found"
  142. );
  143. }