9use num_format::{Locale, ToFormattedString};
15#[command(version, about = "Look for empty directories and delete them", long_about = None)]
17 /// Path to the directory to inspect
18 #[arg(default_value_t = String::from("."))]
22fn main() -> Result<(), std::io::Error> {
23 let cli = Cli::parse();
25 let root = fs::canonicalize(Path::new(&cli.root))?;
26 let result = emptydir::emptydir(&root);
28 match (result.count_deleted, result.count_errors) {
29 (0, 0) => match can_be_deleted::can_be_deleted(&root) {
30 can_be_deleted::DeleteDecision::CannotDelete(reason) => {
31 eprintln!("{}", reason.to_string().red());
37 println!("{}", "Unable to delete empty directories".red());
40 (1, _) => println!("{}", "1 directory deleted".green()),
42 let message = format!(
43 "{} directories deleted",
44 result.count_deleted.to_formatted_string(&Locale::en)
46 println!("{}", message.green());
56 use std::path::PathBuf;
58 use assert_cmd::Command;
59 use predicates::prelude::*;
61 fn test_dir() -> PathBuf {
62 let tmp_dir = tempfile::tempdir().unwrap();
63 let path = tmp_dir.path();
67 fn create_dir(dir: &PathBuf) {
68 fs::create_dir_all(dir).unwrap();
73 reason = "cargo_bin is deprecated, cargo_bin! is not, `use` does not differenciate them"
76 fn it_prints_the_version() {
77 // Match strings like `emptydir 1.2.3`
78 let is_version_string =
79 predicate::str::is_match(r"^emptydir [0-9]+\.[0-9]+\.[0-9]+\n$").unwrap();
81 Command::cargo_bin("emptydir")
86 .stdout(is_version_string)
92 reason = "cargo_bin is deprecated, cargo_bin! is not, `use` does not differenciate them"
95 fn it_deletes_dot_directory() {
98 // Create the directory, but don't put anything in it
101 Command::cargo_bin("emptydir")
107 assert_eq!(dir.exists(), false);