1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

sleep: give usage error on invalid time interval

For example,

    $ sleep xyz
    sleep: invalid time interval 'xyz'
    Try 'sleep --help' for more information.

This matches the behavior of GNU sleep.
This commit is contained in:
Jeffrey Finkelstein 2022-03-20 15:21:50 -04:00
parent 04b219bdef
commit c39c917db7
2 changed files with 10 additions and 2 deletions

View file

@ -9,7 +9,7 @@ use std::thread;
use std::time::Duration; use std::time::Duration;
use uucore::{ use uucore::{
error::{UResult, USimpleError}, error::{UResult, UUsageError},
format_usage, format_usage,
}; };
@ -64,7 +64,7 @@ fn sleep(args: &[&str]) -> UResult<()> {
Duration::new(0, 0), Duration::new(0, 0),
|result, arg| match uucore::parse_time::from_str(&arg[..]) { |result, arg| match uucore::parse_time::from_str(&arg[..]) {
Ok(m) => Ok(m.saturating_add(result)), Ok(m) => Ok(m.saturating_add(result)),
Err(f) => Err(USimpleError::new(1, f)), Err(f) => Err(UUsageError::new(1, f)),
}, },
)?; )?;
thread::sleep(sleep_dur); thread::sleep(sleep_dur);

View file

@ -3,6 +3,14 @@ use crate::common::util::*;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
#[test]
fn test_invalid_time_interval() {
new_ucmd!()
.arg("xyz")
.fails()
.usage_error("invalid time interval 'xyz'");
}
#[test] #[test]
fn test_sleep_no_suffix() { fn test_sleep_no_suffix() {
let millis_100 = Duration::from_millis(100); let millis_100 = Duration::from_millis(100);