Skip to content

Commit

Permalink
Merge pull request #5775 from cakebaker/du_ignore_duplicate_files_fil…
Browse files Browse the repository at this point in the history
…es0_from

du: ignore duplicate names with `--files0-from`
  • Loading branch information
sylvestre authored Jan 3, 2024
2 parents f3833bb + e4fbc31 commit 78405e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,10 @@ fn read_files_from(file_name: &str) -> Result<Vec<PathBuf>, std::io::Error> {
show_error!("{file_name}:{line_number}: invalid zero-length file name");
set_exit_code(1);
} else {
paths.push(PathBuf::from(String::from_utf8_lossy(&path).to_string()));
let p = PathBuf::from(String::from_utf8_lossy(&path).to_string());
if !paths.contains(&p) {
paths.push(p);
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,21 @@ fn test_du_files0_from() {
.stdout_contains("testdir");
}

#[test]
fn test_du_files0_from_ignore_duplicate_file_names() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file = "testfile";

at.touch(file);
at.write("filelist", &format!("{file}\0{file}\0"));

ts.ucmd()
.arg("--files0-from=filelist")
.succeeds()
.stdout_is(format!("0\t{file}\n"));
}

#[test]
fn test_du_files0_from_with_invalid_zero_length_file_names() {
let ts = TestScenario::new(util_name!());
Expand Down Expand Up @@ -1046,6 +1061,23 @@ fn test_du_files0_from_stdin() {
.stdout_contains("testfile2");
}

#[test]
fn test_du_files0_from_stdin_ignore_duplicate_file_names() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file = "testfile";

at.touch(file);

let input = format!("{file}\0{file}");

ts.ucmd()
.arg("--files0-from=-")
.pipe_in(input)
.succeeds()
.stdout_is(format!("0\t{file}\n"));
}

#[test]
fn test_du_files0_from_stdin_with_invalid_zero_length_file_names() {
new_ucmd!()
Expand Down

0 comments on commit 78405e6

Please sign in to comment.