From 85cd1166d717a3ed2ff787f037e1383cdd001837 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Mon, 13 Jan 2025 11:42:58 +0100 Subject: [PATCH] fix unix tests --- crates/core/src/commands/backup.rs | 2 +- crates/core/src/util.rs | 15 +++++++++++++++ crates/core/tests/integration/find.rs | 14 ++++++++------ crates/core/tests/integration/ls.rs | 11 ++++++++--- crates/core/tests/integration/vfs.rs | 2 +- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/crates/core/src/commands/backup.rs b/crates/core/src/commands/backup.rs index 2b026f17..6fe0ee10 100644 --- a/crates/core/src/commands/backup.rs +++ b/crates/core/src/commands/backup.rs @@ -223,7 +223,7 @@ pub(crate) fn backup( 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, } .map(|p| UnixPath::new(p.as_os_str().as_encoded_bytes()).normalize()); diff --git a/crates/core/src/util.rs b/crates/core/src/util.rs index 74791af8..d3d9e5d1 100644 --- a/crates/core/src/util.rs +++ b/crates/core/src/util.rs @@ -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 { @@ -27,3 +29,16 @@ impl GlobMatcherExt for GlobMatcher { self.is_match(path) } } + +#[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(path: &UnixPath, serializer: S) -> Result +where + S: Serializer, +{ + let s = format!("{}", path.display()); + serializer.serialize_str(&s) +} diff --git a/crates/core/tests/integration/find.rs b/crates/core/tests/integration/find.rs index abb24df2..49ec544d 100644 --- a/crates/core/tests/integration/find.rs +++ b/crates/core/tests/integration/find.rs @@ -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; @@ -37,9 +36,10 @@ fn test_find(tar_gz_testdata: Result, set_up_repo: Result) 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, .. } = @@ -52,7 +52,8 @@ fn test_find(tar_gz_testdata: Result, set_up_repo: Result) }; 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| { @@ -60,6 +61,7 @@ fn test_find(tar_gz_testdata: Result, set_up_repo: Result) }; 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(()) } diff --git a/crates/core/tests/integration/ls.rs b/crates/core/tests/integration/ls.rs index e4eb3174..ef2e621b 100644 --- a/crates/core/tests/integration/ls.rs +++ b/crates/core/tests/integration/ls.rs @@ -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( @@ -40,10 +44,11 @@ fn test_ls( let entries: BTreeMap<_, _> = repo .ls(&node, &LsOptions::default())? + .map_ok(|(path, node)| (SerializablePath(path), node)) .collect::>()?; insta_node_redaction.bind(|| { - assert_debug_snapshot!("ls", &entries); + assert_with_win("ls", &entries); }); Ok(()) diff --git a/crates/core/tests/integration/vfs.rs b/crates/core/tests/integration/vfs.rs index 01bd18ee..319db6f0 100644 --- a/crates/core/tests/integration/vfs.rs +++ b/crates/core/tests/integration/vfs.rs @@ -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); });