Add filelike backend tests and fix inverted behaviour of FileBackend::from_path_or_create_and

This commit is contained in:
niluxv 2020-07-12 09:59:05 +02:00
parent 33dde1de47
commit 742de8bf24
2 changed files with 81 additions and 5 deletions

View file

@ -148,7 +148,7 @@ impl FileBackend {
P: AsRef<std::path::Path>,
{
Self::from_path_or_create(path).map(|(mut b, exists)| {
if exists {
if !exists {
closure(&mut b.0)
}
b
@ -187,7 +187,7 @@ impl Backend for MemoryBackend {
mod tests {
use super::{Backend, FileBackend, MemoryBackend};
use failure::Fail;
use std::io::{Read, Seek, SeekFrom};
use std::io::{Read, Seek, SeekFrom, Write};
use tempfile::NamedTempFile;
#[test]
@ -218,8 +218,8 @@ mod tests {
#[cfg_attr(miri, ignore)]
fn test_file_backend_from_path_existing() {
let file = NamedTempFile::new().expect("could not create temporary file");
let (mut backend, existed) = FileBackend::from_path_or_create(file.path().to_owned())
.expect("could not create backend");
let (mut backend, existed) =
FileBackend::from_path_or_create(file.path()).expect("could not create backend");
assert!(existed);
let data = [4, 5, 1, 6, 8, 1];
@ -298,4 +298,42 @@ mod tests {
backend.put_data(&data).unwrap();
assert_eq!(backend.get_data().unwrap(), data);
}
// If the file already exists, the closure shouldn't be called.
#[test]
#[cfg_attr(miri, ignore)]
fn test_file_backend_create_and_existing_nocall() {
let file = NamedTempFile::new().expect("could not create temporary file");
let mut backend = FileBackend::from_path_or_create_and(file.path(), |_| {
panic!("Closure called but file already existed");
})
.expect("could not create backend");
let data = [4, 5, 1, 6, 8, 1];
backend.put_data(&data).expect("could not put data");
assert_eq!(backend.get_data().expect("could not get data"), data);
}
// If the file does not yet exist, the closure should be called.
#[test]
#[cfg_attr(miri, ignore)]
fn test_file_backend_create_and_new() {
let dir = tempfile::tempdir().expect("could not create temporary directory");
let mut file_path = dir.path().to_owned();
file_path.push("rustbreak_path_db.db");
let mut backend = FileBackend::from_path_or_create_and(file_path, |f| {
f.write_all(b"this is a new file")
.expect("could not write to file")
})
.expect("could not create backend");
assert_eq!(
backend.get_data().expect("could not get data"),
b"this is a new file"
);
let data = [4, 5, 1, 6, 8, 1];
backend.put_data(&data).expect("could not put data");
assert_eq!(backend.get_data().expect("could not get data"), data);
dir.close().expect("Error while deleting temp directory!");
}
}

View file

@ -103,7 +103,7 @@ impl Backend for PathBackend {
mod tests {
use super::{Backend, PathBackend};
use failure::Fail;
//use std::any::Any;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
@ -164,4 +164,42 @@ mod tests {
assert_eq!(std::io::ErrorKind::NotFound, io_err.kind());
dir.close().expect("Error while deleting temp directory!");
}
// If the file already exists, the closure shouldn't be called.
#[test]
#[cfg_attr(miri, ignore)]
fn test_path_backend_create_and_existing_nocall() {
let file = NamedTempFile::new().expect("could not create temporary file");
let mut backend = PathBackend::from_path_or_create_and(file.path().to_owned(), |_| {
panic!("Closure called but file already existed");
})
.expect("could not create backend");
let data = [4, 5, 1, 6, 8, 1];
backend.put_data(&data).expect("could not put data");
assert_eq!(backend.get_data().expect("could not get data"), data);
}
// If the file does not yet exist, the closure should be called.
#[test]
#[cfg_attr(miri, ignore)]
fn test_path_backend_create_and_new() {
let dir = tempfile::tempdir().expect("could not create temporary directory");
let mut file_path = dir.path().to_owned();
file_path.push("rustbreak_path_db.db");
let mut backend = PathBackend::from_path_or_create_and(file_path, |f| {
f.write_all(b"this is a new file")
.expect("could not write to file")
})
.expect("could not create backend");
assert_eq!(
backend.get_data().expect("could not get data"),
b"this is a new file"
);
let data = [4, 5, 1, 6, 8, 1];
backend.put_data(&data).expect("could not put data");
assert_eq!(backend.get_data().expect("could not get data"), data);
dir.close().expect("Error while deleting temp directory!");
}
}