Skip to content

Commit d07fcf2

Browse files
committed
add example and integration tests
1 parent 0b402c1 commit d07fcf2

15 files changed

+265
-44
lines changed

crates/core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ xattr = "1"
132132
[dev-dependencies]
133133
expect-test = "1.4.1"
134134
flate2 = "1.0.28"
135+
globset = "0.4.14"
135136
insta = { version = "1.36.1", features = ["redactions", "ron"] }
136137
mockall = "0.12.1"
137138
pretty_assertions = "1.4.0"

crates/core/src/blob/tree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl Tree {
331331
}
332332

333333
/// Results from `find_node_from_path`
334-
#[derive(Debug)]
334+
#[derive(Debug, Serialize)]
335335
pub struct FindNode {
336336
/// found nodes for the given path
337337
pub nodes: Vec<Node>,
@@ -340,7 +340,7 @@ pub struct FindNode {
340340
}
341341

342342
/// Results from `find_matching_nodes`
343-
#[derive(Debug)]
343+
#[derive(Debug, Serialize)]
344344
pub struct FindMatches {
345345
/// found matching paths
346346
pub paths: Vec<PathBuf>,

crates/core/tests/integration.rs

+96-42
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@
2525
2626
use anyhow::Result;
2727
use flate2::read::GzDecoder;
28+
use globset::Glob;
2829
use insta::internals::{Content, ContentPath};
2930
use insta::{assert_ron_snapshot, Settings};
3031
use pretty_assertions::assert_eq;
3132
use rstest::fixture;
3233
use rstest::rstest;
34+
use rustic_core::repofile::{Metadata, Node};
3335
use rustic_core::{
34-
repofile::SnapshotFile, BackupOptions, ConfigOptions, KeyOptions, NoProgressBars, OpenStatus,
35-
PathList, Repository, RepositoryBackends, RepositoryOptions,
36+
repofile::SnapshotFile, BackupOptions, ConfigOptions, KeyOptions, LsOptions, NoProgressBars,
37+
OpenStatus, PathList, Repository, RepositoryBackends, RepositoryOptions,
3638
};
37-
use serde_derive::Serialize;
39+
use rustic_core::{FindMatches, FindNode, RusticResult};
40+
use serde::Serialize;
3841

3942
use rustic_testing::backend::in_memory_backend::InMemoryBackend;
4043

44+
use std::ffi::OsStr;
4145
use std::{
4246
env,
4347
fs::File,
@@ -153,6 +157,13 @@ fn tar_gz_testdata() -> Result<TestSource> {
153157
Ok(TestSource::new(dir))
154158
}
155159

160+
// helper function to do windows-specific snapshots (needed e.g. if paths are contained in the snapshot)
161+
fn assert_with_win<T: Serialize>(test: &str, snap: T) {
162+
#[cfg(windows)]
163+
assert_ron_snapshot!(format!("{test}-windows"), snap);
164+
#[cfg(not(windows))]
165+
assert_ron_snapshot!(format!("{test}-nix"), snap);
166+
}
156167
// Parts of the snapshot summary we want to test against references
157168
//
158169
// # Note
@@ -189,13 +200,7 @@ fn test_backup_with_tar_gz_passes(
189200
// But I think that can get messy with a lot of tests, also checking which settings are currently applied
190201
// will be probably harder
191202
insta_summary_redaction.bind(|| {
192-
#[cfg(windows)]
193-
assert_ron_snapshot!(
194-
"backup-tar-summary-first-windows",
195-
TestSummary(&first_snapshot)
196-
);
197-
#[cfg(not(windows))]
198-
assert_ron_snapshot!("backup-tar-summary-first-nix", TestSummary(&first_snapshot));
203+
assert_with_win("backup-tar-summary-first", TestSummary(&first_snapshot));
199204
});
200205

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

209214
insta_tree_redaction.bind(|| {
210-
#[cfg(windows)]
211-
assert_ron_snapshot!("backup-tar-tree-windows", tree);
212-
#[cfg(not(windows))]
213-
assert_ron_snapshot!("backup-tar-tree-nix", tree);
215+
assert_with_win("backup-tar-tree", tree);
214216
});
215217

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

227229
insta_summary_redaction.bind(|| {
228-
#[cfg(windows)]
229-
assert_ron_snapshot!(
230-
"backup-tar-summary-second-windows",
231-
TestSummary(&second_snapshot)
232-
);
233-
#[cfg(not(windows))]
234-
assert_ron_snapshot!(
235-
"backup-tar-summary-second-nix",
236-
TestSummary(&second_snapshot)
237-
);
230+
assert_with_win("backup-tar-summary-second", TestSummary(&second_snapshot));
238231
});
239232

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

286279
insta_summary_redaction.bind(|| {
287-
#[cfg(windows)]
288-
assert_ron_snapshot!(
289-
"dryrun-tar-summary-first-windows",
290-
TestSummary(&snap_dry_run)
291-
);
292-
#[cfg(not(windows))]
293-
assert_ron_snapshot!("dryrun-tar-summary-first-nix", TestSummary(&snap_dry_run));
280+
assert_with_win("dryrun-tar-summary-first", TestSummary(&snap_dry_run));
294281
});
295282

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

314301
insta_tree_redaction.bind(|| {
315-
#[cfg(windows)]
316-
assert_ron_snapshot!("dryrun-tar-tree-windows", tree);
317-
#[cfg(not(windows))]
318-
assert_ron_snapshot!("dryrun-tar-tree-nix", tree);
302+
assert_with_win("dryrun-tar-tree", tree);
319303
});
320304

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

327311
insta_summary_redaction.bind(|| {
328-
#[cfg(windows)]
329-
assert_ron_snapshot!(
330-
"dryrun-tar-summary-second-windows",
331-
TestSummary(&snap_dry_run)
332-
);
333-
#[cfg(not(windows))]
334-
assert_ron_snapshot!("dryrun-tar-summary-second-nix", TestSummary(&snap_dry_run));
312+
assert_with_win("dryrun-tar-summary-second", TestSummary(&snap_dry_run));
335313
});
336314

337315
// check that no data has been added
@@ -348,3 +326,79 @@ fn test_backup_dry_run_with_tar_gz_passes(
348326
assert_eq!(snap_dry_run.tree, second_snapshot.tree);
349327
Ok(())
350328
}
329+
330+
#[rstest]
331+
fn test_ls(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>) -> Result<()> {
332+
// Fixtures
333+
let (source, repo) = (tar_gz_testdata?, set_up_repo?.to_indexed_ids()?);
334+
let paths = &source.path_list();
335+
336+
// we use as_path to not depend on the actual tempdir
337+
let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?);
338+
// backup test-data
339+
let snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;
340+
341+
// test non-existing entries
342+
let mut node = Node::new_node(
343+
OsStr::new(""),
344+
rustic_core::repofile::NodeType::Dir,
345+
Metadata::default(),
346+
);
347+
node.subtree = Some(snapshot.tree);
348+
349+
// re-read index
350+
let repo = repo.to_indexed_ids()?;
351+
352+
let _entries: Vec<_> = repo
353+
.ls(&node, &LsOptions::default())?
354+
.collect::<RusticResult<_>>()?;
355+
// TODO: Snapshot-test entries
356+
// assert_ron_snapshot!("ls", entries);
357+
Ok(())
358+
}
359+
360+
#[rstest]
361+
fn test_find(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>) -> Result<()> {
362+
// Fixtures
363+
let (source, repo) = (tar_gz_testdata?, set_up_repo?.to_indexed_ids()?);
364+
let paths = &source.path_list();
365+
366+
// we use as_path to not depend on the actual tempdir
367+
let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?);
368+
// backup test-data
369+
let snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;
370+
371+
// re-read index
372+
let repo = repo.to_indexed_ids()?;
373+
374+
// test non-existing path
375+
let not_found = repo.find_nodes_from_path(vec![snapshot.tree], Path::new("not_existing"))?;
376+
assert_with_win("find-nodes-not-found", not_found);
377+
// test non-existing match
378+
let glob = Glob::new("not_existing")?.compile_matcher();
379+
let not_found =
380+
repo.find_matching_nodes(vec![snapshot.tree], &|path, _| glob.is_match(path))?;
381+
assert_with_win("find-matching-nodes-not-found", not_found);
382+
383+
// test existing path
384+
let FindNode { matches, .. } =
385+
repo.find_nodes_from_path(vec![snapshot.tree], Path::new("test/0/tests/testfile"))?;
386+
assert_with_win("find-nodes-existing", matches);
387+
// test existing match
388+
let glob = Glob::new("testfile")?.compile_matcher();
389+
let match_func = |path: &Path, _: &Node| {
390+
glob.is_match(path) || path.file_name().is_some_and(|f| glob.is_match(f))
391+
};
392+
let FindMatches { paths, matches, .. } =
393+
repo.find_matching_nodes(vec![snapshot.tree], &match_func)?;
394+
assert_with_win("find-matching-existing", (paths, matches));
395+
// test existing match
396+
let glob = Glob::new("testfile*")?.compile_matcher();
397+
let match_func = |path: &Path, _: &Node| {
398+
glob.is_match(path) || path.file_name().is_some_and(|f| glob.is_match(f))
399+
};
400+
let FindMatches { paths, matches, .. } =
401+
repo.find_matching_nodes(vec![snapshot.tree], &match_func)?;
402+
assert_with_win("find-matching-wildcard-existing", (paths, matches));
403+
Ok(())
404+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
([
6+
"test/0/tests/testfile",
7+
], [
8+
[
9+
(0, 0),
10+
],
11+
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
([
6+
"test\\0\\tests\\testfile",
7+
], [
8+
[
9+
(0, 0),
10+
],
11+
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
FindMatches(
6+
paths: [],
7+
nodes: [],
8+
matches: [
9+
[],
10+
],
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
FindMatches(
6+
paths: [],
7+
nodes: [],
8+
matches: [
9+
[],
10+
],
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
([
6+
"test/0/tests/testfile",
7+
"test/0/tests/testfile-hardlink",
8+
"test/0/tests/testfile-symlink",
9+
], [
10+
[
11+
(0, 0),
12+
(1, 1),
13+
(2, 2),
14+
],
15+
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
([
6+
"test\\0\\tests\\testfile",
7+
"test\\0\\tests\\testfile-hardlink",
8+
"test\\0\\tests\\testfile-symlink",
9+
], [
10+
[
11+
(0, 0),
12+
(1, 1),
13+
(2, 2),
14+
],
15+
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
[
6+
Some(0),
7+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
[
6+
Some(0),
7+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
FindNode(
6+
nodes: [],
7+
matches: [
8+
None,
9+
],
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
FindNode(
6+
nodes: [],
7+
matches: [
8+
None,
9+
],
10+
)

examples/find/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "find"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
globset = "0.4.14"
11+
rustic_backend = { workspace = true }
12+
rustic_core = { workspace = true }
13+
simplelog = { workspace = true }
14+
15+
[[example]]
16+
name = "find"

0 commit comments

Comments
 (0)