cleanup test

This commit is contained in:
Cyryl Płotnicki 2018-10-04 14:48:41 +01:00
parent 25e887795b
commit 025a5011c3

View file

@ -61,7 +61,7 @@ struct RestoreEngine<'a> {
enum RestoreDescriptor { enum RestoreDescriptor {
All, All,
SpecificPath(String) SpecificPath(String),
} }
impl<'a> RestoreEngine<'a> { impl<'a> RestoreEngine<'a> {
@ -116,14 +116,15 @@ mod rustback {
use dir_diff::is_different; use dir_diff::is_different;
use std::fs::File; use std::fs::File;
use std::io::Error; use std::io::Error;
use std::io::Write;
use tempfile::tempdir;
use BackupEngine;
use RestoreEngine;
use std::path::Path;
use RestoreDescriptor;
use std::io::Read; use std::io::Read;
use std::io::Write;
use std::path::Path;
use tempfile::tempdir;
use tempfile::TempDir; use tempfile::TempDir;
use BackupEngine;
use RestoreDescriptor;
use RestoreDescriptor::SpecificPath;
use RestoreEngine;
#[test] #[test]
fn restore_backed_up_files() -> Result<(), Error> { fn restore_backed_up_files() -> Result<(), Error> {
@ -134,39 +135,42 @@ mod rustback {
source.write_text_to_file("second", "some contents"); source.write_text_to_file("second", "some contents");
source.write_text_to_file("third", "some other contents"); source.write_text_to_file("third", "some other contents");
is_same_after_restore(source.path(), repository.path()) assert_same_after_restore(source.path(), repository.path())
} }
#[test] #[test]
fn restore_older_version_of_file() -> Result<(), Error> { fn restore_older_version_of_file() -> Result<(), Error> {
let source = tempdir()?; let source = Source::new()?;
let repository = tempdir()?; let repository = tempdir()?;
let backup_engine = BackupEngine::new(source.path(), repository.path()); let backup_engine = BackupEngine::new(source.path(), repository.path());
let path = "some path"; let path = "some path";
let new_file_contents = "totally new contents"; let new_contents = "totally new contents";
let restore_target = tempdir()?; let restore_target = tempdir()?;
let restore_engine = RestoreEngine::new(repository.path(), &restore_target.path()); let restore_engine = RestoreEngine::new(repository.path(), &restore_target.path());
let old_contents = "some old contents"; let old_contents = "some old contents";
File::create(source.path().join(path))?.write_all(old_contents.as_bytes())?; source.write_text_to_file(path, old_contents)?;
backup_engine.backup()?; backup_engine.backup()?;
let old_version = backup_engine.file_version(path.as_ref()); let old_version = backup_engine.file_version(path.as_ref());
File::create(source.path().join(path))?.write_all(new_file_contents.as_bytes())?;
source.write_text_to_file(path, new_contents)?;
backup_engine.backup()?; backup_engine.backup()?;
restore_engine.restore_as_of_version(RestoreDescriptor::SpecificPath(path.into()), old_version)?; restore_engine.restore_as_of_version(SpecificPath(path.into()), old_version)?;
let restored_path = restore_target.path().join(path);
let mut actual_contents = String::new();
File::open(restored_path)?.read_to_string(&mut actual_contents)?;
assert_eq!(old_contents, actual_contents);
assert_target_file_contents(restore_target.path(), path, old_contents)?;
Ok(()) Ok(())
} }
fn assert_target_file_contents(target: &Path, filename: &str, expected_contents: &str) -> Result<(), Error> {
let restored_path = target.join(filename);
let mut actual_contents = String::new();
File::open(restored_path)?.read_to_string(&mut actual_contents)?;
assert_eq!(expected_contents, actual_contents);
Ok(())
}
fn is_same_after_restore(source_path: &Path, repository_path: &Path) -> Result<(), Error> { fn assert_same_after_restore(source_path: &Path, repository_path: &Path) -> Result<(), Error> {
let backup_engine = BackupEngine::new(source_path, repository_path); let backup_engine = BackupEngine::new(source_path, repository_path);
backup_engine.backup()?; backup_engine.backup()?;
@ -180,14 +184,12 @@ mod rustback {
} }
struct Source { struct Source {
directory: TempDir directory: TempDir,
} }
impl Source { impl Source {
fn new() -> Result<Self, Error> { fn new() -> Result<Self, Error> {
Ok(Self { Ok(Self { directory: tempdir()? })
directory: tempdir()?
})
} }
fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> { fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> {