Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Jan 14, 2025
1 parent 626d588 commit 17c46e7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 42 deletions.
42 changes: 1 addition & 41 deletions crates/core/src/blob/tree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
borrow::Cow,
cmp::Ordering,
collections::{BTreeMap, BTreeSet, BinaryHeap},
mem,
Expand All @@ -13,7 +12,7 @@ use ignore::overrides::{Override, OverrideBuilder};
use ignore::Match;
use serde::{Deserialize, Deserializer};
use serde_derive::Serialize;
use typed_path::{Component, TypedPath, UnixPath, UnixPathBuf, WindowsComponent, WindowsPrefix};
use typed_path::{Component, UnixPath, UnixPathBuf};

use crate::{
backend::{
Expand Down Expand Up @@ -396,45 +395,6 @@ pub struct FindMatches {
pub matches: Vec<Vec<(usize, usize)>>,
}

/// Converts a [`TypedPath`] to an [`Cow<UnixPath>`].
///
/// # Arguments
///
/// * `p` - The component to convert.
///
/// # Errors
///
/// * If the component is a current or parent directory.
/// * If the component is not UTF-8 conform.
pub(crate) fn typed_path_to_unix_path<'a>(p: &'a TypedPath<'_>) -> Cow<'a, UnixPath> {
match p {
TypedPath::Unix(path) => Cow::Borrowed(path),
TypedPath::Windows(path) => {
let mut unix_path = UnixPathBuf::new();
for c in path.with_windows_encoding().components() {
match c {
WindowsComponent::Prefix(p) => match p.kind() {
WindowsPrefix::Verbatim(p) | WindowsPrefix::DeviceNS(p) => {
unix_path.push(p);
}
WindowsPrefix::VerbatimUNC(_, q) | WindowsPrefix::UNC(_, q) => {
unix_path.push(q);
}
WindowsPrefix::VerbatimDisk(p) | WindowsPrefix::Disk(p) => {
let c = vec![p];
unix_path.push(&c);
}
},
c => {
unix_path.push(c);
}
}
}
Cow::Owned(unix_path)
}
}
}

impl IntoIterator for Tree {
type Item = Node;
type IntoIter = std::vec::IntoIter<Node>;
Expand Down
92 changes: 91 additions & 1 deletion crates/core/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/// Utilities for handling paths on ``rustic_core``
use std::borrow::Cow;

use globset::GlobMatcher;
use serde::{Serialize, Serializer};
use typed_path::{UnixPath, UnixPathBuf};
use typed_path::{
Component, TypedPath, UnixComponent, UnixPath, UnixPathBuf, WindowsComponent, WindowsPath,
WindowsPrefix,
};

/// Extend `globset::GlobMatcher` to allow mathing on unix paths (on every platform)
pub trait GlobMatcherExt {
Expand Down Expand Up @@ -43,3 +48,88 @@ where
let s = format!("{}", path.display());
serializer.serialize_str(&s)

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

View check run for this annotation

Codecov / codecov/patch

crates/core/src/util.rs#L49

Added line #L49 was not covered by tests
}

/// Converts a [`TypedPath`] to an [`Cow<UnixPath>`].
///
/// # Arguments
///
/// * `path` - The path to convert.
#[must_use]
pub fn typed_path_to_unix_path<'a>(path: &'a TypedPath<'_>) -> Cow<'a, UnixPath> {
match path {
TypedPath::Unix(p) => Cow::Borrowed(p),
TypedPath::Windows(p) => Cow::Owned(windows_path_to_unix_path(p)),

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

View check run for this annotation

Codecov / codecov/patch

crates/core/src/util.rs#L58-L61

Added lines #L58 - L61 were not covered by tests
}
}

/// Converts a [`WindowsPath`] to a [`UnixPathBuf`].
///
/// # Arguments
///
/// * `path` - The path to convert.
#[must_use]
pub fn windows_path_to_unix_path(path: &WindowsPath) -> UnixPathBuf {
let mut unix_path = UnixPathBuf::new();
let mut components = path.components();
if let Some(c) = components.next() {
match c {
WindowsComponent::Prefix(p) => {
unix_path.push(UnixComponent::RootDir);
match p.kind() {
WindowsPrefix::Verbatim(p) | WindowsPrefix::DeviceNS(p) => {
unix_path.push(p);
}
WindowsPrefix::VerbatimUNC(_, q) | WindowsPrefix::UNC(_, q) => {
unix_path.push(q);
}
WindowsPrefix::VerbatimDisk(p) | WindowsPrefix::Disk(p) => {
let c = vec![p];
unix_path.push(&c);
}
}
// remove RootDir from iterator
_ = components.next();
}
WindowsComponent::RootDir => {
unix_path.push(UnixComponent::RootDir);
}
c => {
unix_path.push(c.as_bytes());
}
}
}
for c in components {
match c {
WindowsComponent::RootDir => {
unix_path.push(UnixComponent::RootDir);
}
c => {
unix_path.push(c);
}
}
}
unix_path
}

#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
#[case("/", "/")]
#[case(r#"\"#, "/")]
#[case("/test/test2", "/test/test2")]
#[case(r#"\test\test2"#, "/test/test2")]
#[case(r#"C:\"#, "/C")]
#[case(r#"C:\dir"#, "/C/dir")]
#[case(r#"a\b\"#, "a/b")]
#[case(r#"a\b\c"#, "a/b/c")]
fn test_typed_path_to_unix_path(#[case] windows_path: &str, #[case] unix_path: &str) {
assert_eq!(
windows_path_to_unix_path(WindowsPath::new(windows_path))
.to_str()
.unwrap(),
UnixPath::new(unix_path).to_str().unwrap()
);
}
}

0 comments on commit 17c46e7

Please sign in to comment.