Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions library/std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ pub struct SplitPaths<'a> {
/// On most Unix platforms, the separator is `:` and on Windows it is `;`. This
/// also performs unquoting on Windows.
///
/// On Unix systems an empty path corresponds to the current working directory
/// and on Windows systems an empty path is disregarded.
///
/// [`join_paths`] can be used to recombine elements.
///
/// # Panics
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'a> Iterator for SplitPaths<'a> {

let mut in_progress = Vec::new();
let mut in_quote = false;
for b in self.data.by_ref() {
for b in self.data.by_ref().skip_while(|&b| b == ';' as u16) {
if b == '"' as u16 {
in_quote = !in_quote;
} else if b == ';' as u16 && !in_quote {
Expand Down
45 changes: 45 additions & 0 deletions library/std/tests/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2577,3 +2577,48 @@ fn test_trim_trailing_sep() {
assert_eq!(Path::new("c:..\\\\").trim_trailing_sep().as_os_str(), OsStr::new("c:.."));
}
}

// Test: Empty PATH paths
// This test checks how `split_paths` handles empty segments.
// On Unix it should be an empty `Path` and on Windows it should disregard the segment.
#[test]
fn test_only_separators() {
use std::env::split_paths;
#[cfg(unix)]
{
assert_eq!(split_paths("").collect::<Vec<_>>(), vec![PathBuf::new(); 1]);
assert_eq!(split_paths(":").collect::<Vec<_>>(), vec![PathBuf::new(); 2]);
assert_eq!(split_paths("::").collect::<Vec<_>>(), vec![PathBuf::new(); 3]);

assert_eq!(
split_paths("a::").collect::<Vec<_>>(),
vec![PathBuf::from("a"), PathBuf::from(""), PathBuf::from("")]
);
assert_eq!(
split_paths("a::b").collect::<Vec<_>>(),
vec![PathBuf::from("a"), PathBuf::from(""), PathBuf::from("b")]
);
assert_eq!(
split_paths("::b").collect::<Vec<_>>(),
vec![PathBuf::from(""), PathBuf::from(""), PathBuf::from("b")]
);
assert_eq!(
split_paths(":a:").collect::<Vec<_>>(),
vec![PathBuf::from(""), PathBuf::from("a"), PathBuf::from("")]
);
}
#[cfg(windows)]
{
assert_eq!(split_paths("").collect::<Vec<PathBuf>>(), vec![]);
assert_eq!(split_paths(";").collect::<Vec<PathBuf>>(), vec![]);
assert_eq!(split_paths(";;").collect::<Vec<PathBuf>>(), vec![]);

assert_eq!(split_paths("a;;").collect::<Vec<_>>(), vec![PathBuf::from("a")]);
assert_eq!(
split_paths("a;;b").collect::<Vec<_>>(),
vec![PathBuf::from("a"), PathBuf::from("b")]
);
assert_eq!(split_paths(";;b").collect::<Vec<_>>(), vec![PathBuf::from("b")]);
assert_eq!(split_paths(";a;").collect::<Vec<_>>(), vec![PathBuf::from("a")]);
}
}
Loading