Skip to content

Commit

Permalink
fix unix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Jan 13, 2025
1 parent acaae8a commit 85cd116
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/commands/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub(crate) fn backup<P: ProgressBars, S: IndexedIds>(

let as_path = match &opts.as_path {
Some(p) => Some(p),
None if backup_path.len() == 1 => Some(&backup_path[0]),
None if !backup_stdin && backup_path.len() == 1 => Some(&backup_path[0]),
None => None,

Check warning on line 227 in crates/core/src/commands/backup.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/commands/backup.rs#L227

Added line #L227 was not covered by tests
}
.map(|p| UnixPath::new(p.as_os_str().as_encoded_bytes()).normalize());
Expand Down
15 changes: 15 additions & 0 deletions crates/core/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use globset::GlobMatcher;
use serde::{Serialize, Serializer};
use typed_path::{UnixPath, UnixPathBuf};

/// Extend `globset::GlobMatcher` to allow mathing on unix paths (on every platform)
pub trait GlobMatcherExt {
Expand Down Expand Up @@ -27,3 +29,16 @@ impl GlobMatcherExt for GlobMatcher {
self.is_match(path)

Check warning on line 29 in crates/core/src/util.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/util.rs#L27-L29

Added lines #L27 - L29 were not covered by tests
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(transparent)]
/// Like `UnixPathBuf` , but implements `Serialize`
pub struct SerializablePath(#[serde(serialize_with = "serialize_unix_path")] pub UnixPathBuf);

fn serialize_unix_path<S>(path: &UnixPath, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = format!("{}", path.display());
serializer.serialize_str(&s)

Check warning on line 43 in crates/core/src/util.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/util.rs#L43

Added line #L43 was not covered by tests
}
14 changes: 8 additions & 6 deletions crates/core/tests/integration/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use std::{path::PathBuf, str::FromStr};

use anyhow::Result;
use globset::Glob;
use insta::assert_debug_snapshot;
use rstest::rstest;

use rustic_core::{
repofile::{Node, SnapshotFile},
util::GlobMatcherExt,
util::{GlobMatcherExt, SerializablePath},
BackupOptions, FindMatches, FindNode,
};
use typed_path::UnixPath;
Expand All @@ -37,9 +36,10 @@ fn test_find(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>)
assert_with_win("find-nodes-not-found", not_found);
// test non-existing match
let glob = Glob::new("not_existing")?.compile_matcher();
let not_found =
let FindMatches { paths, matches, .. } =
repo.find_matching_nodes(vec![snapshot.tree], &|path, _| glob.is_unix_match(path))?;
assert_debug_snapshot!("find-matching-nodes-not-found", not_found);
assert!(paths.is_empty());
assert_eq!(matches, [[]]);

// test existing path
let FindNode { matches, .. } =
Expand All @@ -52,14 +52,16 @@ fn test_find(tar_gz_testdata: Result<TestSource>, set_up_repo: Result<RepoOpen>)
};
let FindMatches { paths, matches, .. } =
repo.find_matching_nodes(vec![snapshot.tree], &match_func)?;
assert_debug_snapshot!("find-matching-existing", (paths, matches));
let paths: Vec<_> = paths.into_iter().map(SerializablePath).collect();
assert_with_win("find-matching-existing", (paths, matches));
// test existing match
let glob = Glob::new("testfile*")?.compile_matcher();
let match_func = |path: &UnixPath, _: &Node| {
glob.is_unix_match(path) || path.file_name().is_some_and(|f| glob.is_unix_match(f))
};
let FindMatches { paths, matches, .. } =
repo.find_matching_nodes(vec![snapshot.tree], &match_func)?;
assert_debug_snapshot!("find-matching-wildcard-existing", (paths, matches));
let paths: Vec<_> = paths.into_iter().map(SerializablePath).collect();
assert_with_win("find-matching-wildcard-existing", (paths, matches));
Ok(())
}
11 changes: 8 additions & 3 deletions crates/core/tests/integration/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ use std::collections::BTreeMap;
use std::{path::PathBuf, str::FromStr};

use anyhow::Result;
use insta::{assert_debug_snapshot, Settings};
use insta::Settings;
use itertools::Itertools;
use rstest::rstest;

use rustic_core::{
repofile::{Metadata, Node, SnapshotFile},
util::SerializablePath,
BackupOptions, LsOptions, RusticResult,
};

use super::{insta_node_redaction, set_up_repo, tar_gz_testdata, RepoOpen, TestSource};
use super::{
assert_with_win, insta_node_redaction, set_up_repo, tar_gz_testdata, RepoOpen, TestSource,
};

#[rstest]
fn test_ls(
Expand Down Expand Up @@ -40,10 +44,11 @@ fn test_ls(

let entries: BTreeMap<_, _> = repo
.ls(&node, &LsOptions::default())?
.map_ok(|(path, node)| (SerializablePath(path), node))
.collect::<RusticResult<_>>()?;

insta_node_redaction.bind(|| {
assert_debug_snapshot!("ls", &entries);
assert_with_win("ls", &entries);
});

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/core/tests/integration/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_vfs(
let vfs = Vfs::from_dir_node(&node);

// test reading a directory using vfs
let entries = vfs.dir_entries_from_path(&repo, UnixPath::new("test/0/tests/testfile"))?;
let entries = vfs.dir_entries_from_path(&repo, UnixPath::new("test/0/tests"))?;
insta_node_redaction.bind(|| {
assert_with_win("vfs", &entries);
});
Expand Down

0 comments on commit 85cd116

Please sign in to comment.