diff --git a/src/main.rs b/src/main.rs index cda05f8..de476e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ struct RestoreEngine<'a> { enum RestoreDescriptor { All, - SpecificPath(String) + SpecificPath(String), } impl<'a> RestoreEngine<'a> { @@ -116,14 +116,15 @@ mod rustback { use dir_diff::is_different; use std::fs::File; 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::Write; + use std::path::Path; + use tempfile::tempdir; use tempfile::TempDir; + use BackupEngine; + use RestoreDescriptor; + use RestoreDescriptor::SpecificPath; + use RestoreEngine; #[test] 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("third", "some other contents"); - is_same_after_restore(source.path(), repository.path()) + assert_same_after_restore(source.path(), repository.path()) } #[test] fn restore_older_version_of_file() -> Result<(), Error> { - let source = tempdir()?; + let source = Source::new()?; let repository = tempdir()?; let backup_engine = BackupEngine::new(source.path(), repository.path()); let path = "some path"; - let new_file_contents = "totally new contents"; + let new_contents = "totally new contents"; let restore_target = tempdir()?; let restore_engine = RestoreEngine::new(repository.path(), &restore_target.path()); 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()?; 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()?; - restore_engine.restore_as_of_version(RestoreDescriptor::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); + restore_engine.restore_as_of_version(SpecificPath(path.into()), old_version)?; + assert_target_file_contents(restore_target.path(), path, old_contents)?; 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); backup_engine.backup()?; @@ -180,14 +184,12 @@ mod rustback { } struct Source { - directory: TempDir + directory: TempDir, } impl Source { fn new() -> Result { - Ok(Self { - directory: tempdir()? - }) + Ok(Self { directory: tempdir()? }) } fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> {