diff --git a/src/uu/mountpoint/src/mountpoint.rs b/src/uu/mountpoint/src/mountpoint.rs index a6b5368a..1666d531 100644 --- a/src/uu/mountpoint/src/mountpoint.rs +++ b/src/uu/mountpoint/src/mountpoint.rs @@ -3,8 +3,8 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -use clap::Arg; use clap::{crate_version, Command}; +use clap::{Arg, ArgAction}; use std::env; #[cfg(not(windows))] use std::fs; @@ -21,14 +21,23 @@ const USAGE: &str = help_usage!("mountpoint.md"); pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?; let path = matches.get_one::("path"); + let quiet = matches.get_flag("quiet"); if let Some(path) = path { match is_mountpoint(path) { - Ok(true) => println!("{path} is a mountpoint"), + Ok(true) => { + if !quiet { + println!("{path} is a mountpoint"); + } + } Ok(false) => { - println!("{path} is not a mountpoint"); + if !quiet { + println!("{path} is not a mountpoint"); + } set_exit_code(32); } + // --quiet keeps the exit status but drops the message + Err(_) if quiet => set_exit_code(1), Err(e) => return Err(USimpleError::new(1, format!("{path}: {}", strip_errno(&e)))), } } else { @@ -108,6 +117,13 @@ pub fn uu_app() -> Command { .about(ABOUT) .override_usage(format_usage(USAGE)) .infer_long_args(true) + .arg( + Arg::new("quiet") + .short('q') + .long("quiet") + .action(ArgAction::SetTrue) + .help("Be quiet, only set the exit status"), + ) .arg( Arg::new("path") .value_name("PATH") diff --git a/tests/by-util/test_mountpoint.rs b/tests/by-util/test_mountpoint.rs index 7042c8c8..ddb69aab 100644 --- a/tests/by-util/test_mountpoint.rs +++ b/tests/by-util/test_mountpoint.rs @@ -66,3 +66,33 @@ fn test_proc_is_a_mountpoint() { .succeeds() .stdout_is("/proc/ is a mountpoint\n"); } + +#[test] +#[cfg(unix)] +fn test_quiet_mountpoint() { + for flag in ["-q", "--quiet"] { + new_ucmd!().args(&[flag, "/"]).succeeds().no_output(); + } +} + +#[test] +#[cfg(unix)] +fn test_quiet_not_a_mountpoint() { + let (at, mut ucmd) = at_and_ucmd!(); + at.mkdir("dir"); + ucmd.args(&["-q", &at.plus_as_string("dir")]) + .current_dir("/") + .fails() + .code_is(32) + .no_output(); +} + +#[test] +#[cfg(unix)] +fn test_quiet_nonexistent_path() { + new_ucmd!() + .args(&["-q", "does-not-exist"]) + .fails() + .code_is(1) + .no_output(); +}