Failing test for support for older versions
This commit is contained in:
parent
3b78a41deb
commit
a385d3c8a9
1 changed files with 49 additions and 4 deletions
53
src/main.rs
53
src/main.rs
|
@ -39,6 +39,10 @@ impl<'a> BackupEngine<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn file_version(&self, path: &Path) -> u64 {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> {
|
fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> {
|
||||||
if entry.file_type().is_dir() {
|
if entry.file_type().is_dir() {
|
||||||
fs::create_dir(self.repository_path.join(entry.file_name()))?;
|
fs::create_dir(self.repository_path.join(entry.file_name()))?;
|
||||||
|
@ -55,6 +59,11 @@ struct RestoreEngine<'a> {
|
||||||
target_path: &'a Path,
|
target_path: &'a Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum RestoreDescriptor {
|
||||||
|
All,
|
||||||
|
SpecificPath(String)
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> RestoreEngine<'a> {
|
impl<'a> RestoreEngine<'a> {
|
||||||
fn new(repository_path: &'a Path, target_path: &'a Path) -> Self {
|
fn new(repository_path: &'a Path, target_path: &'a Path) -> Self {
|
||||||
RestoreEngine {
|
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);
|
let walker = WalkDir::new(self.repository_path);
|
||||||
for maybe_entry in walker {
|
for maybe_entry in walker {
|
||||||
match maybe_entry {
|
match maybe_entry {
|
||||||
|
@ -104,20 +121,48 @@ mod rustback {
|
||||||
use BackupEngine;
|
use BackupEngine;
|
||||||
use RestoreEngine;
|
use RestoreEngine;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use RestoreDescriptor;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn restore_backed_up_files() -> Result<(), Error> {
|
fn restore_backed_up_files() -> Result<(), Error> {
|
||||||
let source = tempdir()?;
|
let source = tempdir()?;
|
||||||
|
let repository = tempdir()?;
|
||||||
|
|
||||||
File::create(source.path().join("first"))?.write_all("some contents".as_bytes())?;
|
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("second"))?.write_all("some contents".as_bytes())?;
|
||||||
File::create(source.path().join("third"))?.write_all("some other 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())
|
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> {
|
fn is_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);
|
||||||
|
@ -125,7 +170,7 @@ mod rustback {
|
||||||
|
|
||||||
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());
|
||||||
restore_engine.restore()?;
|
restore_engine.restore_all()?;
|
||||||
|
|
||||||
let are_source_and_target_different = is_different(source_path, &restore_target.path()).unwrap();
|
let are_source_and_target_different = is_different(source_path, &restore_target.path()).unwrap();
|
||||||
assert!(!are_source_and_target_different);
|
assert!(!are_source_and_target_different);
|
||||||
|
|
Loading…
Reference in a new issue