Skip to content

Commit dbf67fa

Browse files
committed
pathlist test, still failing due to whitespace
Signed-off-by: simonsan <[email protected]>
1 parent d1862c9 commit dbf67fa

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

crates/core/src/repofile/snapshotfile.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,23 @@ impl PathList {
11181118
)
11191119
}
11201120

1121+
/// Create a `PathList` from a list of `PathBuf`s.
1122+
///
1123+
/// # Arguments
1124+
///
1125+
/// * `source` - The `PathBuf`s to use
1126+
///
1127+
/// # Returns
1128+
///
1129+
/// A `PathList` containing the `PathBuf`s
1130+
pub fn from_iter<I>(source: I) -> Self
1131+
where
1132+
I: IntoIterator,
1133+
I::Item: Into<PathBuf>,
1134+
{
1135+
Self(source.into_iter().map(Into::into).collect())
1136+
}
1137+
11211138
/// Create a `PathList` by parsing a Strings containing paths separated by whitspaces.
11221139
///
11231140
/// # Arguments
@@ -1130,7 +1147,16 @@ impl PathList {
11301147
///
11311148
/// [`SnapshotFileErrorKind::FromSplitError`]: crate::error::SnapshotFileErrorKind::FromSplitError
11321149
pub fn from_string(sources: &str) -> RusticResult<Self> {
1133-
let sources = sources.split(' ').collect::<Vec<_>>();
1150+
// TODO: we need to handle paths, that have a whitespace in them
1151+
// they should be surrounded by single quotes
1152+
let sources_trimmed = sources.trim();
1153+
1154+
if sources_trimmed.is_empty() {
1155+
return Ok(Self::default());
1156+
}
1157+
1158+
// Split by whitespace
1159+
let sources = sources_trimmed.split(' ').collect::<Vec<_>>();
11341160

11351161
Ok(Self::from_strings(sources))
11361162
}
@@ -1199,3 +1225,33 @@ impl PathList {
11991225
Self(paths)
12001226
}
12011227
}
1228+
1229+
#[cfg(test)]
1230+
mod tests {
1231+
1232+
use rstest::rstest;
1233+
1234+
use super::*;
1235+
1236+
#[rstest]
1237+
#[case("/tmp /var/tmp", PathList::from_iter(vec![PathBuf::from("/tmp"), PathBuf::from("/var/tmp")]))]
1238+
#[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\\")]))]
1239+
#[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\\")]))]
1240+
#[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")]))]
1241+
fn test_pathlist_from_string_passes(
1242+
#[case] input: &str,
1243+
#[case] output: PathList,
1244+
) -> RusticResult<()> {
1245+
let pathlist = PathList::from_string(input)?;
1246+
assert_eq!(pathlist, output);
1247+
1248+
Ok(())
1249+
}
1250+
1251+
#[rstest]
1252+
#[case(vec!["/tmp", "/var/tmp"], vec![PathBuf::from("/tmp"), PathBuf::from("/var/tmp")])]
1253+
fn test_pathlist_from_iter_passes(#[case] input: Vec<&str>, #[case] output: Vec<PathBuf>) {
1254+
let pathlist = PathList::from_iter(input);
1255+
assert_eq!(pathlist.paths(), output);
1256+
}
1257+
}

0 commit comments

Comments
 (0)