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 0b402c1 commit d07fcf2
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 44 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
138 changes: 96 additions & 42 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 serde_derive::Serialize;
use rustic_core::{FindMatches, FindNode, RusticResult};
use serde::Serialize;

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

use std::ffi::OsStr;
use std::{
env,
fs::File,
Expand Down Expand Up @@ -153,6 +157,13 @@ fn tar_gz_testdata() -> Result<TestSource> {
Ok(TestSource::new(dir))
}

// helper function to do windows-specific snapshots (needed e.g. if paths are contained in the snapshot)
fn assert_with_win<T: Serialize>(test: &str, snap: T) {
#[cfg(windows)]
assert_ron_snapshot!(format!("{test}-windows"), snap);
#[cfg(not(windows))]

Check warning on line 164 in crates/core/tests/integration.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/tests/integration.rs#L162-L164

Added lines #L162 - L164 were not covered by tests
assert_ron_snapshot!(format!("{test}-nix"), snap);
}
// Parts of the snapshot summary we want to test against references
//
// # Note
Expand Down Expand Up @@ -189,13 +200,7 @@ fn test_backup_with_tar_gz_passes(
// But I think that can get messy with a lot of tests, also checking which settings are currently applied
// will be probably harder
insta_summary_redaction.bind(|| {
#[cfg(windows)]
assert_ron_snapshot!(
"backup-tar-summary-first-windows",
TestSummary(&first_snapshot)
);
#[cfg(not(windows))]
assert_ron_snapshot!("backup-tar-summary-first-nix", TestSummary(&first_snapshot));
assert_with_win("backup-tar-summary-first", TestSummary(&first_snapshot));
});

assert_eq!(first_snapshot.parent, None);
Expand All @@ -207,10 +212,7 @@ fn test_backup_with_tar_gz_passes(
let tree: rustic_core::repofile::Tree = repo.get_tree(&tree.subtree.expect("Sub tree"))?;

insta_tree_redaction.bind(|| {
#[cfg(windows)]
assert_ron_snapshot!("backup-tar-tree-windows", tree);
#[cfg(not(windows))]
assert_ron_snapshot!("backup-tar-tree-nix", tree);
assert_with_win("backup-tar-tree", tree);
});

// get all snapshots and check them
Expand All @@ -225,16 +227,7 @@ fn test_backup_with_tar_gz_passes(
let second_snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;

insta_summary_redaction.bind(|| {
#[cfg(windows)]
assert_ron_snapshot!(
"backup-tar-summary-second-windows",
TestSummary(&second_snapshot)
);
#[cfg(not(windows))]
assert_ron_snapshot!(
"backup-tar-summary-second-nix",
TestSummary(&second_snapshot)
);
assert_with_win("backup-tar-summary-second", TestSummary(&second_snapshot));
});

assert_eq!(second_snapshot.parent, Some(first_snapshot.id));
Expand Down Expand Up @@ -284,13 +277,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?;

insta_summary_redaction.bind(|| {
#[cfg(windows)]
assert_ron_snapshot!(
"dryrun-tar-summary-first-windows",
TestSummary(&snap_dry_run)
);
#[cfg(not(windows))]
assert_ron_snapshot!("dryrun-tar-summary-first-nix", TestSummary(&snap_dry_run));
assert_with_win("dryrun-tar-summary-first", TestSummary(&snap_dry_run));
});

// check that repo is still empty
Expand All @@ -312,10 +299,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
let tree = repo.get_tree(&tree.subtree.expect("Sub tree"))?;

insta_tree_redaction.bind(|| {
#[cfg(windows)]
assert_ron_snapshot!("dryrun-tar-tree-windows", tree);
#[cfg(not(windows))]
assert_ron_snapshot!("dryrun-tar-tree-nix", tree);
assert_with_win("dryrun-tar-tree", tree);
});

// re-read index
Expand All @@ -325,13 +309,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?;

insta_summary_redaction.bind(|| {
#[cfg(windows)]
assert_ron_snapshot!(
"dryrun-tar-summary-second-windows",
TestSummary(&snap_dry_run)
);
#[cfg(not(windows))]
assert_ron_snapshot!("dryrun-tar-summary-second-nix", TestSummary(&snap_dry_run));
assert_with_win("dryrun-tar-summary-second", TestSummary(&snap_dry_run));
});

// check that no data has been added
Expand All @@ -348,3 +326,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_with_win("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_with_win("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_with_win("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_with_win("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_with_win("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: snap
---
([
"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: snap
---
([
"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: snap
---
FindMatches(
paths: [],
nodes: [],
matches: [
[],
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/core/tests/integration.rs
expression: snap
---
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: snap
---
([
"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,15 @@
---
source: crates/core/tests/integration.rs
expression: snap
---
([
"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: snap
---
[
Some(0),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/core/tests/integration.rs
expression: snap
---
[
Some(0),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/core/tests/integration.rs
expression: snap
---
FindNode(
nodes: [],
matches: [
None,
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/core/tests/integration.rs
expression: snap
---
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"
Loading

0 comments on commit d07fcf2

Please sign in to comment.