mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
basename: handle paths comprising only slashes
Fix an issue where `basename` would print the empty path when provided an input comprising only slashes (for example, "/" or "//" or "///"). Now it correctly prints the path "/".
This commit is contained in:
parent
103a9d52ff
commit
0d348f05f2
2 changed files with 34 additions and 1 deletions
|
@ -119,9 +119,15 @@ pub fn uu_app() -> App<'static, 'static> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn basename(fullname: &str, suffix: &str) -> String {
|
fn basename(fullname: &str, suffix: &str) -> String {
|
||||||
// Remove all platform-specific path separators from the end
|
// Remove all platform-specific path separators from the end.
|
||||||
let path = fullname.trim_end_matches(is_separator);
|
let path = fullname.trim_end_matches(is_separator);
|
||||||
|
|
||||||
|
// If the path contained *only* suffix characters (for example, if
|
||||||
|
// `fullname` were "///" and `suffix` were "/"), then `path` would
|
||||||
|
// be left with the empty string. In that case, we set `path` to be
|
||||||
|
// the original `fullname` to avoid returning the empty path.
|
||||||
|
let path = if path.is_empty() { fullname } else { path };
|
||||||
|
|
||||||
// Convert to path buffer and get last path component
|
// Convert to path buffer and get last path component
|
||||||
let pb = PathBuf::from(path);
|
let pb = PathBuf::from(path);
|
||||||
match pb.components().last() {
|
match pb.components().last() {
|
||||||
|
|
|
@ -147,3 +147,30 @@ fn invalid_utf8_args_unix() {
|
||||||
let os_str = OsStr::from_bytes(&source[..]);
|
let os_str = OsStr::from_bytes(&source[..]);
|
||||||
test_invalid_utf8_args(os_str);
|
test_invalid_utf8_args(os_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_root() {
|
||||||
|
new_ucmd!().arg("/").succeeds().stdout_is("/\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_double_slash() {
|
||||||
|
// TODO The GNU tests seem to suggest that some systems treat "//"
|
||||||
|
// as the same directory as "/" directory but not all systems. We
|
||||||
|
// should extend this test to account for that possibility.
|
||||||
|
let expected = if cfg!(windows) { "\\" } else { "/\n" };
|
||||||
|
new_ucmd!().arg("//").succeeds().stdout_is(expected);
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["//", "/"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is(expected);
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["//", "//"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_triple_slash() {
|
||||||
|
new_ucmd!().arg("///").succeeds().stdout_is("/\n");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue