Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/uu/mountpoint/src/mountpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::<String>("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 {
Expand Down Expand Up @@ -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")
Expand Down
30 changes: 30 additions & 0 deletions tests/by-util/test_mountpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Loading