|
25 | 25 |
|
26 | 26 | use anyhow::Result; |
27 | 27 | use flate2::read::GzDecoder; |
| 28 | +use globset::Glob; |
28 | 29 | use insta::internals::{Content, ContentPath}; |
29 | 30 | use insta::{assert_ron_snapshot, Settings}; |
30 | 31 | use pretty_assertions::assert_eq; |
31 | 32 | use rstest::fixture; |
32 | 33 | use rstest::rstest; |
| 34 | +use rustic_core::repofile::{Metadata, Node}; |
33 | 35 | 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, |
36 | 38 | }; |
| 39 | +use rustic_core::{FindMatches, FindNode, RusticResult}; |
37 | 40 | use serde_derive::Serialize; |
38 | 41 |
|
39 | 42 | use rustic_testing::backend::in_memory_backend::InMemoryBackend; |
40 | 43 |
|
| 44 | +use std::ffi::OsStr; |
41 | 45 | use std::{ |
42 | 46 | env, |
43 | 47 | fs::File, |
@@ -348,3 +352,79 @@ fn test_backup_dry_run_with_tar_gz_passes( |
348 | 352 | assert_eq!(snap_dry_run.tree, second_snapshot.tree); |
349 | 353 | Ok(()) |
350 | 354 | } |
| 355 | + |
| 356 | +#[rstest] |
| 357 | +fn test_ls(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>) -> Result<()> { |
| 358 | + // Fixtures |
| 359 | + let (source, repo) = (tar_gz_testdata?, set_up_repo?.to_indexed_ids()?); |
| 360 | + let paths = &source.path_list(); |
| 361 | + |
| 362 | + // we use as_path to not depend on the actual tempdir |
| 363 | + let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?); |
| 364 | + // backup test-data |
| 365 | + let snapshot = repo.backup(&opts, paths, SnapshotFile::default())?; |
| 366 | + |
| 367 | + // test non-existing entries |
| 368 | + let mut node = Node::new_node( |
| 369 | + OsStr::new(""), |
| 370 | + rustic_core::repofile::NodeType::Dir, |
| 371 | + Metadata::default(), |
| 372 | + ); |
| 373 | + node.subtree = Some(snapshot.tree); |
| 374 | + |
| 375 | + // re-read index |
| 376 | + let repo = repo.to_indexed_ids()?; |
| 377 | + |
| 378 | + let _entries: Vec<_> = repo |
| 379 | + .ls(&node, &LsOptions::default())? |
| 380 | + .collect::<RusticResult<_>>()?; |
| 381 | + // TODO: Snapshot-test entries |
| 382 | + // assert_ron_snapshot!("ls", entries); |
| 383 | + Ok(()) |
| 384 | +} |
| 385 | + |
| 386 | +#[rstest] |
| 387 | +fn test_find(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>) -> Result<()> { |
| 388 | + // Fixtures |
| 389 | + let (source, repo) = (tar_gz_testdata?, set_up_repo?.to_indexed_ids()?); |
| 390 | + let paths = &source.path_list(); |
| 391 | + |
| 392 | + // we use as_path to not depend on the actual tempdir |
| 393 | + let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?); |
| 394 | + // backup test-data |
| 395 | + let snapshot = repo.backup(&opts, paths, SnapshotFile::default())?; |
| 396 | + |
| 397 | + // re-read index |
| 398 | + let repo = repo.to_indexed_ids()?; |
| 399 | + |
| 400 | + // test non-existing path |
| 401 | + let not_found = repo.find_nodes_from_path(vec![snapshot.tree], Path::new("not_existing"))?; |
| 402 | + assert_ron_snapshot!("find-nodes-not-found", not_found); |
| 403 | + // test non-existing match |
| 404 | + let glob = Glob::new("not_existing")?.compile_matcher(); |
| 405 | + let not_found = |
| 406 | + repo.find_matching_nodes(vec![snapshot.tree], &|path, _| glob.is_match(path))?; |
| 407 | + assert_ron_snapshot!("find-matching-nodes-not-found", not_found); |
| 408 | + |
| 409 | + // test existing path |
| 410 | + let FindNode { matches, .. } = |
| 411 | + repo.find_nodes_from_path(vec![snapshot.tree], Path::new("test/0/tests/testfile"))?; |
| 412 | + assert_ron_snapshot!("find-nodes-existing", matches); |
| 413 | + // test existing match |
| 414 | + let glob = Glob::new("testfile")?.compile_matcher(); |
| 415 | + let match_func = |path: &Path, _: &Node| { |
| 416 | + glob.is_match(path) || path.file_name().is_some_and(|f| glob.is_match(f)) |
| 417 | + }; |
| 418 | + let FindMatches { paths, matches, .. } = |
| 419 | + repo.find_matching_nodes(vec![snapshot.tree], &match_func)?; |
| 420 | + assert_ron_snapshot!("find-matching-existing", (paths, matches)); |
| 421 | + // test existing match |
| 422 | + let glob = Glob::new("testfile*")?.compile_matcher(); |
| 423 | + let match_func = |path: &Path, _: &Node| { |
| 424 | + glob.is_match(path) || path.file_name().is_some_and(|f| glob.is_match(f)) |
| 425 | + }; |
| 426 | + let FindMatches { paths, matches, .. } = |
| 427 | + repo.find_matching_nodes(vec![snapshot.tree], &match_func)?; |
| 428 | + assert_ron_snapshot!("find-matching-wildcard-existing", (paths, matches)); |
| 429 | + Ok(()) |
| 430 | +} |
0 commit comments