mirror of
https://github.com/uutils/coreutils
synced 2024-11-16 09:48:03 +00:00
Merge pull request #2534 from jfinkels/basename-handle-slashes
basename: handle paths comprising only slashes
This commit is contained in:
commit
9f3debeda3
2 changed files with 36 additions and 1 deletions
|
@ -119,9 +119,15 @@ pub fn uu_app() -> App<'static, 'static> {
|
|||
}
|
||||
|
||||
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);
|
||||
|
||||
// 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
|
||||
let pb = PathBuf::from(path);
|
||||
match pb.components().last() {
|
||||
|
|
|
@ -152,3 +152,32 @@ fn invalid_utf8_args_unix() {
|
|||
let os_str = OsStr::from_bytes(&source[..]);
|
||||
test_invalid_utf8_args(os_str);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_root() {
|
||||
let expected = if cfg!(windows) { "\\\n" } else { "/\n" };
|
||||
new_ucmd!().arg("/").succeeds().stdout_is(expected);
|
||||
}
|
||||
|
||||
#[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) { "\\\n" } 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() {
|
||||
let expected = if cfg!(windows) { "\\\n" } else { "/\n" };
|
||||
new_ucmd!().arg("///").succeeds().stdout_is(expected);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue