Skip to content

Commit

Permalink
add example and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Apr 29, 2024
1 parent a0cd1e0 commit 6edf060
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ xattr = "1"
[dev-dependencies]
expect-test = "1.4.1"
flate2 = "1.0.28"
globset = "0.4.14"
insta = { version = "1.36.1", features = ["redactions", "ron"] }
mockall = "0.12.1"
pretty_assertions = "1.4.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/blob/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl Tree {
}

/// Results from `find_node_from_path`
#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct FindNode {
/// found nodes for the given path
pub nodes: Vec<Node>,
Expand All @@ -340,7 +340,7 @@ pub struct FindNode {
}

/// Results from `find_matching_nodes`
#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct FindMatches {
/// found matching paths
pub paths: Vec<PathBuf>,
Expand Down
84 changes: 82 additions & 2 deletions crates/core/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@
use anyhow::Result;
use flate2::read::GzDecoder;
use globset::Glob;
use insta::internals::{Content, ContentPath};
use insta::{assert_ron_snapshot, Settings};
use pretty_assertions::assert_eq;
use rstest::fixture;
use rstest::rstest;
use rustic_core::repofile::{Metadata, Node};
use rustic_core::{
repofile::SnapshotFile, BackupOptions, ConfigOptions, KeyOptions, NoProgressBars, OpenStatus,
PathList, Repository, RepositoryBackends, RepositoryOptions,
repofile::SnapshotFile, BackupOptions, ConfigOptions, KeyOptions, LsOptions, NoProgressBars,
OpenStatus, PathList, Repository, RepositoryBackends, RepositoryOptions,
};
use rustic_core::{FindMatches, FindNode, RusticResult};
use serde_derive::Serialize;

use rustic_testing::backend::in_memory_backend::InMemoryBackend;

use std::ffi::OsStr;
use std::{
env,
fs::File,
Expand Down Expand Up @@ -348,3 +352,79 @@ fn test_backup_dry_run_with_tar_gz_passes(
assert_eq!(snap_dry_run.tree, second_snapshot.tree);
Ok(())
}

#[rstest]
fn test_ls(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>) -> Result<()> {
// Fixtures
let (source, repo) = (tar_gz_testdata?, set_up_repo?.to_indexed_ids()?);
let paths = &source.path_list();

// we use as_path to not depend on the actual tempdir
let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?);
// backup test-data
let snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;

// test non-existing entries
let mut node = Node::new_node(
OsStr::new(""),
rustic_core::repofile::NodeType::Dir,
Metadata::default(),
);
node.subtree = Some(snapshot.tree);

// re-read index
let repo = repo.to_indexed_ids()?;

let _entries: Vec<_> = repo
.ls(&node, &LsOptions::default())?
.collect::<RusticResult<_>>()?;
// TODO: Snapshot-test entries
// assert_ron_snapshot!("ls", entries);
Ok(())
}

#[rstest]
fn test_find(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>) -> Result<()> {
// Fixtures
let (source, repo) = (tar_gz_testdata?, set_up_repo?.to_indexed_ids()?);
let paths = &source.path_list();

// we use as_path to not depend on the actual tempdir
let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?);
// backup test-data
let snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;

// re-read index
let repo = repo.to_indexed_ids()?;

// test non-existing path
let not_found = repo.find_nodes_from_path(vec![snapshot.tree], Path::new("not_existing"))?;
assert_ron_snapshot!("find-nodes-not-found", not_found);
// test non-existing match
let glob = Glob::new("not_existing")?.compile_matcher();
let not_found =
repo.find_matching_nodes(vec![snapshot.tree], &|path, _| glob.is_match(path))?;
assert_ron_snapshot!("find-matching-nodes-not-found", not_found);

// test existing path
let FindNode { matches, .. } =
repo.find_nodes_from_path(vec![snapshot.tree], Path::new("test/0/tests/testfile"))?;
assert_ron_snapshot!("find-nodes-existing", matches);
// test existing match
let glob = Glob::new("testfile")?.compile_matcher();
let match_func = |path: &Path, _: &Node| {
glob.is_match(path) || path.file_name().is_some_and(|f| glob.is_match(f))
};
let FindMatches { paths, matches, .. } =
repo.find_matching_nodes(vec![snapshot.tree], &match_func)?;
assert_ron_snapshot!("find-matching-existing", (paths, matches));
// test existing match
let glob = Glob::new("testfile*")?.compile_matcher();
let match_func = |path: &Path, _: &Node| {
glob.is_match(path) || path.file_name().is_some_and(|f| glob.is_match(f))
};
let FindMatches { paths, matches, .. } =
repo.find_matching_nodes(vec![snapshot.tree], &match_func)?;
assert_ron_snapshot!("find-matching-wildcard-existing", (paths, matches));
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/core/tests/integration.rs
expression: "(paths, matches)"
---
([
"test/0/tests/testfile",
], [
[
(0, 0),
],
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/core/tests/integration.rs
expression: not_found
---
FindMatches(
paths: [],
nodes: [],
matches: [
[],
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/core/tests/integration.rs
expression: "(paths, matches)"
---
([
"test/0/tests/testfile",
"test/0/tests/testfile-hardlink",
"test/0/tests/testfile-symlink",
], [
[
(0, 0),
(1, 1),
(2, 2),
],
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/core/tests/integration.rs
expression: matches
---
[
Some(0),
]
10 changes: 10 additions & 0 deletions crates/core/tests/snapshots/integration__find-nodes-not-found.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/core/tests/integration.rs
expression: not_found
---
FindNode(
nodes: [],
matches: [
None,
],
)
16 changes: 16 additions & 0 deletions examples/find/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "find"
version = "0.1.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
globset = "0.4.14"
rustic_backend = { workspace = true }
rustic_core = { workspace = true }
simplelog = { workspace = true }

[[example]]
name = "find"
42 changes: 42 additions & 0 deletions examples/find/examples/find.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! `ls` example
use globset::Glob;
use rustic_backend::BackendOptions;
use rustic_core::{FindMatches, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
// Display info logs
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Initialize Backends
let backends = BackendOptions::default()
.repository("/tmp/repo")
.to_backends()?;

// Open repository
let repo_opts = RepositoryOptions::default().password("test ");

let repo = Repository::new(&repo_opts, &backends)?
.open()?
.to_indexed()?;

let mut snapshots = repo.get_all_snapshots()?;
snapshots.sort_unstable();
let tree_ids = snapshots.iter().map(|sn| sn.tree);

let glob = Glob::new("*.rs")?.compile_matcher();
let FindMatches {
paths,
nodes,
matches,
} = repo.find_matching_nodes(tree_ids, &|path, _| glob.is_match(path))?;
for (snap, matches) in snapshots.iter().zip(matches) {
println!("results in {snap:?}");
for (path_idx, node_idx) in matches {
println!("path: {:?}, node: {:?}", paths[path_idx], nodes[node_idx]);
}
}

Ok(())
}

0 comments on commit 6edf060

Please sign in to comment.