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

Merge pull request #4448 from Joining7943/sleep-replace-uucore-from-str-with-fundu

`sleep`: Replace uucore::parse_time::from_str with fundu
This commit is contained in:
Terts Diepraam 2023-03-12 23:42:23 +01:00 committed by GitHub
commit 6708284603
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 15 deletions

View file

@ -20,6 +20,7 @@ exacl
filetime
formatteriteminfo
fsext
fundu
getopts
getrandom
globset

5
Cargo.lock generated
View file

@ -880,9 +880,9 @@ dependencies = [
[[package]]
name = "fundu"
version = "0.3.0"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "925250bc259498d4008ee072bf16586083ab2c491aa4b06b3c4d0a6556cebd74"
checksum = "da58c38fe7b706cead98429d8a8535261addbe55fd531c7d7c7d770346464010"
[[package]]
name = "futures"
@ -3015,6 +3015,7 @@ name = "uu_sleep"
version = "0.0.17"
dependencies = [
"clap",
"fundu",
"uucore",
]

View file

@ -282,7 +282,7 @@ filetime = "0.2"
fnv = "1.0.7"
fs_extra = "1.1.0"
fts-sys = "0.2"
fundu = "0.3.0"
fundu = "0.4.2"
gcd = "2.2"
glob = "0.3.0"
half = "2.1"

View file

@ -16,6 +16,7 @@ path = "src/sleep.rs"
[dependencies]
clap = { workspace=true }
fundu = { workspace=true }
uucore = { workspace=true }
[[bin]]

View file

@ -14,6 +14,7 @@ use uucore::{
};
use clap::{crate_version, Arg, ArgAction, Command};
use fundu::{self, DurationParser, ParseError};
static ABOUT: &str = help_about!("sleep.md");
const USAGE: &str = help_usage!("sleep.md");
@ -61,14 +62,34 @@ pub fn uu_app() -> Command {
fn sleep(args: &[&str]) -> UResult<()> {
let mut arg_error = false;
use fundu::TimeUnit::*;
let parser = DurationParser::with_time_units(&[Second, Minute, Hour, Day]);
let sleep_dur = args
.iter()
.filter_map(|input| {
uucore::parse_time::from_str(input.trim()).ok().or_else(|| {
.filter_map(|input| match parser.parse(input.trim()) {
Ok(duration) => Some(duration),
Err(error) => {
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
})
}
})
.fold(Duration::ZERO, |acc, n| acc.saturating_add(n));

View file

@ -10,11 +10,11 @@ fn test_invalid_time_interval() {
new_ucmd!()
.arg("xyz")
.fails()
.usage_error("invalid time interval 'xyz'");
.usage_error("invalid time interval 'xyz': Invalid character: 'x' at position 1");
new_ucmd!()
.args(&["--", "-1"])
.fails()
.usage_error("invalid time interval '-1'");
.usage_error("invalid time interval '-1': Number was negative");
}
#[test]
@ -204,14 +204,16 @@ fn test_sleep_when_input_has_only_whitespace_then_error(#[case] input: &str) {
.arg(input)
.timeout(Duration::from_secs(10))
.fails()
.usage_error(format!("invalid time interval '{input}'"));
.usage_error(format!(
"invalid time interval '{input}': Found only whitespace in input"
));
}
#[test]
fn test_sleep_when_multiple_input_some_with_error_then_shows_all_errors() {
let expected = "invalid time interval 'abc'\n\
sleep: invalid time interval '1years'\n\
sleep: invalid time interval ' '";
let expected = "invalid time interval 'abc': Invalid character: 'a' at position 1\n\
sleep: invalid time interval '1years': Invalid time unit: 'years' at position 2\n\
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.
// 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!()
.args(&["--", "-1"])
.fails()
.usage_error("invalid time interval '-1'");
.usage_error("invalid time interval '-1': Number was negative");
}

View file

@ -4459,7 +4459,7 @@ fn test_follow_when_files_are_pointing_to_same_relative_file_and_file_stays_same
}
#[rstest]
#[case::exponent_exceed_float_max("1.0e2048")]
#[case::exponent_exceed_float_max("1.0e100000")]
#[case::underscore_delimiter("1_000")]
#[case::only_point(".")]
#[case::space_in_primes("' '")]