From 29f6631554eb6417f4f302d113078e7de43b95b7 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Fri, 3 Nov 2023 16:49:19 +0100 Subject: [PATCH] du: add -P/--no-dereference --- src/uu/du/src/du.rs | 16 +++++++++------- tests/by-util/test_du.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index ad5e87833..ae88ed13b 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -68,6 +68,7 @@ mod options { pub const ONE_FILE_SYSTEM: &str = "one-file-system"; pub const DEREFERENCE: &str = "dereference"; pub const DEREFERENCE_ARGS: &str = "dereference-args"; + pub const NO_DEREFERENCE: &str = "no-dereference"; pub const INODES: &str = "inodes"; pub const EXCLUDE: &str = "exclude"; pub const EXCLUDE_FROM: &str = "exclude-from"; @@ -824,13 +825,14 @@ pub fn uu_app() -> Command { .help("follow only symlinks that are listed on the command line") .action(ArgAction::SetTrue) ) - // .arg( - // Arg::new("no-dereference") - // .short('P') - // .long("no-dereference") - // .help("don't follow any symbolic links (this is the default)") - // .action(ArgAction::SetTrue), - // ) + .arg( + Arg::new(options::NO_DEREFERENCE) + .short('P') + .long(options::NO_DEREFERENCE) + .help("don't follow any symbolic links (this is the default)") + .overrides_with(options::DEREFERENCE) + .action(ArgAction::SetTrue), + ) .arg( Arg::new(options::BLOCK_SIZE_1M) .short('m') diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 9a508da25..7e6bc4775 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -336,6 +336,42 @@ fn _du_dereference(s: &str) { } } +#[cfg(not(windows))] +#[test] +fn test_du_no_dereference() { + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + let dir = "a_dir"; + let symlink = "symlink"; + + at.mkdir(dir); + at.symlink_dir(dir, symlink); + + for arg in ["-P", "--no-dereference"] { + ts.ucmd() + .arg(arg) + .succeeds() + .stdout_contains(dir) + .stdout_does_not_contain(symlink); + + // ensure no-dereference "wins" + ts.ucmd() + .arg("--dereference") + .arg(arg) + .succeeds() + .stdout_contains(dir) + .stdout_does_not_contain(symlink); + + // ensure dereference "wins" + ts.ucmd() + .arg(arg) + .arg("--dereference") + .succeeds() + .stdout_contains(symlink) + .stdout_does_not_contain(dir); + } +} + #[test] fn test_du_inodes_basic() { let ts = TestScenario::new(util_name!());