Skip to content

Commit 0e1c2c7

Browse files
committed
Separately track files and directories removed.
The previous status line was a little awkward in the way it combined both counts. I don't think showing the directories is particularly interesting, so they are only displayed when no files are deleted.
1 parent 4aa9b12 commit 0e1c2c7

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pub struct CleanContext<'cfg> {
3131
pub config: &'cfg Config,
3232
progress: Box<dyn CleaningProgressBar + 'cfg>,
3333
pub dry_run: bool,
34-
num_files_folders_cleaned: u64,
34+
num_files_removed: u64,
35+
num_dirs_removed: u64,
3536
total_bytes_removed: u64,
3637
}
3738

@@ -270,7 +271,8 @@ impl<'cfg> CleanContext<'cfg> {
270271
config,
271272
progress: Box::new(progress),
272273
dry_run: false,
273-
num_files_folders_cleaned: 0,
274+
num_files_removed: 0,
275+
num_dirs_removed: 0,
274276
total_bytes_removed: 0,
275277
}
276278
}
@@ -352,27 +354,27 @@ impl<'cfg> CleanContext<'cfg> {
352354
// byte sizes and not the block sizes.
353355
self.total_bytes_removed += meta.len();
354356
}
357+
self.num_files_removed += 1;
355358
if !self.dry_run {
356359
paths::remove_file(path)?;
357360
}
358361
Ok(())
359362
};
360363

361364
if !meta.is_dir() {
362-
self.num_files_folders_cleaned += 1;
363365
return rm_file(path, Ok(meta));
364366
}
365367

366368
for entry in walkdir::WalkDir::new(path).contents_first(true) {
367369
let entry = entry?;
368370
self.progress.on_clean()?;
369-
self.num_files_folders_cleaned += 1;
370371
if self.dry_run {
371372
self.config
372373
.shell()
373374
.verbose(|shell| Ok(writeln!(shell.out(), "{}", entry.path().display())?))?;
374375
}
375376
if entry.file_type().is_dir() {
377+
self.num_dirs_removed += 1;
376378
// The contents should have been removed by now, but sometimes a race condition is hit
377379
// where other files have been added by the OS. `paths::remove_dir_all` also falls back
378380
// to `std::fs::remove_dir_all`, which may be more reliable than a simple walk in
@@ -401,13 +403,21 @@ impl<'cfg> CleanContext<'cfg> {
401403
format!(", {bytes:.1}{unit} total")
402404
}
403405
};
404-
self.config.shell().status(
405-
status,
406-
format!(
407-
"{} files/directories{byte_count}",
408-
self.num_files_folders_cleaned
409-
),
410-
)
406+
// I think displaying the number of directories removed isn't
407+
// particularly interesting to the user. However, if there are 0
408+
// files, and a nonzero number of directories, cargo should indicate
409+
// that it did *something*, so directory counts are only shown in that
410+
// case.
411+
let file_count = match (self.num_files_removed, self.num_dirs_removed) {
412+
(0, 0) => format!("0 files"),
413+
(0, 1) => format!("1 directory"),
414+
(0, 2..) => format!("{} directories", self.num_dirs_removed),
415+
(1, _) => format!("1 file"),
416+
(2.., _) => format!("{} files", self.num_files_removed),
417+
};
418+
self.config
419+
.shell()
420+
.status(status, format!("{file_count}{byte_count}"))
411421
}
412422

413423
/// Deletes all of the given paths, showing a progress bar as it proceeds.

tests/testsuite/clean.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ fn clean_verbose() {
417417
expected.push_str(&format!("[REMOVING] [..]{}\n", obj.unwrap().display()));
418418
}
419419
}
420-
expected.push_str("[REMOVED] [..] files/directories, [..] total\n");
420+
expected.push_str("[REMOVED] [..] files, [..] total\n");
421421
p.cargo("clean -p bar --verbose")
422422
.with_stderr_unordered(&expected)
423423
.run();
@@ -609,7 +609,7 @@ error: package ID specification `baz` did not match any packages
609609
.with_stderr(
610610
"warning: version qualifier in `-p bar:1.0.0` is ignored, \
611611
cleaning all versions of `bar` found\n\
612-
[REMOVED] [..] files/directories, [..] total",
612+
[REMOVED] [..] files, [..] total",
613613
)
614614
.run();
615615
let mut walker = walkdir::WalkDir::new(p.build_dir())
@@ -707,13 +707,13 @@ fn clean_dry_run() {
707707
// Start with no files.
708708
p.cargo("clean --dry-run")
709709
.with_stdout("")
710-
.with_stderr("[SUMMARY] 0 files/directories")
710+
.with_stderr("[SUMMARY] 0 files")
711711
.run();
712712
p.cargo("check").run();
713713
let before = ls_r();
714714
p.cargo("clean --dry-run")
715715
.with_stdout("[CWD]/target")
716-
.with_stderr("[SUMMARY] [..] files/directories, [..] total")
716+
.with_stderr("[SUMMARY] [..] files, [..] total")
717717
.run();
718718
// Verify it didn't delete anything.
719719
let after = ls_r();
@@ -723,7 +723,7 @@ fn clean_dry_run() {
723723
// Verify the verbose output.
724724
p.cargo("clean --dry-run -v")
725725
.with_stdout_unordered(expected)
726-
.with_stderr("[SUMMARY] [..] files/directories, [..] total")
726+
.with_stderr("[SUMMARY] [..] files, [..] total")
727727
.run();
728728
}
729729

tests/testsuite/profile_custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ fn clean_custom_dirname() {
544544

545545
// This should clean 'other'
546546
p.cargo("clean --profile=other")
547-
.with_stderr("[REMOVED] [..] files/directories, [..] total")
547+
.with_stderr("[REMOVED] [..] files, [..] total")
548548
.run();
549549
assert!(p.build_dir().join("debug").is_dir());
550550
assert!(!p.build_dir().join("other").is_dir());

tests/testsuite/script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ fn cmd_clean_with_embedded() {
980980
.with_stderr(
981981
"\
982982
[WARNING] `package.edition` is unspecified, defaulting to `2021`
983-
[REMOVED] [..] files/directories, [..] total
983+
[REMOVED] [..] files, [..] total
984984
",
985985
)
986986
.run();

0 commit comments

Comments
 (0)