Skip to content

Commit

Permalink
pathlist test, still failing due to whitespace
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Mar 12, 2024
1 parent d1862c9 commit dbf67fa
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion crates/core/src/repofile/snapshotfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,23 @@ impl PathList {
)
}

/// Create a `PathList` from a list of `PathBuf`s.
///
/// # Arguments
///
/// * `source` - The `PathBuf`s to use
///
/// # Returns
///
/// A `PathList` containing the `PathBuf`s
pub fn from_iter<I>(source: I) -> Self
where
I: IntoIterator,
I::Item: Into<PathBuf>,
{
Self(source.into_iter().map(Into::into).collect())
}

/// Create a `PathList` by parsing a Strings containing paths separated by whitspaces.
///
/// # Arguments
Expand All @@ -1130,7 +1147,16 @@ impl PathList {
///
/// [`SnapshotFileErrorKind::FromSplitError`]: crate::error::SnapshotFileErrorKind::FromSplitError
pub fn from_string(sources: &str) -> RusticResult<Self> {
let sources = sources.split(' ').collect::<Vec<_>>();
// TODO: we need to handle paths, that have a whitespace in them
// they should be surrounded by single quotes
let sources_trimmed = sources.trim();

if sources_trimmed.is_empty() {
return Ok(Self::default());
}

// Split by whitespace
let sources = sources_trimmed.split(' ').collect::<Vec<_>>();

Ok(Self::from_strings(sources))
}
Expand Down Expand Up @@ -1199,3 +1225,33 @@ impl PathList {
Self(paths)
}
}

#[cfg(test)]
mod tests {

use rstest::rstest;

use super::*;

#[rstest]
#[case("/tmp /var/tmp", PathList::from_iter(vec![PathBuf::from("/tmp"), PathBuf::from("/var/tmp")]))]
#[case(" C:\\Users\\test_user\\backup\\myfiles\\ C:\\backup\\this\\ ", PathList::from_iter(vec![PathBuf::from("C:\\Users\\test_user\\backup\\myfiles\\"), PathBuf::from("C:\\backup\\this\\")]))]
#[case("' C:\\Users\\test_user\\Back Up\\myfiles\\' 'C:\\backup\\this\\'", PathList::from_iter(vec![PathBuf::from("C:\\Users\\test_user\\Back Up\\myfiles\\"), PathBuf::from("C:\\backup\\this\\")]))]
#[case("'/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App' '/Users/Alice/Library/Application Support/com.Bar-Corp.Foo-App'", PathList::from_iter(vec![PathBuf::from("/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App"), PathBuf::from("/Users/Alice/Library/Application Support/com.Bar-Corp.Foo-App")]))]
fn test_pathlist_from_string_passes(
#[case] input: &str,
#[case] output: PathList,
) -> RusticResult<()> {
let pathlist = PathList::from_string(input)?;
assert_eq!(pathlist, output);

Ok(())
}

#[rstest]
#[case(vec!["/tmp", "/var/tmp"], vec![PathBuf::from("/tmp"), PathBuf::from("/var/tmp")])]
fn test_pathlist_from_iter_passes(#[case] input: Vec<&str>, #[case] output: Vec<PathBuf>) {
let pathlist = PathList::from_iter(input);
assert_eq!(pathlist.paths(), output);
}
}

0 comments on commit dbf67fa

Please sign in to comment.