From a385d3c8a94b7afcad3a6087053294f0561cd349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Thu, 4 Oct 2018 14:23:13 +0100 Subject: [PATCH] Failing test for support for older versions --- src/main.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index c998680..e0bf5cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,10 @@ impl<'a> BackupEngine<'a> { Ok(()) } + fn file_version(&self, path: &Path) -> u64 { + 0 + } + fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> { if entry.file_type().is_dir() { fs::create_dir(self.repository_path.join(entry.file_name()))?; @@ -55,6 +59,11 @@ struct RestoreEngine<'a> { target_path: &'a Path, } +enum RestoreDescriptor { + All, + SpecificPath(String) +} + impl<'a> RestoreEngine<'a> { fn new(repository_path: &'a Path, target_path: &'a Path) -> Self { RestoreEngine { @@ -63,7 +72,15 @@ impl<'a> RestoreEngine<'a> { } } - fn restore(&self) -> Result<(), io::Error> { + fn restore_all(&self) -> Result<(), io::Error> { + self.restore(RestoreDescriptor::All) + } + + fn restore(&self, what: RestoreDescriptor) -> Result<(), io::Error> { + self.restore_as_of_version(what, 0) + } + + fn restore_as_of_version(&self, what: RestoreDescriptor, version: u64) -> Result<(), io::Error> { let walker = WalkDir::new(self.repository_path); for maybe_entry in walker { match maybe_entry { @@ -104,20 +121,48 @@ mod rustback { use BackupEngine; use RestoreEngine; use std::path::Path; + use RestoreDescriptor; + use std::io::Read; #[test] fn restore_backed_up_files() -> Result<(), Error> { let source = tempdir()?; + let repository = tempdir()?; File::create(source.path().join("first"))?.write_all("some contents".as_bytes())?; File::create(source.path().join("second"))?.write_all("some contents".as_bytes())?; File::create(source.path().join("third"))?.write_all("some other contents".as_bytes())?; - let repository = tempdir()?; - is_same_after_restore(source.path(), repository.path()) } + #[test] + fn restore_older_version_of_file() -> Result<(), Error> { + let source = tempdir()?; + let repository = tempdir()?; + let backup_engine = BackupEngine::new(source.path(), repository.path()); + let path = "first"; + let new_file_contents = "totally new contents"; + let restore_target = tempdir()?; + let restore_engine = RestoreEngine::new(repository.path(), &restore_target.path()); + let old_contents = "some contents"; + + File::create(source.path().join(path))?.write_all(old_contents.as_bytes())?; + 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())?; + 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); + + Ok(()) + } fn is_same_after_restore(source_path: &Path, repository_path: &Path) -> Result<(), Error> { let backup_engine = BackupEngine::new(source_path, repository_path); @@ -125,7 +170,7 @@ mod rustback { let restore_target = tempdir()?; let restore_engine = RestoreEngine::new(repository_path, &restore_target.path()); - restore_engine.restore()?; + restore_engine.restore_all()?; let are_source_and_target_different = is_different(source_path, &restore_target.path()).unwrap(); assert!(!are_source_and_target_different);