diff --git a/src/lib.rs b/src/lib.rs index 8b827ba..fd6768b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,5 +4,7 @@ pub mod repository; pub mod restore; pub mod source; +pub mod test; + mod index; mod repository_item; diff --git a/src/test/assertions.rs b/src/test/assertions.rs new file mode 100644 index 0000000..3d353ad --- /dev/null +++ b/src/test/assertions.rs @@ -0,0 +1,75 @@ +use crate::error::BakareError; +use crate::repository::Repository; +use crate::{backup, restore}; +use std::fs::File; +use std::io::Read; +use std::path::Path; +use tempfile::tempdir; +use walkdir::WalkDir; + +pub fn assert_target_file_contents(restored_path: &Path, expected_contents: &str) -> Result<(), BakareError> { + let mut actual_contents = String::new(); + assert!(restored_path.exists(), "Expected '{}' to be there", restored_path.display()); + File::open(restored_path)?.read_to_string(&mut actual_contents)?; + assert_eq!(expected_contents, actual_contents); + Ok(()) +} + +pub fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> { + let repository_path = tempdir()?.into_path(); + let restore_target = tempdir()?.into_path(); + + assert_ne!(source_path, repository_path); + assert_ne!(repository_path, restore_target); + + Repository::init(repository_path.as_path())?; + { + let mut backup_repository = Repository::open(repository_path.as_path())?; + let mut backup_engine = backup::Engine::new(source_path, &mut backup_repository); + backup_engine.backup()?; + } + { + let restore_repository = Repository::open(repository_path.as_path())?; + let restore_engine = restore::Engine::new(&restore_repository, &restore_target)?; + restore_engine.restore_all()?; + } + + assert_directory_trees_have_same_contents(source_path, restore_target.as_path())?; + Ok(()) +} + +fn assert_directory_trees_have_same_contents(left: &Path, right: &Path) -> Result<(), BakareError> { + let left_files = get_sorted_files_recursively(left)?; + let right_files = get_sorted_files_recursively(right)?; + + let pairs = left_files.iter().zip(right_files); + for (l, r) in pairs { + assert_eq!(l.file_name(), r.file_name()); + let mut fl = File::open(l)?; + let mut fr = File::open(r)?; + let mut bl = vec![]; + let mut br = vec![]; + fl.read_to_end(&mut bl)?; + fr.read_to_end(&mut br)?; + assert_eq!(bl, br); + } + Ok(()) +} + +fn get_sorted_files_recursively(path: &Path) -> Result>, BakareError> { + let walker = WalkDir::new(path).sort_by(|a, b| a.file_name().cmp(b.file_name())); + + let mut result = vec![]; + + for maybe_entry in walker { + let entry = maybe_entry?; + if entry.path() == path { + continue; + } + if entry.path().is_file() { + result.push(Box::from(entry.path())); + } + } + + Ok(result) +} diff --git a/src/test/mod.rs b/src/test/mod.rs new file mode 100644 index 0000000..5324bda --- /dev/null +++ b/src/test/mod.rs @@ -0,0 +1 @@ +pub mod assertions; diff --git a/tests/system_tests.rs b/tests/system_tests.rs index e7f7b13..33148bd 100644 --- a/tests/system_tests.rs +++ b/tests/system_tests.rs @@ -1,16 +1,13 @@ use bakare::backup; use bakare::restore; -use bakare::source::TempSource; - use bakare::error::BakareError; use bakare::repository::Repository; +use bakare::source::TempSource; + +use bakare::test::assertions::{assert_same_after_restore, assert_target_file_contents}; use std::fs; -use std::fs::File; -use std::io::Read; -use std::path::Path; use tempfile::tempdir; -use walkdir::WalkDir; #[test] fn restore_multiple_files() -> Result<(), BakareError> { @@ -60,20 +57,20 @@ fn restore_older_version_of_file() -> Result<(), BakareError> { let repository_path = tempdir()?.into_path(); Repository::init(repository_path.as_path())?; - let relative_path_text = "some path"; - let file_path = source.file_path(relative_path_text); + let source_file_relative_path = "some path"; + let source_file_full_path = source.file_path(source_file_relative_path); let old_contents = "some old contents"; { let mut backup_repository = Repository::open(repository_path.as_path())?; let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository); - source.write_text_to_file(relative_path_text, old_contents)?; + source.write_text_to_file(source_file_relative_path, old_contents)?; backup_engine.backup()?; } let old_version = { let reading_repository = Repository::open(repository_path.as_path())?; - let item = reading_repository.item_by_source_path(&file_path)?; + let item = reading_repository.item_by_source_path(&source_file_full_path)?; assert!(item.is_some()); let item = item.unwrap(); item.version().clone() @@ -83,87 +80,20 @@ fn restore_older_version_of_file() -> Result<(), BakareError> { let new_contents = "totally new contents"; let mut backup_repository = Repository::open(repository_path.as_path())?; let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository); - source.write_text_to_file(relative_path_text, new_contents)?; + source.write_text_to_file(source_file_relative_path, new_contents)?; backup_engine.backup()?; } let restore_repository = Repository::open(repository_path.as_path())?; let restore_target = tempdir()?; let restore_engine = restore::Engine::new(&restore_repository, &restore_target.path())?; - let old_item = restore_repository.item_by_source_path_and_version(&file_path, &old_version)?; + let old_item = restore_repository.item_by_source_path_and_version(&source_file_full_path, &old_version)?; restore_engine.restore(&old_item.unwrap())?; - let source_file_full_path = source.file_path(relative_path_text); let restored_file_path = restore_target.path().join(source_file_full_path.strip_prefix("/")?); assert_target_file_contents(&restored_file_path, old_contents) } -fn assert_target_file_contents(restored_path: &Path, expected_contents: &str) -> Result<(), BakareError> { - let mut actual_contents = String::new(); - assert!(restored_path.exists(), "Expected '{}' to be there", restored_path.display()); - File::open(restored_path)?.read_to_string(&mut actual_contents)?; - assert_eq!(expected_contents, actual_contents); - Ok(()) -} - -fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> { - let repository_path = tempdir()?.into_path(); - let restore_target = tempdir()?.into_path(); - - assert_ne!(source_path, repository_path); - assert_ne!(repository_path, restore_target); - - Repository::init(repository_path.as_path())?; - { - let mut backup_repository = Repository::open(repository_path.as_path())?; - let mut backup_engine = backup::Engine::new(source_path, &mut backup_repository); - backup_engine.backup()?; - } - { - let restore_repository = Repository::open(repository_path.as_path())?; - let restore_engine = restore::Engine::new(&restore_repository, &restore_target)?; - restore_engine.restore_all()?; - } - - assert_directory_trees_have_same_contents(source_path, restore_target.as_path())?; - Ok(()) -} - -fn assert_directory_trees_have_same_contents(left: &Path, right: &Path) -> Result<(), BakareError> { - let left_files = get_sorted_files_recursively(left)?; - let right_files = get_sorted_files_recursively(right)?; - - let pairs = left_files.iter().zip(right_files); - for (l, r) in pairs { - assert_eq!(l.file_name(), r.file_name()); - let mut fl = File::open(l)?; - let mut fr = File::open(r)?; - let mut bl = vec![]; - let mut br = vec![]; - fl.read_to_end(&mut bl)?; - fr.read_to_end(&mut br)?; - assert_eq!(bl, br); - } - Ok(()) -} - -fn get_sorted_files_recursively(path: &Path) -> Result>, BakareError> { - let walker = WalkDir::new(path).sort_by(|a, b| a.file_name().cmp(b.file_name())); - - let mut result = vec![]; - - for maybe_entry in walker { - let entry = maybe_entry?; - if entry.path() == path { - continue; - } - if entry.path().is_file() { - result.push(Box::from(entry.path())); - } - } - - Ok(result) -} // TODO: restore latest version by default // TODO: deduplicate data // TODO: test that index is stored separately from data