Skip to content

additional edge cases tests for path.rs 🧪 #141105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
88 changes: 87 additions & 1 deletion library/std/tests/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ pub fn test_push() {
tp!("foo//", "bar", r"foo//bar");
tp!(r"foo\\", "bar", r"foo\\bar");
tp!("foo/.", "bar", r"foo/.\bar");
tp!("foo./.", "bar", r"foo./.\bar");
tp!("foo./.", "bar", r"foo././bar");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changing an existing test?

tp!(r"foo\.", "bar", r"foo\.\bar");
tp!(r"foo.\.", "bar", r"foo.\.\bar");
tp!("foo", "", "foo\\");
Expand Down Expand Up @@ -1976,3 +1976,89 @@ fn clone_to_uninit() {
unsafe { a.clone_to_uninit(ptr::from_mut::<Path>(&mut b).cast()) };
assert_eq!(a, &*b);
}

// Test: Only separators (e.g., "/" or "\\")
// This test checks how Path handles a string that consists only of path separators.
// It should recognize the root and not treat it as a normal component.
#[test]
fn test_only_separators() {
let path = Path::new("/////");
assert!(path.has_root());
assert_eq!(path.iter().count(), 1);
assert_eq!(path.parent(), None);
}

// Test: Non-ASCII/Unicode
// This test verifies that Path can handle Unicode and non-ASCII characters in the path.
// It ensures that such paths are not rejected or misinterpreted.
#[test]
fn test_non_ascii_unicode() {
let path = Path::new("/tmp/❤/🚀/file.txt");
assert!(path.to_str().is_some());
assert_eq!(path.file_name(), Some(OsStr::new("file.txt")));
}


// Test: Reserved device names (Windows)
// This test ensures that reserved device names like "CON", "PRN", etc., are handled as normal paths on non-Windows platforms,
// and as special cases on Windows (if applicable).
Comment on lines +2002 to +2004
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't seem to describe the test.

#[test]
#[cfg(windows)]
fn test_reserved_device_names() {
for &name in &["CON", "PRN", "AUX", "NUL", "COM1", "LPT1"] {
let path = Path::new(name);
assert_eq!(path.file_name(), Some(OsStr::new(name)));
assert_eq!(path.extension(), None);
}
}

// Test: Trailing dots/spaces (Windows)
// This test checks how Path handles trailing dots or spaces, which are special on Windows.
// On Unix, these should be treated as normal characters.
Comment on lines +2015 to +2017
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path mostly works the same across platforms (separator differences and roots/prefixes not withstanding). If there's a difference then you should have tests for both Windows and non-Windows that shows the difference.

#[test]
#[cfg(windows)]
fn test_trailing_dots_and_spaces() {
let path = Path::new("foo. ");
assert_eq!(path.file_stem(), Some(OsStr::new("foo")));
assert_eq!(path.extension(), Some(OsStr::new(" ")));
assert_eq!(path.file_name(), Some(OsStr::new("foo. ")));
assert_eq!(path.to_str(), Some("foo. "));
let path = Path::new("bar...");
assert_eq!(path.file_stem(), Some(OsStr::new("bar")));
assert_eq!(path.extension(), Some(OsStr::new("...")));
assert_eq!(path.file_name(), Some(OsStr::new("bar...")));
assert_eq!(path.to_str(), Some("bar..."));
}

// Test: Only extension (e.g., ".gitignore")
// This test verifies that files with only an extension and no base name are handled correctly.
// It checks that the extension is recognized and the file stem is None or empty as appropriate.
Comment on lines +2033 to +2035
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really not have a test for this anywhere else?

#[test]
fn test_only_extension() {
let path = Path::new(".ext");
assert_eq!(path.extension(), None);
assert_eq!(path.file_stem(), Some(OsStr::new(".ext")));
assert_eq!(path.file_name(), Some(OsStr::new(".ext")));
}

// Test: Long components
// This test checks that Path can handle very long path components without truncation or error.
// It ensures that the length of the component is preserved.
Comment on lines +2044 to +2046
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this would be useful for us specifically? We don't usually check that slice wrappers work on the whole slice.

#[test]
fn test_long_component() {
let long = "a".repeat(300);
let path = Path::new(&long);
assert_eq!(path.file_name(), Some(OsStr::new(&long)));
assert_eq!(path.to_str(), Some(long.as_str()));
assert_eq!(path.iter().count(), 1);
}

// Test: Embedded newlines
// This test verifies that newlines within path components are preserved and do not break path parsing.
// It ensures that Path treats newlines as normal characters.
#[test]
fn test_embedded_newline() {
let path = Path::new("foo\nbar");
assert_eq!(path.file_name(), Some(OsStr::new("foo\nbar")));
assert_eq!(path.to_str(), Some("foo\nbar"));
}
Loading