Skip to content

Commit

Permalink
du: deduplicate the input
Browse files Browse the repository at this point in the history
Should fix:
tests/du/hard-link.sh
  • Loading branch information
sylvestre authored and cakebaker committed Nov 29, 2024
1 parent 79f991d commit bea5ce3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let summarize = matches.get_flag(options::SUMMARIZE);

let count_links = matches.get_flag(options::COUNT_LINKS);

let max_depth = parse_depth(
matches
.get_one::<String>(options::MAX_DEPTH)
Expand All @@ -670,13 +672,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

read_files_from(file_from)?
} else {

Check failure on line 674 in src/uu/du/src/du.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-24.04, unix)

ERROR: `cargo clippy`: this `else { if .. }` block can be collapsed (file:'src/uu/du/src/du.rs', line:674)
match matches.get_one::<String>(options::FILE) {
Some(_) => matches
.get_many::<String>(options::FILE)
.unwrap()
.map(PathBuf::from)
.collect(),
None => vec![PathBuf::from(".")],
if let Some(files) = matches.get_many::<String>(options::FILE) {
let files = files.map(PathBuf::from);
if count_links {
files.collect()
} else {
// Deduplicate while preserving order
let mut seen = std::collections::HashSet::new();
files
.filter(|path| seen.insert(path.clone()))
.collect::<Vec<_>>()
}
} else {
vec![PathBuf::from(".")]
}
};

Expand Down Expand Up @@ -719,7 +727,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else {
Deref::None
},
count_links: matches.get_flag(options::COUNT_LINKS),
count_links,
verbose: matches.get_flag(options::VERBOSE),
excludes: build_exclude_patterns(&matches)?,
};
Expand Down
58 changes: 58 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,3 +1194,61 @@ fn test_human_size() {
.succeeds()
.stdout_contains(format!("1.0K {dir}"));
}

#[cfg(not(target_os = "android"))]
#[test]
fn test_du_deduplicated_input_args() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;

at.mkdir("d");
at.mkdir("d/d");
at.touch("d/f");
at.hard_link("d/f", "d/h");

let result = ts
.ucmd()
.arg("--inodes")
.arg("d")
.arg("d")
.arg("d")
.succeeds();
result.no_stderr();

let result_seq: Vec<String> = result
.stdout_str()
.lines()
.map(|x| x.parse().unwrap())
.collect();
#[cfg(windows)]
assert_eq!(result_seq, ["1\td\\d", "3\td"]);
#[cfg(not(windows))]
assert_eq!(result_seq, ["1\td/d", "3\td"]);
}

#[cfg(not(target_os = "android"))]
#[test]
fn test_du_no_deduplicated_input_args() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;

at.mkdir("d");
at.touch("d/d");

let result = ts
.ucmd()
.arg("--inodes")
.arg("-l")
.arg("d")
.arg("d")
.arg("d")
.succeeds();
result.no_stderr();

let result_seq: Vec<String> = result
.stdout_str()
.lines()
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq, ["2\td", "2\td", "2\td"]);
}

0 comments on commit bea5ce3

Please sign in to comment.