Skip to content

Commit

Permalink
Make the recursive option of cargo-sweep recurse past Cargo directori…
Browse files Browse the repository at this point in the history
…es (#78)

* Make the recursive option of cargo-sweep recurse past Cargo directories

When passing the -r (recursive) option to cargo-sweep, it is supposed to
be recursive over the subdirectories of the root path it is given
(whether implicit or explicit). cargo-sweep erronously assumed that
a Cargo project could not contain unrelated subprojects.

This patch lets cargo-sweep keep recursing past cargo roots.

The drawback of this patch is that cargo-sweep will need to recurse more,
and as such becomes a bit slower to run.

Fixes #66

* Fix the empty_project_output test for macos

The empty_project_output test doesn't have the same traversing/logging
order on macos as it does on whatever os/machine it was written for.
This commit tries to improve the situation somewhat by making the test
runnable on macos as well, while sacrificing readability and test accuracy.

An alternative fix would be to not use regex for the test at all.

* Make cargo-sweep output parseable by human-size
  • Loading branch information
bes authored Feb 5, 2023
1 parent 70dcd87 commit dbb2133
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 55 deletions.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ fn find_cargo_projects(root: &Path, include_hidden: bool) -> Vec<PathBuf> {
}
if let Some(target_directory) = is_cargo_root(entry.path()) {
target_paths.insert(target_directory);
iter.skip_current_dir(); // no reason to look at the src and such
// Previously cargo-sweep skipped subdirectories here, but it is valid for
// subdirectories to contain cargo roots.
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Formats a number of bytes into the closest binary SI unit, i.e KiB, MiB etc.
pub fn format_bytes(bytes: u64) -> String {
let prefixes = ["bytes", "KiB", "MiB", "GiB", "TiB"];
let prefixes = ["B", "KiB", "MiB", "GiB", "TiB"];
let mut bytes = bytes as f64;
for prefix in prefixes.iter() {
if bytes < 1024. {
Expand All @@ -18,7 +18,26 @@ mod tests {
#[test]
fn test_format_bytes() {
assert_eq!(format_bytes(1024), "1.00 KiB");
assert_eq!(format_bytes(1023), "1023.00 bytes");
assert_eq!(format_bytes(1023), "1023.00 B");
assert_eq!(format_bytes(500 * 1024 * 1024), "500.00 MiB");

// Assert that the human-size crate can parse the output from cargo-sweep
assert_eq!("1.00 B".parse::<human_size::Size>().unwrap().to_bytes(), 1);
assert_eq!(
"1.00 KiB".parse::<human_size::Size>().unwrap().to_bytes(),
1024
);
assert_eq!(
"1.00 MiB".parse::<human_size::Size>().unwrap().to_bytes(),
1024 * 1024
);
assert_eq!(
"1.00 GiB".parse::<human_size::Size>().unwrap().to_bytes(),
1024 * 1024 * 1024
);
assert_eq!(
"1.00 TiB".parse::<human_size::Size>().unwrap().to_bytes(),
1024 * 1024 * 1024 * 1024
);
}
}
Loading

0 comments on commit dbb2133

Please sign in to comment.