From 58a08eed7aed7d83d5472d92d77d43b917505d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Sat, 22 Dec 2018 16:09:22 +0000 Subject: [PATCH] extract source --- Cargo.toml | 2 +- src/backup.rs | 35 +++++++++++++++++++++++++++++++++-- src/lib.rs | 10 ++-------- src/source.rs | 24 ++++++++++++++++++++++++ tests/system_tests.rs | 22 ++-------------------- 5 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 src/source.rs diff --git a/Cargo.toml b/Cargo.toml index ec873dd..d20a99b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,8 @@ license = "AGPL-3.0" [dependencies] walkdir = "2.2" rust-crypto = "0.2" +tempfile = "3.0" [dev-dependencies] -tempfile = "3.0" rust-crypto = "0.2" dir-diff = "0.3" diff --git a/src/backup.rs b/src/backup.rs index c76a5e7..9a1b1c1 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -10,8 +10,25 @@ pub struct Engine<'a> { repository_path: &'a Path, } +trait Index {} + +struct InMemoryIndex {} + +impl InMemoryIndex { + fn new() -> Self { + InMemoryIndex {} + } +} + +impl Index for InMemoryIndex {} + impl<'a> Engine<'a> { pub fn new(source_path: &'a Path, repository_path: &'a Path) -> Self { + let index = InMemoryIndex::new(); + Engine::new_with_index(source_path, repository_path, index) + } + + fn new_with_index(source_path: &'a Path, repository_path: &'a Path, index: impl Index) -> Self { Engine { source_path, repository_path, @@ -51,12 +68,26 @@ impl<'a> Engine<'a> { #[cfg(test)] mod should { + use super::*; + use tempfile::tempdir; + + use crate::source::Source; + #[test] - fn store_file_where_index_tells_it() { - // fake index, all files stores at the same path + fn store_file_where_index_tells_it() -> Result<(), io::Error> { + let index = FakeIndex {}; + + let source = Source::new()?; + let repository = tempdir()?; + let engine = Engine::new_with_index(source.path(), repository.path(), index); + // backup // see if repo contains one file at the faked path assert!(false); + Ok(()) } + struct FakeIndex {} + + impl Index for FakeIndex {} } diff --git a/src/lib.rs b/src/lib.rs index d26f206..ace8b5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,6 @@ -extern crate core; -extern crate crypto; -#[cfg(test)] -extern crate dir_diff; -#[cfg(test)] -extern crate tempfile; -extern crate walkdir; - pub mod backup; pub mod restore; +pub mod source; + mod storage; diff --git a/src/source.rs b/src/source.rs new file mode 100644 index 0000000..997d92f --- /dev/null +++ b/src/source.rs @@ -0,0 +1,24 @@ +use std::fs::File; +use std::io::Error; +use std::io::Write; +use std::path::Path; +use tempfile::tempdir; +use tempfile::TempDir; + +pub struct Source { + directory: TempDir, +} + +impl Source { + pub fn new() -> Result { + Ok(Self { directory: tempdir()? }) + } + + pub fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> { + Ok(File::create(self.directory.path().join(filename))?.write_all(text.as_bytes())?) + } + + pub fn path(&self) -> &Path { + self.directory.path() + } +} diff --git a/tests/system_tests.rs b/tests/system_tests.rs index 0692d9d..8c85b11 100644 --- a/tests/system_tests.rs +++ b/tests/system_tests.rs @@ -2,14 +2,14 @@ use bakare::backup; use bakare::restore; use bakare::restore::WhatToRestore::SpecificPath; +use bakare::source::Source; + use dir_diff::is_different; use std::fs::File; use std::io::Error; use std::io::Read; -use std::io::Write; use std::path::Path; use tempfile::tempdir; -use tempfile::TempDir; #[test] fn restore_backed_up_files() -> Result<(), Error> { @@ -70,21 +70,3 @@ fn assert_same_after_restore(source_path: &Path, repository_path: &Path) -> Resu assert!(!are_source_and_target_different); Ok(()) } - -struct Source { - directory: TempDir, -} - -impl Source { - fn new() -> Result { - Ok(Self { directory: tempdir()? }) - } - - fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> { - Ok(File::create(self.directory.path().join(filename))?.write_all(text.as_bytes())?) - } - - fn path(&self) -> &Path { - self.directory.path() - } -}