From fd4e1cfb285a94febd3c9f690c17ef1b2d4b8319 Mon Sep 17 00:00:00 2001 From: Biplab Mochan Gartia <45629823+biplab5464@users.noreply.github.com> Date: Fri, 2 Feb 2024 21:30:35 +0530 Subject: [PATCH] stat: should fail without arguments #5928 (#5935) * stat: should fail without arguments #5928 * style and lint issue stat: should fail without arguments #5928 * style and lint issue stat: should fail without arguments #5928 * style and lint 2 issue stat: should fail without arguments #5928 --------- Co-authored-by: biplab5464 --- src/uu/stat/src/stat.rs | 8 +++++++- tests/by-util/test_stat.rs | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index a0ece4cce..1c5bd8919 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -546,10 +546,16 @@ impl Stater { } fn new(matches: &ArgMatches) -> UResult { - let files = matches + let files: Vec = matches .get_many::(options::FILES) .map(|v| v.map(OsString::from).collect()) .unwrap_or_default(); + if files.is_empty() { + return Err(Box::new(USimpleError { + code: 1, + message: "missing operand\nTry 'stat --help' for more information.".to_string(), + })); + } let format_str = if matches.contains_id(options::PRINTF) { matches .get_one::(options::PRINTF) diff --git a/tests/by-util/test_stat.rs b/tests/by-util/test_stat.rs index e918d54e9..189b1d441 100644 --- a/tests/by-util/test_stat.rs +++ b/tests/by-util/test_stat.rs @@ -338,3 +338,10 @@ fn test_stdin_redirect() { .stdout_contains("File: -") .succeeded(); } + +#[test] +fn test_without_argument() { + new_ucmd!() + .fails() + .stderr_contains("missing operand\nTry 'stat --help' for more information."); +}