mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
sleep: Use fundu error types to improve error messages
This commit is contained in:
parent
3a28b616d9
commit
5fb091a4fb
2 changed files with 30 additions and 12 deletions
|
@ -14,7 +14,7 @@ use uucore::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::{crate_version, Arg, ArgAction, Command};
|
use clap::{crate_version, Arg, ArgAction, Command};
|
||||||
use fundu::{self, DurationParser};
|
use fundu::{self, DurationParser, ParseError};
|
||||||
|
|
||||||
static ABOUT: &str = help_about!("sleep.md");
|
static ABOUT: &str = help_about!("sleep.md");
|
||||||
const USAGE: &str = help_usage!("sleep.md");
|
const USAGE: &str = help_usage!("sleep.md");
|
||||||
|
@ -68,12 +68,28 @@ fn sleep(args: &[&str]) -> UResult<()> {
|
||||||
|
|
||||||
let sleep_dur = args
|
let sleep_dur = args
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|input| {
|
.filter_map(|input| match parser.parse(input.trim()) {
|
||||||
parser.parse(input.trim()).ok().or_else(|| {
|
Ok(duration) => Some(duration),
|
||||||
|
Err(error) => {
|
||||||
arg_error = true;
|
arg_error = true;
|
||||||
show_error!("invalid time interval '{input}'");
|
|
||||||
|
let reason = match error {
|
||||||
|
ParseError::Empty if input.is_empty() => "Input was empty".to_string(),
|
||||||
|
ParseError::Empty => "Found only whitespace in input".to_string(),
|
||||||
|
ParseError::Syntax(pos, description)
|
||||||
|
| ParseError::TimeUnit(pos, description) => {
|
||||||
|
format!("{description} at position {}", pos.saturating_add(1))
|
||||||
|
}
|
||||||
|
ParseError::NegativeExponentOverflow | ParseError::PositiveExponentOverflow => {
|
||||||
|
"Exponent was out of bounds".to_string()
|
||||||
|
}
|
||||||
|
ParseError::NegativeNumber => "Number was negative".to_string(),
|
||||||
|
error => error.to_string(),
|
||||||
|
};
|
||||||
|
show_error!("invalid time interval '{input}': {reason}");
|
||||||
|
|
||||||
None
|
None
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
.fold(Duration::ZERO, |acc, n| acc.saturating_add(n));
|
.fold(Duration::ZERO, |acc, n| acc.saturating_add(n));
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,11 @@ fn test_invalid_time_interval() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.arg("xyz")
|
.arg("xyz")
|
||||||
.fails()
|
.fails()
|
||||||
.usage_error("invalid time interval 'xyz'");
|
.usage_error("invalid time interval 'xyz': Invalid character: 'x' at position 1");
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["--", "-1"])
|
.args(&["--", "-1"])
|
||||||
.fails()
|
.fails()
|
||||||
.usage_error("invalid time interval '-1'");
|
.usage_error("invalid time interval '-1': Number was negative");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -204,14 +204,16 @@ fn test_sleep_when_input_has_only_whitespace_then_error(#[case] input: &str) {
|
||||||
.arg(input)
|
.arg(input)
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
.fails()
|
.fails()
|
||||||
.usage_error(format!("invalid time interval '{input}'"));
|
.usage_error(format!(
|
||||||
|
"invalid time interval '{input}': Found only whitespace in input"
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sleep_when_multiple_input_some_with_error_then_shows_all_errors() {
|
fn test_sleep_when_multiple_input_some_with_error_then_shows_all_errors() {
|
||||||
let expected = "invalid time interval 'abc'\n\
|
let expected = "invalid time interval 'abc': Invalid character: 'a' at position 1\n\
|
||||||
sleep: invalid time interval '1years'\n\
|
sleep: invalid time interval '1years': Invalid time unit: 'years' at position 2\n\
|
||||||
sleep: invalid time interval ' '";
|
sleep: invalid time interval ' ': Found only whitespace in input";
|
||||||
|
|
||||||
// Even if one of the arguments is valid, but the rest isn't, we should still fail and exit early.
|
// Even if one of the arguments is valid, but the rest isn't, we should still fail and exit early.
|
||||||
// So, the timeout of 10 seconds ensures we haven't executed `thread::sleep` with the only valid
|
// So, the timeout of 10 seconds ensures we haven't executed `thread::sleep` with the only valid
|
||||||
|
@ -228,5 +230,5 @@ fn test_negative_interval() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["--", "-1"])
|
.args(&["--", "-1"])
|
||||||
.fails()
|
.fails()
|
||||||
.usage_error("invalid time interval '-1'");
|
.usage_error("invalid time interval '-1': Number was negative");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue