Fix test check for file contents

This commit is contained in:
Cyryl Płotnicki 2019-09-07 10:29:27 +01:00
parent f397c99659
commit 6ed47937ee
4 changed files with 14 additions and 9 deletions

View file

@ -68,9 +68,9 @@ impl Index {
fn repository_item(&self, i: &IndexItem) -> RepositoryItem { fn repository_item(&self, i: &IndexItem) -> RepositoryItem {
let index_item = i.clone(); let index_item = i.clone();
let relative_path = Path::new(index_item.relative_path.as_str()); let relative_path = Path::new(&index_item.relative_path);
let repository_path = Path::new(&self.repository_path); let repository_path = Path::new(&self.repository_path);
let original_source_path = Path::new(index_item.original_source_path.as_str()); let original_source_path = Path::new(&index_item.original_source_path);
let absolute_path = repository_path.join(relative_path); let absolute_path = repository_path.join(relative_path);
let absolute_path = absolute_path.as_path(); let absolute_path = absolute_path.as_path();
RepositoryItem::from(original_source_path, absolute_path, relative_path, index_item.version) RepositoryItem::from(original_source_path, absolute_path, relative_path, index_item.version)

View file

@ -69,7 +69,7 @@ impl<'a> Repository<'a> {
if source_path.is_file() { if source_path.is_file() {
println!("storing {} as {}", source_path.display(), destination_path.display()); println!("storing {} as {}", source_path.display(), destination_path.display());
fs::create_dir_all(destination_path.parent().unwrap())?; fs::create_dir_all(destination_path.parent().unwrap())?;
let version = Repository::calculate_initial_version(source_path)?; let version = Repository::calculate_version(source_path)?;
fs::copy(source_path, destination_path)?; fs::copy(source_path, destination_path)?;
self.index.remember(RepositoryItem::from( self.index.remember(RepositoryItem::from(
@ -83,7 +83,7 @@ impl<'a> Repository<'a> {
Ok(()) Ok(())
} }
fn calculate_initial_version(source_path: &Path) -> Result<Box<[u8]>, BakareError> { fn calculate_version(source_path: &Path) -> Result<Box<[u8]>, BakareError> {
let source_file = File::open(source_path)?; let source_file = File::open(source_path)?;
let mut reader = BufReader::new(source_file); let mut reader = BufReader::new(source_file);
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();

View file

@ -20,12 +20,16 @@ impl RepositoryItem {
version, version,
} }
} }
pub fn save(&self, save_to: &Path) -> Result<(), BakareError> { pub fn save(&self, save_to: &Path) -> Result<(), BakareError> {
if !save_to.is_absolute() { if !save_to.is_absolute() {
return Err(BakareError::PathToStoreNotAbsolute); return Err(BakareError::PathToStoreNotAbsolute);
} }
let target_path = save_to.join(self.relative_path.clone()); let target_path = save_to.join(&self.relative_path);
if !target_path.is_absolute() {
return Err(BakareError::PathToStoreNotAbsolute);
}
let parent = target_path.parent().unwrap(); let parent = target_path.parent().unwrap();
if !parent.exists() { if !parent.exists() {
println!("Creating {}", parent.display()); println!("Creating {}", parent.display());
@ -35,7 +39,7 @@ impl RepositoryItem {
return Err(BakareError::CorruptedRepoNoFile); return Err(BakareError::CorruptedRepoNoFile);
} }
println!("Saving {} to {}", self.absolute_path.display(), target_path.display()); println!("Saving {} to {}", self.absolute_path.display(), target_path.display());
fs::copy(self.absolute_path.clone(), target_path.clone())?; fs::copy(&self.absolute_path, &target_path)?;
Ok(()) Ok(())
} }

View file

@ -30,13 +30,13 @@ fn restore_files_after_reopening_repository() -> Result<(), BakareError> {
let restore_target = tempdir()?.into_path(); let restore_target = tempdir()?.into_path();
Repository::init(repository_path.as_path())?; Repository::init(repository_path.as_path())?;
let relative_path_text = "some file path"; let source_file_relative_path = "some file path";
let original_contents = "some old contents"; let original_contents = "some old contents";
{ {
let mut backup_repository = Repository::open(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); let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(relative_path_text, original_contents)?; source.write_text_to_file(source_file_relative_path, original_contents)?;
backup_engine.backup()?; backup_engine.backup()?;
} }
@ -46,7 +46,8 @@ fn restore_files_after_reopening_repository() -> Result<(), BakareError> {
restore_engine.restore_all()?; restore_engine.restore_all()?;
} }
let restored_file_path = restore_target.join(relative_path_text); let source_file_full_path = source.file_path(source_file_relative_path);
let restored_file_path = restore_target.join(source_file_full_path.strip_prefix("/")?);
let contents = fs::read_to_string(restored_file_path)?; let contents = fs::read_to_string(restored_file_path)?;
assert_eq!(contents, original_contents); assert_eq!(contents, original_contents);