Cleanup tests

This commit is contained in:
Cyryl Płotnicki 2019-09-07 14:42:30 +01:00
parent 0a53f8c1f5
commit 66391862c1
6 changed files with 106 additions and 79 deletions

View file

@ -1,15 +1,11 @@
use std::fs::File;
use std::path::Path; use std::path::Path;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::error::BakareError; use crate::error::BakareError;
use crate::repository::ItemVersion;
use crate::repository_item::RepositoryItem; use crate::repository_item::RepositoryItem;
use std::fmt;
use std::fmt::Formatter;
use std::fs::File;
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)]
pub struct ItemVersion(Box<[u8]>);
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)]
pub struct IndexItem { pub struct IndexItem {
@ -25,24 +21,6 @@ pub struct Index {
repository_path: String, repository_path: String,
} }
impl AsRef<[u8]> for ItemVersion {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl From<&[u8]> for ItemVersion {
fn from(a: &[u8]) -> Self {
ItemVersion(Box::from(a))
}
}
impl fmt::Display for ItemVersion {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", hex::encode(self))
}
}
impl Index { impl Index {
pub fn new(repository_path: &Path) -> Self { pub fn new(repository_path: &Path) -> Self {
Index { Index {

View file

@ -1,11 +1,13 @@
use std::path::Path; use std::path::Path;
use std::{fs, io}; use std::{fmt, fs, io};
use crate::error::BakareError; use crate::error::BakareError;
use crate::index::{Index, IndexIterator, ItemVersion}; use crate::index::{Index, IndexIterator};
use crate::repository_item::RepositoryItem; use crate::repository_item::RepositoryItem;
use serde::{Deserialize, Serialize};
use sha2::Digest; use sha2::Digest;
use sha2::Sha512; use sha2::Sha512;
use std::fmt::Formatter;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
@ -19,6 +21,27 @@ pub struct Repository<'a> {
index: Index, index: Index,
} }
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)]
pub struct ItemVersion(Box<[u8]>);
impl AsRef<[u8]> for ItemVersion {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl From<&[u8]> for ItemVersion {
fn from(a: &[u8]) -> Self {
ItemVersion(Box::from(a))
}
}
impl fmt::Display for ItemVersion {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", hex::encode(self))
}
}
pub struct RepositoryIterator<'a> { pub struct RepositoryIterator<'a> {
index_iterator: IndexIterator<'a>, index_iterator: IndexIterator<'a>,
} }

View file

@ -1,5 +1,5 @@
use crate::error::BakareError; use crate::error::BakareError;
use crate::index::ItemVersion; use crate::repository::ItemVersion;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::path::Path; use std::path::Path;
use std::{fmt, fs}; use std::{fmt, fs};

View file

@ -1,7 +1,6 @@
use std::path::Path; use std::path::Path;
use crate::error::BakareError; use crate::error::BakareError;
use crate::index::ItemVersion;
use crate::repository::Repository; use crate::repository::Repository;
use crate::repository_item::RepositoryItem; use crate::repository_item::RepositoryItem;

View file

@ -1,12 +1,16 @@
use crate::error::BakareError; use std::fs;
use crate::repository::Repository;
use crate::{backup, restore};
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::Path; use std::path::Path;
use tempfile::tempdir; use tempfile::tempdir;
use walkdir::WalkDir; use walkdir::WalkDir;
use crate::{backup, restore};
use crate::error::BakareError;
use crate::repository::{ItemVersion, Repository};
use crate::source::TempSource;
pub fn assert_target_file_contents(restored_path: &Path, expected_contents: &str) -> Result<(), BakareError> { pub fn assert_target_file_contents(restored_path: &Path, expected_contents: &str) -> Result<(), BakareError> {
let mut actual_contents = String::new(); let mut actual_contents = String::new();
assert!(restored_path.exists(), "Expected '{}' to be there", restored_path.display()); assert!(restored_path.exists(), "Expected '{}' to be there", restored_path.display());
@ -38,6 +42,67 @@ pub fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError>
Ok(()) Ok(())
} }
pub fn assert_restored_from_version_has_contents(
repository_path: &Path,
source_file_full_path: &Path,
old_contents: &str,
old_version: &ItemVersion,
) -> Result<(), BakareError> {
let restore_repository = Repository::open(repository_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(&source_file_full_path, &old_version)?;
restore_engine.restore(&old_item.unwrap())?;
let restored_file_path = restore_target.path().join(source_file_full_path.strip_prefix("/")?);
assert_target_file_contents(&restored_file_path, old_contents)
}
pub fn item_version(repository_path: &Path, source_file_full_path: &Path) -> Result<ItemVersion, BakareError> {
let old_version = {
let reading_repository = Repository::open(repository_path)?;
let item = reading_repository.item_by_source_path(&source_file_full_path)?;
assert!(item.is_some());
let item = item.unwrap();
item.version().clone()
};
Ok(old_version)
}
pub fn read_restored_file_contents(
source: TempSource,
restore_target: &Path,
source_file_relative_path: &str,
) -> Result<String, BakareError> {
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)?;
Ok(contents)
}
pub fn restore_all_from_reloaded_repository(repository_path: &Path, restore_target: &Path) -> Result<(), BakareError> {
{
let restore_repository = Repository::open(repository_path)?;
let restore_engine = restore::Engine::new(&restore_repository, &restore_target)?;
restore_engine.restore_all()?;
Ok(())
}
}
pub fn backup_file_with_contents(
source: &TempSource,
repository_path: &Path,
source_file_relative_path: &str,
contents: &str,
) -> Result<(), BakareError> {
{
let mut backup_repository = Repository::open(repository_path)?;
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(source_file_relative_path, contents)?;
backup_engine.backup()?;
Ok(())
}
}
fn assert_directory_trees_have_same_contents(left: &Path, right: &Path) -> Result<(), BakareError> { fn assert_directory_trees_have_same_contents(left: &Path, right: &Path) -> Result<(), BakareError> {
let left_files = get_sorted_files_recursively(left)?; let left_files = get_sorted_files_recursively(left)?;
let right_files = get_sorted_files_recursively(right)?; let right_files = get_sorted_files_recursively(right)?;

View file

@ -1,13 +1,9 @@
use bakare::backup; use tempfile::tempdir;
use bakare::restore;
use bakare::error::BakareError; use bakare::error::BakareError;
use bakare::repository::Repository; use bakare::repository::Repository;
use bakare::source::TempSource; use bakare::source::TempSource;
use bakare::test::assertions::*;
use bakare::test::assertions::{assert_same_after_restore, assert_target_file_contents};
use std::fs;
use tempfile::tempdir;
#[test] #[test]
fn restore_multiple_files() -> Result<(), BakareError> { fn restore_multiple_files() -> Result<(), BakareError> {
@ -30,22 +26,11 @@ fn restore_files_after_reopening_repository() -> Result<(), BakareError> {
let source_file_relative_path = "some file path"; let source_file_relative_path = "some file path";
let original_contents = "some old contents"; let original_contents = "some old contents";
{ backup_file_with_contents(&source, &repository_path, source_file_relative_path, original_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(source_file_relative_path, original_contents)?;
backup_engine.backup()?;
}
{ restore_all_from_reloaded_repository(&repository_path, &restore_target)?;
let restore_repository = Repository::open(repository_path.as_path())?;
let restore_engine = restore::Engine::new(&restore_repository, &restore_target)?;
restore_engine.restore_all()?;
}
let source_file_full_path = source.file_path(source_file_relative_path); let contents = read_restored_file_contents(source, &restore_target, 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)?;
assert_eq!(contents, original_contents); assert_eq!(contents, original_contents);
Ok(()) Ok(())
@ -61,37 +46,14 @@ fn restore_older_version_of_file() -> Result<(), BakareError> {
let source_file_full_path = source.file_path(source_file_relative_path); let source_file_full_path = source.file_path(source_file_relative_path);
let old_contents = "some old contents"; let old_contents = "some old contents";
{ backup_file_with_contents(&source, &repository_path, source_file_relative_path, 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(source_file_relative_path, old_contents)?;
backup_engine.backup()?;
}
let old_version = { let old_version = item_version(&repository_path, &source_file_full_path)?;
let reading_repository = Repository::open(repository_path.as_path())?;
let item = reading_repository.item_by_source_path(&source_file_full_path)?;
assert!(item.is_some());
let item = item.unwrap();
item.version().clone()
};
{
let new_contents = "totally new contents"; let new_contents = "totally new contents";
let mut backup_repository = Repository::open(repository_path.as_path())?; backup_file_with_contents(&source, &repository_path, source_file_relative_path, new_contents)?;
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(source_file_relative_path, new_contents)?;
backup_engine.backup()?;
}
let restore_repository = Repository::open(repository_path.as_path())?; assert_restored_from_version_has_contents(&repository_path, &source_file_full_path, old_contents, &old_version)
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(&source_file_full_path, &old_version)?;
restore_engine.restore(&old_item.unwrap())?;
let restored_file_path = restore_target.path().join(source_file_full_path.strip_prefix("/")?);
assert_target_file_contents(&restored_file_path, old_contents)
} }
// TODO: restore latest version by default // TODO: restore latest version by default