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
152 lines
4.0 KiB
use regex::Regex;
|
|
use schematics::storage::{SimpleStorage, Storage};
|
|
use std::fs::{create_dir, File};
|
|
use std::path::Path;
|
|
use tempfile::{tempdir, TempDir};
|
|
|
|
fn get_dir<'a, I: IntoIterator<Item = &'a &'a str>>(content: I) -> TempDir {
|
|
let tmp_dir = tempdir().expect("create temp dir");
|
|
let storage_path = tmp_dir.path().join("_filename");
|
|
create_dir(storage_path.clone()).unwrap();
|
|
for path in content {
|
|
let file_path = storage_path.join(path);
|
|
File::create(file_path).expect("create temp file");
|
|
}
|
|
tmp_dir
|
|
}
|
|
|
|
fn get_storage(path: &Path) -> SimpleStorage {
|
|
SimpleStorage::new(path.join("_filename").into())
|
|
}
|
|
|
|
// Normal cases
|
|
|
|
#[test]
|
|
fn single_file() {
|
|
let dir = get_dir(&["12345"]);
|
|
let storage = get_storage(dir.path());
|
|
|
|
assert!(
|
|
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
|
|
.unwrap()
|
|
.is_match(storage.write_filename().to_str().unwrap())
|
|
);
|
|
assert_eq!(
|
|
dir.path().join("_filename").join("12345"),
|
|
Path::new(&*storage.read_filename().unwrap())
|
|
);
|
|
assert_eq!(storage.recent_date().unwrap(), 12345);
|
|
}
|
|
|
|
#[test]
|
|
fn two_files() {
|
|
let dir = get_dir(&["12345", "23456"]);
|
|
let storage = get_storage(dir.path());
|
|
|
|
assert!(
|
|
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
|
|
.unwrap()
|
|
.is_match(storage.write_filename().to_str().unwrap())
|
|
);
|
|
assert_eq!(
|
|
dir.path().join("_filename").join("23456"),
|
|
Path::new(&*storage.read_filename().unwrap())
|
|
);
|
|
assert_eq!(storage.recent_date().unwrap(), 23456);
|
|
}
|
|
|
|
#[test]
|
|
fn another_two_files() {
|
|
let dir = get_dir(&["23456", "12345"]);
|
|
let storage = get_storage(dir.path());
|
|
|
|
assert!(
|
|
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
|
|
.unwrap()
|
|
.is_match(storage.write_filename().to_str().unwrap())
|
|
);
|
|
assert_eq!(
|
|
dir.path().join("_filename").join("23456"),
|
|
Path::new(&*storage.read_filename().unwrap())
|
|
);
|
|
assert_eq!(storage.recent_date().unwrap(), 23456);
|
|
}
|
|
|
|
#[test]
|
|
fn three_files() {
|
|
let dir = get_dir(&["23456", "9", "12345"]);
|
|
let storage = get_storage(dir.path());
|
|
|
|
assert!(
|
|
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
|
|
.unwrap()
|
|
.is_match(storage.write_filename().to_str().unwrap())
|
|
);
|
|
assert_eq!(
|
|
dir.path().join("_filename").join("23456"),
|
|
Path::new(&*storage.read_filename().unwrap())
|
|
);
|
|
assert_eq!(storage.recent_date().unwrap(), 23456);
|
|
}
|
|
|
|
// Bad cases
|
|
|
|
#[test]
|
|
fn empty_storage() {
|
|
let dir = tempdir().expect("create temp dir");
|
|
let storage = get_storage(dir.path());
|
|
|
|
assert!(
|
|
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
|
|
.unwrap()
|
|
.is_match(storage.write_filename().to_str().unwrap())
|
|
);
|
|
assert!(storage.read_filename().is_err());
|
|
assert_eq!(
|
|
storage.read_filename().unwrap_err().to_string(),
|
|
"No such file or directory (os error 2)"
|
|
);
|
|
assert!(storage.recent_date().is_err());
|
|
assert_eq!(
|
|
storage.recent_date().unwrap_err().to_string(),
|
|
"No such file or directory (os error 2)"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_storage_for_filename() {
|
|
let dir = get_dir(&[]);
|
|
let storage = get_storage(dir.path());
|
|
|
|
assert!(
|
|
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
|
|
.unwrap()
|
|
.is_match(storage.write_filename().to_str().unwrap())
|
|
);
|
|
assert!(storage.read_filename().is_err());
|
|
assert_eq!(
|
|
storage.read_filename().unwrap_err().to_string(),
|
|
"Not found"
|
|
);
|
|
assert!(storage.recent_date().is_err());
|
|
assert_eq!(storage.recent_date().unwrap_err().to_string(), "Not found");
|
|
}
|
|
|
|
#[test]
|
|
fn bad_file() {
|
|
let dir = get_dir(&["abba"]);
|
|
let storage = get_storage(dir.path());
|
|
|
|
assert!(
|
|
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
|
|
.unwrap()
|
|
.is_match(storage.write_filename().to_str().unwrap())
|
|
);
|
|
assert!(storage.read_filename().is_err());
|
|
assert_eq!(
|
|
storage.read_filename().unwrap_err().to_string(),
|
|
"Not found"
|
|
);
|
|
assert!(storage.recent_date().is_err());
|
|
assert_eq!(storage.recent_date().unwrap_err().to_string(), "Not found");
|
|
}
|