Skip to content

Commit

Permalink
impl from_iter
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan authored and aawsome committed Mar 12, 2024
1 parent e5f883f commit 6cc4309
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ coverage/*.info

# local repo config
.cargo/config.toml

# Generated by Tests
crates/core/tests/generated/
8 changes: 7 additions & 1 deletion crates/core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ pub mod in_memory_backend {

impl InMemoryBackend {
/// Create a new (empty) `InMemoryBackend`
#[must_use]
pub fn new() -> Self {
Self(RwLock::new(EnumMap::from_fn(|_| BTreeMap::new())))
}
Expand All @@ -584,7 +585,12 @@ pub mod in_memory_backend {
fn list_with_size(&self, tpe: FileType) -> Result<Vec<(Id, u32)>> {
Ok(self.0.read().unwrap()[tpe]
.iter()
.map(|(id, byte)| (*id, byte.len() as u32))
.map(|(id, byte)| {
(
*id,
u32::try_from(byte.len()).expect("byte length is too large"),
)
})
.collect())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ implement [`serde::Serialize`] and [`serde::Deserialize`].
let config_opts = ConfigOptions::default();
let _repo = Repository::new(&repo_opts, backends.clone()).unwrap().init(&key_opts, &config_opts).unwrap();
let _repo = Repository::new(&repo_opts, &backends.clone()).unwrap().init(&key_opts, &config_opts).unwrap();
// We could have used _repo directly, but open the repository again to show how to open it...
let repo = Repository::new(&repo_opts, backends).unwrap().open().unwrap();
let repo = Repository::new(&repo_opts, &backends).unwrap().open().unwrap();
// Get all snapshots from the repository
let snaps = repo.get_all_snapshots().unwrap();
Expand Down
17 changes: 17 additions & 0 deletions crates/core/src/repofile/snapshotfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,23 @@ impl PathList {
)
}

/// Create a `PathList` from a list of `PathBuf`s.
///
/// # Arguments
///
/// * `source` - The `PathBuf`s to use
///
/// # Returns
///
/// A `PathList` containing the `PathBuf`s
pub fn from_iter<I>(source: I) -> Self
where
I: IntoIterator,
I::Item: Into<PathBuf>,
{
Self(source.into_iter().map(Into::into).collect())
}

/// Create a `PathList` by parsing a Strings containing paths separated by whitspaces.
///
/// # Arguments
Expand Down
79 changes: 41 additions & 38 deletions crates/core/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn set_up_repo() -> Result<Repository<NoProgressBars, OpenStatus>> {
let be = InMemoryBackend::new();
let be = RepositoryBackends::new(Arc::new(be), None);
let options = RepositoryOptions::default().password("test");
let repo = Repository::new(&options, be)?;
let repo = Repository::new(&options, &be)?;
let key_opts = KeyOptions::default();
let config_opts = &ConfigOptions::default();
let repo = repo.init(&key_opts, config_opts)?;
Expand All @@ -38,7 +38,11 @@ impl TestSource {
}

fn paths(&self) -> Result<PathList> {
Ok(PathList::from_string(self.0.path().to_str().unwrap())?)
let sources = self.0.path().to_str().unwrap();

let path_list = PathList::from_string(sources)?;

Ok(path_list)
}
}

Expand All @@ -62,25 +66,25 @@ impl<'a> std::fmt::Debug for TestSummary<'a> {
// leave out info we expect to change:
// Ids, times, tree sizes (as used uid/username is saved in trees)
let mut b = f.debug_struct("TestSnap");
b.field("hostname", &self.0.hostname);
b.field("paths", &self.0.paths);
b.field("label", &self.0.label);
b.field("tags", &self.0.tags);
_ = b.field("hostname", &self.0.hostname);
_ = b.field("paths", &self.0.paths);
_ = b.field("label", &self.0.label);
_ = b.field("tags", &self.0.tags);

let s = self.0.summary.as_ref().unwrap();
b.field("files_new", &s.files_new);
b.field("files_changed", &s.files_changed);
b.field("files_unmodified", &s.files_unmodified);
b.field("total_files_processed", &s.total_files_processed);
b.field("total_bytes_processed", &s.total_bytes_processed);
b.field("dirs_new", &s.dirs_new);
b.field("dirs_changed", &s.dirs_changed);
b.field("dirs_unmodified", &s.dirs_unmodified);
b.field("total_dirs_processed", &s.total_dirs_processed);
b.field("data_blobs", &s.data_blobs);
b.field("tree_blobs", &s.tree_blobs);
b.field("data_added_files", &s.data_added_files);
b.field("data_added_files_packed", &s.data_added_files_packed);
_ = b.field("files_new", &s.files_new);
_ = b.field("files_changed", &s.files_changed);
_ = b.field("files_unmodified", &s.files_unmodified);
_ = b.field("total_files_processed", &s.total_files_processed);
_ = b.field("total_bytes_processed", &s.total_bytes_processed);
_ = b.field("dirs_new", &s.dirs_new);
_ = b.field("dirs_changed", &s.dirs_changed);
_ = b.field("dirs_unmodified", &s.dirs_unmodified);
_ = b.field("total_dirs_processed", &s.total_dirs_processed);
_ = b.field("data_blobs", &s.data_blobs);
_ = b.field("tree_blobs", &s.tree_blobs);
_ = b.field("data_added_files", &s.data_added_files);
_ = b.field("data_added_files_packed", &s.data_added_files_packed);
b.finish()
}
}
Expand All @@ -91,18 +95,19 @@ fn backup() -> Result<()> {
// SimpleLogger::init(log::LevelFilter::Debug, Config::default())?;
let source = set_up_testdata("backup-data.tar.gz")?;
let paths = &source.paths()?;

let repo = set_up_repo()?.to_indexed_ids()?;
// we use as_path to not depend on the actual tempdir
let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?);

// first backup
let snap1 = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_debug_snapshot!(TestSummary(&snap1));
assert_eq!(snap1.parent, None);
let first_backup = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_debug_snapshot!(TestSummary(&first_backup));
assert_eq!(first_backup.parent, None);

// get all snapshots and check them
let snaps = repo.get_all_snapshots()?;
assert_eq!(vec![snap1.clone()], snaps);
let all_snapshots = repo.get_all_snapshots()?;
assert_eq!(vec![first_backup.clone()], all_snapshots);
// save list of pack files
let packs1: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();

Expand All @@ -111,13 +116,13 @@ fn backup() -> Result<()> {
// second backup
let snap2 = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_debug_snapshot!(TestSummary(&snap2));
assert_eq!(snap2.parent, Some(snap1.id));
assert_eq!(snap1.tree, snap2.tree);
assert_eq!(snap2.parent, Some(first_backup.id));
assert_eq!(first_backup.tree, snap2.tree);

// get all snapshots and check them
let mut snaps = repo.get_all_snapshots()?;
snaps.sort_unstable();
assert_eq!(vec![snap1, snap2], snaps);
let mut all_snapshots = repo.get_all_snapshots()?;
all_snapshots.sort_unstable();
assert_eq!(vec![first_backup, snap2], all_snapshots);

// pack files should be unchanged
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
Expand All @@ -141,15 +146,13 @@ fn backup_dry_run() -> Result<()> {
// check that repo is still empty
let snaps = repo.get_all_snapshots()?;
assert_eq!(snaps.len(), 0);
let packs: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
assert_eq!(packs.len(), 0);
let indexes: Vec<_> = repo.list(rustic_core::FileType::Index)?.collect();
assert_eq!(indexes.len(), 0);
assert_eq!(repo.list(rustic_core::FileType::Pack)?.count(), 0);
assert_eq!(repo.list(rustic_core::FileType::Index)?.count(), 0);

// first real backup
let opts = opts.dry_run(false);
let snap1 = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_eq!(snap_dry_run.tree, snap1.tree);
let first_snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_eq!(snap_dry_run.tree, first_snapshot.tree);
let packs: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();

// re-read index
Expand All @@ -160,15 +163,15 @@ fn backup_dry_run() -> Result<()> {
assert_debug_snapshot!(TestSummary(&snap_dry_run));
// check that no data has been added
let snaps = repo.get_all_snapshots()?;
assert_eq!(snaps, vec![snap1.clone()]);
assert_eq!(snaps, vec![first_snapshot]);
let packs_dry_run: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
assert_eq!(packs_dry_run, packs);

// re-read index
let repo = repo.to_indexed_ids()?;
// second real backup
let opts = opts.dry_run(false);
let snap2 = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_eq!(snap_dry_run.tree, snap2.tree);
let second_snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_eq!(snap_dry_run.tree, second_snapshot.tree);
Ok(())
}

0 comments on commit 6cc4309

Please sign in to comment.