Skip to content

Commit cf3fece

Browse files
authored
Merge pull request #8374 from sylvestre/issue-8373
basename: handle a corner case with /. - Closes #8373
2 parents 5392c34 + c5268a8 commit cf3fece

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/uu/basename/src/basename.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ pub fn uu_app() -> Command {
119119
}
120120

121121
fn basename(fullname: &str, suffix: &str) -> String {
122+
// Handle special case where path ends with /.
123+
if fullname.ends_with("/.") {
124+
return ".".to_string();
125+
}
126+
122127
// Convert to path buffer and get last path component
123128
let pb = PathBuf::from(fullname);
124129

tests/by-util/test_basename.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ fn test_triple_slash() {
183183
new_ucmd!().arg("///").succeeds().stdout_is(expected);
184184
}
185185

186+
#[test]
187+
fn test_trailing_dot() {
188+
new_ucmd!().arg("/.").succeeds().stdout_is(".\n");
189+
new_ucmd!().arg("hello/.").succeeds().stdout_is(".\n");
190+
new_ucmd!().arg("/foo/bar/.").succeeds().stdout_is(".\n");
191+
}
192+
186193
#[test]
187194
fn test_simple_format() {
188195
new_ucmd!().args(&["a-a", "-a"]).succeeds().stdout_is("a\n");

0 commit comments

Comments
 (0)